blob: 4bbd3c33b3cdb8126cb1e50ac8bbb06087bd2082 [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
541 if (options & SEARCH_START)
542 extra_col = 0;
543#ifdef FEAT_MBYTE
544 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
545 else if (has_mbyte && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
546 && pos->col < MAXCOL - 2)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000547 {
548 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
549 if (*ptr == NUL)
550 extra_col = 1;
551 else
552 extra_col = (*mb_ptr2len)(ptr);
553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#endif
555 else
556 extra_col = 1;
557
Bram Moolenaar280f1262006-01-30 00:14:18 +0000558 /*
559 * find the string
560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 called_emsg = FALSE;
562 do /* loop for count */
563 {
564 start_pos = *pos; /* remember start pos for detecting no match */
565 found = 0; /* default: not found */
566 at_first_line = TRUE; /* default: start in first line */
567 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
568 {
569 pos->lnum = 1;
570 pos->col = 0;
571 at_first_line = FALSE; /* not in first line now */
572 }
573
574 /*
575 * Start searching in current line, unless searching backwards and
576 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000577 * If we are searching backwards, in column 0, and not including the
578 * current position, gain some efficiency by skipping back a line.
579 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000581 if (dir == BACKWARD && start_pos.col == 0
582 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
584 lnum = pos->lnum - 1;
585 at_first_line = FALSE;
586 }
587 else
588 lnum = pos->lnum;
589
590 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
591 {
592 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
593 lnum += dir, at_first_line = FALSE)
594 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000595 /* Stop after checking "stop_lnum", if it's set. */
596 if (stop_lnum != 0 && (dir == FORWARD
597 ? lnum > stop_lnum : lnum < stop_lnum))
598 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000599#ifdef FEAT_RELTIME
600 /* Stop after passing the "tm" time limit. */
601 if (tm != NULL && profile_passed_limit(tm))
602 break;
603#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000606 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 nmatched = vim_regexec_multi(&regmatch, win, buf,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000609 lnum, (colnr_T)0,
610#ifdef FEAT_RELTIME
611 tm
612#else
613 NULL
614#endif
615 );
Bram Moolenaar071d4272004-06-13 20:20:40 +0000616 /* Abort searching on an error (e.g., out of stack). */
617 if (called_emsg)
618 break;
619 if (nmatched > 0)
620 {
621 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000622 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 endpos = regmatch.endpos[0];
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000624#ifdef FEAT_EVAL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000625 submatch = first_submatch(&regmatch);
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000626#endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000627 /* Line me be past end of buffer for "\n\zs". */
628 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
629 ptr = (char_u *)"";
630 else
631 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000632
633 /*
634 * Forward search in the first line: match should be after
635 * the start position. If not, continue at the end of the
636 * match (this is vi compatible) or on the next char.
637 */
638 if (dir == FORWARD && at_first_line)
639 {
640 match_ok = TRUE;
641 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000642 * When the match starts in a next line it's certainly
643 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000644 * When match lands on a NUL the cursor will be put
645 * one back afterwards, compare with that position,
646 * otherwise "/$" will get stuck on end of line.
647 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000648 while (matchpos.lnum == 0
649 && ((options & SEARCH_END)
650 ? (nmatched == 1
651 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000653 : ((int)matchpos.col
654 - (ptr[matchpos.col] == NUL)
655 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000656 {
657 /*
658 * If vi-compatible searching, continue at the end
659 * of the match, otherwise continue one position
660 * forward.
661 */
662 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
663 {
664 if (nmatched > 1)
665 {
666 /* end is in next line, thus no match in
667 * this line */
668 match_ok = FALSE;
669 break;
670 }
671 matchcol = endpos.col;
672 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000673 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 && ptr[matchcol] != NUL)
675 {
676#ifdef FEAT_MBYTE
677 if (has_mbyte)
678 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000679 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000680 else
681#endif
682 ++matchcol;
683 }
684 }
685 else
686 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000687 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000688 if (ptr[matchcol] != NUL)
689 {
690#ifdef FEAT_MBYTE
691 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000692 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000693 + matchcol);
694 else
695#endif
696 ++matchcol;
697 }
698 }
699 if (ptr[matchcol] == NUL
700 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000701 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000702 matchcol,
703#ifdef FEAT_RELTIME
704 tm
705#else
706 NULL
707#endif
708 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000709 {
710 match_ok = FALSE;
711 break;
712 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000713 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 endpos = regmatch.endpos[0];
715# ifdef FEAT_EVAL
716 submatch = first_submatch(&regmatch);
717# endif
718
719 /* Need to get the line pointer again, a
720 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000721 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000722 }
723 if (!match_ok)
724 continue;
725 }
726 if (dir == BACKWARD)
727 {
728 /*
729 * Now, if there are multiple matches on this line,
730 * we have to get the last one. Or the last one before
731 * the cursor, if we're on that line.
732 * When putting the new cursor at the end, compare
733 * relative to the end of the match.
734 */
735 match_ok = FALSE;
736 for (;;)
737 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000738 /* Remember a position that is before the start
739 * position, we use it if it's the last match in
740 * the line. Always accept a position after
741 * wrapping around. */
742 if (loop
743 || ((options & SEARCH_END)
744 ? (lnum + regmatch.endpos[0].lnum
745 < start_pos.lnum
746 || (lnum + regmatch.endpos[0].lnum
747 == start_pos.lnum
748 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000750 <= (int)start_pos.col))
751 : (lnum + regmatch.startpos[0].lnum
752 < start_pos.lnum
753 || (lnum + regmatch.startpos[0].lnum
754 == start_pos.lnum
755 && (int)regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000756 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000757 <= (int)start_pos.col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000758 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000759 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000760 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 endpos = regmatch.endpos[0];
762# ifdef FEAT_EVAL
763 submatch = first_submatch(&regmatch);
764# endif
765 }
766 else
767 break;
768
769 /*
770 * We found a valid match, now check if there is
771 * another one after it.
772 * If vi-compatible searching, continue at the end
773 * of the match, otherwise continue one position
774 * forward.
775 */
776 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
777 {
778 if (nmatched > 1)
779 break;
780 matchcol = endpos.col;
781 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000782 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000783 && ptr[matchcol] != NUL)
784 {
785#ifdef FEAT_MBYTE
786 if (has_mbyte)
787 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000788 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000789 else
790#endif
791 ++matchcol;
792 }
793 }
794 else
795 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000796 /* Stop when the match is in a next line. */
797 if (matchpos.lnum > 0)
798 break;
799 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000800 if (ptr[matchcol] != NUL)
801 {
802#ifdef FEAT_MBYTE
803 if (has_mbyte)
804 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000805 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000806 else
807#endif
808 ++matchcol;
809 }
810 }
811 if (ptr[matchcol] == NUL
812 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000813 win, buf, lnum + matchpos.lnum,
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000814 matchcol,
815#ifdef FEAT_RELTIME
816 tm
817#else
818 NULL
819#endif
820 )) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 break;
822
823 /* Need to get the line pointer again, a
824 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000825 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000826 }
827
828 /*
829 * If there is only a match after the cursor, skip
830 * this match.
831 */
832 if (!match_ok)
833 continue;
834 }
835
836 if (options & SEARCH_END && !(options & SEARCH_NOOF))
837 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000838 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000839 pos->col = endpos.col - 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000840#ifdef FEAT_MBYTE
841 if (has_mbyte)
842 {
Bram Moolenaarfb7c90c2007-01-16 15:01:41 +0000843 /* 'e' offset may put us just below the last line */
844 if (pos->lnum > buf->b_ml.ml_line_count)
Bram Moolenaar8c471fa2007-01-16 21:14:45 +0000845 ptr = (char_u *)"";
Bram Moolenaarfb7c90c2007-01-16 15:01:41 +0000846 else
847 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000848 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
849 }
850#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000851 }
852 else
853 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000854 pos->lnum = lnum + matchpos.lnum;
855 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000856 }
857#ifdef FEAT_VIRTUALEDIT
858 pos->coladd = 0;
859#endif
860 found = 1;
861
862 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000863 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000864 search_match_endcol = endpos.col;
865 break;
866 }
867 line_breakcheck(); /* stop if ctrl-C typed */
868 if (got_int)
869 break;
870
871#ifdef FEAT_SEARCH_EXTRA
872 /* Cancel searching if a character was typed. Used for
873 * 'incsearch'. Don't check too often, that would slowdown
874 * searching too much. */
875 if ((options & SEARCH_PEEK)
876 && ((lnum - pos->lnum) & 0x3f) == 0
877 && char_avail())
878 {
879 break_loop = TRUE;
880 break;
881 }
882#endif
883
884 if (loop && lnum == start_pos.lnum)
885 break; /* if second loop, stop where started */
886 }
887 at_first_line = FALSE;
888
889 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000890 * Stop the search if wrapscan isn't set, "stop_lnum" is
891 * specified, after an interrupt, after a match and after looping
892 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000893 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000894 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
895 || break_loop || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000896 break;
897
898 /*
899 * If 'wrapscan' is set we continue at the other end of the file.
900 * If 'shortmess' does not contain 's', we give a message.
901 * This message is also remembered in keep_msg for when the screen
902 * is redrawn. The keep_msg is cleared whenever another message is
903 * written.
904 */
905 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000906 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000907 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000908 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +0000909 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
910 give_warning((char_u *)_(dir == BACKWARD
911 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000912 }
913 if (got_int || called_emsg || break_loop)
914 break;
915 }
916 while (--count > 0 && found); /* stop after count matches or no match */
917
918 vim_free(regmatch.regprog);
919
Bram Moolenaar280f1262006-01-30 00:14:18 +0000920 called_emsg |= save_called_emsg;
921
Bram Moolenaar071d4272004-06-13 20:20:40 +0000922 if (!found) /* did not find it */
923 {
924 if (got_int)
925 EMSG(_(e_interr));
926 else if ((options & SEARCH_MSG) == SEARCH_MSG)
927 {
928 if (p_ws)
929 EMSG2(_(e_patnotf2), mr_pattern);
930 else if (lnum == 0)
931 EMSG2(_("E384: search hit TOP without match for: %s"),
932 mr_pattern);
933 else
934 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
935 mr_pattern);
936 }
937 return FAIL;
938 }
939
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000940 /* A pattern like "\n\zs" may go past the last line. */
941 if (pos->lnum > buf->b_ml.ml_line_count)
942 {
943 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000944 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000945 if (pos->col > 0)
946 --pos->col;
947 }
948
Bram Moolenaar071d4272004-06-13 20:20:40 +0000949 return submatch + 1;
950}
951
952#ifdef FEAT_EVAL
953/*
954 * Return the number of the first subpat that matched.
955 */
956 static int
957first_submatch(rp)
958 regmmatch_T *rp;
959{
960 int submatch;
961
962 for (submatch = 1; ; ++submatch)
963 {
964 if (rp->startpos[submatch].lnum >= 0)
965 break;
966 if (submatch == 9)
967 {
968 submatch = 0;
969 break;
970 }
971 }
972 return submatch;
973}
974#endif
975
976/*
977 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000978 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000979 * If 'dirc' is 0: use previous dir.
980 * If 'pat' is NULL or empty : use previous string.
981 * If 'options & SEARCH_REV' : go in reverse of previous dir.
982 * If 'options & SEARCH_ECHO': echo the search command and handle options
983 * If 'options & SEARCH_MSG' : may give error message
984 * If 'options & SEARCH_OPT' : interpret optional flags
985 * If 'options & SEARCH_HIS' : put search pattern in history
986 * If 'options & SEARCH_NOOF': don't add offset to position
987 * If 'options & SEARCH_MARK': set previous context mark
988 * If 'options & SEARCH_KEEP': keep previous search pattern
989 * If 'options & SEARCH_START': accept match at curpos itself
990 * If 'options & SEARCH_PEEK': check for typed char, cancel search
991 *
992 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
993 * makes the movement linewise without moving the match position.
994 *
995 * return 0 for failure, 1 for found, 2 for found and line offset added
996 */
997 int
Bram Moolenaar91a4e822008-01-19 14:59:58 +0000998do_search(oap, dirc, pat, count, options, tm)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000999 oparg_T *oap; /* can be NULL */
1000 int dirc; /* '/' or '?' */
1001 char_u *pat;
1002 long count;
1003 int options;
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001004 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001005{
1006 pos_T pos; /* position of the last match */
1007 char_u *searchstr;
1008 struct soffset old_off;
1009 int retval; /* Return value */
1010 char_u *p;
1011 long c;
1012 char_u *dircp;
1013 char_u *strcopy = NULL;
1014 char_u *ps;
1015
1016 /*
1017 * A line offset is not remembered, this is vi compatible.
1018 */
1019 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1020 {
1021 spats[0].off.line = FALSE;
1022 spats[0].off.off = 0;
1023 }
1024
1025 /*
1026 * Save the values for when (options & SEARCH_KEEP) is used.
1027 * (there is no "if ()" around this because gcc wants them initialized)
1028 */
1029 old_off = spats[0].off;
1030
1031 pos = curwin->w_cursor; /* start searching at the cursor position */
1032
1033 /*
1034 * Find out the direction of the search.
1035 */
1036 if (dirc == 0)
1037 dirc = spats[0].off.dir;
1038 else
1039 spats[0].off.dir = dirc;
1040 if (options & SEARCH_REV)
1041 {
1042#ifdef WIN32
1043 /* There is a bug in the Visual C++ 2.2 compiler which means that
1044 * dirc always ends up being '/' */
1045 dirc = (dirc == '/') ? '?' : '/';
1046#else
1047 if (dirc == '/')
1048 dirc = '?';
1049 else
1050 dirc = '/';
1051#endif
1052 }
1053
1054#ifdef FEAT_FOLDING
1055 /* If the cursor is in a closed fold, don't find another match in the same
1056 * fold. */
1057 if (dirc == '/')
1058 {
1059 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1060 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1061 }
1062 else
1063 {
1064 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1065 pos.col = 0;
1066 }
1067#endif
1068
1069#ifdef FEAT_SEARCH_EXTRA
1070 /*
1071 * Turn 'hlsearch' highlighting back on.
1072 */
1073 if (no_hlsearch && !(options & SEARCH_KEEP))
1074 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001075 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001076 no_hlsearch = FALSE;
1077 }
1078#endif
1079
1080 /*
1081 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1082 */
1083 for (;;)
1084 {
1085 searchstr = pat;
1086 dircp = NULL;
1087 /* use previous pattern */
1088 if (pat == NULL || *pat == NUL || *pat == dirc)
1089 {
1090 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1091 {
1092 EMSG(_(e_noprevre));
1093 retval = 0;
1094 goto end_do_search;
1095 }
1096 /* make search_regcomp() use spats[RE_SEARCH].pat */
1097 searchstr = (char_u *)"";
1098 }
1099
1100 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1101 {
1102 /*
1103 * Find end of regular expression.
1104 * If there is a matching '/' or '?', toss it.
1105 */
1106 ps = strcopy;
1107 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1108 if (strcopy != ps)
1109 {
1110 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001111 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001112 pat = strcopy;
1113 searchstr = strcopy;
1114 }
1115 if (*p == dirc)
1116 {
1117 dircp = p; /* remember where we put the NUL */
1118 *p++ = NUL;
1119 }
1120 spats[0].off.line = FALSE;
1121 spats[0].off.end = FALSE;
1122 spats[0].off.off = 0;
1123 /*
1124 * Check for a line offset or a character offset.
1125 * For get_address (echo off) we don't check for a character
1126 * offset, because it is meaningless and the 's' could be a
1127 * substitute command.
1128 */
1129 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1130 spats[0].off.line = TRUE;
1131 else if ((options & SEARCH_OPT) &&
1132 (*p == 'e' || *p == 's' || *p == 'b'))
1133 {
1134 if (*p == 'e') /* end */
1135 spats[0].off.end = SEARCH_END;
1136 ++p;
1137 }
1138 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1139 {
1140 /* 'nr' or '+nr' or '-nr' */
1141 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1142 spats[0].off.off = atol((char *)p);
1143 else if (*p == '-') /* single '-' */
1144 spats[0].off.off = -1;
1145 else /* single '+' */
1146 spats[0].off.off = 1;
1147 ++p;
1148 while (VIM_ISDIGIT(*p)) /* skip number */
1149 ++p;
1150 }
1151
1152 /* compute length of search command for get_address() */
1153 searchcmdlen += (int)(p - pat);
1154
1155 pat = p; /* put pat after search command */
1156 }
1157
1158 if ((options & SEARCH_ECHO) && messaging()
1159 && !cmd_silent && msg_silent == 0)
1160 {
1161 char_u *msgbuf;
1162 char_u *trunc;
1163
1164 if (*searchstr == NUL)
1165 p = spats[last_idx].pat;
1166 else
1167 p = searchstr;
1168 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1169 if (msgbuf != NULL)
1170 {
1171 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001172#ifdef FEAT_MBYTE
1173 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1174 {
1175 /* Use a space to draw the composing char on. */
1176 msgbuf[1] = ' ';
1177 STRCPY(msgbuf + 2, p);
1178 }
1179 else
1180#endif
1181 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001182 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1183 {
1184 p = msgbuf + STRLEN(msgbuf);
1185 *p++ = dirc;
1186 if (spats[0].off.end)
1187 *p++ = 'e';
1188 else if (!spats[0].off.line)
1189 *p++ = 's';
1190 if (spats[0].off.off > 0 || spats[0].off.line)
1191 *p++ = '+';
1192 if (spats[0].off.off != 0 || spats[0].off.line)
1193 sprintf((char *)p, "%ld", spats[0].off.off);
1194 else
1195 *p = NUL;
1196 }
1197
1198 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001199 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001200
1201#ifdef FEAT_RIGHTLEFT
1202 /* The search pattern could be shown on the right in rightleft
1203 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1204 * it would be blanked out again very soon. Show it on the
1205 * left, but do reverse the text. */
1206 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1207 {
1208 char_u *r;
1209
1210 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1211 if (r != NULL)
1212 {
1213 vim_free(trunc);
1214 trunc = r;
1215 }
1216 }
1217#endif
1218 if (trunc != NULL)
1219 {
1220 msg_outtrans(trunc);
1221 vim_free(trunc);
1222 }
1223 else
1224 msg_outtrans(msgbuf);
1225 msg_clr_eos();
1226 msg_check();
1227 vim_free(msgbuf);
1228
1229 gotocmdline(FALSE);
1230 out_flush();
1231 msg_nowait = TRUE; /* don't wait for this message */
1232 }
1233 }
1234
1235 /*
1236 * If there is a character offset, subtract it from the current
1237 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001238 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001239 * This is not done for a line offset, because then we would not be vi
1240 * compatible.
1241 */
Bram Moolenaared203462004-06-16 11:19:22 +00001242 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001243 {
1244 if (spats[0].off.off > 0)
1245 {
1246 for (c = spats[0].off.off; c; --c)
1247 if (decl(&pos) == -1)
1248 break;
1249 if (c) /* at start of buffer */
1250 {
1251 pos.lnum = 0; /* allow lnum == 0 here */
1252 pos.col = MAXCOL;
1253 }
1254 }
1255 else
1256 {
1257 for (c = spats[0].off.off; c; ++c)
1258 if (incl(&pos) == -1)
1259 break;
1260 if (c) /* at end of buffer */
1261 {
1262 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1263 pos.col = 0;
1264 }
1265 }
1266 }
1267
1268#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1269 if (p_altkeymap && curwin->w_p_rl)
1270 lrFswap(searchstr,0);
1271#endif
1272
1273 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1274 searchstr, count, spats[0].off.end + (options &
1275 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1276 + SEARCH_MSG + SEARCH_START
1277 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar91a4e822008-01-19 14:59:58 +00001278 RE_LAST, (linenr_T)0, tm);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001279
1280 if (dircp != NULL)
1281 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1282 if (c == FAIL)
1283 {
1284 retval = 0;
1285 goto end_do_search;
1286 }
1287 if (spats[0].off.end && oap != NULL)
1288 oap->inclusive = TRUE; /* 'e' includes last character */
1289
1290 retval = 1; /* pattern found */
1291
1292 /*
1293 * Add character and/or line offset
1294 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001295 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001296 {
1297 if (spats[0].off.line) /* Add the offset to the line number. */
1298 {
1299 c = pos.lnum + spats[0].off.off;
1300 if (c < 1)
1301 pos.lnum = 1;
1302 else if (c > curbuf->b_ml.ml_line_count)
1303 pos.lnum = curbuf->b_ml.ml_line_count;
1304 else
1305 pos.lnum = c;
1306 pos.col = 0;
1307
1308 retval = 2; /* pattern found, line offset added */
1309 }
Bram Moolenaared203462004-06-16 11:19:22 +00001310 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001311 {
1312 /* to the right, check for end of file */
1313 if (spats[0].off.off > 0)
1314 {
1315 for (c = spats[0].off.off; c; --c)
1316 if (incl(&pos) == -1)
1317 break;
1318 }
1319 /* to the left, check for start of file */
1320 else
1321 {
1322 if ((c = pos.col + spats[0].off.off) >= 0)
1323 pos.col = c;
1324 else
1325 for (c = spats[0].off.off; c; ++c)
1326 if (decl(&pos) == -1)
1327 break;
1328 }
1329 }
1330 }
1331
1332 /*
1333 * The search command can be followed by a ';' to do another search.
1334 * For example: "/pat/;/foo/+3;?bar"
1335 * This is like doing another search command, except:
1336 * - The remembered direction '/' or '?' is from the first search.
1337 * - When an error happens the cursor isn't moved at all.
1338 * Don't do this when called by get_address() (it handles ';' itself).
1339 */
1340 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1341 break;
1342
1343 dirc = *++pat;
1344 if (dirc != '?' && dirc != '/')
1345 {
1346 retval = 0;
1347 EMSG(_("E386: Expected '?' or '/' after ';'"));
1348 goto end_do_search;
1349 }
1350 ++pat;
1351 }
1352
1353 if (options & SEARCH_MARK)
1354 setpcmark();
1355 curwin->w_cursor = pos;
1356 curwin->w_set_curswant = TRUE;
1357
1358end_do_search:
1359 if (options & SEARCH_KEEP)
1360 spats[0].off = old_off;
1361 vim_free(strcopy);
1362
1363 return retval;
1364}
1365
1366#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1367/*
1368 * search_for_exact_line(buf, pos, dir, pat)
1369 *
1370 * Search for a line starting with the given pattern (ignoring leading
1371 * white-space), starting from pos and going in direction dir. pos will
1372 * contain the position of the match found. Blank lines match only if
1373 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1374 * Return OK for success, or FAIL if no line found.
1375 */
1376 int
1377search_for_exact_line(buf, pos, dir, pat)
1378 buf_T *buf;
1379 pos_T *pos;
1380 int dir;
1381 char_u *pat;
1382{
1383 linenr_T start = 0;
1384 char_u *ptr;
1385 char_u *p;
1386
1387 if (buf->b_ml.ml_line_count == 0)
1388 return FAIL;
1389 for (;;)
1390 {
1391 pos->lnum += dir;
1392 if (pos->lnum < 1)
1393 {
1394 if (p_ws)
1395 {
1396 pos->lnum = buf->b_ml.ml_line_count;
1397 if (!shortmess(SHM_SEARCH))
1398 give_warning((char_u *)_(top_bot_msg), TRUE);
1399 }
1400 else
1401 {
1402 pos->lnum = 1;
1403 break;
1404 }
1405 }
1406 else if (pos->lnum > buf->b_ml.ml_line_count)
1407 {
1408 if (p_ws)
1409 {
1410 pos->lnum = 1;
1411 if (!shortmess(SHM_SEARCH))
1412 give_warning((char_u *)_(bot_top_msg), TRUE);
1413 }
1414 else
1415 {
1416 pos->lnum = 1;
1417 break;
1418 }
1419 }
1420 if (pos->lnum == start)
1421 break;
1422 if (start == 0)
1423 start = pos->lnum;
1424 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1425 p = skipwhite(ptr);
1426 pos->col = (colnr_T) (p - ptr);
1427
1428 /* when adding lines the matching line may be empty but it is not
1429 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001430 if ((compl_cont_status & CONT_ADDING)
1431 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001432 {
1433 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1434 return OK;
1435 }
1436 else if (*p != NUL) /* ignore empty lines */
1437 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001438 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1439 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001440 return OK;
1441 }
1442 }
1443 return FAIL;
1444}
1445#endif /* FEAT_INS_EXPAND */
1446
1447/*
1448 * Character Searches
1449 */
1450
1451/*
1452 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1453 * position of the character, otherwise move to just before the char.
1454 * Do this "cap->count1" times.
1455 * Return FAIL or OK.
1456 */
1457 int
1458searchc(cap, t_cmd)
1459 cmdarg_T *cap;
1460 int t_cmd;
1461{
1462 int c = cap->nchar; /* char to search for */
1463 int dir = cap->arg; /* TRUE for searching forward */
1464 long count = cap->count1; /* repeat count */
1465 static int lastc = NUL; /* last character searched for */
1466 static int lastcdir; /* last direction of character search */
1467 static int last_t_cmd; /* last search t_cmd */
1468 int col;
1469 char_u *p;
1470 int len;
1471#ifdef FEAT_MBYTE
1472 static char_u bytes[MB_MAXBYTES];
1473 static int bytelen = 1; /* >1 for multi-byte char */
1474#endif
1475
1476 if (c != NUL) /* normal search: remember args for repeat */
1477 {
1478 if (!KeyStuffed) /* don't remember when redoing */
1479 {
1480 lastc = c;
1481 lastcdir = dir;
1482 last_t_cmd = t_cmd;
1483#ifdef FEAT_MBYTE
1484 bytelen = (*mb_char2bytes)(c, bytes);
1485 if (cap->ncharC1 != 0)
1486 {
1487 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1488 if (cap->ncharC2 != 0)
1489 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1490 }
1491#endif
1492 }
1493 }
1494 else /* repeat previous search */
1495 {
1496 if (lastc == NUL)
1497 return FAIL;
1498 if (dir) /* repeat in opposite direction */
1499 dir = -lastcdir;
1500 else
1501 dir = lastcdir;
1502 t_cmd = last_t_cmd;
1503 c = lastc;
1504 /* For multi-byte re-use last bytes[] and bytelen. */
1505 }
1506
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001507 if (dir == BACKWARD)
1508 cap->oap->inclusive = FALSE;
1509 else
1510 cap->oap->inclusive = TRUE;
1511
Bram Moolenaar071d4272004-06-13 20:20:40 +00001512 p = ml_get_curline();
1513 col = curwin->w_cursor.col;
1514 len = (int)STRLEN(p);
1515
1516 while (count--)
1517 {
1518#ifdef FEAT_MBYTE
1519 if (has_mbyte)
1520 {
1521 for (;;)
1522 {
1523 if (dir > 0)
1524 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001525 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001526 if (col >= len)
1527 return FAIL;
1528 }
1529 else
1530 {
1531 if (col == 0)
1532 return FAIL;
1533 col -= (*mb_head_off)(p, p + col - 1) + 1;
1534 }
1535 if (bytelen == 1)
1536 {
1537 if (p[col] == c)
1538 break;
1539 }
1540 else
1541 {
1542 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1543 break;
1544 }
1545 }
1546 }
1547 else
1548#endif
1549 {
1550 for (;;)
1551 {
1552 if ((col += dir) < 0 || col >= len)
1553 return FAIL;
1554 if (p[col] == c)
1555 break;
1556 }
1557 }
1558 }
1559
1560 if (t_cmd)
1561 {
1562 /* backup to before the character (possibly double-byte) */
1563 col -= dir;
1564#ifdef FEAT_MBYTE
1565 if (has_mbyte)
1566 {
1567 if (dir < 0)
1568 /* Landed on the search char which is bytelen long */
1569 col += bytelen - 1;
1570 else
1571 /* To previous char, which may be multi-byte. */
1572 col -= (*mb_head_off)(p, p + col);
1573 }
1574#endif
1575 }
1576 curwin->w_cursor.col = col;
1577
1578 return OK;
1579}
1580
1581/*
1582 * "Other" Searches
1583 */
1584
1585/*
1586 * findmatch - find the matching paren or brace
1587 *
1588 * Improvement over vi: Braces inside quotes are ignored.
1589 */
1590 pos_T *
1591findmatch(oap, initc)
1592 oparg_T *oap;
1593 int initc;
1594{
1595 return findmatchlimit(oap, initc, 0, 0);
1596}
1597
1598/*
1599 * Return TRUE if the character before "linep[col]" equals "ch".
1600 * Return FALSE if "col" is zero.
1601 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1602 * is NULL.
1603 * Handles multibyte string correctly.
1604 */
1605 static int
1606check_prevcol(linep, col, ch, prevcol)
1607 char_u *linep;
1608 int col;
1609 int ch;
1610 int *prevcol;
1611{
1612 --col;
1613#ifdef FEAT_MBYTE
1614 if (col > 0 && has_mbyte)
1615 col -= (*mb_head_off)(linep, linep + col);
1616#endif
1617 if (prevcol)
1618 *prevcol = col;
1619 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1620}
1621
1622/*
1623 * findmatchlimit -- find the matching paren or brace, if it exists within
1624 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1625 * the edge of the file.
1626 *
1627 * "initc" is the character to find a match for. NUL means to find the
1628 * character at or after the cursor.
1629 *
1630 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1631 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1632 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1633 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001634 *
1635 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1636 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001637 */
1638
1639 pos_T *
1640findmatchlimit(oap, initc, flags, maxtravel)
1641 oparg_T *oap;
1642 int initc;
1643 int flags;
1644 int maxtravel;
1645{
1646 static pos_T pos; /* current search position */
1647 int findc = 0; /* matching brace */
1648 int c;
1649 int count = 0; /* cumulative number of braces */
1650 int backwards = FALSE; /* init for gcc */
1651 int inquote = FALSE; /* TRUE when inside quotes */
1652 char_u *linep; /* pointer to current line */
1653 char_u *ptr;
1654 int do_quotes; /* check for quotes in current line */
1655 int at_start; /* do_quotes value at start position */
1656 int hash_dir = 0; /* Direction searched for # things */
1657 int comment_dir = 0; /* Direction searched for comments */
1658 pos_T match_pos; /* Where last slash-star was found */
1659 int start_in_quotes; /* start position is in quotes */
1660 int traveled = 0; /* how far we've searched so far */
1661 int ignore_cend = FALSE; /* ignore comment end */
1662 int cpo_match; /* vi compatible matching */
1663 int cpo_bsl; /* don't recognize backslashes */
1664 int match_escaped = 0; /* search for escaped match */
1665 int dir; /* Direction to search */
1666 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001667#ifdef FEAT_LISP
1668 int lispcomm = FALSE; /* inside of Lisp-style comment */
1669 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1670#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001671
1672 pos = curwin->w_cursor;
1673 linep = ml_get(pos.lnum);
1674
1675 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1676 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1677
1678 /* Direction to search when initc is '/', '*' or '#' */
1679 if (flags & FM_BACKWARD)
1680 dir = BACKWARD;
1681 else if (flags & FM_FORWARD)
1682 dir = FORWARD;
1683 else
1684 dir = 0;
1685
1686 /*
1687 * if initc given, look in the table for the matching character
1688 * '/' and '*' are special cases: look for start or end of comment.
1689 * When '/' is used, we ignore running backwards into an star-slash, for
1690 * "[*" command, we just want to find any comment.
1691 */
1692 if (initc == '/' || initc == '*')
1693 {
1694 comment_dir = dir;
1695 if (initc == '/')
1696 ignore_cend = TRUE;
1697 backwards = (dir == FORWARD) ? FALSE : TRUE;
1698 initc = NUL;
1699 }
1700 else if (initc != '#' && initc != NUL)
1701 {
1702 /* 'matchpairs' is "x:y,x:y" */
1703 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1704 {
1705 if (*ptr == initc)
1706 {
1707 findc = initc;
1708 initc = ptr[2];
1709 backwards = TRUE;
1710 break;
1711 }
1712 ptr += 2;
1713 if (*ptr == initc)
1714 {
1715 findc = initc;
1716 initc = ptr[-2];
1717 backwards = FALSE;
1718 break;
1719 }
1720 if (ptr[1] != ',')
1721 break;
1722 }
1723 if (!findc) /* invalid initc! */
1724 return NULL;
1725 }
1726 /*
1727 * Either initc is '#', or no initc was given and we need to look under the
1728 * cursor.
1729 */
1730 else
1731 {
1732 if (initc == '#')
1733 {
1734 hash_dir = dir;
1735 }
1736 else
1737 {
1738 /*
1739 * initc was not given, must look for something to match under
1740 * or near the cursor.
1741 * Only check for special things when 'cpo' doesn't have '%'.
1742 */
1743 if (!cpo_match)
1744 {
1745 /* Are we before or at #if, #else etc.? */
1746 ptr = skipwhite(linep);
1747 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1748 {
1749 ptr = skipwhite(ptr + 1);
1750 if ( STRNCMP(ptr, "if", 2) == 0
1751 || STRNCMP(ptr, "endif", 5) == 0
1752 || STRNCMP(ptr, "el", 2) == 0)
1753 hash_dir = 1;
1754 }
1755
1756 /* Are we on a comment? */
1757 else if (linep[pos.col] == '/')
1758 {
1759 if (linep[pos.col + 1] == '*')
1760 {
1761 comment_dir = FORWARD;
1762 backwards = FALSE;
1763 pos.col++;
1764 }
1765 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1766 {
1767 comment_dir = BACKWARD;
1768 backwards = TRUE;
1769 pos.col--;
1770 }
1771 }
1772 else if (linep[pos.col] == '*')
1773 {
1774 if (linep[pos.col + 1] == '/')
1775 {
1776 comment_dir = BACKWARD;
1777 backwards = TRUE;
1778 }
1779 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1780 {
1781 comment_dir = FORWARD;
1782 backwards = FALSE;
1783 }
1784 }
1785 }
1786
1787 /*
1788 * If we are not on a comment or the # at the start of a line, then
1789 * look for brace anywhere on this line after the cursor.
1790 */
1791 if (!hash_dir && !comment_dir)
1792 {
1793 /*
1794 * Find the brace under or after the cursor.
1795 * If beyond the end of the line, use the last character in
1796 * the line.
1797 */
1798 if (linep[pos.col] == NUL && pos.col)
1799 --pos.col;
1800 for (;;)
1801 {
1802 initc = linep[pos.col];
1803 if (initc == NUL)
1804 break;
1805
1806 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1807 {
1808 if (*ptr == initc)
1809 {
1810 findc = ptr[2];
1811 backwards = FALSE;
1812 break;
1813 }
1814 ptr += 2;
1815 if (*ptr == initc)
1816 {
1817 findc = ptr[-2];
1818 backwards = TRUE;
1819 break;
1820 }
1821 if (!*++ptr)
1822 break;
1823 }
1824 if (findc)
1825 break;
1826#ifdef FEAT_MBYTE
1827 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001828 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001829 else
1830#endif
1831 ++pos.col;
1832 }
1833 if (!findc)
1834 {
1835 /* no brace in the line, maybe use " #if" then */
1836 if (!cpo_match && *skipwhite(linep) == '#')
1837 hash_dir = 1;
1838 else
1839 return NULL;
1840 }
1841 else if (!cpo_bsl)
1842 {
1843 int col, bslcnt = 0;
1844
1845 /* Set "match_escaped" if there are an odd number of
1846 * backslashes. */
1847 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1848 bslcnt++;
1849 match_escaped = (bslcnt & 1);
1850 }
1851 }
1852 }
1853 if (hash_dir)
1854 {
1855 /*
1856 * Look for matching #if, #else, #elif, or #endif
1857 */
1858 if (oap != NULL)
1859 oap->motion_type = MLINE; /* Linewise for this case only */
1860 if (initc != '#')
1861 {
1862 ptr = skipwhite(skipwhite(linep) + 1);
1863 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1864 hash_dir = 1;
1865 else if (STRNCMP(ptr, "endif", 5) == 0)
1866 hash_dir = -1;
1867 else
1868 return NULL;
1869 }
1870 pos.col = 0;
1871 while (!got_int)
1872 {
1873 if (hash_dir > 0)
1874 {
1875 if (pos.lnum == curbuf->b_ml.ml_line_count)
1876 break;
1877 }
1878 else if (pos.lnum == 1)
1879 break;
1880 pos.lnum += hash_dir;
1881 linep = ml_get(pos.lnum);
1882 line_breakcheck(); /* check for CTRL-C typed */
1883 ptr = skipwhite(linep);
1884 if (*ptr != '#')
1885 continue;
1886 pos.col = (colnr_T) (ptr - linep);
1887 ptr = skipwhite(ptr + 1);
1888 if (hash_dir > 0)
1889 {
1890 if (STRNCMP(ptr, "if", 2) == 0)
1891 count++;
1892 else if (STRNCMP(ptr, "el", 2) == 0)
1893 {
1894 if (count == 0)
1895 return &pos;
1896 }
1897 else if (STRNCMP(ptr, "endif", 5) == 0)
1898 {
1899 if (count == 0)
1900 return &pos;
1901 count--;
1902 }
1903 }
1904 else
1905 {
1906 if (STRNCMP(ptr, "if", 2) == 0)
1907 {
1908 if (count == 0)
1909 return &pos;
1910 count--;
1911 }
1912 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1913 {
1914 if (count == 0)
1915 return &pos;
1916 }
1917 else if (STRNCMP(ptr, "endif", 5) == 0)
1918 count++;
1919 }
1920 }
1921 return NULL;
1922 }
1923 }
1924
1925#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00001926 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001927 * paren/brace in the other direction. */
1928 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1929 backwards = !backwards;
1930#endif
1931
1932 do_quotes = -1;
1933 start_in_quotes = MAYBE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001934 clearpos(&match_pos);
1935
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001937 if ((backwards && comment_dir)
1938#ifdef FEAT_LISP
1939 || lisp
1940#endif
1941 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001942 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001943#ifdef FEAT_LISP
1944 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1945 lispcomm = TRUE; /* find match inside this comment */
1946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 while (!got_int)
1948 {
1949 /*
1950 * Go to the next position, forward or backward. We could use
1951 * inc() and dec() here, but that is much slower
1952 */
1953 if (backwards)
1954 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001955#ifdef FEAT_LISP
1956 /* char to match is inside of comment, don't search outside */
1957 if (lispcomm && pos.col < (colnr_T)comment_col)
1958 break;
1959#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001960 if (pos.col == 0) /* at start of line, go to prev. one */
1961 {
1962 if (pos.lnum == 1) /* start of file */
1963 break;
1964 --pos.lnum;
1965
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001966 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 break;
1968
1969 linep = ml_get(pos.lnum);
1970 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1971 do_quotes = -1;
1972 line_breakcheck();
1973
1974 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001975 if (comment_dir
1976#ifdef FEAT_LISP
1977 || lisp
1978#endif
1979 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001980 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001981#ifdef FEAT_LISP
1982 /* skip comment */
1983 if (lisp && comment_col != MAXCOL)
1984 pos.col = comment_col;
1985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 else
1988 {
1989 --pos.col;
1990#ifdef FEAT_MBYTE
1991 if (has_mbyte)
1992 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1993#endif
1994 }
1995 }
1996 else /* forward search */
1997 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001998 if (linep[pos.col] == NUL
1999 /* at end of line, go to next one */
2000#ifdef FEAT_LISP
2001 /* don't search for match in comment */
2002 || (lisp && comment_col != MAXCOL
2003 && pos.col == (colnr_T)comment_col)
2004#endif
2005 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002006 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002007 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2008#ifdef FEAT_LISP
2009 /* line is exhausted and comment with it,
2010 * don't search for match in code */
2011 || lispcomm
2012#endif
2013 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00002014 break;
2015 ++pos.lnum;
2016
2017 if (maxtravel && traveled++ > maxtravel)
2018 break;
2019
2020 linep = ml_get(pos.lnum);
2021 pos.col = 0;
2022 do_quotes = -1;
2023 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002024#ifdef FEAT_LISP
2025 if (lisp) /* find comment pos in new line */
2026 comment_col = check_linecomment(linep);
2027#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002028 }
2029 else
2030 {
2031#ifdef FEAT_MBYTE
2032 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002033 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002034 else
2035#endif
2036 ++pos.col;
2037 }
2038 }
2039
2040 /*
2041 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2042 */
2043 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2044 (linep[0] == '{' || linep[0] == '}'))
2045 {
2046 if (linep[0] == findc && count == 0) /* match! */
2047 return &pos;
2048 break; /* out of scope */
2049 }
2050
2051 if (comment_dir)
2052 {
2053 /* Note: comments do not nest, and we ignore quotes in them */
2054 /* TODO: ignore comment brackets inside strings */
2055 if (comment_dir == FORWARD)
2056 {
2057 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2058 {
2059 pos.col++;
2060 return &pos;
2061 }
2062 }
2063 else /* Searching backwards */
2064 {
2065 /*
2066 * A comment may contain / * or / /, it may also start or end
2067 * with / * /. Ignore a / * after / /.
2068 */
2069 if (pos.col == 0)
2070 continue;
2071 else if ( linep[pos.col - 1] == '/'
2072 && linep[pos.col] == '*'
2073 && (int)pos.col < comment_col)
2074 {
2075 count++;
2076 match_pos = pos;
2077 match_pos.col--;
2078 }
2079 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2080 {
2081 if (count > 0)
2082 pos = match_pos;
2083 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2084 && (int)pos.col <= comment_col)
2085 pos.col -= 2;
2086 else if (ignore_cend)
2087 continue;
2088 else
2089 return NULL;
2090 return &pos;
2091 }
2092 }
2093 continue;
2094 }
2095
2096 /*
2097 * If smart matching ('cpoptions' does not contain '%'), braces inside
2098 * of quotes are ignored, but only if there is an even number of
2099 * quotes in the line.
2100 */
2101 if (cpo_match)
2102 do_quotes = 0;
2103 else if (do_quotes == -1)
2104 {
2105 /*
2106 * Count the number of quotes in the line, skipping \" and '"'.
2107 * Watch out for "\\".
2108 */
2109 at_start = do_quotes;
2110 for (ptr = linep; *ptr; ++ptr)
2111 {
2112 if (ptr == linep + pos.col + backwards)
2113 at_start = (do_quotes & 1);
2114 if (*ptr == '"'
2115 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2116 ++do_quotes;
2117 if (*ptr == '\\' && ptr[1] != NUL)
2118 ++ptr;
2119 }
2120 do_quotes &= 1; /* result is 1 with even number of quotes */
2121
2122 /*
2123 * If we find an uneven count, check current line and previous
2124 * one for a '\' at the end.
2125 */
2126 if (!do_quotes)
2127 {
2128 inquote = FALSE;
2129 if (ptr[-1] == '\\')
2130 {
2131 do_quotes = 1;
2132 if (start_in_quotes == MAYBE)
2133 {
2134 /* Do we need to use at_start here? */
2135 inquote = TRUE;
2136 start_in_quotes = TRUE;
2137 }
2138 else if (backwards)
2139 inquote = TRUE;
2140 }
2141 if (pos.lnum > 1)
2142 {
2143 ptr = ml_get(pos.lnum - 1);
2144 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2145 {
2146 do_quotes = 1;
2147 if (start_in_quotes == MAYBE)
2148 {
2149 inquote = at_start;
2150 if (inquote)
2151 start_in_quotes = TRUE;
2152 }
2153 else if (!backwards)
2154 inquote = TRUE;
2155 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002156
2157 /* ml_get() only keeps one line, need to get linep again */
2158 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002159 }
2160 }
2161 }
2162 if (start_in_quotes == MAYBE)
2163 start_in_quotes = FALSE;
2164
2165 /*
2166 * If 'smartmatch' is set:
2167 * Things inside quotes are ignored by setting 'inquote'. If we
2168 * find a quote without a preceding '\' invert 'inquote'. At the
2169 * end of a line not ending in '\' we reset 'inquote'.
2170 *
2171 * In lines with an uneven number of quotes (without preceding '\')
2172 * we do not know which part to ignore. Therefore we only set
2173 * inquote if the number of quotes in a line is even, unless this
2174 * line or the previous one ends in a '\'. Complicated, isn't it?
2175 */
2176 switch (c = linep[pos.col])
2177 {
2178 case NUL:
2179 /* at end of line without trailing backslash, reset inquote */
2180 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2181 {
2182 inquote = FALSE;
2183 start_in_quotes = FALSE;
2184 }
2185 break;
2186
2187 case '"':
2188 /* a quote that is preceded with an odd number of backslashes is
2189 * ignored */
2190 if (do_quotes)
2191 {
2192 int col;
2193
2194 for (col = pos.col - 1; col >= 0; --col)
2195 if (linep[col] != '\\')
2196 break;
2197 if ((((int)pos.col - 1 - col) & 1) == 0)
2198 {
2199 inquote = !inquote;
2200 start_in_quotes = FALSE;
2201 }
2202 }
2203 break;
2204
2205 /*
2206 * If smart matching ('cpoptions' does not contain '%'):
2207 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2208 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2209 * skipped, there is never a brace in them.
2210 * Ignore this when finding matches for `'.
2211 */
2212 case '\'':
2213 if (!cpo_match && initc != '\'' && findc != '\'')
2214 {
2215 if (backwards)
2216 {
2217 if (pos.col > 1)
2218 {
2219 if (linep[pos.col - 2] == '\'')
2220 {
2221 pos.col -= 2;
2222 break;
2223 }
2224 else if (linep[pos.col - 2] == '\\' &&
2225 pos.col > 2 && linep[pos.col - 3] == '\'')
2226 {
2227 pos.col -= 3;
2228 break;
2229 }
2230 }
2231 }
2232 else if (linep[pos.col + 1]) /* forward search */
2233 {
2234 if (linep[pos.col + 1] == '\\' &&
2235 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2236 {
2237 pos.col += 3;
2238 break;
2239 }
2240 else if (linep[pos.col + 2] == '\'')
2241 {
2242 pos.col += 2;
2243 break;
2244 }
2245 }
2246 }
2247 /* FALLTHROUGH */
2248
2249 default:
2250#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002251 /*
2252 * For Lisp skip over backslashed (), {} and [].
2253 * (actually, we skip #\( et al)
2254 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002255 if (curbuf->b_p_lisp
2256 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002257 && pos.col > 1
2258 && check_prevcol(linep, pos.col, '\\', NULL)
2259 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002260 break;
2261#endif
2262
2263 /* Check for match outside of quotes, and inside of
2264 * quotes when the start is also inside of quotes. */
2265 if ((!inquote || start_in_quotes == TRUE)
2266 && (c == initc || c == findc))
2267 {
2268 int col, bslcnt = 0;
2269
2270 if (!cpo_bsl)
2271 {
2272 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2273 bslcnt++;
2274 }
2275 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2276 * what we expect. */
2277 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2278 {
2279 if (c == initc)
2280 count++;
2281 else
2282 {
2283 if (count == 0)
2284 return &pos;
2285 count--;
2286 }
2287 }
2288 }
2289 }
2290 }
2291
2292 if (comment_dir == BACKWARD && count > 0)
2293 {
2294 pos = match_pos;
2295 return &pos;
2296 }
2297 return (pos_T *)NULL; /* never found it */
2298}
2299
2300/*
2301 * Check if line[] contains a / / comment.
2302 * Return MAXCOL if not, otherwise return the column.
2303 * TODO: skip strings.
2304 */
2305 static int
2306check_linecomment(line)
2307 char_u *line;
2308{
2309 char_u *p;
2310
2311 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002312#ifdef FEAT_LISP
2313 /* skip Lispish one-line comments */
2314 if (curbuf->b_p_lisp)
2315 {
2316 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2317 {
2318 int instr = FALSE; /* inside of string */
2319
2320 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002321 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002322 {
2323 if (*p == '"')
2324 {
2325 if (instr)
2326 {
2327 if (*(p - 1) != '\\') /* skip escaped quote */
2328 instr = FALSE;
2329 }
2330 else if (p == line || ((p - line) >= 2
2331 /* skip #\" form */
2332 && *(p - 1) != '\\' && *(p - 2) != '#'))
2333 instr = TRUE;
2334 }
2335 else if (!instr && ((p - line) < 2
2336 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2337 break; /* found! */
2338 ++p;
2339 }
2340 }
2341 else
2342 p = NULL;
2343 }
2344 else
2345#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002346 while ((p = vim_strchr(p, '/')) != NULL)
2347 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002348 /* accept a double /, unless it's preceded with * and followed by *,
2349 * because * / / * is an end and start of a C comment */
2350 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002351 break;
2352 ++p;
2353 }
2354
2355 if (p == NULL)
2356 return MAXCOL;
2357 return (int)(p - line);
2358}
2359
2360/*
2361 * Move cursor briefly to character matching the one under the cursor.
2362 * Used for Insert mode and "r" command.
2363 * Show the match only if it is visible on the screen.
2364 * If there isn't a match, then beep.
2365 */
2366 void
2367showmatch(c)
2368 int c; /* char to show match for */
2369{
2370 pos_T *lpos, save_cursor;
2371 pos_T mpos;
2372 colnr_T vcol;
2373 long save_so;
2374 long save_siso;
2375#ifdef CURSOR_SHAPE
2376 int save_state;
2377#endif
2378 colnr_T save_dollar_vcol;
2379 char_u *p;
2380
2381 /*
2382 * Only show match for chars in the 'matchpairs' option.
2383 */
2384 /* 'matchpairs' is "x:y,x:y" */
2385 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2386 {
2387#ifdef FEAT_RIGHTLEFT
2388 if (*p == c && (curwin->w_p_rl ^ p_ri))
2389 break;
2390#endif
2391 p += 2;
2392 if (*p == c
2393#ifdef FEAT_RIGHTLEFT
2394 && !(curwin->w_p_rl ^ p_ri)
2395#endif
2396 )
2397 break;
2398 if (p[1] != ',')
2399 return;
2400 }
2401
2402 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2403 vim_beep();
2404 else if (lpos->lnum >= curwin->w_topline)
2405 {
2406 if (!curwin->w_p_wrap)
2407 getvcol(curwin, lpos, NULL, &vcol, NULL);
2408 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2409 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2410 {
2411 mpos = *lpos; /* save the pos, update_screen() may change it */
2412 save_cursor = curwin->w_cursor;
2413 save_so = p_so;
2414 save_siso = p_siso;
2415 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2416 * stop displaying the "$". */
2417 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2418 dollar_vcol = 0;
2419 ++curwin->w_virtcol; /* do display ')' just before "$" */
2420 update_screen(VALID); /* show the new char first */
2421
2422 save_dollar_vcol = dollar_vcol;
2423#ifdef CURSOR_SHAPE
2424 save_state = State;
2425 State = SHOWMATCH;
2426 ui_cursor_shape(); /* may show different cursor shape */
2427#endif
2428 curwin->w_cursor = mpos; /* move to matching char */
2429 p_so = 0; /* don't use 'scrolloff' here */
2430 p_siso = 0; /* don't use 'sidescrolloff' here */
2431 showruler(FALSE);
2432 setcursor();
2433 cursor_on(); /* make sure that the cursor is shown */
2434 out_flush();
2435#ifdef FEAT_GUI
2436 if (gui.in_use)
2437 {
2438 gui_update_cursor(TRUE, FALSE);
2439 gui_mch_flush();
2440 }
2441#endif
2442 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2443 * which resets it if the matching position is in a previous line
2444 * and has a higher column number. */
2445 dollar_vcol = save_dollar_vcol;
2446
2447 /*
2448 * brief pause, unless 'm' is present in 'cpo' and a character is
2449 * available.
2450 */
2451 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2452 ui_delay(p_mat * 100L, TRUE);
2453 else if (!char_avail())
2454 ui_delay(p_mat * 100L, FALSE);
2455 curwin->w_cursor = save_cursor; /* restore cursor position */
2456 p_so = save_so;
2457 p_siso = save_siso;
2458#ifdef CURSOR_SHAPE
2459 State = save_state;
2460 ui_cursor_shape(); /* may show different cursor shape */
2461#endif
2462 }
2463 }
2464}
2465
2466/*
2467 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002468 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002469 * space or a line break. Also stop at an empty line.
2470 * Return OK if the next sentence was found.
2471 */
2472 int
2473findsent(dir, count)
2474 int dir;
2475 long count;
2476{
2477 pos_T pos, tpos;
2478 int c;
2479 int (*func) __ARGS((pos_T *));
2480 int startlnum;
2481 int noskip = FALSE; /* do not skip blanks */
2482 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002483 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002484
2485 pos = curwin->w_cursor;
2486 if (dir == FORWARD)
2487 func = incl;
2488 else
2489 func = decl;
2490
2491 while (count--)
2492 {
2493 /*
2494 * if on an empty line, skip upto a non-empty line
2495 */
2496 if (gchar_pos(&pos) == NUL)
2497 {
2498 do
2499 if ((*func)(&pos) == -1)
2500 break;
2501 while (gchar_pos(&pos) == NUL);
2502 if (dir == FORWARD)
2503 goto found;
2504 }
2505 /*
2506 * if on the start of a paragraph or a section and searching forward,
2507 * go to the next line
2508 */
2509 else if (dir == FORWARD && pos.col == 0 &&
2510 startPS(pos.lnum, NUL, FALSE))
2511 {
2512 if (pos.lnum == curbuf->b_ml.ml_line_count)
2513 return FAIL;
2514 ++pos.lnum;
2515 goto found;
2516 }
2517 else if (dir == BACKWARD)
2518 decl(&pos);
2519
2520 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002521 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002522 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2523 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2524 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002525 if (vim_strchr((char_u *)".!?", c) != NULL)
2526 {
2527 /* Only skip over a '.', '!' and '?' once. */
2528 if (found_dot)
2529 break;
2530 found_dot = TRUE;
2531 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532 if (decl(&pos) == -1)
2533 break;
2534 /* when going forward: Stop in front of empty line */
2535 if (lineempty(pos.lnum) && dir == FORWARD)
2536 {
2537 incl(&pos);
2538 goto found;
2539 }
2540 }
2541
2542 /* remember the line where the search started */
2543 startlnum = pos.lnum;
2544 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2545
2546 for (;;) /* find end of sentence */
2547 {
2548 c = gchar_pos(&pos);
2549 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2550 {
2551 if (dir == BACKWARD && pos.lnum != startlnum)
2552 ++pos.lnum;
2553 break;
2554 }
2555 if (c == '.' || c == '!' || c == '?')
2556 {
2557 tpos = pos;
2558 do
2559 if ((c = inc(&tpos)) == -1)
2560 break;
2561 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2562 != NULL);
2563 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2564 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2565 && gchar_pos(&tpos) == ' ')))
2566 {
2567 pos = tpos;
2568 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2569 inc(&pos);
2570 break;
2571 }
2572 }
2573 if ((*func)(&pos) == -1)
2574 {
2575 if (count)
2576 return FAIL;
2577 noskip = TRUE;
2578 break;
2579 }
2580 }
2581found:
2582 /* skip white space */
2583 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2584 if (incl(&pos) == -1)
2585 break;
2586 }
2587
2588 setpcmark();
2589 curwin->w_cursor = pos;
2590 return OK;
2591}
2592
2593/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002594 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002595 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002596 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002597 * If 'what' is '{' or '}' we go to the next section.
2598 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002599 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002600 */
2601 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002602findpar(pincl, dir, count, what, both)
2603 int *pincl; /* Return: TRUE if last char is to be included */
2604 int dir;
2605 long count;
2606 int what;
2607 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002608{
2609 linenr_T curr;
2610 int did_skip; /* TRUE after separating lines have been skipped */
2611 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002612 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002613#ifdef FEAT_FOLDING
2614 linenr_T fold_first; /* first line of a closed fold */
2615 linenr_T fold_last; /* last line of a closed fold */
2616 int fold_skipped; /* TRUE if a closed fold was skipped this
2617 iteration */
2618#endif
2619
2620 curr = curwin->w_cursor.lnum;
2621
2622 while (count--)
2623 {
2624 did_skip = FALSE;
2625 for (first = TRUE; ; first = FALSE)
2626 {
2627 if (*ml_get(curr) != NUL)
2628 did_skip = TRUE;
2629
2630#ifdef FEAT_FOLDING
2631 /* skip folded lines */
2632 fold_skipped = FALSE;
2633 if (first && hasFolding(curr, &fold_first, &fold_last))
2634 {
2635 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2636 fold_skipped = TRUE;
2637 }
2638#endif
2639
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002640 /* POSIX has it's own ideas of what a paragraph boundary is and it
2641 * doesn't match historical Vi: It also stops at a "{" in the
2642 * first column and at an empty line. */
2643 if (!first && did_skip && (startPS(curr, what, both)
2644 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002645 break;
2646
2647#ifdef FEAT_FOLDING
2648 if (fold_skipped)
2649 curr -= dir;
2650#endif
2651 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2652 {
2653 if (count)
2654 return FALSE;
2655 curr -= dir;
2656 break;
2657 }
2658 }
2659 }
2660 setpcmark();
2661 if (both && *ml_get(curr) == '}') /* include line with '}' */
2662 ++curr;
2663 curwin->w_cursor.lnum = curr;
2664 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2665 {
2666 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2667 {
2668 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002669 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002670 }
2671 }
2672 else
2673 curwin->w_cursor.col = 0;
2674 return TRUE;
2675}
2676
2677/*
2678 * check if the string 's' is a nroff macro that is in option 'opt'
2679 */
2680 static int
2681inmacro(opt, s)
2682 char_u *opt;
2683 char_u *s;
2684{
2685 char_u *macro;
2686
2687 for (macro = opt; macro[0]; ++macro)
2688 {
2689 /* Accept two characters in the option being equal to two characters
2690 * in the line. A space in the option matches with a space in the
2691 * line or the line having ended. */
2692 if ( (macro[0] == s[0]
2693 || (macro[0] == ' '
2694 && (s[0] == NUL || s[0] == ' ')))
2695 && (macro[1] == s[1]
2696 || ((macro[1] == NUL || macro[1] == ' ')
2697 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2698 break;
2699 ++macro;
2700 if (macro[0] == NUL)
2701 break;
2702 }
2703 return (macro[0] != NUL);
2704}
2705
2706/*
2707 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2708 * If 'para' is '{' or '}' only check for sections.
2709 * If 'both' is TRUE also stop at '}'
2710 */
2711 int
2712startPS(lnum, para, both)
2713 linenr_T lnum;
2714 int para;
2715 int both;
2716{
2717 char_u *s;
2718
2719 s = ml_get(lnum);
2720 if (*s == para || *s == '\f' || (both && *s == '}'))
2721 return TRUE;
2722 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2723 (!para && inmacro(p_para, s + 1))))
2724 return TRUE;
2725 return FALSE;
2726}
2727
2728/*
2729 * The following routines do the word searches performed by the 'w', 'W',
2730 * 'b', 'B', 'e', and 'E' commands.
2731 */
2732
2733/*
2734 * To perform these searches, characters are placed into one of three
2735 * classes, and transitions between classes determine word boundaries.
2736 *
2737 * The classes are:
2738 *
2739 * 0 - white space
2740 * 1 - punctuation
2741 * 2 or higher - keyword characters (letters, digits and underscore)
2742 */
2743
2744static int cls_bigword; /* TRUE for "W", "B" or "E" */
2745
2746/*
2747 * cls() - returns the class of character at curwin->w_cursor
2748 *
2749 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2750 * from class 2 and higher are reported as class 1 since only white space
2751 * boundaries are of interest.
2752 */
2753 static int
2754cls()
2755{
2756 int c;
2757
2758 c = gchar_cursor();
2759#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2760 if (p_altkeymap && c == F_BLANK)
2761 return 0;
2762#endif
2763 if (c == ' ' || c == '\t' || c == NUL)
2764 return 0;
2765#ifdef FEAT_MBYTE
2766 if (enc_dbcs != 0 && c > 0xFF)
2767 {
2768 /* If cls_bigword, report multi-byte chars as class 1. */
2769 if (enc_dbcs == DBCS_KOR && cls_bigword)
2770 return 1;
2771
2772 /* process code leading/trailing bytes */
2773 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2774 }
2775 if (enc_utf8)
2776 {
2777 c = utf_class(c);
2778 if (c != 0 && cls_bigword)
2779 return 1;
2780 return c;
2781 }
2782#endif
2783
2784 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2785 if (cls_bigword)
2786 return 1;
2787
2788 if (vim_iswordc(c))
2789 return 2;
2790 return 1;
2791}
2792
2793
2794/*
2795 * fwd_word(count, type, eol) - move forward one word
2796 *
2797 * Returns FAIL if the cursor was already at the end of the file.
2798 * If eol is TRUE, last word stops at end of line (for operators).
2799 */
2800 int
2801fwd_word(count, bigword, eol)
2802 long count;
2803 int bigword; /* "W", "E" or "B" */
2804 int eol;
2805{
2806 int sclass; /* starting class */
2807 int i;
2808 int last_line;
2809
2810#ifdef FEAT_VIRTUALEDIT
2811 curwin->w_cursor.coladd = 0;
2812#endif
2813 cls_bigword = bigword;
2814 while (--count >= 0)
2815 {
2816#ifdef FEAT_FOLDING
2817 /* When inside a range of folded lines, move to the last char of the
2818 * last line. */
2819 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2820 coladvance((colnr_T)MAXCOL);
2821#endif
2822 sclass = cls();
2823
2824 /*
2825 * We always move at least one character, unless on the last
2826 * character in the buffer.
2827 */
2828 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2829 i = inc_cursor();
2830 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2831 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00002832 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002833 return OK;
2834
2835 /*
2836 * Go one char past end of current word (if any)
2837 */
2838 if (sclass != 0)
2839 while (cls() == sclass)
2840 {
2841 i = inc_cursor();
2842 if (i == -1 || (i >= 1 && eol && count == 0))
2843 return OK;
2844 }
2845
2846 /*
2847 * go to next non-white
2848 */
2849 while (cls() == 0)
2850 {
2851 /*
2852 * We'll stop if we land on a blank line
2853 */
2854 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2855 break;
2856
2857 i = inc_cursor();
2858 if (i == -1 || (i >= 1 && eol && count == 0))
2859 return OK;
2860 }
2861 }
2862 return OK;
2863}
2864
2865/*
2866 * bck_word() - move backward 'count' words
2867 *
2868 * If stop is TRUE and we are already on the start of a word, move one less.
2869 *
2870 * Returns FAIL if top of the file was reached.
2871 */
2872 int
2873bck_word(count, bigword, stop)
2874 long count;
2875 int bigword;
2876 int stop;
2877{
2878 int sclass; /* starting class */
2879
2880#ifdef FEAT_VIRTUALEDIT
2881 curwin->w_cursor.coladd = 0;
2882#endif
2883 cls_bigword = bigword;
2884 while (--count >= 0)
2885 {
2886#ifdef FEAT_FOLDING
2887 /* When inside a range of folded lines, move to the first char of the
2888 * first line. */
2889 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2890 curwin->w_cursor.col = 0;
2891#endif
2892 sclass = cls();
2893 if (dec_cursor() == -1) /* started at start of file */
2894 return FAIL;
2895
2896 if (!stop || sclass == cls() || sclass == 0)
2897 {
2898 /*
2899 * Skip white space before the word.
2900 * Stop on an empty line.
2901 */
2902 while (cls() == 0)
2903 {
2904 if (curwin->w_cursor.col == 0
2905 && lineempty(curwin->w_cursor.lnum))
2906 goto finished;
2907 if (dec_cursor() == -1) /* hit start of file, stop here */
2908 return OK;
2909 }
2910
2911 /*
2912 * Move backward to start of this word.
2913 */
2914 if (skip_chars(cls(), BACKWARD))
2915 return OK;
2916 }
2917
2918 inc_cursor(); /* overshot - forward one */
2919finished:
2920 stop = FALSE;
2921 }
2922 return OK;
2923}
2924
2925/*
2926 * end_word() - move to the end of the word
2927 *
2928 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2929 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2930 * motion crosses blank lines. When the real vi crosses a blank line in an
2931 * 'e' motion, the cursor is placed on the FIRST character of the next
2932 * non-blank line. The 'E' command, however, works correctly. Since this
2933 * appears to be a bug, I have not duplicated it here.
2934 *
2935 * Returns FAIL if end of the file was reached.
2936 *
2937 * If stop is TRUE and we are already on the end of a word, move one less.
2938 * If empty is TRUE stop on an empty line.
2939 */
2940 int
2941end_word(count, bigword, stop, empty)
2942 long count;
2943 int bigword;
2944 int stop;
2945 int empty;
2946{
2947 int sclass; /* starting class */
2948
2949#ifdef FEAT_VIRTUALEDIT
2950 curwin->w_cursor.coladd = 0;
2951#endif
2952 cls_bigword = bigword;
2953 while (--count >= 0)
2954 {
2955#ifdef FEAT_FOLDING
2956 /* When inside a range of folded lines, move to the last char of the
2957 * last line. */
2958 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2959 coladvance((colnr_T)MAXCOL);
2960#endif
2961 sclass = cls();
2962 if (inc_cursor() == -1)
2963 return FAIL;
2964
2965 /*
2966 * If we're in the middle of a word, we just have to move to the end
2967 * of it.
2968 */
2969 if (cls() == sclass && sclass != 0)
2970 {
2971 /*
2972 * Move forward to end of the current word
2973 */
2974 if (skip_chars(sclass, FORWARD))
2975 return FAIL;
2976 }
2977 else if (!stop || sclass == 0)
2978 {
2979 /*
2980 * We were at the end of a word. Go to the end of the next word.
2981 * First skip white space, if 'empty' is TRUE, stop at empty line.
2982 */
2983 while (cls() == 0)
2984 {
2985 if (empty && curwin->w_cursor.col == 0
2986 && lineempty(curwin->w_cursor.lnum))
2987 goto finished;
2988 if (inc_cursor() == -1) /* hit end of file, stop here */
2989 return FAIL;
2990 }
2991
2992 /*
2993 * Move forward to the end of this word.
2994 */
2995 if (skip_chars(cls(), FORWARD))
2996 return FAIL;
2997 }
2998 dec_cursor(); /* overshot - one char backward */
2999finished:
3000 stop = FALSE; /* we move only one word less */
3001 }
3002 return OK;
3003}
3004
3005/*
3006 * Move back to the end of the word.
3007 *
3008 * Returns FAIL if start of the file was reached.
3009 */
3010 int
3011bckend_word(count, bigword, eol)
3012 long count;
3013 int bigword; /* TRUE for "B" */
3014 int eol; /* TRUE: stop at end of line. */
3015{
3016 int sclass; /* starting class */
3017 int i;
3018
3019#ifdef FEAT_VIRTUALEDIT
3020 curwin->w_cursor.coladd = 0;
3021#endif
3022 cls_bigword = bigword;
3023 while (--count >= 0)
3024 {
3025 sclass = cls();
3026 if ((i = dec_cursor()) == -1)
3027 return FAIL;
3028 if (eol && i == 1)
3029 return OK;
3030
3031 /*
3032 * Move backward to before the start of this word.
3033 */
3034 if (sclass != 0)
3035 {
3036 while (cls() == sclass)
3037 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3038 return OK;
3039 }
3040
3041 /*
3042 * Move backward to end of the previous word
3043 */
3044 while (cls() == 0)
3045 {
3046 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3047 break;
3048 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3049 return OK;
3050 }
3051 }
3052 return OK;
3053}
3054
3055/*
3056 * Skip a row of characters of the same class.
3057 * Return TRUE when end-of-file reached, FALSE otherwise.
3058 */
3059 static int
3060skip_chars(cclass, dir)
3061 int cclass;
3062 int dir;
3063{
3064 while (cls() == cclass)
3065 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3066 return TRUE;
3067 return FALSE;
3068}
3069
3070#ifdef FEAT_TEXTOBJ
3071/*
3072 * Go back to the start of the word or the start of white space
3073 */
3074 static void
3075back_in_line()
3076{
3077 int sclass; /* starting class */
3078
3079 sclass = cls();
3080 for (;;)
3081 {
3082 if (curwin->w_cursor.col == 0) /* stop at start of line */
3083 break;
3084 dec_cursor();
3085 if (cls() != sclass) /* stop at start of word */
3086 {
3087 inc_cursor();
3088 break;
3089 }
3090 }
3091}
3092
3093 static void
3094find_first_blank(posp)
3095 pos_T *posp;
3096{
3097 int c;
3098
3099 while (decl(posp) != -1)
3100 {
3101 c = gchar_pos(posp);
3102 if (!vim_iswhite(c))
3103 {
3104 incl(posp);
3105 break;
3106 }
3107 }
3108}
3109
3110/*
3111 * Skip count/2 sentences and count/2 separating white spaces.
3112 */
3113 static void
3114findsent_forward(count, at_start_sent)
3115 long count;
3116 int at_start_sent; /* cursor is at start of sentence */
3117{
3118 while (count--)
3119 {
3120 findsent(FORWARD, 1L);
3121 if (at_start_sent)
3122 find_first_blank(&curwin->w_cursor);
3123 if (count == 0 || at_start_sent)
3124 decl(&curwin->w_cursor);
3125 at_start_sent = !at_start_sent;
3126 }
3127}
3128
3129/*
3130 * Find word under cursor, cursor at end.
3131 * Used while an operator is pending, and in Visual mode.
3132 */
3133 int
3134current_word(oap, count, include, bigword)
3135 oparg_T *oap;
3136 long count;
3137 int include; /* TRUE: include word and white space */
3138 int bigword; /* FALSE == word, TRUE == WORD */
3139{
3140 pos_T start_pos;
3141 pos_T pos;
3142 int inclusive = TRUE;
3143 int include_white = FALSE;
3144
3145 cls_bigword = bigword;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003146 clearpos(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003147
3148#ifdef FEAT_VISUAL
3149 /* Correct cursor when 'selection' is exclusive */
3150 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3151 dec_cursor();
3152
3153 /*
3154 * When Visual mode is not active, or when the VIsual area is only one
3155 * character, select the word and/or white space under the cursor.
3156 */
3157 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3158#endif
3159 {
3160 /*
3161 * Go to start of current word or white space.
3162 */
3163 back_in_line();
3164 start_pos = curwin->w_cursor;
3165
3166 /*
3167 * If the start is on white space, and white space should be included
3168 * (" word"), or start is not on white space, and white space should
3169 * not be included ("word"), find end of word.
3170 */
3171 if ((cls() == 0) == include)
3172 {
3173 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3174 return FAIL;
3175 }
3176 else
3177 {
3178 /*
3179 * If the start is not on white space, and white space should be
3180 * included ("word "), or start is on white space and white
3181 * space should not be included (" "), find start of word.
3182 * If we end up in the first column of the next line (single char
3183 * word) back up to end of the line.
3184 */
3185 fwd_word(1L, bigword, TRUE);
3186 if (curwin->w_cursor.col == 0)
3187 decl(&curwin->w_cursor);
3188 else
3189 oneleft();
3190
3191 if (include)
3192 include_white = TRUE;
3193 }
3194
3195#ifdef FEAT_VISUAL
3196 if (VIsual_active)
3197 {
3198 /* should do something when inclusive == FALSE ! */
3199 VIsual = start_pos;
3200 redraw_curbuf_later(INVERTED); /* update the inversion */
3201 }
3202 else
3203#endif
3204 {
3205 oap->start = start_pos;
3206 oap->motion_type = MCHAR;
3207 }
3208 --count;
3209 }
3210
3211 /*
3212 * When count is still > 0, extend with more objects.
3213 */
3214 while (count > 0)
3215 {
3216 inclusive = TRUE;
3217#ifdef FEAT_VISUAL
3218 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3219 {
3220 /*
3221 * In Visual mode, with cursor at start: move cursor back.
3222 */
3223 if (decl(&curwin->w_cursor) == -1)
3224 return FAIL;
3225 if (include != (cls() != 0))
3226 {
3227 if (bck_word(1L, bigword, TRUE) == FAIL)
3228 return FAIL;
3229 }
3230 else
3231 {
3232 if (bckend_word(1L, bigword, TRUE) == FAIL)
3233 return FAIL;
3234 (void)incl(&curwin->w_cursor);
3235 }
3236 }
3237 else
3238#endif
3239 {
3240 /*
3241 * Move cursor forward one word and/or white area.
3242 */
3243 if (incl(&curwin->w_cursor) == -1)
3244 return FAIL;
3245 if (include != (cls() == 0))
3246 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003247 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003248 return FAIL;
3249 /*
3250 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003251 * the first character on the line.
3252 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003253 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003254 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003255 inclusive = FALSE;
3256 }
3257 else
3258 {
3259 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3260 return FAIL;
3261 }
3262 }
3263 --count;
3264 }
3265
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003266 if (include_white && (cls() != 0
3267 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003268 {
3269 /*
3270 * If we don't include white space at the end, move the start
3271 * to include some white space there. This makes "daw" work
3272 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003273 * word). Also when "2daw" deletes "word." at the end of the line
3274 * (cursor is at start of next line).
3275 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003276 */
3277 pos = curwin->w_cursor; /* save cursor position */
3278 curwin->w_cursor = start_pos;
3279 if (oneleft() == OK)
3280 {
3281 back_in_line();
3282 if (cls() == 0 && curwin->w_cursor.col > 0)
3283 {
3284#ifdef FEAT_VISUAL
3285 if (VIsual_active)
3286 VIsual = curwin->w_cursor;
3287 else
3288#endif
3289 oap->start = curwin->w_cursor;
3290 }
3291 }
3292 curwin->w_cursor = pos; /* put cursor back at end */
3293 }
3294
3295#ifdef FEAT_VISUAL
3296 if (VIsual_active)
3297 {
3298 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3299 inc_cursor();
3300 if (VIsual_mode == 'V')
3301 {
3302 VIsual_mode = 'v';
3303 redraw_cmdline = TRUE; /* show mode later */
3304 }
3305 }
3306 else
3307#endif
3308 oap->inclusive = inclusive;
3309
3310 return OK;
3311}
3312
3313/*
3314 * Find sentence(s) under the cursor, cursor at end.
3315 * When Visual active, extend it by one or more sentences.
3316 */
3317 int
3318current_sent(oap, count, include)
3319 oparg_T *oap;
3320 long count;
3321 int include;
3322{
3323 pos_T start_pos;
3324 pos_T pos;
3325 int start_blank;
3326 int c;
3327 int at_start_sent;
3328 long ncount;
3329
3330 start_pos = curwin->w_cursor;
3331 pos = start_pos;
3332 findsent(FORWARD, 1L); /* Find start of next sentence. */
3333
3334#ifdef FEAT_VISUAL
3335 /*
3336 * When visual area is bigger than one character: Extend it.
3337 */
3338 if (VIsual_active && !equalpos(start_pos, VIsual))
3339 {
3340extend:
3341 if (lt(start_pos, VIsual))
3342 {
3343 /*
3344 * Cursor at start of Visual area.
3345 * Find out where we are:
3346 * - in the white space before a sentence
3347 * - in a sentence or just after it
3348 * - at the start of a sentence
3349 */
3350 at_start_sent = TRUE;
3351 decl(&pos);
3352 while (lt(pos, curwin->w_cursor))
3353 {
3354 c = gchar_pos(&pos);
3355 if (!vim_iswhite(c))
3356 {
3357 at_start_sent = FALSE;
3358 break;
3359 }
3360 incl(&pos);
3361 }
3362 if (!at_start_sent)
3363 {
3364 findsent(BACKWARD, 1L);
3365 if (equalpos(curwin->w_cursor, start_pos))
3366 at_start_sent = TRUE; /* exactly at start of sentence */
3367 else
3368 /* inside a sentence, go to its end (start of next) */
3369 findsent(FORWARD, 1L);
3370 }
3371 if (include) /* "as" gets twice as much as "is" */
3372 count *= 2;
3373 while (count--)
3374 {
3375 if (at_start_sent)
3376 find_first_blank(&curwin->w_cursor);
3377 c = gchar_cursor();
3378 if (!at_start_sent || (!include && !vim_iswhite(c)))
3379 findsent(BACKWARD, 1L);
3380 at_start_sent = !at_start_sent;
3381 }
3382 }
3383 else
3384 {
3385 /*
3386 * Cursor at end of Visual area.
3387 * Find out where we are:
3388 * - just before a sentence
3389 * - just before or in the white space before a sentence
3390 * - in a sentence
3391 */
3392 incl(&pos);
3393 at_start_sent = TRUE;
3394 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3395 {
3396 at_start_sent = FALSE;
3397 while (lt(pos, curwin->w_cursor))
3398 {
3399 c = gchar_pos(&pos);
3400 if (!vim_iswhite(c))
3401 {
3402 at_start_sent = TRUE;
3403 break;
3404 }
3405 incl(&pos);
3406 }
3407 if (at_start_sent) /* in the sentence */
3408 findsent(BACKWARD, 1L);
3409 else /* in/before white before a sentence */
3410 curwin->w_cursor = start_pos;
3411 }
3412
3413 if (include) /* "as" gets twice as much as "is" */
3414 count *= 2;
3415 findsent_forward(count, at_start_sent);
3416 if (*p_sel == 'e')
3417 ++curwin->w_cursor.col;
3418 }
3419 return OK;
3420 }
3421#endif
3422
3423 /*
3424 * If cursor started on blank, check if it is just before the start of the
3425 * next sentence.
3426 */
3427 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3428 incl(&pos);
3429 if (equalpos(pos, curwin->w_cursor))
3430 {
3431 start_blank = TRUE;
3432 find_first_blank(&start_pos); /* go back to first blank */
3433 }
3434 else
3435 {
3436 start_blank = FALSE;
3437 findsent(BACKWARD, 1L);
3438 start_pos = curwin->w_cursor;
3439 }
3440 if (include)
3441 ncount = count * 2;
3442 else
3443 {
3444 ncount = count;
3445 if (start_blank)
3446 --ncount;
3447 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003448 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003449 findsent_forward(ncount, TRUE);
3450 else
3451 decl(&curwin->w_cursor);
3452
3453 if (include)
3454 {
3455 /*
3456 * If the blank in front of the sentence is included, exclude the
3457 * blanks at the end of the sentence, go back to the first blank.
3458 * If there are no trailing blanks, try to include leading blanks.
3459 */
3460 if (start_blank)
3461 {
3462 find_first_blank(&curwin->w_cursor);
3463 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3464 if (vim_iswhite(c))
3465 decl(&curwin->w_cursor);
3466 }
3467 else if (c = gchar_cursor(), !vim_iswhite(c))
3468 find_first_blank(&start_pos);
3469 }
3470
3471#ifdef FEAT_VISUAL
3472 if (VIsual_active)
3473 {
3474 /* avoid getting stuck with "is" on a single space before a sent. */
3475 if (equalpos(start_pos, curwin->w_cursor))
3476 goto extend;
3477 if (*p_sel == 'e')
3478 ++curwin->w_cursor.col;
3479 VIsual = start_pos;
3480 VIsual_mode = 'v';
3481 redraw_curbuf_later(INVERTED); /* update the inversion */
3482 }
3483 else
3484#endif
3485 {
3486 /* include a newline after the sentence, if there is one */
3487 if (incl(&curwin->w_cursor) == -1)
3488 oap->inclusive = TRUE;
3489 else
3490 oap->inclusive = FALSE;
3491 oap->start = start_pos;
3492 oap->motion_type = MCHAR;
3493 }
3494 return OK;
3495}
3496
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003497/*
3498 * Find block under the cursor, cursor at end.
3499 * "what" and "other" are two matching parenthesis/paren/etc.
3500 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003501 int
3502current_block(oap, count, include, what, other)
3503 oparg_T *oap;
3504 long count;
3505 int include; /* TRUE == include white space */
3506 int what; /* '(', '{', etc. */
3507 int other; /* ')', '}', etc. */
3508{
3509 pos_T old_pos;
3510 pos_T *pos = NULL;
3511 pos_T start_pos;
3512 pos_T *end_pos;
3513 pos_T old_start, old_end;
3514 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003515 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516
3517 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003518 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003519 old_start = old_end;
3520
3521 /*
3522 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3523 */
3524#ifdef FEAT_VISUAL
3525 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3526#endif
3527 {
3528 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003529 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003530 while (inindent(1))
3531 if (inc_cursor() != 0)
3532 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003533 if (gchar_cursor() == what)
3534 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003535 ++curwin->w_cursor.col;
3536 }
3537#ifdef FEAT_VISUAL
3538 else if (lt(VIsual, curwin->w_cursor))
3539 {
3540 old_start = VIsual;
3541 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3542 }
3543 else
3544 old_end = VIsual;
3545#endif
3546
3547 /*
3548 * Search backwards for unclosed '(', '{', etc..
3549 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003550 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003551 */
3552 save_cpo = p_cpo;
3553 p_cpo = (char_u *)"%";
3554 while (count-- > 0)
3555 {
3556 if ((pos = findmatch(NULL, what)) == NULL)
3557 break;
3558 curwin->w_cursor = *pos;
3559 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3560 }
3561 p_cpo = save_cpo;
3562
3563 /*
3564 * Search for matching ')', '}', etc.
3565 * Put this position in curwin->w_cursor.
3566 */
3567 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3568 {
3569 curwin->w_cursor = old_pos;
3570 return FAIL;
3571 }
3572 curwin->w_cursor = *end_pos;
3573
3574 /*
3575 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3576 * If the ending '}' is only preceded by indent, skip that indent.
3577 * But only if the resulting area is not smaller than what we started with.
3578 */
3579 while (!include)
3580 {
3581 incl(&start_pos);
3582 sol = (curwin->w_cursor.col == 0);
3583 decl(&curwin->w_cursor);
3584 if (what == '{')
3585 while (inindent(1))
3586 {
3587 sol = TRUE;
3588 if (decl(&curwin->w_cursor) != 0)
3589 break;
3590 }
3591#ifdef FEAT_VISUAL
3592 /*
3593 * In Visual mode, when the resulting area is not bigger than what we
3594 * started with, extend it to the next block, and then exclude again.
3595 */
3596 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3597 && VIsual_active)
3598 {
3599 curwin->w_cursor = old_start;
3600 decl(&curwin->w_cursor);
3601 if ((pos = findmatch(NULL, what)) == NULL)
3602 {
3603 curwin->w_cursor = old_pos;
3604 return FAIL;
3605 }
3606 start_pos = *pos;
3607 curwin->w_cursor = *pos;
3608 if ((end_pos = findmatch(NULL, other)) == NULL)
3609 {
3610 curwin->w_cursor = old_pos;
3611 return FAIL;
3612 }
3613 curwin->w_cursor = *end_pos;
3614 }
3615 else
3616#endif
3617 break;
3618 }
3619
3620#ifdef FEAT_VISUAL
3621 if (VIsual_active)
3622 {
3623 if (*p_sel == 'e')
3624 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003625 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003626 inc(&curwin->w_cursor); /* include the line break */
3627 VIsual = start_pos;
3628 VIsual_mode = 'v';
3629 redraw_curbuf_later(INVERTED); /* update the inversion */
3630 showmode();
3631 }
3632 else
3633#endif
3634 {
3635 oap->start = start_pos;
3636 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003637 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003638 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003639 incl(&curwin->w_cursor);
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003640 else if (lt(start_pos, curwin->w_cursor))
3641 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003642 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003643 else
3644 /* End is before the start (no text in between <>, [], etc.): don't
3645 * operate on any text. */
3646 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003647 }
3648
3649 return OK;
3650}
3651
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003652static int in_html_tag __ARGS((int));
3653
3654/*
3655 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3656 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3657 */
3658 static int
3659in_html_tag(end_tag)
3660 int end_tag;
3661{
3662 char_u *line = ml_get_curline();
3663 char_u *p;
3664 int c;
3665 int lc = NUL;
3666 pos_T pos;
3667
3668#ifdef FEAT_MBYTE
3669 if (enc_dbcs)
3670 {
3671 char_u *lp = NULL;
3672
3673 /* We search forward until the cursor, because searching backwards is
3674 * very slow for DBCS encodings. */
3675 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3676 if (*p == '>' || *p == '<')
3677 {
3678 lc = *p;
3679 lp = p;
3680 }
3681 if (*p != '<') /* check for '<' under cursor */
3682 {
3683 if (lc != '<')
3684 return FALSE;
3685 p = lp;
3686 }
3687 }
3688 else
3689#endif
3690 {
3691 for (p = line + curwin->w_cursor.col; p > line; )
3692 {
3693 if (*p == '<') /* find '<' under/before cursor */
3694 break;
3695 mb_ptr_back(line, p);
3696 if (*p == '>') /* find '>' before cursor */
3697 break;
3698 }
3699 if (*p != '<')
3700 return FALSE;
3701 }
3702
3703 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003704 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003705
3706 mb_ptr_adv(p);
3707 if (end_tag)
3708 /* check that there is a '/' after the '<' */
3709 return *p == '/';
3710
3711 /* check that there is no '/' after the '<' */
3712 if (*p == '/')
3713 return FALSE;
3714
3715 /* check that the matching '>' is not preceded by '/' */
3716 for (;;)
3717 {
3718 if (inc(&pos) < 0)
3719 return FALSE;
3720 c = *ml_get_pos(&pos);
3721 if (c == '>')
3722 break;
3723 lc = c;
3724 }
3725 return lc != '/';
3726}
3727
3728/*
3729 * Find tag block under the cursor, cursor at end.
3730 */
3731 int
3732current_tagblock(oap, count_arg, include)
3733 oparg_T *oap;
3734 long count_arg;
3735 int include; /* TRUE == include white space */
3736{
3737 long count = count_arg;
3738 long n;
3739 pos_T old_pos;
3740 pos_T start_pos;
3741 pos_T end_pos;
3742 pos_T old_start, old_end;
3743 char_u *spat, *epat;
3744 char_u *p;
3745 char_u *cp;
3746 int len;
3747 int r;
3748 int do_include = include;
3749 int save_p_ws = p_ws;
3750 int retval = FAIL;
3751
3752 p_ws = FALSE;
3753
3754 old_pos = curwin->w_cursor;
3755 old_end = curwin->w_cursor; /* remember where we started */
3756 old_start = old_end;
3757
3758 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003759 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003760 */
3761#ifdef FEAT_VISUAL
3762 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3763#endif
3764 {
3765 setpcmark();
3766
3767 /* ignore indent */
3768 while (inindent(1))
3769 if (inc_cursor() != 0)
3770 break;
3771
3772 if (in_html_tag(FALSE))
3773 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003774 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003775 while (*ml_get_cursor() != '>')
3776 if (inc_cursor() < 0)
3777 break;
3778 }
3779 else if (in_html_tag(TRUE))
3780 {
3781 /* cursor on end tag, move to just before it */
3782 while (*ml_get_cursor() != '<')
3783 if (dec_cursor() < 0)
3784 break;
3785 dec_cursor();
3786 old_end = curwin->w_cursor;
3787 }
3788 }
3789#ifdef FEAT_VISUAL
3790 else if (lt(VIsual, curwin->w_cursor))
3791 {
3792 old_start = VIsual;
3793 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3794 }
3795 else
3796 old_end = VIsual;
3797#endif
3798
3799again:
3800 /*
3801 * Search backwards for unclosed "<aaa>".
3802 * Put this position in start_pos.
3803 */
3804 for (n = 0; n < count; ++n)
3805 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003806 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003807 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003808 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00003809 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003810 {
3811 curwin->w_cursor = old_pos;
3812 goto theend;
3813 }
3814 }
3815 start_pos = curwin->w_cursor;
3816
3817 /*
3818 * Search for matching "</aaa>". First isolate the "aaa".
3819 */
3820 inc_cursor();
3821 p = ml_get_cursor();
3822 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3823 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003824 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003825 if (len == 0)
3826 {
3827 curwin->w_cursor = old_pos;
3828 goto theend;
3829 }
3830 spat = alloc(len + 29);
3831 epat = alloc(len + 9);
3832 if (spat == NULL || epat == NULL)
3833 {
3834 vim_free(spat);
3835 vim_free(epat);
3836 curwin->w_cursor = old_pos;
3837 goto theend;
3838 }
3839 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3840 sprintf((char *)epat, "</%.*s>\\c", len, p);
3841
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003842 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
Bram Moolenaar76929292008-01-06 19:07:36 +00003843 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003844
3845 vim_free(spat);
3846 vim_free(epat);
3847
3848 if (r < 1 || lt(curwin->w_cursor, old_end))
3849 {
3850 /* Can't find other end or it's before the previous end. Could be a
3851 * HTML tag that doesn't have a matching end. Search backwards for
3852 * another starting tag. */
3853 count = 1;
3854 curwin->w_cursor = start_pos;
3855 goto again;
3856 }
3857
3858 if (do_include || r < 1)
3859 {
3860 /* Include up to the '>'. */
3861 while (*ml_get_cursor() != '>')
3862 if (inc_cursor() < 0)
3863 break;
3864 }
3865 else
3866 {
3867 /* Exclude the '<' of the end tag. */
3868 if (*ml_get_cursor() == '<')
3869 dec_cursor();
3870 }
3871 end_pos = curwin->w_cursor;
3872
3873 if (!do_include)
3874 {
3875 /* Exclude the start tag. */
3876 curwin->w_cursor = start_pos;
3877 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003878 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003879 {
3880 inc_cursor();
3881 start_pos = curwin->w_cursor;
3882 break;
3883 }
3884 curwin->w_cursor = end_pos;
3885
Bram Moolenaar45360022005-07-21 21:08:21 +00003886 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003887 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003888 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003889 {
3890 do_include = TRUE;
3891 curwin->w_cursor = old_start;
3892 count = count_arg;
3893 goto again;
3894 }
3895 }
3896
3897#ifdef FEAT_VISUAL
3898 if (VIsual_active)
3899 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003900 /* If the end is before the start there is no text between tags, select
3901 * the char under the cursor. */
3902 if (lt(end_pos, start_pos))
3903 curwin->w_cursor = start_pos;
3904 else if (*p_sel == 'e')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003905 ++curwin->w_cursor.col;
3906 VIsual = start_pos;
3907 VIsual_mode = 'v';
3908 redraw_curbuf_later(INVERTED); /* update the inversion */
3909 showmode();
3910 }
3911 else
3912#endif
3913 {
3914 oap->start = start_pos;
3915 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003916 if (lt(end_pos, start_pos))
3917 {
3918 /* End is before the start: there is no text between tags; operate
3919 * on an empty area. */
3920 curwin->w_cursor = start_pos;
3921 oap->inclusive = FALSE;
3922 }
3923 else
3924 oap->inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003925 }
3926 retval = OK;
3927
3928theend:
3929 p_ws = save_p_ws;
3930 return retval;
3931}
3932
Bram Moolenaar071d4272004-06-13 20:20:40 +00003933 int
3934current_par(oap, count, include, type)
3935 oparg_T *oap;
3936 long count;
3937 int include; /* TRUE == include white space */
3938 int type; /* 'p' for paragraph, 'S' for section */
3939{
3940 linenr_T start_lnum;
3941 linenr_T end_lnum;
3942 int white_in_front;
3943 int dir;
3944 int start_is_white;
3945 int prev_start_is_white;
3946 int retval = OK;
3947 int do_white = FALSE;
3948 int t;
3949 int i;
3950
3951 if (type == 'S') /* not implemented yet */
3952 return FAIL;
3953
3954 start_lnum = curwin->w_cursor.lnum;
3955
3956#ifdef FEAT_VISUAL
3957 /*
3958 * When visual area is more than one line: extend it.
3959 */
3960 if (VIsual_active && start_lnum != VIsual.lnum)
3961 {
3962extend:
3963 if (start_lnum < VIsual.lnum)
3964 dir = BACKWARD;
3965 else
3966 dir = FORWARD;
3967 for (i = count; --i >= 0; )
3968 {
3969 if (start_lnum ==
3970 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3971 {
3972 retval = FAIL;
3973 break;
3974 }
3975
3976 prev_start_is_white = -1;
3977 for (t = 0; t < 2; ++t)
3978 {
3979 start_lnum += dir;
3980 start_is_white = linewhite(start_lnum);
3981 if (prev_start_is_white == start_is_white)
3982 {
3983 start_lnum -= dir;
3984 break;
3985 }
3986 for (;;)
3987 {
3988 if (start_lnum == (dir == BACKWARD
3989 ? 1 : curbuf->b_ml.ml_line_count))
3990 break;
3991 if (start_is_white != linewhite(start_lnum + dir)
3992 || (!start_is_white
3993 && startPS(start_lnum + (dir > 0
3994 ? 1 : 0), 0, 0)))
3995 break;
3996 start_lnum += dir;
3997 }
3998 if (!include)
3999 break;
4000 if (start_lnum == (dir == BACKWARD
4001 ? 1 : curbuf->b_ml.ml_line_count))
4002 break;
4003 prev_start_is_white = start_is_white;
4004 }
4005 }
4006 curwin->w_cursor.lnum = start_lnum;
4007 curwin->w_cursor.col = 0;
4008 return retval;
4009 }
4010#endif
4011
4012 /*
4013 * First move back to the start_lnum of the paragraph or white lines
4014 */
4015 white_in_front = linewhite(start_lnum);
4016 while (start_lnum > 1)
4017 {
4018 if (white_in_front) /* stop at first white line */
4019 {
4020 if (!linewhite(start_lnum - 1))
4021 break;
4022 }
4023 else /* stop at first non-white line of start of paragraph */
4024 {
4025 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4026 break;
4027 }
4028 --start_lnum;
4029 }
4030
4031 /*
4032 * Move past the end of any white lines.
4033 */
4034 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004035 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4036 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004037
4038 --end_lnum;
4039 i = count;
4040 if (!include && white_in_front)
4041 --i;
4042 while (i--)
4043 {
4044 if (end_lnum == curbuf->b_ml.ml_line_count)
4045 return FAIL;
4046
4047 if (!include)
4048 do_white = linewhite(end_lnum + 1);
4049
4050 if (include || !do_white)
4051 {
4052 ++end_lnum;
4053 /*
4054 * skip to end of paragraph
4055 */
4056 while (end_lnum < curbuf->b_ml.ml_line_count
4057 && !linewhite(end_lnum + 1)
4058 && !startPS(end_lnum + 1, 0, 0))
4059 ++end_lnum;
4060 }
4061
4062 if (i == 0 && white_in_front && include)
4063 break;
4064
4065 /*
4066 * skip to end of white lines after paragraph
4067 */
4068 if (include || do_white)
4069 while (end_lnum < curbuf->b_ml.ml_line_count
4070 && linewhite(end_lnum + 1))
4071 ++end_lnum;
4072 }
4073
4074 /*
4075 * If there are no empty lines at the end, try to find some empty lines at
4076 * the start (unless that has been done already).
4077 */
4078 if (!white_in_front && !linewhite(end_lnum) && include)
4079 while (start_lnum > 1 && linewhite(start_lnum - 1))
4080 --start_lnum;
4081
4082#ifdef FEAT_VISUAL
4083 if (VIsual_active)
4084 {
4085 /* Problem: when doing "Vipipip" nothing happens in a single white
4086 * line, we get stuck there. Trap this here. */
4087 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4088 goto extend;
4089 VIsual.lnum = start_lnum;
4090 VIsual_mode = 'V';
4091 redraw_curbuf_later(INVERTED); /* update the inversion */
4092 showmode();
4093 }
4094 else
4095#endif
4096 {
4097 oap->start.lnum = start_lnum;
4098 oap->start.col = 0;
4099 oap->motion_type = MLINE;
4100 }
4101 curwin->w_cursor.lnum = end_lnum;
4102 curwin->w_cursor.col = 0;
4103
4104 return OK;
4105}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004106
4107static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4108static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4109
4110/*
4111 * Search quote char from string line[col].
4112 * Quote character escaped by one of the characters in "escape" is not counted
4113 * as a quote.
4114 * Returns column number of "quotechar" or -1 when not found.
4115 */
4116 static int
4117find_next_quote(line, col, quotechar, escape)
4118 char_u *line;
4119 int col;
4120 int quotechar;
4121 char_u *escape; /* escape characters, can be NULL */
4122{
4123 int c;
4124
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004125 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004126 {
4127 c = line[col];
4128 if (c == NUL)
4129 return -1;
4130 else if (escape != NULL && vim_strchr(escape, c))
4131 ++col;
4132 else if (c == quotechar)
4133 break;
4134#ifdef FEAT_MBYTE
4135 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004136 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004137 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004138#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004139 ++col;
4140 }
4141 return col;
4142}
4143
4144/*
4145 * Search backwards in "line" from column "col_start" to find "quotechar".
4146 * Quote character escaped by one of the characters in "escape" is not counted
4147 * as a quote.
4148 * Return the found column or zero.
4149 */
4150 static int
4151find_prev_quote(line, col_start, quotechar, escape)
4152 char_u *line;
4153 int col_start;
4154 int quotechar;
4155 char_u *escape; /* escape characters, can be NULL */
4156{
4157 int n;
4158
4159 while (col_start > 0)
4160 {
4161 --col_start;
4162#ifdef FEAT_MBYTE
4163 col_start -= (*mb_head_off)(line, line + col_start);
4164#endif
4165 n = 0;
4166 if (escape != NULL)
4167 while (col_start - n > 0 && vim_strchr(escape,
4168 line[col_start - n - 1]) != NULL)
4169 ++n;
4170 if (n & 1)
4171 col_start -= n; /* uneven number of escape chars, skip it */
4172 else if (line[col_start] == quotechar)
4173 break;
4174 }
4175 return col_start;
4176}
4177
4178/*
4179 * Find quote under the cursor, cursor at end.
4180 * Returns TRUE if found, else FALSE.
4181 */
4182 int
4183current_quote(oap, count, include, quotechar)
4184 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004185 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004186 int include; /* TRUE == include quote char */
4187 int quotechar; /* Quote character */
4188{
4189 char_u *line = ml_get_curline();
4190 int col_end;
4191 int col_start = curwin->w_cursor.col;
4192 int inclusive = FALSE;
4193#ifdef FEAT_VISUAL
4194 int vis_empty = TRUE; /* Visual selection <= 1 char */
4195 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004196 int inside_quotes = FALSE; /* Looks like "i'" done before */
4197 int selected_quote = FALSE; /* Has quote inside selection */
4198 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004199
4200 /* Correct cursor when 'selection' is exclusive */
4201 if (VIsual_active)
4202 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004203 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004204 if (*p_sel == 'e' && vis_bef_curs)
4205 dec_cursor();
4206 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004207 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004208
4209 if (!vis_empty)
4210 {
4211 /* Check if the existing selection exactly spans the text inside
4212 * quotes. */
4213 if (vis_bef_curs)
4214 {
4215 inside_quotes = VIsual.col > 0
4216 && line[VIsual.col - 1] == quotechar
4217 && line[curwin->w_cursor.col] != NUL
4218 && line[curwin->w_cursor.col + 1] == quotechar;
4219 i = VIsual.col;
4220 col_end = curwin->w_cursor.col;
4221 }
4222 else
4223 {
4224 inside_quotes = curwin->w_cursor.col > 0
4225 && line[curwin->w_cursor.col - 1] == quotechar
4226 && line[VIsual.col] != NUL
4227 && line[VIsual.col + 1] == quotechar;
4228 i = curwin->w_cursor.col;
4229 col_end = VIsual.col;
4230 }
4231
4232 /* Find out if we have a quote in the selection. */
4233 while (i <= col_end)
4234 if (line[i++] == quotechar)
4235 {
4236 selected_quote = TRUE;
4237 break;
4238 }
4239 }
4240
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004241 if (!vis_empty && line[col_start] == quotechar)
4242 {
4243 /* Already selecting something and on a quote character. Find the
4244 * next quoted string. */
4245 if (vis_bef_curs)
4246 {
4247 /* Assume we are on a closing quote: move to after the next
4248 * opening quote. */
4249 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4250 if (col_start < 0)
4251 return FALSE;
4252 col_end = find_next_quote(line, col_start + 1, quotechar,
4253 curbuf->b_p_qe);
4254 if (col_end < 0)
4255 {
4256 /* We were on a starting quote perhaps? */
4257 col_end = col_start;
4258 col_start = curwin->w_cursor.col;
4259 }
4260 }
4261 else
4262 {
4263 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4264 if (line[col_end] != quotechar)
4265 return FALSE;
4266 col_start = find_prev_quote(line, col_end, quotechar,
4267 curbuf->b_p_qe);
4268 if (line[col_start] != quotechar)
4269 {
4270 /* We were on an ending quote perhaps? */
4271 col_start = col_end;
4272 col_end = curwin->w_cursor.col;
4273 }
4274 }
4275 }
4276 else
4277#endif
4278
4279 if (line[col_start] == quotechar
4280#ifdef FEAT_VISUAL
4281 || !vis_empty
4282#endif
4283 )
4284 {
4285 int first_col = col_start;
4286
4287#ifdef FEAT_VISUAL
4288 if (!vis_empty)
4289 {
4290 if (vis_bef_curs)
4291 first_col = find_next_quote(line, col_start, quotechar, NULL);
4292 else
4293 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4294 }
4295#endif
4296 /* The cursor is on a quote, we don't know if it's the opening or
4297 * closing quote. Search from the start of the line to find out.
4298 * Also do this when there is a Visual area, a' may leave the cursor
4299 * in between two strings. */
4300 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004301 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004302 {
4303 /* Find open quote character. */
4304 col_start = find_next_quote(line, col_start, quotechar, NULL);
4305 if (col_start < 0 || col_start > first_col)
4306 return FALSE;
4307 /* Find close quote character. */
4308 col_end = find_next_quote(line, col_start + 1, quotechar,
4309 curbuf->b_p_qe);
4310 if (col_end < 0)
4311 return FALSE;
4312 /* If is cursor between start and end quote character, it is
4313 * target text object. */
4314 if (col_start <= first_col && first_col <= col_end)
4315 break;
4316 col_start = col_end + 1;
4317 }
4318 }
4319 else
4320 {
4321 /* Search backward for a starting quote. */
4322 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4323 if (line[col_start] != quotechar)
4324 {
4325 /* No quote before the cursor, look after the cursor. */
4326 col_start = find_next_quote(line, col_start, quotechar, NULL);
4327 if (col_start < 0)
4328 return FALSE;
4329 }
4330
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 }
4337
4338 /* When "include" is TRUE, include spaces after closing quote or before
4339 * the starting quote. */
4340 if (include)
4341 {
4342 if (vim_iswhite(line[col_end + 1]))
4343 while (vim_iswhite(line[col_end + 1]))
4344 ++col_end;
4345 else
4346 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4347 --col_start;
4348 }
4349
Bram Moolenaarab194812005-09-14 21:40:12 +00004350 /* Set start position. After vi" another i" must include the ".
4351 * For v2i" include the quotes. */
4352 if (!include && count < 2
4353#ifdef FEAT_VISUAL
4354 && (vis_empty || !inside_quotes)
4355#endif
4356 )
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004357 ++col_start;
4358 curwin->w_cursor.col = col_start;
4359#ifdef FEAT_VISUAL
4360 if (VIsual_active)
4361 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004362 /* Set the start of the Visual area when the Visual area was empty, we
4363 * were just inside quotes or the Visual area didn't start at a quote
4364 * and didn't include a quote.
4365 */
4366 if (vis_empty
4367 || (vis_bef_curs
4368 && !selected_quote
4369 && (inside_quotes
4370 || (line[VIsual.col] != quotechar
4371 && (VIsual.col == 0
4372 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004373 {
4374 VIsual = curwin->w_cursor;
4375 redraw_curbuf_later(INVERTED);
4376 }
4377 }
4378 else
4379#endif
4380 {
4381 oap->start = curwin->w_cursor;
4382 oap->motion_type = MCHAR;
4383 }
4384
4385 /* Set end position. */
4386 curwin->w_cursor.col = col_end;
Bram Moolenaarab194812005-09-14 21:40:12 +00004387 if ((include || count > 1
4388#ifdef FEAT_VISUAL
4389 /* After vi" another i" must include the ". */
4390 || (!vis_empty && inside_quotes)
4391#endif
4392 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004393 inclusive = TRUE;
4394#ifdef FEAT_VISUAL
4395 if (VIsual_active)
4396 {
4397 if (vis_empty || vis_bef_curs)
4398 {
4399 /* decrement cursor when 'selection' is not exclusive */
4400 if (*p_sel != 'e')
4401 dec_cursor();
4402 }
4403 else
4404 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004405 /* Cursor is at start of Visual area. Set the end of the Visual
4406 * area when it was just inside quotes or it didn't end at a
4407 * quote. */
4408 if (inside_quotes
4409 || (!selected_quote
4410 && line[VIsual.col] != quotechar
4411 && (line[VIsual.col] == NUL
4412 || line[VIsual.col + 1] != quotechar)))
4413 {
4414 dec_cursor();
4415 VIsual = curwin->w_cursor;
4416 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004417 curwin->w_cursor.col = col_start;
4418 }
4419 if (VIsual_mode == 'V')
4420 {
4421 VIsual_mode = 'v';
4422 redraw_cmdline = TRUE; /* show mode later */
4423 }
4424 }
4425 else
4426#endif
4427 {
4428 /* Set inclusive and other oap's flags. */
4429 oap->inclusive = inclusive;
4430 }
4431
4432 return OK;
4433}
4434
4435#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004436
4437#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4438 || defined(PROTO)
4439/*
4440 * return TRUE if line 'lnum' is empty or has white chars only.
4441 */
4442 int
4443linewhite(lnum)
4444 linenr_T lnum;
4445{
4446 char_u *p;
4447
4448 p = skipwhite(ml_get(lnum));
4449 return (*p == NUL);
4450}
4451#endif
4452
4453#if defined(FEAT_FIND_ID) || defined(PROTO)
4454/*
4455 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004456 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004457 */
4458/*ARGSUSED*/
4459 void
4460find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4461 type, count, action, start_lnum, end_lnum)
4462 char_u *ptr; /* pointer to search pattern */
4463 int dir; /* direction of expansion */
4464 int len; /* length of search pattern */
4465 int whole; /* match whole words only */
4466 int skip_comments; /* don't match inside comments */
4467 int type; /* Type of search; are we looking for a type?
4468 a macro? */
4469 long count;
4470 int action; /* What to do when we find it */
4471 linenr_T start_lnum; /* first line to start searching */
4472 linenr_T end_lnum; /* last line for searching */
4473{
4474 SearchedFile *files; /* Stack of included files */
4475 SearchedFile *bigger; /* When we need more space */
4476 int max_path_depth = 50;
4477 long match_count = 1;
4478
4479 char_u *pat;
4480 char_u *new_fname;
4481 char_u *curr_fname = curbuf->b_fname;
4482 char_u *prev_fname = NULL;
4483 linenr_T lnum;
4484 int depth;
4485 int depth_displayed; /* For type==CHECK_PATH */
4486 int old_files;
4487 int already_searched;
4488 char_u *file_line;
4489 char_u *line;
4490 char_u *p;
4491 char_u save_char;
4492 int define_matched;
4493 regmatch_T regmatch;
4494 regmatch_T incl_regmatch;
4495 regmatch_T def_regmatch;
4496 int matched = FALSE;
4497 int did_show = FALSE;
4498 int found = FALSE;
4499 int i;
4500 char_u *already = NULL;
4501 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004502 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004503#ifdef RISCOS
4504 int previous_munging = __riscosify_control;
4505#endif
4506#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4507 win_T *curwin_save = NULL;
4508#endif
4509
4510 regmatch.regprog = NULL;
4511 incl_regmatch.regprog = NULL;
4512 def_regmatch.regprog = NULL;
4513
4514 file_line = alloc(LSIZE);
4515 if (file_line == NULL)
4516 return;
4517
4518#ifdef RISCOS
4519 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004520 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004521#endif
4522
4523 if (type != CHECK_PATH && type != FIND_DEFINE
4524#ifdef FEAT_INS_EXPAND
4525 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4526 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004527 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004528#endif
4529 )
4530 {
4531 pat = alloc(len + 5);
4532 if (pat == NULL)
4533 goto fpip_end;
4534 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4535 /* ignore case according to p_ic, p_scs and pat */
4536 regmatch.rm_ic = ignorecase(pat);
4537 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4538 vim_free(pat);
4539 if (regmatch.regprog == NULL)
4540 goto fpip_end;
4541 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004542 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4543 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004544 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004545 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004546 if (incl_regmatch.regprog == NULL)
4547 goto fpip_end;
4548 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4549 }
4550 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4551 {
4552 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4553 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4554 if (def_regmatch.regprog == NULL)
4555 goto fpip_end;
4556 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4557 }
4558 files = (SearchedFile *)lalloc_clear((long_u)
4559 (max_path_depth * sizeof(SearchedFile)), TRUE);
4560 if (files == NULL)
4561 goto fpip_end;
4562 old_files = max_path_depth;
4563 depth = depth_displayed = -1;
4564
4565 lnum = start_lnum;
4566 if (end_lnum > curbuf->b_ml.ml_line_count)
4567 end_lnum = curbuf->b_ml.ml_line_count;
4568 if (lnum > end_lnum) /* do at least one line */
4569 lnum = end_lnum;
4570 line = ml_get(lnum);
4571
4572 for (;;)
4573 {
4574 if (incl_regmatch.regprog != NULL
4575 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4576 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004577 char_u *p_fname = (curr_fname == curbuf->b_fname)
4578 ? curbuf->b_ffname : curr_fname;
4579
4580 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4581 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4582 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004583 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004584 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4585 else
4586 /* Use text after match with 'include'. */
4587 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004588 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004589 already_searched = FALSE;
4590 if (new_fname != NULL)
4591 {
4592 /* Check whether we have already searched in this file */
4593 for (i = 0;; i++)
4594 {
4595 if (i == depth + 1)
4596 i = old_files;
4597 if (i == max_path_depth)
4598 break;
4599 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4600 {
4601 if (type != CHECK_PATH &&
4602 action == ACTION_SHOW_ALL && files[i].matched)
4603 {
4604 msg_putchar('\n'); /* cursor below last one */
4605 if (!got_int) /* don't display if 'q'
4606 typed at "--more--"
4607 mesage */
4608 {
4609 msg_home_replace_hl(new_fname);
4610 MSG_PUTS(_(" (includes previously listed match)"));
4611 prev_fname = NULL;
4612 }
4613 }
4614 vim_free(new_fname);
4615 new_fname = NULL;
4616 already_searched = TRUE;
4617 break;
4618 }
4619 }
4620 }
4621
4622 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4623 || (new_fname == NULL && !already_searched)))
4624 {
4625 if (did_show)
4626 msg_putchar('\n'); /* cursor below last one */
4627 else
4628 {
4629 gotocmdline(TRUE); /* cursor at status line */
4630 MSG_PUTS_TITLE(_("--- Included files "));
4631 if (action != ACTION_SHOW_ALL)
4632 MSG_PUTS_TITLE(_("not found "));
4633 MSG_PUTS_TITLE(_("in path ---\n"));
4634 }
4635 did_show = TRUE;
4636 while (depth_displayed < depth && !got_int)
4637 {
4638 ++depth_displayed;
4639 for (i = 0; i < depth_displayed; i++)
4640 MSG_PUTS(" ");
4641 msg_home_replace(files[depth_displayed].name);
4642 MSG_PUTS(" -->\n");
4643 }
4644 if (!got_int) /* don't display if 'q' typed
4645 for "--more--" message */
4646 {
4647 for (i = 0; i <= depth_displayed; i++)
4648 MSG_PUTS(" ");
4649 if (new_fname != NULL)
4650 {
4651 /* using "new_fname" is more reliable, e.g., when
4652 * 'includeexpr' is set. */
4653 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4654 }
4655 else
4656 {
4657 /*
4658 * Isolate the file name.
4659 * Include the surrounding "" or <> if present.
4660 */
4661 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4662 ;
4663 for (i = 0; vim_isfilec(p[i]); i++)
4664 ;
4665 if (i == 0)
4666 {
4667 /* Nothing found, use the rest of the line. */
4668 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004669 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004670 }
4671 else
4672 {
4673 if (p[-1] == '"' || p[-1] == '<')
4674 {
4675 --p;
4676 ++i;
4677 }
4678 if (p[i] == '"' || p[i] == '>')
4679 ++i;
4680 }
4681 save_char = p[i];
4682 p[i] = NUL;
4683 msg_outtrans_attr(p, hl_attr(HLF_D));
4684 p[i] = save_char;
4685 }
4686
4687 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4688 {
4689 if (already_searched)
4690 MSG_PUTS(_(" (Already listed)"));
4691 else
4692 MSG_PUTS(_(" NOT FOUND"));
4693 }
4694 }
4695 out_flush(); /* output each line directly */
4696 }
4697
4698 if (new_fname != NULL)
4699 {
4700 /* Push the new file onto the file stack */
4701 if (depth + 1 == old_files)
4702 {
4703 bigger = (SearchedFile *)lalloc((long_u)(
4704 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4705 if (bigger != NULL)
4706 {
4707 for (i = 0; i <= depth; i++)
4708 bigger[i] = files[i];
4709 for (i = depth + 1; i < old_files + max_path_depth; i++)
4710 {
4711 bigger[i].fp = NULL;
4712 bigger[i].name = NULL;
4713 bigger[i].lnum = 0;
4714 bigger[i].matched = FALSE;
4715 }
4716 for (i = old_files; i < max_path_depth; i++)
4717 bigger[i + max_path_depth] = files[i];
4718 old_files += max_path_depth;
4719 max_path_depth *= 2;
4720 vim_free(files);
4721 files = bigger;
4722 }
4723 }
4724 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4725 == NULL)
4726 vim_free(new_fname);
4727 else
4728 {
4729 if (++depth == old_files)
4730 {
4731 /*
4732 * lalloc() for 'bigger' must have failed above. We
4733 * will forget one of our already visited files now.
4734 */
4735 vim_free(files[old_files].name);
4736 ++old_files;
4737 }
4738 files[depth].name = curr_fname = new_fname;
4739 files[depth].lnum = 0;
4740 files[depth].matched = FALSE;
4741#ifdef FEAT_INS_EXPAND
4742 if (action == ACTION_EXPAND)
4743 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004744 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00004745 vim_snprintf((char*)IObuff, IOSIZE,
4746 _("Scanning included file: %s"),
4747 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004748 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4749 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004750 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004751#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004752 if (p_verbose >= 5)
4753 {
4754 verbose_enter();
4755 smsg((char_u *)_("Searching included file %s"),
4756 (char *)new_fname);
4757 verbose_leave();
4758 }
4759
Bram Moolenaar071d4272004-06-13 20:20:40 +00004760 }
4761 }
4762 }
4763 else
4764 {
4765 /*
4766 * Check if the line is a define (type == FIND_DEFINE)
4767 */
4768 p = line;
4769search_line:
4770 define_matched = FALSE;
4771 if (def_regmatch.regprog != NULL
4772 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4773 {
4774 /*
4775 * Pattern must be first identifier after 'define', so skip
4776 * to that position before checking for match of pattern. Also
4777 * don't let it match beyond the end of this identifier.
4778 */
4779 p = def_regmatch.endp[0];
4780 while (*p && !vim_iswordc(*p))
4781 p++;
4782 define_matched = TRUE;
4783 }
4784
4785 /*
4786 * Look for a match. Don't do this if we are looking for a
4787 * define and this line didn't match define_prog above.
4788 */
4789 if (def_regmatch.regprog == NULL || define_matched)
4790 {
4791 if (define_matched
4792#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004793 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004794#endif
4795 )
4796 {
4797 /* compare the first "len" chars from "ptr" */
4798 startp = skipwhite(p);
4799 if (p_ic)
4800 matched = !MB_STRNICMP(startp, ptr, len);
4801 else
4802 matched = !STRNCMP(startp, ptr, len);
4803 if (matched && define_matched && whole
4804 && vim_iswordc(startp[len]))
4805 matched = FALSE;
4806 }
4807 else if (regmatch.regprog != NULL
4808 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4809 {
4810 matched = TRUE;
4811 startp = regmatch.startp[0];
4812 /*
4813 * Check if the line is not a comment line (unless we are
4814 * looking for a define). A line starting with "# define"
4815 * is not considered to be a comment line.
4816 */
4817 if (!define_matched && skip_comments)
4818 {
4819#ifdef FEAT_COMMENTS
4820 if ((*line != '#' ||
4821 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4822 && get_leader_len(line, NULL, FALSE))
4823 matched = FALSE;
4824
4825 /*
4826 * Also check for a "/ *" or "/ /" before the match.
4827 * Skips lines like "int backwards; / * normal index
4828 * * /" when looking for "normal".
4829 * Note: Doesn't skip "/ *" in comments.
4830 */
4831 p = skipwhite(line);
4832 if (matched
4833 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4834#endif
4835 for (p = line; *p && p < startp; ++p)
4836 {
4837 if (matched
4838 && p[0] == '/'
4839 && (p[1] == '*' || p[1] == '/'))
4840 {
4841 matched = FALSE;
4842 /* After "//" all text is comment */
4843 if (p[1] == '/')
4844 break;
4845 ++p;
4846 }
4847 else if (!matched && p[0] == '*' && p[1] == '/')
4848 {
4849 /* Can find match after "* /". */
4850 matched = TRUE;
4851 ++p;
4852 }
4853 }
4854 }
4855 }
4856 }
4857 }
4858 if (matched)
4859 {
4860#ifdef FEAT_INS_EXPAND
4861 if (action == ACTION_EXPAND)
4862 {
4863 int reuse = 0;
4864 int add_r;
4865 char_u *aux;
4866
4867 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4868 break;
4869 found = TRUE;
4870 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004871 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004872 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004873 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004874 if (vim_iswordp(p))
4875 goto exit_matched;
4876 p = find_word_start(p);
4877 }
4878 p = find_word_end(p);
4879 i = (int)(p - aux);
4880
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004881 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004882 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004883 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004884 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00004885
4886 /* Get the next line: when "depth" < 0 from the current
4887 * buffer, otherwise from the included file. Jump to
4888 * exit_matched when past the last line. */
4889 if (depth < 0)
4890 {
4891 if (lnum >= end_lnum)
4892 goto exit_matched;
4893 line = ml_get(++lnum);
4894 }
4895 else if (vim_fgets(line = file_line,
4896 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004897 goto exit_matched;
4898
4899 /* we read a line, set "already" to check this "line" later
4900 * if depth >= 0 we'll increase files[depth].lnum far
4901 * bellow -- Acevedo */
4902 already = aux = p = skipwhite(line);
4903 p = find_word_start(p);
4904 p = find_word_end(p);
4905 if (p > aux)
4906 {
4907 if (*aux != ')' && IObuff[i-1] != TAB)
4908 {
4909 if (IObuff[i-1] != ' ')
4910 IObuff[i++] = ' ';
4911 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4912 if (p_js
4913 && (IObuff[i-2] == '.'
4914 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4915 && (IObuff[i-2] == '?'
4916 || IObuff[i-2] == '!'))))
4917 IObuff[i++] = ' ';
4918 }
4919 /* copy as much as posible of the new word */
4920 if (p - aux >= IOSIZE - i)
4921 p = aux + IOSIZE - i - 1;
4922 STRNCPY(IObuff + i, aux, p - aux);
4923 i += (int)(p - aux);
4924 reuse |= CONT_S_IPOS;
4925 }
4926 IObuff[i] = NUL;
4927 aux = IObuff;
4928
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004929 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004930 goto exit_matched;
4931 }
4932
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004933 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004934 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4935 dir, reuse);
4936 if (add_r == OK)
4937 /* if dir was BACKWARD then honor it just once */
4938 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004939 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004940 break;
4941 }
4942 else
4943#endif
4944 if (action == ACTION_SHOW_ALL)
4945 {
4946 found = TRUE;
4947 if (!did_show)
4948 gotocmdline(TRUE); /* cursor at status line */
4949 if (curr_fname != prev_fname)
4950 {
4951 if (did_show)
4952 msg_putchar('\n'); /* cursor below last one */
4953 if (!got_int) /* don't display if 'q' typed
4954 at "--more--" mesage */
4955 msg_home_replace_hl(curr_fname);
4956 prev_fname = curr_fname;
4957 }
4958 did_show = TRUE;
4959 if (!got_int)
4960 show_pat_in_path(line, type, TRUE, action,
4961 (depth == -1) ? NULL : files[depth].fp,
4962 (depth == -1) ? &lnum : &files[depth].lnum,
4963 match_count++);
4964
4965 /* Set matched flag for this file and all the ones that
4966 * include it */
4967 for (i = 0; i <= depth; ++i)
4968 files[i].matched = TRUE;
4969 }
4970 else if (--count <= 0)
4971 {
4972 found = TRUE;
4973 if (depth == -1 && lnum == curwin->w_cursor.lnum
4974#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4975 && g_do_tagpreview == 0
4976#endif
4977 )
4978 EMSG(_("E387: Match is on current line"));
4979 else if (action == ACTION_SHOW)
4980 {
4981 show_pat_in_path(line, type, did_show, action,
4982 (depth == -1) ? NULL : files[depth].fp,
4983 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4984 did_show = TRUE;
4985 }
4986 else
4987 {
4988#ifdef FEAT_GUI
4989 need_mouse_correct = TRUE;
4990#endif
4991#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4992 /* ":psearch" uses the preview window */
4993 if (g_do_tagpreview != 0)
4994 {
4995 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004996 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004997 }
4998#endif
4999 if (action == ACTION_SPLIT)
5000 {
5001#ifdef FEAT_WINDOWS
5002 if (win_split(0, 0) == FAIL)
5003#endif
5004 break;
5005#ifdef FEAT_SCROLLBIND
5006 curwin->w_p_scb = FALSE;
5007#endif
5008 }
5009 if (depth == -1)
5010 {
5011 /* match in current file */
5012#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5013 if (g_do_tagpreview != 0)
5014 {
5015 if (getfile(0, curwin_save->w_buffer->b_fname,
5016 NULL, TRUE, lnum, FALSE) > 0)
5017 break; /* failed to jump to file */
5018 }
5019 else
5020#endif
5021 setpcmark();
5022 curwin->w_cursor.lnum = lnum;
5023 }
5024 else
5025 {
5026 if (getfile(0, files[depth].name, NULL, TRUE,
5027 files[depth].lnum, FALSE) > 0)
5028 break; /* failed to jump to file */
5029 /* autocommands may have changed the lnum, we don't
5030 * want that here */
5031 curwin->w_cursor.lnum = files[depth].lnum;
5032 }
5033 }
5034 if (action != ACTION_SHOW)
5035 {
5036 curwin->w_cursor.col = (colnr_T) (startp - line);
5037 curwin->w_set_curswant = TRUE;
5038 }
5039
5040#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5041 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005042 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043 {
5044 /* Return cursor to where we were */
5045 validate_cursor();
5046 redraw_later(VALID);
5047 win_enter(curwin_save, TRUE);
5048 }
5049#endif
5050 break;
5051 }
5052#ifdef FEAT_INS_EXPAND
5053exit_matched:
5054#endif
5055 matched = FALSE;
5056 /* look for other matches in the rest of the line if we
5057 * are not at the end of it already */
5058 if (def_regmatch.regprog == NULL
5059#ifdef FEAT_INS_EXPAND
5060 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005061 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005062#endif
5063 && *(p = startp + 1))
5064 goto search_line;
5065 }
5066 line_breakcheck();
5067#ifdef FEAT_INS_EXPAND
5068 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00005069 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005070 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005071#else
5072 if (got_int)
5073#endif
5074 break;
5075
5076 /*
5077 * Read the next line. When reading an included file and encountering
5078 * end-of-file, close the file and continue in the file that included
5079 * it.
5080 */
5081 while (depth >= 0 && !already
5082 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5083 {
5084 fclose(files[depth].fp);
5085 --old_files;
5086 files[old_files].name = files[depth].name;
5087 files[old_files].matched = files[depth].matched;
5088 --depth;
5089 curr_fname = (depth == -1) ? curbuf->b_fname
5090 : files[depth].name;
5091 if (depth < depth_displayed)
5092 depth_displayed = depth;
5093 }
5094 if (depth >= 0) /* we could read the line */
5095 files[depth].lnum++;
5096 else if (!already)
5097 {
5098 if (++lnum > end_lnum)
5099 break;
5100 line = ml_get(lnum);
5101 }
5102 already = NULL;
5103 }
5104 /* End of big for (;;) loop. */
5105
5106 /* Close any files that are still open. */
5107 for (i = 0; i <= depth; i++)
5108 {
5109 fclose(files[i].fp);
5110 vim_free(files[i].name);
5111 }
5112 for (i = old_files; i < max_path_depth; i++)
5113 vim_free(files[i].name);
5114 vim_free(files);
5115
5116 if (type == CHECK_PATH)
5117 {
5118 if (!did_show)
5119 {
5120 if (action != ACTION_SHOW_ALL)
5121 MSG(_("All included files were found"));
5122 else
5123 MSG(_("No included files"));
5124 }
5125 }
5126 else if (!found
5127#ifdef FEAT_INS_EXPAND
5128 && action != ACTION_EXPAND
5129#endif
5130 )
5131 {
5132#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005133 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005134#else
5135 if (got_int)
5136#endif
5137 EMSG(_(e_interr));
5138 else if (type == FIND_DEFINE)
5139 EMSG(_("E388: Couldn't find definition"));
5140 else
5141 EMSG(_("E389: Couldn't find pattern"));
5142 }
5143 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5144 msg_end();
5145
5146fpip_end:
5147 vim_free(file_line);
5148 vim_free(regmatch.regprog);
5149 vim_free(incl_regmatch.regprog);
5150 vim_free(def_regmatch.regprog);
5151
5152#ifdef RISCOS
5153 /* Restore previous file munging state. */
5154 __riscosify_control = previous_munging;
5155#endif
5156}
5157
5158 static void
5159show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5160 char_u *line;
5161 int type;
5162 int did_show;
5163 int action;
5164 FILE *fp;
5165 linenr_T *lnum;
5166 long count;
5167{
5168 char_u *p;
5169
5170 if (did_show)
5171 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005172 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005173 gotocmdline(TRUE); /* cursor at status line */
5174 if (got_int) /* 'q' typed at "--more--" message */
5175 return;
5176 for (;;)
5177 {
5178 p = line + STRLEN(line) - 1;
5179 if (fp != NULL)
5180 {
5181 /* We used fgets(), so get rid of newline at end */
5182 if (p >= line && *p == '\n')
5183 --p;
5184 if (p >= line && *p == '\r')
5185 --p;
5186 *(p + 1) = NUL;
5187 }
5188 if (action == ACTION_SHOW_ALL)
5189 {
5190 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5191 msg_puts(IObuff);
5192 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5193 /* Highlight line numbers */
5194 msg_puts_attr(IObuff, hl_attr(HLF_N));
5195 MSG_PUTS(" ");
5196 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005197 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005198 out_flush(); /* show one line at a time */
5199
5200 /* Definition continues until line that doesn't end with '\' */
5201 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5202 break;
5203
5204 if (fp != NULL)
5205 {
5206 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5207 break;
5208 ++*lnum;
5209 }
5210 else
5211 {
5212 if (++*lnum > curbuf->b_ml.ml_line_count)
5213 break;
5214 line = ml_get(*lnum);
5215 }
5216 msg_putchar('\n');
5217 }
5218}
5219#endif
5220
5221#ifdef FEAT_VIMINFO
5222 int
5223read_viminfo_search_pattern(virp, force)
5224 vir_T *virp;
5225 int force;
5226{
5227 char_u *lp;
5228 int idx = -1;
5229 int magic = FALSE;
5230 int no_scs = FALSE;
5231 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005232 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005233 long off = 0;
5234 int setlast = FALSE;
5235#ifdef FEAT_SEARCH_EXTRA
5236 static int hlsearch_on = FALSE;
5237#endif
5238 char_u *val;
5239
5240 /*
5241 * Old line types:
5242 * "/pat", "&pat": search/subst. pat
5243 * "~/pat", "~&pat": last used search/subst. pat
5244 * New line types:
5245 * "~h", "~H": hlsearch highlighting off/on
5246 * "~<magic><smartcase><line><end><off><last><which>pat"
5247 * <magic>: 'm' off, 'M' on
5248 * <smartcase>: 's' off, 'S' on
5249 * <line>: 'L' line offset, 'l' char offset
5250 * <end>: 'E' from end, 'e' from start
5251 * <off>: decimal, offset
5252 * <last>: '~' last used pattern
5253 * <which>: '/' search pat, '&' subst. pat
5254 */
5255 lp = virp->vir_line;
5256 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5257 {
5258 if (lp[1] == 'M') /* magic on */
5259 magic = TRUE;
5260 if (lp[2] == 's')
5261 no_scs = TRUE;
5262 if (lp[3] == 'L')
5263 off_line = TRUE;
5264 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005265 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005266 lp += 5;
5267 off = getdigits(&lp);
5268 }
5269 if (lp[0] == '~') /* use this pattern for last-used pattern */
5270 {
5271 setlast = TRUE;
5272 lp++;
5273 }
5274 if (lp[0] == '/')
5275 idx = RE_SEARCH;
5276 else if (lp[0] == '&')
5277 idx = RE_SUBST;
5278#ifdef FEAT_SEARCH_EXTRA
5279 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5280 hlsearch_on = FALSE;
5281 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5282 hlsearch_on = TRUE;
5283#endif
5284 if (idx >= 0)
5285 {
5286 if (force || spats[idx].pat == NULL)
5287 {
5288 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5289 TRUE);
5290 if (val != NULL)
5291 {
5292 set_last_search_pat(val, idx, magic, setlast);
5293 vim_free(val);
5294 spats[idx].no_scs = no_scs;
5295 spats[idx].off.line = off_line;
5296 spats[idx].off.end = off_end;
5297 spats[idx].off.off = off;
5298#ifdef FEAT_SEARCH_EXTRA
5299 if (setlast)
5300 no_hlsearch = !hlsearch_on;
5301#endif
5302 }
5303 }
5304 }
5305 return viminfo_readline(virp);
5306}
5307
5308 void
5309write_viminfo_search_pattern(fp)
5310 FILE *fp;
5311{
5312 if (get_viminfo_parameter('/') != 0)
5313 {
5314#ifdef FEAT_SEARCH_EXTRA
5315 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5316 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5317#endif
5318 wvsp_one(fp, RE_SEARCH, "", '/');
5319 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5320 }
5321}
5322
5323 static void
5324wvsp_one(fp, idx, s, sc)
5325 FILE *fp; /* file to write to */
5326 int idx; /* spats[] index */
5327 char *s; /* search pat */
5328 int sc; /* dir char */
5329{
5330 if (spats[idx].pat != NULL)
5331 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005332 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005333 /* off.dir is not stored, it's reset to forward */
5334 fprintf(fp, "%c%c%c%c%ld%s%c",
5335 spats[idx].magic ? 'M' : 'm', /* magic */
5336 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5337 spats[idx].off.line ? 'L' : 'l', /* line offset */
5338 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5339 spats[idx].off.off, /* offset */
5340 last_idx == idx ? "~" : "", /* last used pat */
5341 sc);
5342 viminfo_writestring(fp, spats[idx].pat);
5343 }
5344}
5345#endif /* FEAT_VIMINFO */