blob: 84fcbab0cb10adc588b4af00d65d648c5844e6ff [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
37static char_u *top_bot_msg = (char_u *)N_("search hit TOP, continuing at BOTTOM");
38static char_u *bot_top_msg = (char_u *)N_("search hit BOTTOM, continuing at TOP");
39
40/*
41 * This file contains various searching-related routines. These fall into
42 * three groups:
43 * 1. string searches (for /, ?, n, and N)
44 * 2. character searches within a single line (for f, F, t, T, etc)
45 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
46 */
47
48/*
49 * String searches
50 *
51 * The string search functions are divided into two levels:
52 * lowest: searchit(); uses an pos_T for starting position and found match.
53 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
54 *
55 * The last search pattern is remembered for repeating the same search.
56 * This pattern is shared between the :g, :s, ? and / commands.
57 * This is in search_regcomp().
58 *
59 * The actual string matching is done using a heavily modified version of
60 * Henry Spencer's regular expression library. See regexp.c.
61 */
62
63/* The offset for a search command is store in a soff struct */
64/* Note: only spats[0].off is really used */
65struct soffset
66{
67 int dir; /* search direction */
68 int line; /* search has line offset */
69 int end; /* search set cursor at end */
70 long off; /* line or char offset */
71};
72
73/* A search pattern and its attributes are stored in a spat struct */
74struct spat
75{
76 char_u *pat; /* the pattern (in allocated memory) or NULL */
77 int magic; /* magicness of the pattern */
78 int no_scs; /* no smarcase for this pattern */
79 struct soffset off;
80};
81
82/*
83 * Two search patterns are remembered: One for the :substitute command and
84 * one for other searches. last_idx points to the one that was used the last
85 * time.
86 */
87static struct spat spats[2] =
88{
89 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
90 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
91};
92
93static int last_idx = 0; /* index in spats[] for RE_LAST */
94
95#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
96/* copy of spats[], for keeping the search patterns while executing autocmds */
97static struct spat saved_spats[2];
98static int saved_last_idx = 0;
99# ifdef FEAT_SEARCH_EXTRA
100static int saved_no_hlsearch = 0;
101# endif
102#endif
103
104static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
105#ifdef FEAT_RIGHTLEFT
106static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
107static char_u *reverse_text __ARGS((char_u *s));
108#endif
109
110#ifdef FEAT_FIND_ID
111/*
112 * Type used by find_pattern_in_path() to remember which included files have
113 * been searched already.
114 */
115typedef struct SearchedFile
116{
117 FILE *fp; /* File pointer */
118 char_u *name; /* Full name of file */
119 linenr_T lnum; /* Line we were up to in file */
120 int matched; /* Found a match in this file */
121} SearchedFile;
122#endif
123
124/*
125 * translate search pattern for vim_regcomp()
126 *
127 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
128 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
129 * pat_save == RE_BOTH: save pat in both patterns (:global command)
130 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
131 * pat_use == RE_SUBST: use previous sustitute pattern if "pat" is NULL
132 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
133 * options & SEARCH_HIS: put search string in history
134 * options & SEARCH_KEEP: keep previous search pattern
135 *
136 * returns FAIL if failed, OK otherwise.
137 */
138 int
139search_regcomp(pat, pat_save, pat_use, options, regmatch)
140 char_u *pat;
141 int pat_save;
142 int pat_use;
143 int options;
144 regmmatch_T *regmatch; /* return: pattern and ignore-case flag */
145{
146 int magic;
147 int i;
148
149 rc_did_emsg = FALSE;
150 magic = p_magic;
151
152 /*
153 * If no pattern given, use a previously defined pattern.
154 */
155 if (pat == NULL || *pat == NUL)
156 {
157 if (pat_use == RE_LAST)
158 i = last_idx;
159 else
160 i = pat_use;
161 if (spats[i].pat == NULL) /* pattern was never defined */
162 {
163 if (pat_use == RE_SUBST)
164 EMSG(_(e_nopresub));
165 else
166 EMSG(_(e_noprevre));
167 rc_did_emsg = TRUE;
168 return FAIL;
169 }
170 pat = spats[i].pat;
171 magic = spats[i].magic;
172 no_smartcase = spats[i].no_scs;
173 }
174#ifdef FEAT_CMDHIST
175 else if (options & SEARCH_HIS) /* put new pattern in history */
176 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
177#endif
178
179#ifdef FEAT_RIGHTLEFT
180 if (mr_pattern_alloced)
181 {
182 vim_free(mr_pattern);
183 mr_pattern_alloced = FALSE;
184 }
185
186 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
187 {
188 char_u *rev_pattern;
189
190 rev_pattern = reverse_text(pat);
191 if (rev_pattern == NULL)
192 mr_pattern = pat; /* out of memory, keep normal pattern. */
193 else
194 {
195 mr_pattern = rev_pattern;
196 mr_pattern_alloced = TRUE;
197 }
198 }
199 else
200#endif
201 mr_pattern = pat;
202
203 /*
204 * Save the currently used pattern in the appropriate place,
205 * unless the pattern should not be remembered.
206 */
207 if (!(options & SEARCH_KEEP))
208 {
209 /* search or global command */
210 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
211 save_re_pat(RE_SEARCH, pat, magic);
212 /* substitute or global command */
213 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
214 save_re_pat(RE_SUBST, pat, magic);
215 }
216
217 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000218 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000219 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
220 if (regmatch->regprog == NULL)
221 return FAIL;
222 return OK;
223}
224
225/*
226 * Get search pattern used by search_regcomp().
227 */
228 char_u *
229get_search_pat()
230{
231 return mr_pattern;
232}
233
234#ifdef FEAT_RIGHTLEFT
235/*
236 * Reverse text into allocated memory.
237 * Returns the allocated string, NULL when out of memory.
238 */
239 static char_u *
240reverse_text(s)
241 char_u *s;
242{
243 unsigned len;
244 unsigned s_i, rev_i;
245 char_u *rev;
246
247 /*
248 * Reverse the pattern.
249 */
250 len = (unsigned)STRLEN(s);
251 rev = alloc(len + 1);
252 if (rev != NULL)
253 {
254 rev_i = len;
255 for (s_i = 0; s_i < len; ++s_i)
256 {
257# ifdef FEAT_MBYTE
258 if (has_mbyte)
259 {
260 int mb_len;
261
262 mb_len = (*mb_ptr2len_check)(s + s_i);
263 rev_i -= mb_len;
264 mch_memmove(rev + rev_i, s + s_i, mb_len);
265 s_i += mb_len - 1;
266 }
267 else
268# endif
269 rev[--rev_i] = s[s_i];
270
271 }
272 rev[len] = NUL;
273 }
274 return rev;
275}
276#endif
277
278 static void
279save_re_pat(idx, pat, magic)
280 int idx;
281 char_u *pat;
282 int magic;
283{
284 if (spats[idx].pat != pat)
285 {
286 vim_free(spats[idx].pat);
287 spats[idx].pat = vim_strsave(pat);
288 spats[idx].magic = magic;
289 spats[idx].no_scs = no_smartcase;
290 last_idx = idx;
291#ifdef FEAT_SEARCH_EXTRA
292 /* If 'hlsearch' set and search pat changed: need redraw. */
293 if (p_hls)
294 redraw_all_later(NOT_VALID);
295 no_hlsearch = FALSE;
296#endif
297 }
298}
299
300#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
301/*
302 * Save the search patterns, so they can be restored later.
303 * Used before/after executing autocommands and user functions.
304 */
305static int save_level = 0;
306
307 void
308save_search_patterns()
309{
310 if (save_level++ == 0)
311 {
312 saved_spats[0] = spats[0];
313 if (spats[0].pat != NULL)
314 saved_spats[0].pat = vim_strsave(spats[0].pat);
315 saved_spats[1] = spats[1];
316 if (spats[1].pat != NULL)
317 saved_spats[1].pat = vim_strsave(spats[1].pat);
318 saved_last_idx = last_idx;
319# ifdef FEAT_SEARCH_EXTRA
320 saved_no_hlsearch = no_hlsearch;
321# endif
322 }
323}
324
325 void
326restore_search_patterns()
327{
328 if (--save_level == 0)
329 {
330 vim_free(spats[0].pat);
331 spats[0] = saved_spats[0];
332 vim_free(spats[1].pat);
333 spats[1] = saved_spats[1];
334 last_idx = saved_last_idx;
335# ifdef FEAT_SEARCH_EXTRA
336 no_hlsearch = saved_no_hlsearch;
337# endif
338 }
339}
340#endif
341
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000342#if defined(EXITFREE) || defined(PROTO)
343 void
344free_search_patterns()
345{
346 vim_free(spats[0].pat);
347 vim_free(spats[1].pat);
348}
349#endif
350
Bram Moolenaar071d4272004-06-13 20:20:40 +0000351/*
352 * Return TRUE when case should be ignored for search pattern "pat".
353 * Uses the 'ignorecase' and 'smartcase' options.
354 */
355 int
356ignorecase(pat)
357 char_u *pat;
358{
359 char_u *p;
360 int ic;
361
362 ic = p_ic;
363 if (ic && !no_smartcase && p_scs
364#ifdef FEAT_INS_EXPAND
365 && !(ctrl_x_mode && curbuf->b_p_inf)
366#endif
367 )
368 {
369 /* don't ignore case if pattern has uppercase */
370 for (p = pat; *p; )
371 {
372#ifdef FEAT_MBYTE
373 int l;
374
375 if (has_mbyte && (l = (*mb_ptr2len_check)(p)) > 1)
376 {
377 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
378 {
379 ic = FALSE;
380 break;
381 }
382 p += l;
383 }
384 else
385#endif
386 if (*p == '\\' && p[1] != NUL) /* skip "\S" et al. */
387 p += 2;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000388 else if (isupper(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000389 {
390 ic = FALSE;
391 break;
392 }
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000393 else
394 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000395 }
396 }
397 no_smartcase = FALSE;
398
399 return ic;
400}
401
402 char_u *
403last_search_pat()
404{
405 return spats[last_idx].pat;
406}
407
408/*
409 * Reset search direction to forward. For "gd" and "gD" commands.
410 */
411 void
412reset_search_dir()
413{
414 spats[0].off.dir = '/';
415}
416
417#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
418/*
419 * Set the last search pattern. For ":let @/ =" and viminfo.
420 * Also set the saved search pattern, so that this works in an autocommand.
421 */
422 void
423set_last_search_pat(s, idx, magic, setlast)
424 char_u *s;
425 int idx;
426 int magic;
427 int setlast;
428{
429 vim_free(spats[idx].pat);
430 /* An empty string means that nothing should be matched. */
431 if (*s == NUL)
432 spats[idx].pat = NULL;
433 else
434 spats[idx].pat = vim_strsave(s);
435 spats[idx].magic = magic;
436 spats[idx].no_scs = FALSE;
437 spats[idx].off.dir = '/';
438 spats[idx].off.line = FALSE;
439 spats[idx].off.end = FALSE;
440 spats[idx].off.off = 0;
441 if (setlast)
442 last_idx = idx;
443 if (save_level)
444 {
445 vim_free(saved_spats[idx].pat);
446 saved_spats[idx] = spats[0];
447 if (spats[idx].pat == NULL)
448 saved_spats[idx].pat = NULL;
449 else
450 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
451 saved_last_idx = last_idx;
452 }
453# ifdef FEAT_SEARCH_EXTRA
454 /* If 'hlsearch' set and search pat changed: need redraw. */
455 if (p_hls && idx == last_idx && !no_hlsearch)
456 redraw_all_later(NOT_VALID);
457# endif
458}
459#endif
460
461#ifdef FEAT_SEARCH_EXTRA
462/*
463 * Get a regexp program for the last used search pattern.
464 * This is used for highlighting all matches in a window.
465 * Values returned in regmatch->regprog and regmatch->rmm_ic.
466 */
467 void
468last_pat_prog(regmatch)
469 regmmatch_T *regmatch;
470{
471 if (spats[last_idx].pat == NULL)
472 {
473 regmatch->regprog = NULL;
474 return;
475 }
476 ++emsg_off; /* So it doesn't beep if bad expr */
477 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
478 --emsg_off;
479}
480#endif
481
482/*
483 * lowest level search function.
484 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
485 * Start at position 'pos' and return the found position in 'pos'.
486 *
487 * if (options & SEARCH_MSG) == 0 don't give any messages
488 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
489 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
490 * if (options & SEARCH_HIS) put search pattern in history
491 * if (options & SEARCH_END) return position at end of match
492 * if (options & SEARCH_START) accept match at pos itself
493 * if (options & SEARCH_KEEP) keep previous search pattern
494 * if (options & SEARCH_FOLD) match only once in a closed fold
495 * if (options & SEARCH_PEEK) check for typed char, cancel search
496 *
497 * Return FAIL (zero) for failure, non-zero for success.
498 * When FEAT_EVAL is defined, returns the index of the first matching
499 * subpattern plus one; one if there was none.
500 */
501 int
502searchit(win, buf, pos, dir, pat, count, options, pat_use)
503 win_T *win; /* window to search in; can be NULL for a
504 buffer without a window! */
505 buf_T *buf;
506 pos_T *pos;
507 int dir;
508 char_u *pat;
509 long count;
510 int options;
511 int pat_use;
512{
513 int found;
514 linenr_T lnum; /* no init to shut up Apollo cc */
515 regmmatch_T regmatch;
516 char_u *ptr;
517 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000518 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000519 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000520 int loop;
521 pos_T start_pos;
522 int at_first_line;
523 int extra_col;
524 int match_ok;
525 long nmatched;
526 int submatch = 0;
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)
547 extra_col = (*mb_ptr2len_check)(ml_get_buf(buf, pos->lnum, FALSE)
548 + pos->col);
549#endif
550 else
551 extra_col = 1;
552
553/*
554 * find the string
555 */
556 called_emsg = FALSE;
557 do /* loop for count */
558 {
559 start_pos = *pos; /* remember start pos for detecting no match */
560 found = 0; /* default: not found */
561 at_first_line = TRUE; /* default: start in first line */
562 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
563 {
564 pos->lnum = 1;
565 pos->col = 0;
566 at_first_line = FALSE; /* not in first line now */
567 }
568
569 /*
570 * Start searching in current line, unless searching backwards and
571 * we're in column 0.
572 */
573 if (dir == BACKWARD && start_pos.col == 0)
574 {
575 lnum = pos->lnum - 1;
576 at_first_line = FALSE;
577 }
578 else
579 lnum = pos->lnum;
580
581 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
582 {
583 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
584 lnum += dir, at_first_line = FALSE)
585 {
586 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000587 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000588 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000589 nmatched = vim_regexec_multi(&regmatch, win, buf,
590 lnum, (colnr_T)0);
591 /* Abort searching on an error (e.g., out of stack). */
592 if (called_emsg)
593 break;
594 if (nmatched > 0)
595 {
596 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000597 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000598 endpos = regmatch.endpos[0];
599# ifdef FEAT_EVAL
600 submatch = first_submatch(&regmatch);
601# endif
Bram Moolenaar677ee682005-01-27 14:41:15 +0000602 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000603
604 /*
605 * Forward search in the first line: match should be after
606 * the start position. If not, continue at the end of the
607 * match (this is vi compatible) or on the next char.
608 */
609 if (dir == FORWARD && at_first_line)
610 {
611 match_ok = TRUE;
612 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000613 * When the match starts in a next line it's certainly
614 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000615 * When match lands on a NUL the cursor will be put
616 * one back afterwards, compare with that position,
617 * otherwise "/$" will get stuck on end of line.
618 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000619 while (matchpos.lnum == 0
620 && ((options & SEARCH_END)
621 ? (nmatched == 1
622 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000623 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000624 : ((int)matchpos.col
625 - (ptr[matchpos.col] == NUL)
626 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000627 {
628 /*
629 * If vi-compatible searching, continue at the end
630 * of the match, otherwise continue one position
631 * forward.
632 */
633 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
634 {
635 if (nmatched > 1)
636 {
637 /* end is in next line, thus no match in
638 * this line */
639 match_ok = FALSE;
640 break;
641 }
642 matchcol = endpos.col;
643 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000644 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000645 && ptr[matchcol] != NUL)
646 {
647#ifdef FEAT_MBYTE
648 if (has_mbyte)
649 matchcol +=
650 (*mb_ptr2len_check)(ptr + matchcol);
651 else
652#endif
653 ++matchcol;
654 }
655 }
656 else
657 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000658 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000659 if (ptr[matchcol] != NUL)
660 {
661#ifdef FEAT_MBYTE
662 if (has_mbyte)
663 matchcol += (*mb_ptr2len_check)(ptr
664 + matchcol);
665 else
666#endif
667 ++matchcol;
668 }
669 }
670 if (ptr[matchcol] == NUL
671 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000672 win, buf, lnum + matchpos.lnum,
673 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 {
675 match_ok = FALSE;
676 break;
677 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000678 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000679 endpos = regmatch.endpos[0];
680# ifdef FEAT_EVAL
681 submatch = first_submatch(&regmatch);
682# endif
683
684 /* Need to get the line pointer again, a
685 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000686 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 }
688 if (!match_ok)
689 continue;
690 }
691 if (dir == BACKWARD)
692 {
693 /*
694 * Now, if there are multiple matches on this line,
695 * we have to get the last one. Or the last one before
696 * the cursor, if we're on that line.
697 * When putting the new cursor at the end, compare
698 * relative to the end of the match.
699 */
700 match_ok = FALSE;
701 for (;;)
702 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000703 /* Remember a position that is before the start
704 * position, we use it if it's the last match in
705 * the line. Always accept a position after
706 * wrapping around. */
707 if (loop
708 || ((options & SEARCH_END)
709 ? (lnum + regmatch.endpos[0].lnum
710 < start_pos.lnum
711 || (lnum + regmatch.endpos[0].lnum
712 == start_pos.lnum
713 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000714 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000715 <= (int)start_pos.col))
716 : (lnum + regmatch.startpos[0].lnum
717 < start_pos.lnum
718 || (lnum + regmatch.startpos[0].lnum
719 == start_pos.lnum
720 && (int)regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000722 <= (int)start_pos.col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000723 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000724 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000725 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000726 endpos = regmatch.endpos[0];
727# ifdef FEAT_EVAL
728 submatch = first_submatch(&regmatch);
729# endif
730 }
731 else
732 break;
733
734 /*
735 * We found a valid match, now check if there is
736 * another one after it.
737 * If vi-compatible searching, continue at the end
738 * of the match, otherwise continue one position
739 * forward.
740 */
741 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
742 {
743 if (nmatched > 1)
744 break;
745 matchcol = endpos.col;
746 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000747 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000748 && ptr[matchcol] != NUL)
749 {
750#ifdef FEAT_MBYTE
751 if (has_mbyte)
752 matchcol +=
753 (*mb_ptr2len_check)(ptr + matchcol);
754 else
755#endif
756 ++matchcol;
757 }
758 }
759 else
760 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000761 /* Stop when the match is in a next line. */
762 if (matchpos.lnum > 0)
763 break;
764 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000765 if (ptr[matchcol] != NUL)
766 {
767#ifdef FEAT_MBYTE
768 if (has_mbyte)
769 matchcol +=
770 (*mb_ptr2len_check)(ptr + matchcol);
771 else
772#endif
773 ++matchcol;
774 }
775 }
776 if (ptr[matchcol] == NUL
777 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000778 win, buf, lnum + matchpos.lnum,
779 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000780 break;
781
782 /* Need to get the line pointer again, a
783 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000784 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000785 }
786
787 /*
788 * If there is only a match after the cursor, skip
789 * this match.
790 */
791 if (!match_ok)
792 continue;
793 }
794
795 if (options & SEARCH_END && !(options & SEARCH_NOOF))
796 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000797 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000798 pos->col = endpos.col - 1;
799 }
800 else
801 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000802 pos->lnum = lnum + matchpos.lnum;
803 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000804 }
805#ifdef FEAT_VIRTUALEDIT
806 pos->coladd = 0;
807#endif
808 found = 1;
809
810 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000811 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000812 search_match_endcol = endpos.col;
813 break;
814 }
815 line_breakcheck(); /* stop if ctrl-C typed */
816 if (got_int)
817 break;
818
819#ifdef FEAT_SEARCH_EXTRA
820 /* Cancel searching if a character was typed. Used for
821 * 'incsearch'. Don't check too often, that would slowdown
822 * searching too much. */
823 if ((options & SEARCH_PEEK)
824 && ((lnum - pos->lnum) & 0x3f) == 0
825 && char_avail())
826 {
827 break_loop = TRUE;
828 break;
829 }
830#endif
831
832 if (loop && lnum == start_pos.lnum)
833 break; /* if second loop, stop where started */
834 }
835 at_first_line = FALSE;
836
837 /*
838 * Stop the search if wrapscan isn't set, after an interrupt,
839 * after a match and after looping twice.
840 */
841 if (!p_ws || got_int || called_emsg || break_loop || found || loop)
842 break;
843
844 /*
845 * If 'wrapscan' is set we continue at the other end of the file.
846 * If 'shortmess' does not contain 's', we give a message.
847 * This message is also remembered in keep_msg for when the screen
848 * is redrawn. The keep_msg is cleared whenever another message is
849 * written.
850 */
851 if (dir == BACKWARD) /* start second loop at the other end */
852 {
853 lnum = buf->b_ml.ml_line_count;
854 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
855 give_warning((char_u *)_(top_bot_msg), TRUE);
856 }
857 else
858 {
859 lnum = 1;
860 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
861 give_warning((char_u *)_(bot_top_msg), TRUE);
862 }
863 }
864 if (got_int || called_emsg || break_loop)
865 break;
866 }
867 while (--count > 0 && found); /* stop after count matches or no match */
868
869 vim_free(regmatch.regprog);
870
871 if (!found) /* did not find it */
872 {
873 if (got_int)
874 EMSG(_(e_interr));
875 else if ((options & SEARCH_MSG) == SEARCH_MSG)
876 {
877 if (p_ws)
878 EMSG2(_(e_patnotf2), mr_pattern);
879 else if (lnum == 0)
880 EMSG2(_("E384: search hit TOP without match for: %s"),
881 mr_pattern);
882 else
883 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
884 mr_pattern);
885 }
886 return FAIL;
887 }
888
889 return submatch + 1;
890}
891
892#ifdef FEAT_EVAL
893/*
894 * Return the number of the first subpat that matched.
895 */
896 static int
897first_submatch(rp)
898 regmmatch_T *rp;
899{
900 int submatch;
901
902 for (submatch = 1; ; ++submatch)
903 {
904 if (rp->startpos[submatch].lnum >= 0)
905 break;
906 if (submatch == 9)
907 {
908 submatch = 0;
909 break;
910 }
911 }
912 return submatch;
913}
914#endif
915
916/*
917 * Highest level string search function.
918 * Search for the 'count'th occurence of pattern 'pat' in direction 'dirc'
919 * If 'dirc' is 0: use previous dir.
920 * If 'pat' is NULL or empty : use previous string.
921 * If 'options & SEARCH_REV' : go in reverse of previous dir.
922 * If 'options & SEARCH_ECHO': echo the search command and handle options
923 * If 'options & SEARCH_MSG' : may give error message
924 * If 'options & SEARCH_OPT' : interpret optional flags
925 * If 'options & SEARCH_HIS' : put search pattern in history
926 * If 'options & SEARCH_NOOF': don't add offset to position
927 * If 'options & SEARCH_MARK': set previous context mark
928 * If 'options & SEARCH_KEEP': keep previous search pattern
929 * If 'options & SEARCH_START': accept match at curpos itself
930 * If 'options & SEARCH_PEEK': check for typed char, cancel search
931 *
932 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
933 * makes the movement linewise without moving the match position.
934 *
935 * return 0 for failure, 1 for found, 2 for found and line offset added
936 */
937 int
938do_search(oap, dirc, pat, count, options)
939 oparg_T *oap; /* can be NULL */
940 int dirc; /* '/' or '?' */
941 char_u *pat;
942 long count;
943 int options;
944{
945 pos_T pos; /* position of the last match */
946 char_u *searchstr;
947 struct soffset old_off;
948 int retval; /* Return value */
949 char_u *p;
950 long c;
951 char_u *dircp;
952 char_u *strcopy = NULL;
953 char_u *ps;
954
955 /*
956 * A line offset is not remembered, this is vi compatible.
957 */
958 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
959 {
960 spats[0].off.line = FALSE;
961 spats[0].off.off = 0;
962 }
963
964 /*
965 * Save the values for when (options & SEARCH_KEEP) is used.
966 * (there is no "if ()" around this because gcc wants them initialized)
967 */
968 old_off = spats[0].off;
969
970 pos = curwin->w_cursor; /* start searching at the cursor position */
971
972 /*
973 * Find out the direction of the search.
974 */
975 if (dirc == 0)
976 dirc = spats[0].off.dir;
977 else
978 spats[0].off.dir = dirc;
979 if (options & SEARCH_REV)
980 {
981#ifdef WIN32
982 /* There is a bug in the Visual C++ 2.2 compiler which means that
983 * dirc always ends up being '/' */
984 dirc = (dirc == '/') ? '?' : '/';
985#else
986 if (dirc == '/')
987 dirc = '?';
988 else
989 dirc = '/';
990#endif
991 }
992
993#ifdef FEAT_FOLDING
994 /* If the cursor is in a closed fold, don't find another match in the same
995 * fold. */
996 if (dirc == '/')
997 {
998 if (hasFolding(pos.lnum, NULL, &pos.lnum))
999 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1000 }
1001 else
1002 {
1003 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1004 pos.col = 0;
1005 }
1006#endif
1007
1008#ifdef FEAT_SEARCH_EXTRA
1009 /*
1010 * Turn 'hlsearch' highlighting back on.
1011 */
1012 if (no_hlsearch && !(options & SEARCH_KEEP))
1013 {
1014 redraw_all_later(NOT_VALID);
1015 no_hlsearch = FALSE;
1016 }
1017#endif
1018
1019 /*
1020 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1021 */
1022 for (;;)
1023 {
1024 searchstr = pat;
1025 dircp = NULL;
1026 /* use previous pattern */
1027 if (pat == NULL || *pat == NUL || *pat == dirc)
1028 {
1029 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1030 {
1031 EMSG(_(e_noprevre));
1032 retval = 0;
1033 goto end_do_search;
1034 }
1035 /* make search_regcomp() use spats[RE_SEARCH].pat */
1036 searchstr = (char_u *)"";
1037 }
1038
1039 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1040 {
1041 /*
1042 * Find end of regular expression.
1043 * If there is a matching '/' or '?', toss it.
1044 */
1045 ps = strcopy;
1046 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1047 if (strcopy != ps)
1048 {
1049 /* made a copy of "pat" to change "\?" to "?" */
1050 searchcmdlen += STRLEN(pat) - STRLEN(strcopy);
1051 pat = strcopy;
1052 searchstr = strcopy;
1053 }
1054 if (*p == dirc)
1055 {
1056 dircp = p; /* remember where we put the NUL */
1057 *p++ = NUL;
1058 }
1059 spats[0].off.line = FALSE;
1060 spats[0].off.end = FALSE;
1061 spats[0].off.off = 0;
1062 /*
1063 * Check for a line offset or a character offset.
1064 * For get_address (echo off) we don't check for a character
1065 * offset, because it is meaningless and the 's' could be a
1066 * substitute command.
1067 */
1068 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1069 spats[0].off.line = TRUE;
1070 else if ((options & SEARCH_OPT) &&
1071 (*p == 'e' || *p == 's' || *p == 'b'))
1072 {
1073 if (*p == 'e') /* end */
1074 spats[0].off.end = SEARCH_END;
1075 ++p;
1076 }
1077 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1078 {
1079 /* 'nr' or '+nr' or '-nr' */
1080 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1081 spats[0].off.off = atol((char *)p);
1082 else if (*p == '-') /* single '-' */
1083 spats[0].off.off = -1;
1084 else /* single '+' */
1085 spats[0].off.off = 1;
1086 ++p;
1087 while (VIM_ISDIGIT(*p)) /* skip number */
1088 ++p;
1089 }
1090
1091 /* compute length of search command for get_address() */
1092 searchcmdlen += (int)(p - pat);
1093
1094 pat = p; /* put pat after search command */
1095 }
1096
1097 if ((options & SEARCH_ECHO) && messaging()
1098 && !cmd_silent && msg_silent == 0)
1099 {
1100 char_u *msgbuf;
1101 char_u *trunc;
1102
1103 if (*searchstr == NUL)
1104 p = spats[last_idx].pat;
1105 else
1106 p = searchstr;
1107 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1108 if (msgbuf != NULL)
1109 {
1110 msgbuf[0] = dirc;
1111 STRCPY(msgbuf + 1, p);
1112 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1113 {
1114 p = msgbuf + STRLEN(msgbuf);
1115 *p++ = dirc;
1116 if (spats[0].off.end)
1117 *p++ = 'e';
1118 else if (!spats[0].off.line)
1119 *p++ = 's';
1120 if (spats[0].off.off > 0 || spats[0].off.line)
1121 *p++ = '+';
1122 if (spats[0].off.off != 0 || spats[0].off.line)
1123 sprintf((char *)p, "%ld", spats[0].off.off);
1124 else
1125 *p = NUL;
1126 }
1127
1128 msg_start();
1129 trunc = msg_strtrunc(msgbuf);
1130
1131#ifdef FEAT_RIGHTLEFT
1132 /* The search pattern could be shown on the right in rightleft
1133 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1134 * it would be blanked out again very soon. Show it on the
1135 * left, but do reverse the text. */
1136 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1137 {
1138 char_u *r;
1139
1140 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1141 if (r != NULL)
1142 {
1143 vim_free(trunc);
1144 trunc = r;
1145 }
1146 }
1147#endif
1148 if (trunc != NULL)
1149 {
1150 msg_outtrans(trunc);
1151 vim_free(trunc);
1152 }
1153 else
1154 msg_outtrans(msgbuf);
1155 msg_clr_eos();
1156 msg_check();
1157 vim_free(msgbuf);
1158
1159 gotocmdline(FALSE);
1160 out_flush();
1161 msg_nowait = TRUE; /* don't wait for this message */
1162 }
1163 }
1164
1165 /*
1166 * If there is a character offset, subtract it from the current
1167 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001168 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001169 * This is not done for a line offset, because then we would not be vi
1170 * compatible.
1171 */
Bram Moolenaared203462004-06-16 11:19:22 +00001172 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001173 {
1174 if (spats[0].off.off > 0)
1175 {
1176 for (c = spats[0].off.off; c; --c)
1177 if (decl(&pos) == -1)
1178 break;
1179 if (c) /* at start of buffer */
1180 {
1181 pos.lnum = 0; /* allow lnum == 0 here */
1182 pos.col = MAXCOL;
1183 }
1184 }
1185 else
1186 {
1187 for (c = spats[0].off.off; c; ++c)
1188 if (incl(&pos) == -1)
1189 break;
1190 if (c) /* at end of buffer */
1191 {
1192 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1193 pos.col = 0;
1194 }
1195 }
1196 }
1197
1198#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1199 if (p_altkeymap && curwin->w_p_rl)
1200 lrFswap(searchstr,0);
1201#endif
1202
1203 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1204 searchstr, count, spats[0].off.end + (options &
1205 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1206 + SEARCH_MSG + SEARCH_START
1207 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
1208 RE_LAST);
1209
1210 if (dircp != NULL)
1211 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1212 if (c == FAIL)
1213 {
1214 retval = 0;
1215 goto end_do_search;
1216 }
1217 if (spats[0].off.end && oap != NULL)
1218 oap->inclusive = TRUE; /* 'e' includes last character */
1219
1220 retval = 1; /* pattern found */
1221
1222 /*
1223 * Add character and/or line offset
1224 */
1225 if (!(options & SEARCH_NOOF) || *pat == ';')
1226 {
1227 if (spats[0].off.line) /* Add the offset to the line number. */
1228 {
1229 c = pos.lnum + spats[0].off.off;
1230 if (c < 1)
1231 pos.lnum = 1;
1232 else if (c > curbuf->b_ml.ml_line_count)
1233 pos.lnum = curbuf->b_ml.ml_line_count;
1234 else
1235 pos.lnum = c;
1236 pos.col = 0;
1237
1238 retval = 2; /* pattern found, line offset added */
1239 }
Bram Moolenaared203462004-06-16 11:19:22 +00001240 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001241 {
1242 /* to the right, check for end of file */
1243 if (spats[0].off.off > 0)
1244 {
1245 for (c = spats[0].off.off; c; --c)
1246 if (incl(&pos) == -1)
1247 break;
1248 }
1249 /* to the left, check for start of file */
1250 else
1251 {
1252 if ((c = pos.col + spats[0].off.off) >= 0)
1253 pos.col = c;
1254 else
1255 for (c = spats[0].off.off; c; ++c)
1256 if (decl(&pos) == -1)
1257 break;
1258 }
1259 }
1260 }
1261
1262 /*
1263 * The search command can be followed by a ';' to do another search.
1264 * For example: "/pat/;/foo/+3;?bar"
1265 * This is like doing another search command, except:
1266 * - The remembered direction '/' or '?' is from the first search.
1267 * - When an error happens the cursor isn't moved at all.
1268 * Don't do this when called by get_address() (it handles ';' itself).
1269 */
1270 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1271 break;
1272
1273 dirc = *++pat;
1274 if (dirc != '?' && dirc != '/')
1275 {
1276 retval = 0;
1277 EMSG(_("E386: Expected '?' or '/' after ';'"));
1278 goto end_do_search;
1279 }
1280 ++pat;
1281 }
1282
1283 if (options & SEARCH_MARK)
1284 setpcmark();
1285 curwin->w_cursor = pos;
1286 curwin->w_set_curswant = TRUE;
1287
1288end_do_search:
1289 if (options & SEARCH_KEEP)
1290 spats[0].off = old_off;
1291 vim_free(strcopy);
1292
1293 return retval;
1294}
1295
1296#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1297/*
1298 * search_for_exact_line(buf, pos, dir, pat)
1299 *
1300 * Search for a line starting with the given pattern (ignoring leading
1301 * white-space), starting from pos and going in direction dir. pos will
1302 * contain the position of the match found. Blank lines match only if
1303 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1304 * Return OK for success, or FAIL if no line found.
1305 */
1306 int
1307search_for_exact_line(buf, pos, dir, pat)
1308 buf_T *buf;
1309 pos_T *pos;
1310 int dir;
1311 char_u *pat;
1312{
1313 linenr_T start = 0;
1314 char_u *ptr;
1315 char_u *p;
1316
1317 if (buf->b_ml.ml_line_count == 0)
1318 return FAIL;
1319 for (;;)
1320 {
1321 pos->lnum += dir;
1322 if (pos->lnum < 1)
1323 {
1324 if (p_ws)
1325 {
1326 pos->lnum = buf->b_ml.ml_line_count;
1327 if (!shortmess(SHM_SEARCH))
1328 give_warning((char_u *)_(top_bot_msg), TRUE);
1329 }
1330 else
1331 {
1332 pos->lnum = 1;
1333 break;
1334 }
1335 }
1336 else if (pos->lnum > buf->b_ml.ml_line_count)
1337 {
1338 if (p_ws)
1339 {
1340 pos->lnum = 1;
1341 if (!shortmess(SHM_SEARCH))
1342 give_warning((char_u *)_(bot_top_msg), TRUE);
1343 }
1344 else
1345 {
1346 pos->lnum = 1;
1347 break;
1348 }
1349 }
1350 if (pos->lnum == start)
1351 break;
1352 if (start == 0)
1353 start = pos->lnum;
1354 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1355 p = skipwhite(ptr);
1356 pos->col = (colnr_T) (p - ptr);
1357
1358 /* when adding lines the matching line may be empty but it is not
1359 * ignored because we are interested in the next line -- Acevedo */
1360 if ((continue_status & CONT_ADDING) && !(continue_status & CONT_SOL))
1361 {
1362 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1363 return OK;
1364 }
1365 else if (*p != NUL) /* ignore empty lines */
1366 { /* expanding lines or words */
1367 if ((p_ic ? MB_STRNICMP(p, pat, completion_length)
1368 : STRNCMP(p, pat, completion_length)) == 0)
1369 return OK;
1370 }
1371 }
1372 return FAIL;
1373}
1374#endif /* FEAT_INS_EXPAND */
1375
1376/*
1377 * Character Searches
1378 */
1379
1380/*
1381 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1382 * position of the character, otherwise move to just before the char.
1383 * Do this "cap->count1" times.
1384 * Return FAIL or OK.
1385 */
1386 int
1387searchc(cap, t_cmd)
1388 cmdarg_T *cap;
1389 int t_cmd;
1390{
1391 int c = cap->nchar; /* char to search for */
1392 int dir = cap->arg; /* TRUE for searching forward */
1393 long count = cap->count1; /* repeat count */
1394 static int lastc = NUL; /* last character searched for */
1395 static int lastcdir; /* last direction of character search */
1396 static int last_t_cmd; /* last search t_cmd */
1397 int col;
1398 char_u *p;
1399 int len;
1400#ifdef FEAT_MBYTE
1401 static char_u bytes[MB_MAXBYTES];
1402 static int bytelen = 1; /* >1 for multi-byte char */
1403#endif
1404
1405 if (c != NUL) /* normal search: remember args for repeat */
1406 {
1407 if (!KeyStuffed) /* don't remember when redoing */
1408 {
1409 lastc = c;
1410 lastcdir = dir;
1411 last_t_cmd = t_cmd;
1412#ifdef FEAT_MBYTE
1413 bytelen = (*mb_char2bytes)(c, bytes);
1414 if (cap->ncharC1 != 0)
1415 {
1416 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1417 if (cap->ncharC2 != 0)
1418 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1419 }
1420#endif
1421 }
1422 }
1423 else /* repeat previous search */
1424 {
1425 if (lastc == NUL)
1426 return FAIL;
1427 if (dir) /* repeat in opposite direction */
1428 dir = -lastcdir;
1429 else
1430 dir = lastcdir;
1431 t_cmd = last_t_cmd;
1432 c = lastc;
1433 /* For multi-byte re-use last bytes[] and bytelen. */
1434 }
1435
1436 p = ml_get_curline();
1437 col = curwin->w_cursor.col;
1438 len = (int)STRLEN(p);
1439
1440 while (count--)
1441 {
1442#ifdef FEAT_MBYTE
1443 if (has_mbyte)
1444 {
1445 for (;;)
1446 {
1447 if (dir > 0)
1448 {
1449 col += (*mb_ptr2len_check)(p + col);
1450 if (col >= len)
1451 return FAIL;
1452 }
1453 else
1454 {
1455 if (col == 0)
1456 return FAIL;
1457 col -= (*mb_head_off)(p, p + col - 1) + 1;
1458 }
1459 if (bytelen == 1)
1460 {
1461 if (p[col] == c)
1462 break;
1463 }
1464 else
1465 {
1466 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1467 break;
1468 }
1469 }
1470 }
1471 else
1472#endif
1473 {
1474 for (;;)
1475 {
1476 if ((col += dir) < 0 || col >= len)
1477 return FAIL;
1478 if (p[col] == c)
1479 break;
1480 }
1481 }
1482 }
1483
1484 if (t_cmd)
1485 {
1486 /* backup to before the character (possibly double-byte) */
1487 col -= dir;
1488#ifdef FEAT_MBYTE
1489 if (has_mbyte)
1490 {
1491 if (dir < 0)
1492 /* Landed on the search char which is bytelen long */
1493 col += bytelen - 1;
1494 else
1495 /* To previous char, which may be multi-byte. */
1496 col -= (*mb_head_off)(p, p + col);
1497 }
1498#endif
1499 }
1500 curwin->w_cursor.col = col;
1501
1502 return OK;
1503}
1504
1505/*
1506 * "Other" Searches
1507 */
1508
1509/*
1510 * findmatch - find the matching paren or brace
1511 *
1512 * Improvement over vi: Braces inside quotes are ignored.
1513 */
1514 pos_T *
1515findmatch(oap, initc)
1516 oparg_T *oap;
1517 int initc;
1518{
1519 return findmatchlimit(oap, initc, 0, 0);
1520}
1521
1522/*
1523 * Return TRUE if the character before "linep[col]" equals "ch".
1524 * Return FALSE if "col" is zero.
1525 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1526 * is NULL.
1527 * Handles multibyte string correctly.
1528 */
1529 static int
1530check_prevcol(linep, col, ch, prevcol)
1531 char_u *linep;
1532 int col;
1533 int ch;
1534 int *prevcol;
1535{
1536 --col;
1537#ifdef FEAT_MBYTE
1538 if (col > 0 && has_mbyte)
1539 col -= (*mb_head_off)(linep, linep + col);
1540#endif
1541 if (prevcol)
1542 *prevcol = col;
1543 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1544}
1545
1546/*
1547 * findmatchlimit -- find the matching paren or brace, if it exists within
1548 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1549 * the edge of the file.
1550 *
1551 * "initc" is the character to find a match for. NUL means to find the
1552 * character at or after the cursor.
1553 *
1554 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1555 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1556 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1557 * FM_SKIPCOMM skip comments (not implemented yet!)
1558 */
1559
1560 pos_T *
1561findmatchlimit(oap, initc, flags, maxtravel)
1562 oparg_T *oap;
1563 int initc;
1564 int flags;
1565 int maxtravel;
1566{
1567 static pos_T pos; /* current search position */
1568 int findc = 0; /* matching brace */
1569 int c;
1570 int count = 0; /* cumulative number of braces */
1571 int backwards = FALSE; /* init for gcc */
1572 int inquote = FALSE; /* TRUE when inside quotes */
1573 char_u *linep; /* pointer to current line */
1574 char_u *ptr;
1575 int do_quotes; /* check for quotes in current line */
1576 int at_start; /* do_quotes value at start position */
1577 int hash_dir = 0; /* Direction searched for # things */
1578 int comment_dir = 0; /* Direction searched for comments */
1579 pos_T match_pos; /* Where last slash-star was found */
1580 int start_in_quotes; /* start position is in quotes */
1581 int traveled = 0; /* how far we've searched so far */
1582 int ignore_cend = FALSE; /* ignore comment end */
1583 int cpo_match; /* vi compatible matching */
1584 int cpo_bsl; /* don't recognize backslashes */
1585 int match_escaped = 0; /* search for escaped match */
1586 int dir; /* Direction to search */
1587 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001588#ifdef FEAT_LISP
1589 int lispcomm = FALSE; /* inside of Lisp-style comment */
1590 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1591#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001592
1593 pos = curwin->w_cursor;
1594 linep = ml_get(pos.lnum);
1595
1596 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1597 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1598
1599 /* Direction to search when initc is '/', '*' or '#' */
1600 if (flags & FM_BACKWARD)
1601 dir = BACKWARD;
1602 else if (flags & FM_FORWARD)
1603 dir = FORWARD;
1604 else
1605 dir = 0;
1606
1607 /*
1608 * if initc given, look in the table for the matching character
1609 * '/' and '*' are special cases: look for start or end of comment.
1610 * When '/' is used, we ignore running backwards into an star-slash, for
1611 * "[*" command, we just want to find any comment.
1612 */
1613 if (initc == '/' || initc == '*')
1614 {
1615 comment_dir = dir;
1616 if (initc == '/')
1617 ignore_cend = TRUE;
1618 backwards = (dir == FORWARD) ? FALSE : TRUE;
1619 initc = NUL;
1620 }
1621 else if (initc != '#' && initc != NUL)
1622 {
1623 /* 'matchpairs' is "x:y,x:y" */
1624 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1625 {
1626 if (*ptr == initc)
1627 {
1628 findc = initc;
1629 initc = ptr[2];
1630 backwards = TRUE;
1631 break;
1632 }
1633 ptr += 2;
1634 if (*ptr == initc)
1635 {
1636 findc = initc;
1637 initc = ptr[-2];
1638 backwards = FALSE;
1639 break;
1640 }
1641 if (ptr[1] != ',')
1642 break;
1643 }
1644 if (!findc) /* invalid initc! */
1645 return NULL;
1646 }
1647 /*
1648 * Either initc is '#', or no initc was given and we need to look under the
1649 * cursor.
1650 */
1651 else
1652 {
1653 if (initc == '#')
1654 {
1655 hash_dir = dir;
1656 }
1657 else
1658 {
1659 /*
1660 * initc was not given, must look for something to match under
1661 * or near the cursor.
1662 * Only check for special things when 'cpo' doesn't have '%'.
1663 */
1664 if (!cpo_match)
1665 {
1666 /* Are we before or at #if, #else etc.? */
1667 ptr = skipwhite(linep);
1668 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1669 {
1670 ptr = skipwhite(ptr + 1);
1671 if ( STRNCMP(ptr, "if", 2) == 0
1672 || STRNCMP(ptr, "endif", 5) == 0
1673 || STRNCMP(ptr, "el", 2) == 0)
1674 hash_dir = 1;
1675 }
1676
1677 /* Are we on a comment? */
1678 else if (linep[pos.col] == '/')
1679 {
1680 if (linep[pos.col + 1] == '*')
1681 {
1682 comment_dir = FORWARD;
1683 backwards = FALSE;
1684 pos.col++;
1685 }
1686 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1687 {
1688 comment_dir = BACKWARD;
1689 backwards = TRUE;
1690 pos.col--;
1691 }
1692 }
1693 else if (linep[pos.col] == '*')
1694 {
1695 if (linep[pos.col + 1] == '/')
1696 {
1697 comment_dir = BACKWARD;
1698 backwards = TRUE;
1699 }
1700 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1701 {
1702 comment_dir = FORWARD;
1703 backwards = FALSE;
1704 }
1705 }
1706 }
1707
1708 /*
1709 * If we are not on a comment or the # at the start of a line, then
1710 * look for brace anywhere on this line after the cursor.
1711 */
1712 if (!hash_dir && !comment_dir)
1713 {
1714 /*
1715 * Find the brace under or after the cursor.
1716 * If beyond the end of the line, use the last character in
1717 * the line.
1718 */
1719 if (linep[pos.col] == NUL && pos.col)
1720 --pos.col;
1721 for (;;)
1722 {
1723 initc = linep[pos.col];
1724 if (initc == NUL)
1725 break;
1726
1727 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1728 {
1729 if (*ptr == initc)
1730 {
1731 findc = ptr[2];
1732 backwards = FALSE;
1733 break;
1734 }
1735 ptr += 2;
1736 if (*ptr == initc)
1737 {
1738 findc = ptr[-2];
1739 backwards = TRUE;
1740 break;
1741 }
1742 if (!*++ptr)
1743 break;
1744 }
1745 if (findc)
1746 break;
1747#ifdef FEAT_MBYTE
1748 if (has_mbyte)
1749 pos.col += (*mb_ptr2len_check)(linep + pos.col);
1750 else
1751#endif
1752 ++pos.col;
1753 }
1754 if (!findc)
1755 {
1756 /* no brace in the line, maybe use " #if" then */
1757 if (!cpo_match && *skipwhite(linep) == '#')
1758 hash_dir = 1;
1759 else
1760 return NULL;
1761 }
1762 else if (!cpo_bsl)
1763 {
1764 int col, bslcnt = 0;
1765
1766 /* Set "match_escaped" if there are an odd number of
1767 * backslashes. */
1768 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1769 bslcnt++;
1770 match_escaped = (bslcnt & 1);
1771 }
1772 }
1773 }
1774 if (hash_dir)
1775 {
1776 /*
1777 * Look for matching #if, #else, #elif, or #endif
1778 */
1779 if (oap != NULL)
1780 oap->motion_type = MLINE; /* Linewise for this case only */
1781 if (initc != '#')
1782 {
1783 ptr = skipwhite(skipwhite(linep) + 1);
1784 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1785 hash_dir = 1;
1786 else if (STRNCMP(ptr, "endif", 5) == 0)
1787 hash_dir = -1;
1788 else
1789 return NULL;
1790 }
1791 pos.col = 0;
1792 while (!got_int)
1793 {
1794 if (hash_dir > 0)
1795 {
1796 if (pos.lnum == curbuf->b_ml.ml_line_count)
1797 break;
1798 }
1799 else if (pos.lnum == 1)
1800 break;
1801 pos.lnum += hash_dir;
1802 linep = ml_get(pos.lnum);
1803 line_breakcheck(); /* check for CTRL-C typed */
1804 ptr = skipwhite(linep);
1805 if (*ptr != '#')
1806 continue;
1807 pos.col = (colnr_T) (ptr - linep);
1808 ptr = skipwhite(ptr + 1);
1809 if (hash_dir > 0)
1810 {
1811 if (STRNCMP(ptr, "if", 2) == 0)
1812 count++;
1813 else if (STRNCMP(ptr, "el", 2) == 0)
1814 {
1815 if (count == 0)
1816 return &pos;
1817 }
1818 else if (STRNCMP(ptr, "endif", 5) == 0)
1819 {
1820 if (count == 0)
1821 return &pos;
1822 count--;
1823 }
1824 }
1825 else
1826 {
1827 if (STRNCMP(ptr, "if", 2) == 0)
1828 {
1829 if (count == 0)
1830 return &pos;
1831 count--;
1832 }
1833 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1834 {
1835 if (count == 0)
1836 return &pos;
1837 }
1838 else if (STRNCMP(ptr, "endif", 5) == 0)
1839 count++;
1840 }
1841 }
1842 return NULL;
1843 }
1844 }
1845
1846#ifdef FEAT_RIGHTLEFT
1847 /* This is just guessing: when 'rightleft' is set, search for a maching
1848 * paren/brace in the other direction. */
1849 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1850 backwards = !backwards;
1851#endif
1852
1853 do_quotes = -1;
1854 start_in_quotes = MAYBE;
1855 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001856 if ((backwards && comment_dir)
1857#ifdef FEAT_LISP
1858 || lisp
1859#endif
1860 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001861 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001862#ifdef FEAT_LISP
1863 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1864 lispcomm = TRUE; /* find match inside this comment */
1865#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001866 while (!got_int)
1867 {
1868 /*
1869 * Go to the next position, forward or backward. We could use
1870 * inc() and dec() here, but that is much slower
1871 */
1872 if (backwards)
1873 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001874#ifdef FEAT_LISP
1875 /* char to match is inside of comment, don't search outside */
1876 if (lispcomm && pos.col < (colnr_T)comment_col)
1877 break;
1878#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001879 if (pos.col == 0) /* at start of line, go to prev. one */
1880 {
1881 if (pos.lnum == 1) /* start of file */
1882 break;
1883 --pos.lnum;
1884
1885 if (maxtravel && traveled++ > maxtravel)
1886 break;
1887
1888 linep = ml_get(pos.lnum);
1889 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1890 do_quotes = -1;
1891 line_breakcheck();
1892
1893 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001894 if (comment_dir
1895#ifdef FEAT_LISP
1896 || lisp
1897#endif
1898 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001899 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001900#ifdef FEAT_LISP
1901 /* skip comment */
1902 if (lisp && comment_col != MAXCOL)
1903 pos.col = comment_col;
1904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 }
1906 else
1907 {
1908 --pos.col;
1909#ifdef FEAT_MBYTE
1910 if (has_mbyte)
1911 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1912#endif
1913 }
1914 }
1915 else /* forward search */
1916 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001917 if (linep[pos.col] == NUL
1918 /* at end of line, go to next one */
1919#ifdef FEAT_LISP
1920 /* don't search for match in comment */
1921 || (lisp && comment_col != MAXCOL
1922 && pos.col == (colnr_T)comment_col)
1923#endif
1924 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001925 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001926 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
1927#ifdef FEAT_LISP
1928 /* line is exhausted and comment with it,
1929 * don't search for match in code */
1930 || lispcomm
1931#endif
1932 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001933 break;
1934 ++pos.lnum;
1935
1936 if (maxtravel && traveled++ > maxtravel)
1937 break;
1938
1939 linep = ml_get(pos.lnum);
1940 pos.col = 0;
1941 do_quotes = -1;
1942 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001943#ifdef FEAT_LISP
1944 if (lisp) /* find comment pos in new line */
1945 comment_col = check_linecomment(linep);
1946#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001947 }
1948 else
1949 {
1950#ifdef FEAT_MBYTE
1951 if (has_mbyte)
1952 pos.col += (*mb_ptr2len_check)(linep + pos.col);
1953 else
1954#endif
1955 ++pos.col;
1956 }
1957 }
1958
1959 /*
1960 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
1961 */
1962 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
1963 (linep[0] == '{' || linep[0] == '}'))
1964 {
1965 if (linep[0] == findc && count == 0) /* match! */
1966 return &pos;
1967 break; /* out of scope */
1968 }
1969
1970 if (comment_dir)
1971 {
1972 /* Note: comments do not nest, and we ignore quotes in them */
1973 /* TODO: ignore comment brackets inside strings */
1974 if (comment_dir == FORWARD)
1975 {
1976 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
1977 {
1978 pos.col++;
1979 return &pos;
1980 }
1981 }
1982 else /* Searching backwards */
1983 {
1984 /*
1985 * A comment may contain / * or / /, it may also start or end
1986 * with / * /. Ignore a / * after / /.
1987 */
1988 if (pos.col == 0)
1989 continue;
1990 else if ( linep[pos.col - 1] == '/'
1991 && linep[pos.col] == '*'
1992 && (int)pos.col < comment_col)
1993 {
1994 count++;
1995 match_pos = pos;
1996 match_pos.col--;
1997 }
1998 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
1999 {
2000 if (count > 0)
2001 pos = match_pos;
2002 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2003 && (int)pos.col <= comment_col)
2004 pos.col -= 2;
2005 else if (ignore_cend)
2006 continue;
2007 else
2008 return NULL;
2009 return &pos;
2010 }
2011 }
2012 continue;
2013 }
2014
2015 /*
2016 * If smart matching ('cpoptions' does not contain '%'), braces inside
2017 * of quotes are ignored, but only if there is an even number of
2018 * quotes in the line.
2019 */
2020 if (cpo_match)
2021 do_quotes = 0;
2022 else if (do_quotes == -1)
2023 {
2024 /*
2025 * Count the number of quotes in the line, skipping \" and '"'.
2026 * Watch out for "\\".
2027 */
2028 at_start = do_quotes;
2029 for (ptr = linep; *ptr; ++ptr)
2030 {
2031 if (ptr == linep + pos.col + backwards)
2032 at_start = (do_quotes & 1);
2033 if (*ptr == '"'
2034 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2035 ++do_quotes;
2036 if (*ptr == '\\' && ptr[1] != NUL)
2037 ++ptr;
2038 }
2039 do_quotes &= 1; /* result is 1 with even number of quotes */
2040
2041 /*
2042 * If we find an uneven count, check current line and previous
2043 * one for a '\' at the end.
2044 */
2045 if (!do_quotes)
2046 {
2047 inquote = FALSE;
2048 if (ptr[-1] == '\\')
2049 {
2050 do_quotes = 1;
2051 if (start_in_quotes == MAYBE)
2052 {
2053 /* Do we need to use at_start here? */
2054 inquote = TRUE;
2055 start_in_quotes = TRUE;
2056 }
2057 else if (backwards)
2058 inquote = TRUE;
2059 }
2060 if (pos.lnum > 1)
2061 {
2062 ptr = ml_get(pos.lnum - 1);
2063 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2064 {
2065 do_quotes = 1;
2066 if (start_in_quotes == MAYBE)
2067 {
2068 inquote = at_start;
2069 if (inquote)
2070 start_in_quotes = TRUE;
2071 }
2072 else if (!backwards)
2073 inquote = TRUE;
2074 }
2075 }
2076 }
2077 }
2078 if (start_in_quotes == MAYBE)
2079 start_in_quotes = FALSE;
2080
2081 /*
2082 * If 'smartmatch' is set:
2083 * Things inside quotes are ignored by setting 'inquote'. If we
2084 * find a quote without a preceding '\' invert 'inquote'. At the
2085 * end of a line not ending in '\' we reset 'inquote'.
2086 *
2087 * In lines with an uneven number of quotes (without preceding '\')
2088 * we do not know which part to ignore. Therefore we only set
2089 * inquote if the number of quotes in a line is even, unless this
2090 * line or the previous one ends in a '\'. Complicated, isn't it?
2091 */
2092 switch (c = linep[pos.col])
2093 {
2094 case NUL:
2095 /* at end of line without trailing backslash, reset inquote */
2096 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2097 {
2098 inquote = FALSE;
2099 start_in_quotes = FALSE;
2100 }
2101 break;
2102
2103 case '"':
2104 /* a quote that is preceded with an odd number of backslashes is
2105 * ignored */
2106 if (do_quotes)
2107 {
2108 int col;
2109
2110 for (col = pos.col - 1; col >= 0; --col)
2111 if (linep[col] != '\\')
2112 break;
2113 if ((((int)pos.col - 1 - col) & 1) == 0)
2114 {
2115 inquote = !inquote;
2116 start_in_quotes = FALSE;
2117 }
2118 }
2119 break;
2120
2121 /*
2122 * If smart matching ('cpoptions' does not contain '%'):
2123 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2124 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2125 * skipped, there is never a brace in them.
2126 * Ignore this when finding matches for `'.
2127 */
2128 case '\'':
2129 if (!cpo_match && initc != '\'' && findc != '\'')
2130 {
2131 if (backwards)
2132 {
2133 if (pos.col > 1)
2134 {
2135 if (linep[pos.col - 2] == '\'')
2136 {
2137 pos.col -= 2;
2138 break;
2139 }
2140 else if (linep[pos.col - 2] == '\\' &&
2141 pos.col > 2 && linep[pos.col - 3] == '\'')
2142 {
2143 pos.col -= 3;
2144 break;
2145 }
2146 }
2147 }
2148 else if (linep[pos.col + 1]) /* forward search */
2149 {
2150 if (linep[pos.col + 1] == '\\' &&
2151 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2152 {
2153 pos.col += 3;
2154 break;
2155 }
2156 else if (linep[pos.col + 2] == '\'')
2157 {
2158 pos.col += 2;
2159 break;
2160 }
2161 }
2162 }
2163 /* FALLTHROUGH */
2164
2165 default:
2166#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002167 /*
2168 * For Lisp skip over backslashed (), {} and [].
2169 * (actually, we skip #\( et al)
2170 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002171 if (curbuf->b_p_lisp
2172 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002173 && pos.col > 1
2174 && check_prevcol(linep, pos.col, '\\', NULL)
2175 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002176 break;
2177#endif
2178
2179 /* Check for match outside of quotes, and inside of
2180 * quotes when the start is also inside of quotes. */
2181 if ((!inquote || start_in_quotes == TRUE)
2182 && (c == initc || c == findc))
2183 {
2184 int col, bslcnt = 0;
2185
2186 if (!cpo_bsl)
2187 {
2188 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2189 bslcnt++;
2190 }
2191 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2192 * what we expect. */
2193 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2194 {
2195 if (c == initc)
2196 count++;
2197 else
2198 {
2199 if (count == 0)
2200 return &pos;
2201 count--;
2202 }
2203 }
2204 }
2205 }
2206 }
2207
2208 if (comment_dir == BACKWARD && count > 0)
2209 {
2210 pos = match_pos;
2211 return &pos;
2212 }
2213 return (pos_T *)NULL; /* never found it */
2214}
2215
2216/*
2217 * Check if line[] contains a / / comment.
2218 * Return MAXCOL if not, otherwise return the column.
2219 * TODO: skip strings.
2220 */
2221 static int
2222check_linecomment(line)
2223 char_u *line;
2224{
2225 char_u *p;
2226
2227 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002228#ifdef FEAT_LISP
2229 /* skip Lispish one-line comments */
2230 if (curbuf->b_p_lisp)
2231 {
2232 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2233 {
2234 int instr = FALSE; /* inside of string */
2235
2236 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002237 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002238 {
2239 if (*p == '"')
2240 {
2241 if (instr)
2242 {
2243 if (*(p - 1) != '\\') /* skip escaped quote */
2244 instr = FALSE;
2245 }
2246 else if (p == line || ((p - line) >= 2
2247 /* skip #\" form */
2248 && *(p - 1) != '\\' && *(p - 2) != '#'))
2249 instr = TRUE;
2250 }
2251 else if (!instr && ((p - line) < 2
2252 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2253 break; /* found! */
2254 ++p;
2255 }
2256 }
2257 else
2258 p = NULL;
2259 }
2260 else
2261#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002262 while ((p = vim_strchr(p, '/')) != NULL)
2263 {
2264 if (p[1] == '/')
2265 break;
2266 ++p;
2267 }
2268
2269 if (p == NULL)
2270 return MAXCOL;
2271 return (int)(p - line);
2272}
2273
2274/*
2275 * Move cursor briefly to character matching the one under the cursor.
2276 * Used for Insert mode and "r" command.
2277 * Show the match only if it is visible on the screen.
2278 * If there isn't a match, then beep.
2279 */
2280 void
2281showmatch(c)
2282 int c; /* char to show match for */
2283{
2284 pos_T *lpos, save_cursor;
2285 pos_T mpos;
2286 colnr_T vcol;
2287 long save_so;
2288 long save_siso;
2289#ifdef CURSOR_SHAPE
2290 int save_state;
2291#endif
2292 colnr_T save_dollar_vcol;
2293 char_u *p;
2294
2295 /*
2296 * Only show match for chars in the 'matchpairs' option.
2297 */
2298 /* 'matchpairs' is "x:y,x:y" */
2299 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2300 {
2301#ifdef FEAT_RIGHTLEFT
2302 if (*p == c && (curwin->w_p_rl ^ p_ri))
2303 break;
2304#endif
2305 p += 2;
2306 if (*p == c
2307#ifdef FEAT_RIGHTLEFT
2308 && !(curwin->w_p_rl ^ p_ri)
2309#endif
2310 )
2311 break;
2312 if (p[1] != ',')
2313 return;
2314 }
2315
2316 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2317 vim_beep();
2318 else if (lpos->lnum >= curwin->w_topline)
2319 {
2320 if (!curwin->w_p_wrap)
2321 getvcol(curwin, lpos, NULL, &vcol, NULL);
2322 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2323 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2324 {
2325 mpos = *lpos; /* save the pos, update_screen() may change it */
2326 save_cursor = curwin->w_cursor;
2327 save_so = p_so;
2328 save_siso = p_siso;
2329 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2330 * stop displaying the "$". */
2331 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2332 dollar_vcol = 0;
2333 ++curwin->w_virtcol; /* do display ')' just before "$" */
2334 update_screen(VALID); /* show the new char first */
2335
2336 save_dollar_vcol = dollar_vcol;
2337#ifdef CURSOR_SHAPE
2338 save_state = State;
2339 State = SHOWMATCH;
2340 ui_cursor_shape(); /* may show different cursor shape */
2341#endif
2342 curwin->w_cursor = mpos; /* move to matching char */
2343 p_so = 0; /* don't use 'scrolloff' here */
2344 p_siso = 0; /* don't use 'sidescrolloff' here */
2345 showruler(FALSE);
2346 setcursor();
2347 cursor_on(); /* make sure that the cursor is shown */
2348 out_flush();
2349#ifdef FEAT_GUI
2350 if (gui.in_use)
2351 {
2352 gui_update_cursor(TRUE, FALSE);
2353 gui_mch_flush();
2354 }
2355#endif
2356 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2357 * which resets it if the matching position is in a previous line
2358 * and has a higher column number. */
2359 dollar_vcol = save_dollar_vcol;
2360
2361 /*
2362 * brief pause, unless 'm' is present in 'cpo' and a character is
2363 * available.
2364 */
2365 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2366 ui_delay(p_mat * 100L, TRUE);
2367 else if (!char_avail())
2368 ui_delay(p_mat * 100L, FALSE);
2369 curwin->w_cursor = save_cursor; /* restore cursor position */
2370 p_so = save_so;
2371 p_siso = save_siso;
2372#ifdef CURSOR_SHAPE
2373 State = save_state;
2374 ui_cursor_shape(); /* may show different cursor shape */
2375#endif
2376 }
2377 }
2378}
2379
2380/*
2381 * findsent(dir, count) - Find the start of the next sentence in direction
2382 * 'dir' Sentences are supposed to end in ".", "!" or "?" followed by white
2383 * space or a line break. Also stop at an empty line.
2384 * Return OK if the next sentence was found.
2385 */
2386 int
2387findsent(dir, count)
2388 int dir;
2389 long count;
2390{
2391 pos_T pos, tpos;
2392 int c;
2393 int (*func) __ARGS((pos_T *));
2394 int startlnum;
2395 int noskip = FALSE; /* do not skip blanks */
2396 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002397 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002398
2399 pos = curwin->w_cursor;
2400 if (dir == FORWARD)
2401 func = incl;
2402 else
2403 func = decl;
2404
2405 while (count--)
2406 {
2407 /*
2408 * if on an empty line, skip upto a non-empty line
2409 */
2410 if (gchar_pos(&pos) == NUL)
2411 {
2412 do
2413 if ((*func)(&pos) == -1)
2414 break;
2415 while (gchar_pos(&pos) == NUL);
2416 if (dir == FORWARD)
2417 goto found;
2418 }
2419 /*
2420 * if on the start of a paragraph or a section and searching forward,
2421 * go to the next line
2422 */
2423 else if (dir == FORWARD && pos.col == 0 &&
2424 startPS(pos.lnum, NUL, FALSE))
2425 {
2426 if (pos.lnum == curbuf->b_ml.ml_line_count)
2427 return FAIL;
2428 ++pos.lnum;
2429 goto found;
2430 }
2431 else if (dir == BACKWARD)
2432 decl(&pos);
2433
2434 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002435 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002436 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2437 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2438 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002439 if (vim_strchr((char_u *)".!?", c) != NULL)
2440 {
2441 /* Only skip over a '.', '!' and '?' once. */
2442 if (found_dot)
2443 break;
2444 found_dot = TRUE;
2445 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 if (decl(&pos) == -1)
2447 break;
2448 /* when going forward: Stop in front of empty line */
2449 if (lineempty(pos.lnum) && dir == FORWARD)
2450 {
2451 incl(&pos);
2452 goto found;
2453 }
2454 }
2455
2456 /* remember the line where the search started */
2457 startlnum = pos.lnum;
2458 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2459
2460 for (;;) /* find end of sentence */
2461 {
2462 c = gchar_pos(&pos);
2463 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2464 {
2465 if (dir == BACKWARD && pos.lnum != startlnum)
2466 ++pos.lnum;
2467 break;
2468 }
2469 if (c == '.' || c == '!' || c == '?')
2470 {
2471 tpos = pos;
2472 do
2473 if ((c = inc(&tpos)) == -1)
2474 break;
2475 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2476 != NULL);
2477 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2478 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2479 && gchar_pos(&tpos) == ' ')))
2480 {
2481 pos = tpos;
2482 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2483 inc(&pos);
2484 break;
2485 }
2486 }
2487 if ((*func)(&pos) == -1)
2488 {
2489 if (count)
2490 return FAIL;
2491 noskip = TRUE;
2492 break;
2493 }
2494 }
2495found:
2496 /* skip white space */
2497 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2498 if (incl(&pos) == -1)
2499 break;
2500 }
2501
2502 setpcmark();
2503 curwin->w_cursor = pos;
2504 return OK;
2505}
2506
2507/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002508 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002509 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002510 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002511 * If 'what' is '{' or '}' we go to the next section.
2512 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002513 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002514 */
2515 int
2516findpar(oap, dir, count, what, both)
2517 oparg_T *oap;
2518 int dir;
2519 long count;
2520 int what;
2521 int both;
2522{
2523 linenr_T curr;
2524 int did_skip; /* TRUE after separating lines have been skipped */
2525 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002526 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002527#ifdef FEAT_FOLDING
2528 linenr_T fold_first; /* first line of a closed fold */
2529 linenr_T fold_last; /* last line of a closed fold */
2530 int fold_skipped; /* TRUE if a closed fold was skipped this
2531 iteration */
2532#endif
2533
2534 curr = curwin->w_cursor.lnum;
2535
2536 while (count--)
2537 {
2538 did_skip = FALSE;
2539 for (first = TRUE; ; first = FALSE)
2540 {
2541 if (*ml_get(curr) != NUL)
2542 did_skip = TRUE;
2543
2544#ifdef FEAT_FOLDING
2545 /* skip folded lines */
2546 fold_skipped = FALSE;
2547 if (first && hasFolding(curr, &fold_first, &fold_last))
2548 {
2549 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2550 fold_skipped = TRUE;
2551 }
2552#endif
2553
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002554 /* POSIX has it's own ideas of what a paragraph boundary is and it
2555 * doesn't match historical Vi: It also stops at a "{" in the
2556 * first column and at an empty line. */
2557 if (!first && did_skip && (startPS(curr, what, both)
2558 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002559 break;
2560
2561#ifdef FEAT_FOLDING
2562 if (fold_skipped)
2563 curr -= dir;
2564#endif
2565 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2566 {
2567 if (count)
2568 return FALSE;
2569 curr -= dir;
2570 break;
2571 }
2572 }
2573 }
2574 setpcmark();
2575 if (both && *ml_get(curr) == '}') /* include line with '}' */
2576 ++curr;
2577 curwin->w_cursor.lnum = curr;
2578 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2579 {
2580 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2581 {
2582 --curwin->w_cursor.col;
2583 oap->inclusive = TRUE;
2584 }
2585 }
2586 else
2587 curwin->w_cursor.col = 0;
2588 return TRUE;
2589}
2590
2591/*
2592 * check if the string 's' is a nroff macro that is in option 'opt'
2593 */
2594 static int
2595inmacro(opt, s)
2596 char_u *opt;
2597 char_u *s;
2598{
2599 char_u *macro;
2600
2601 for (macro = opt; macro[0]; ++macro)
2602 {
2603 /* Accept two characters in the option being equal to two characters
2604 * in the line. A space in the option matches with a space in the
2605 * line or the line having ended. */
2606 if ( (macro[0] == s[0]
2607 || (macro[0] == ' '
2608 && (s[0] == NUL || s[0] == ' ')))
2609 && (macro[1] == s[1]
2610 || ((macro[1] == NUL || macro[1] == ' ')
2611 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2612 break;
2613 ++macro;
2614 if (macro[0] == NUL)
2615 break;
2616 }
2617 return (macro[0] != NUL);
2618}
2619
2620/*
2621 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2622 * If 'para' is '{' or '}' only check for sections.
2623 * If 'both' is TRUE also stop at '}'
2624 */
2625 int
2626startPS(lnum, para, both)
2627 linenr_T lnum;
2628 int para;
2629 int both;
2630{
2631 char_u *s;
2632
2633 s = ml_get(lnum);
2634 if (*s == para || *s == '\f' || (both && *s == '}'))
2635 return TRUE;
2636 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2637 (!para && inmacro(p_para, s + 1))))
2638 return TRUE;
2639 return FALSE;
2640}
2641
2642/*
2643 * The following routines do the word searches performed by the 'w', 'W',
2644 * 'b', 'B', 'e', and 'E' commands.
2645 */
2646
2647/*
2648 * To perform these searches, characters are placed into one of three
2649 * classes, and transitions between classes determine word boundaries.
2650 *
2651 * The classes are:
2652 *
2653 * 0 - white space
2654 * 1 - punctuation
2655 * 2 or higher - keyword characters (letters, digits and underscore)
2656 */
2657
2658static int cls_bigword; /* TRUE for "W", "B" or "E" */
2659
2660/*
2661 * cls() - returns the class of character at curwin->w_cursor
2662 *
2663 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2664 * from class 2 and higher are reported as class 1 since only white space
2665 * boundaries are of interest.
2666 */
2667 static int
2668cls()
2669{
2670 int c;
2671
2672 c = gchar_cursor();
2673#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2674 if (p_altkeymap && c == F_BLANK)
2675 return 0;
2676#endif
2677 if (c == ' ' || c == '\t' || c == NUL)
2678 return 0;
2679#ifdef FEAT_MBYTE
2680 if (enc_dbcs != 0 && c > 0xFF)
2681 {
2682 /* If cls_bigword, report multi-byte chars as class 1. */
2683 if (enc_dbcs == DBCS_KOR && cls_bigword)
2684 return 1;
2685
2686 /* process code leading/trailing bytes */
2687 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2688 }
2689 if (enc_utf8)
2690 {
2691 c = utf_class(c);
2692 if (c != 0 && cls_bigword)
2693 return 1;
2694 return c;
2695 }
2696#endif
2697
2698 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2699 if (cls_bigword)
2700 return 1;
2701
2702 if (vim_iswordc(c))
2703 return 2;
2704 return 1;
2705}
2706
2707
2708/*
2709 * fwd_word(count, type, eol) - move forward one word
2710 *
2711 * Returns FAIL if the cursor was already at the end of the file.
2712 * If eol is TRUE, last word stops at end of line (for operators).
2713 */
2714 int
2715fwd_word(count, bigword, eol)
2716 long count;
2717 int bigword; /* "W", "E" or "B" */
2718 int eol;
2719{
2720 int sclass; /* starting class */
2721 int i;
2722 int last_line;
2723
2724#ifdef FEAT_VIRTUALEDIT
2725 curwin->w_cursor.coladd = 0;
2726#endif
2727 cls_bigword = bigword;
2728 while (--count >= 0)
2729 {
2730#ifdef FEAT_FOLDING
2731 /* When inside a range of folded lines, move to the last char of the
2732 * last line. */
2733 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2734 coladvance((colnr_T)MAXCOL);
2735#endif
2736 sclass = cls();
2737
2738 /*
2739 * We always move at least one character, unless on the last
2740 * character in the buffer.
2741 */
2742 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2743 i = inc_cursor();
2744 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2745 return FAIL;
2746 if (i == 1 && eol && count == 0) /* started at last char in line */
2747 return OK;
2748
2749 /*
2750 * Go one char past end of current word (if any)
2751 */
2752 if (sclass != 0)
2753 while (cls() == sclass)
2754 {
2755 i = inc_cursor();
2756 if (i == -1 || (i >= 1 && eol && count == 0))
2757 return OK;
2758 }
2759
2760 /*
2761 * go to next non-white
2762 */
2763 while (cls() == 0)
2764 {
2765 /*
2766 * We'll stop if we land on a blank line
2767 */
2768 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2769 break;
2770
2771 i = inc_cursor();
2772 if (i == -1 || (i >= 1 && eol && count == 0))
2773 return OK;
2774 }
2775 }
2776 return OK;
2777}
2778
2779/*
2780 * bck_word() - move backward 'count' words
2781 *
2782 * If stop is TRUE and we are already on the start of a word, move one less.
2783 *
2784 * Returns FAIL if top of the file was reached.
2785 */
2786 int
2787bck_word(count, bigword, stop)
2788 long count;
2789 int bigword;
2790 int stop;
2791{
2792 int sclass; /* starting class */
2793
2794#ifdef FEAT_VIRTUALEDIT
2795 curwin->w_cursor.coladd = 0;
2796#endif
2797 cls_bigword = bigword;
2798 while (--count >= 0)
2799 {
2800#ifdef FEAT_FOLDING
2801 /* When inside a range of folded lines, move to the first char of the
2802 * first line. */
2803 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2804 curwin->w_cursor.col = 0;
2805#endif
2806 sclass = cls();
2807 if (dec_cursor() == -1) /* started at start of file */
2808 return FAIL;
2809
2810 if (!stop || sclass == cls() || sclass == 0)
2811 {
2812 /*
2813 * Skip white space before the word.
2814 * Stop on an empty line.
2815 */
2816 while (cls() == 0)
2817 {
2818 if (curwin->w_cursor.col == 0
2819 && lineempty(curwin->w_cursor.lnum))
2820 goto finished;
2821 if (dec_cursor() == -1) /* hit start of file, stop here */
2822 return OK;
2823 }
2824
2825 /*
2826 * Move backward to start of this word.
2827 */
2828 if (skip_chars(cls(), BACKWARD))
2829 return OK;
2830 }
2831
2832 inc_cursor(); /* overshot - forward one */
2833finished:
2834 stop = FALSE;
2835 }
2836 return OK;
2837}
2838
2839/*
2840 * end_word() - move to the end of the word
2841 *
2842 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2843 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2844 * motion crosses blank lines. When the real vi crosses a blank line in an
2845 * 'e' motion, the cursor is placed on the FIRST character of the next
2846 * non-blank line. The 'E' command, however, works correctly. Since this
2847 * appears to be a bug, I have not duplicated it here.
2848 *
2849 * Returns FAIL if end of the file was reached.
2850 *
2851 * If stop is TRUE and we are already on the end of a word, move one less.
2852 * If empty is TRUE stop on an empty line.
2853 */
2854 int
2855end_word(count, bigword, stop, empty)
2856 long count;
2857 int bigword;
2858 int stop;
2859 int empty;
2860{
2861 int sclass; /* starting class */
2862
2863#ifdef FEAT_VIRTUALEDIT
2864 curwin->w_cursor.coladd = 0;
2865#endif
2866 cls_bigword = bigword;
2867 while (--count >= 0)
2868 {
2869#ifdef FEAT_FOLDING
2870 /* When inside a range of folded lines, move to the last char of the
2871 * last line. */
2872 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2873 coladvance((colnr_T)MAXCOL);
2874#endif
2875 sclass = cls();
2876 if (inc_cursor() == -1)
2877 return FAIL;
2878
2879 /*
2880 * If we're in the middle of a word, we just have to move to the end
2881 * of it.
2882 */
2883 if (cls() == sclass && sclass != 0)
2884 {
2885 /*
2886 * Move forward to end of the current word
2887 */
2888 if (skip_chars(sclass, FORWARD))
2889 return FAIL;
2890 }
2891 else if (!stop || sclass == 0)
2892 {
2893 /*
2894 * We were at the end of a word. Go to the end of the next word.
2895 * First skip white space, if 'empty' is TRUE, stop at empty line.
2896 */
2897 while (cls() == 0)
2898 {
2899 if (empty && curwin->w_cursor.col == 0
2900 && lineempty(curwin->w_cursor.lnum))
2901 goto finished;
2902 if (inc_cursor() == -1) /* hit end of file, stop here */
2903 return FAIL;
2904 }
2905
2906 /*
2907 * Move forward to the end of this word.
2908 */
2909 if (skip_chars(cls(), FORWARD))
2910 return FAIL;
2911 }
2912 dec_cursor(); /* overshot - one char backward */
2913finished:
2914 stop = FALSE; /* we move only one word less */
2915 }
2916 return OK;
2917}
2918
2919/*
2920 * Move back to the end of the word.
2921 *
2922 * Returns FAIL if start of the file was reached.
2923 */
2924 int
2925bckend_word(count, bigword, eol)
2926 long count;
2927 int bigword; /* TRUE for "B" */
2928 int eol; /* TRUE: stop at end of line. */
2929{
2930 int sclass; /* starting class */
2931 int i;
2932
2933#ifdef FEAT_VIRTUALEDIT
2934 curwin->w_cursor.coladd = 0;
2935#endif
2936 cls_bigword = bigword;
2937 while (--count >= 0)
2938 {
2939 sclass = cls();
2940 if ((i = dec_cursor()) == -1)
2941 return FAIL;
2942 if (eol && i == 1)
2943 return OK;
2944
2945 /*
2946 * Move backward to before the start of this word.
2947 */
2948 if (sclass != 0)
2949 {
2950 while (cls() == sclass)
2951 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2952 return OK;
2953 }
2954
2955 /*
2956 * Move backward to end of the previous word
2957 */
2958 while (cls() == 0)
2959 {
2960 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
2961 break;
2962 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2963 return OK;
2964 }
2965 }
2966 return OK;
2967}
2968
2969/*
2970 * Skip a row of characters of the same class.
2971 * Return TRUE when end-of-file reached, FALSE otherwise.
2972 */
2973 static int
2974skip_chars(cclass, dir)
2975 int cclass;
2976 int dir;
2977{
2978 while (cls() == cclass)
2979 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
2980 return TRUE;
2981 return FALSE;
2982}
2983
2984#ifdef FEAT_TEXTOBJ
2985/*
2986 * Go back to the start of the word or the start of white space
2987 */
2988 static void
2989back_in_line()
2990{
2991 int sclass; /* starting class */
2992
2993 sclass = cls();
2994 for (;;)
2995 {
2996 if (curwin->w_cursor.col == 0) /* stop at start of line */
2997 break;
2998 dec_cursor();
2999 if (cls() != sclass) /* stop at start of word */
3000 {
3001 inc_cursor();
3002 break;
3003 }
3004 }
3005}
3006
3007 static void
3008find_first_blank(posp)
3009 pos_T *posp;
3010{
3011 int c;
3012
3013 while (decl(posp) != -1)
3014 {
3015 c = gchar_pos(posp);
3016 if (!vim_iswhite(c))
3017 {
3018 incl(posp);
3019 break;
3020 }
3021 }
3022}
3023
3024/*
3025 * Skip count/2 sentences and count/2 separating white spaces.
3026 */
3027 static void
3028findsent_forward(count, at_start_sent)
3029 long count;
3030 int at_start_sent; /* cursor is at start of sentence */
3031{
3032 while (count--)
3033 {
3034 findsent(FORWARD, 1L);
3035 if (at_start_sent)
3036 find_first_blank(&curwin->w_cursor);
3037 if (count == 0 || at_start_sent)
3038 decl(&curwin->w_cursor);
3039 at_start_sent = !at_start_sent;
3040 }
3041}
3042
3043/*
3044 * Find word under cursor, cursor at end.
3045 * Used while an operator is pending, and in Visual mode.
3046 */
3047 int
3048current_word(oap, count, include, bigword)
3049 oparg_T *oap;
3050 long count;
3051 int include; /* TRUE: include word and white space */
3052 int bigword; /* FALSE == word, TRUE == WORD */
3053{
3054 pos_T start_pos;
3055 pos_T pos;
3056 int inclusive = TRUE;
3057 int include_white = FALSE;
3058
3059 cls_bigword = bigword;
3060
3061#ifdef FEAT_VISUAL
3062 /* Correct cursor when 'selection' is exclusive */
3063 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3064 dec_cursor();
3065
3066 /*
3067 * When Visual mode is not active, or when the VIsual area is only one
3068 * character, select the word and/or white space under the cursor.
3069 */
3070 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3071#endif
3072 {
3073 /*
3074 * Go to start of current word or white space.
3075 */
3076 back_in_line();
3077 start_pos = curwin->w_cursor;
3078
3079 /*
3080 * If the start is on white space, and white space should be included
3081 * (" word"), or start is not on white space, and white space should
3082 * not be included ("word"), find end of word.
3083 */
3084 if ((cls() == 0) == include)
3085 {
3086 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3087 return FAIL;
3088 }
3089 else
3090 {
3091 /*
3092 * If the start is not on white space, and white space should be
3093 * included ("word "), or start is on white space and white
3094 * space should not be included (" "), find start of word.
3095 * If we end up in the first column of the next line (single char
3096 * word) back up to end of the line.
3097 */
3098 fwd_word(1L, bigword, TRUE);
3099 if (curwin->w_cursor.col == 0)
3100 decl(&curwin->w_cursor);
3101 else
3102 oneleft();
3103
3104 if (include)
3105 include_white = TRUE;
3106 }
3107
3108#ifdef FEAT_VISUAL
3109 if (VIsual_active)
3110 {
3111 /* should do something when inclusive == FALSE ! */
3112 VIsual = start_pos;
3113 redraw_curbuf_later(INVERTED); /* update the inversion */
3114 }
3115 else
3116#endif
3117 {
3118 oap->start = start_pos;
3119 oap->motion_type = MCHAR;
3120 }
3121 --count;
3122 }
3123
3124 /*
3125 * When count is still > 0, extend with more objects.
3126 */
3127 while (count > 0)
3128 {
3129 inclusive = TRUE;
3130#ifdef FEAT_VISUAL
3131 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3132 {
3133 /*
3134 * In Visual mode, with cursor at start: move cursor back.
3135 */
3136 if (decl(&curwin->w_cursor) == -1)
3137 return FAIL;
3138 if (include != (cls() != 0))
3139 {
3140 if (bck_word(1L, bigword, TRUE) == FAIL)
3141 return FAIL;
3142 }
3143 else
3144 {
3145 if (bckend_word(1L, bigword, TRUE) == FAIL)
3146 return FAIL;
3147 (void)incl(&curwin->w_cursor);
3148 }
3149 }
3150 else
3151#endif
3152 {
3153 /*
3154 * Move cursor forward one word and/or white area.
3155 */
3156 if (incl(&curwin->w_cursor) == -1)
3157 return FAIL;
3158 if (include != (cls() == 0))
3159 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003160 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003161 return FAIL;
3162 /*
3163 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003164 * the first character on the line.
3165 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003166 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003167 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003168 inclusive = FALSE;
3169 }
3170 else
3171 {
3172 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3173 return FAIL;
3174 }
3175 }
3176 --count;
3177 }
3178
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003179 if (include_white && (cls() != 0
3180 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003181 {
3182 /*
3183 * If we don't include white space at the end, move the start
3184 * to include some white space there. This makes "daw" work
3185 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003186 * word). Also when "2daw" deletes "word." at the end of the line
3187 * (cursor is at start of next line).
3188 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003189 */
3190 pos = curwin->w_cursor; /* save cursor position */
3191 curwin->w_cursor = start_pos;
3192 if (oneleft() == OK)
3193 {
3194 back_in_line();
3195 if (cls() == 0 && curwin->w_cursor.col > 0)
3196 {
3197#ifdef FEAT_VISUAL
3198 if (VIsual_active)
3199 VIsual = curwin->w_cursor;
3200 else
3201#endif
3202 oap->start = curwin->w_cursor;
3203 }
3204 }
3205 curwin->w_cursor = pos; /* put cursor back at end */
3206 }
3207
3208#ifdef FEAT_VISUAL
3209 if (VIsual_active)
3210 {
3211 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3212 inc_cursor();
3213 if (VIsual_mode == 'V')
3214 {
3215 VIsual_mode = 'v';
3216 redraw_cmdline = TRUE; /* show mode later */
3217 }
3218 }
3219 else
3220#endif
3221 oap->inclusive = inclusive;
3222
3223 return OK;
3224}
3225
3226/*
3227 * Find sentence(s) under the cursor, cursor at end.
3228 * When Visual active, extend it by one or more sentences.
3229 */
3230 int
3231current_sent(oap, count, include)
3232 oparg_T *oap;
3233 long count;
3234 int include;
3235{
3236 pos_T start_pos;
3237 pos_T pos;
3238 int start_blank;
3239 int c;
3240 int at_start_sent;
3241 long ncount;
3242
3243 start_pos = curwin->w_cursor;
3244 pos = start_pos;
3245 findsent(FORWARD, 1L); /* Find start of next sentence. */
3246
3247#ifdef FEAT_VISUAL
3248 /*
3249 * When visual area is bigger than one character: Extend it.
3250 */
3251 if (VIsual_active && !equalpos(start_pos, VIsual))
3252 {
3253extend:
3254 if (lt(start_pos, VIsual))
3255 {
3256 /*
3257 * Cursor at start of Visual area.
3258 * Find out where we are:
3259 * - in the white space before a sentence
3260 * - in a sentence or just after it
3261 * - at the start of a sentence
3262 */
3263 at_start_sent = TRUE;
3264 decl(&pos);
3265 while (lt(pos, curwin->w_cursor))
3266 {
3267 c = gchar_pos(&pos);
3268 if (!vim_iswhite(c))
3269 {
3270 at_start_sent = FALSE;
3271 break;
3272 }
3273 incl(&pos);
3274 }
3275 if (!at_start_sent)
3276 {
3277 findsent(BACKWARD, 1L);
3278 if (equalpos(curwin->w_cursor, start_pos))
3279 at_start_sent = TRUE; /* exactly at start of sentence */
3280 else
3281 /* inside a sentence, go to its end (start of next) */
3282 findsent(FORWARD, 1L);
3283 }
3284 if (include) /* "as" gets twice as much as "is" */
3285 count *= 2;
3286 while (count--)
3287 {
3288 if (at_start_sent)
3289 find_first_blank(&curwin->w_cursor);
3290 c = gchar_cursor();
3291 if (!at_start_sent || (!include && !vim_iswhite(c)))
3292 findsent(BACKWARD, 1L);
3293 at_start_sent = !at_start_sent;
3294 }
3295 }
3296 else
3297 {
3298 /*
3299 * Cursor at end of Visual area.
3300 * Find out where we are:
3301 * - just before a sentence
3302 * - just before or in the white space before a sentence
3303 * - in a sentence
3304 */
3305 incl(&pos);
3306 at_start_sent = TRUE;
3307 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3308 {
3309 at_start_sent = FALSE;
3310 while (lt(pos, curwin->w_cursor))
3311 {
3312 c = gchar_pos(&pos);
3313 if (!vim_iswhite(c))
3314 {
3315 at_start_sent = TRUE;
3316 break;
3317 }
3318 incl(&pos);
3319 }
3320 if (at_start_sent) /* in the sentence */
3321 findsent(BACKWARD, 1L);
3322 else /* in/before white before a sentence */
3323 curwin->w_cursor = start_pos;
3324 }
3325
3326 if (include) /* "as" gets twice as much as "is" */
3327 count *= 2;
3328 findsent_forward(count, at_start_sent);
3329 if (*p_sel == 'e')
3330 ++curwin->w_cursor.col;
3331 }
3332 return OK;
3333 }
3334#endif
3335
3336 /*
3337 * If cursor started on blank, check if it is just before the start of the
3338 * next sentence.
3339 */
3340 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3341 incl(&pos);
3342 if (equalpos(pos, curwin->w_cursor))
3343 {
3344 start_blank = TRUE;
3345 find_first_blank(&start_pos); /* go back to first blank */
3346 }
3347 else
3348 {
3349 start_blank = FALSE;
3350 findsent(BACKWARD, 1L);
3351 start_pos = curwin->w_cursor;
3352 }
3353 if (include)
3354 ncount = count * 2;
3355 else
3356 {
3357 ncount = count;
3358 if (start_blank)
3359 --ncount;
3360 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003361 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003362 findsent_forward(ncount, TRUE);
3363 else
3364 decl(&curwin->w_cursor);
3365
3366 if (include)
3367 {
3368 /*
3369 * If the blank in front of the sentence is included, exclude the
3370 * blanks at the end of the sentence, go back to the first blank.
3371 * If there are no trailing blanks, try to include leading blanks.
3372 */
3373 if (start_blank)
3374 {
3375 find_first_blank(&curwin->w_cursor);
3376 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3377 if (vim_iswhite(c))
3378 decl(&curwin->w_cursor);
3379 }
3380 else if (c = gchar_cursor(), !vim_iswhite(c))
3381 find_first_blank(&start_pos);
3382 }
3383
3384#ifdef FEAT_VISUAL
3385 if (VIsual_active)
3386 {
3387 /* avoid getting stuck with "is" on a single space before a sent. */
3388 if (equalpos(start_pos, curwin->w_cursor))
3389 goto extend;
3390 if (*p_sel == 'e')
3391 ++curwin->w_cursor.col;
3392 VIsual = start_pos;
3393 VIsual_mode = 'v';
3394 redraw_curbuf_later(INVERTED); /* update the inversion */
3395 }
3396 else
3397#endif
3398 {
3399 /* include a newline after the sentence, if there is one */
3400 if (incl(&curwin->w_cursor) == -1)
3401 oap->inclusive = TRUE;
3402 else
3403 oap->inclusive = FALSE;
3404 oap->start = start_pos;
3405 oap->motion_type = MCHAR;
3406 }
3407 return OK;
3408}
3409
3410 int
3411current_block(oap, count, include, what, other)
3412 oparg_T *oap;
3413 long count;
3414 int include; /* TRUE == include white space */
3415 int what; /* '(', '{', etc. */
3416 int other; /* ')', '}', etc. */
3417{
3418 pos_T old_pos;
3419 pos_T *pos = NULL;
3420 pos_T start_pos;
3421 pos_T *end_pos;
3422 pos_T old_start, old_end;
3423 char_u *save_cpo;
3424 int sol = FALSE; /* { at start of line */
3425
3426 old_pos = curwin->w_cursor;
3427 old_end = curwin->w_cursor; /* remember where we started */
3428 old_start = old_end;
3429
3430 /*
3431 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3432 */
3433#ifdef FEAT_VISUAL
3434 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3435#endif
3436 {
3437 setpcmark();
3438 if (what == '{') /* ignore indent */
3439 while (inindent(1))
3440 if (inc_cursor() != 0)
3441 break;
3442 if (gchar_cursor() == what) /* cursor on '(' or '{' */
3443 ++curwin->w_cursor.col;
3444 }
3445#ifdef FEAT_VISUAL
3446 else if (lt(VIsual, curwin->w_cursor))
3447 {
3448 old_start = VIsual;
3449 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3450 }
3451 else
3452 old_end = VIsual;
3453#endif
3454
3455 /*
3456 * Search backwards for unclosed '(', '{', etc..
3457 * Put this position in start_pos.
3458 * Ignory quotes here.
3459 */
3460 save_cpo = p_cpo;
3461 p_cpo = (char_u *)"%";
3462 while (count-- > 0)
3463 {
3464 if ((pos = findmatch(NULL, what)) == NULL)
3465 break;
3466 curwin->w_cursor = *pos;
3467 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3468 }
3469 p_cpo = save_cpo;
3470
3471 /*
3472 * Search for matching ')', '}', etc.
3473 * Put this position in curwin->w_cursor.
3474 */
3475 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3476 {
3477 curwin->w_cursor = old_pos;
3478 return FAIL;
3479 }
3480 curwin->w_cursor = *end_pos;
3481
3482 /*
3483 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3484 * If the ending '}' is only preceded by indent, skip that indent.
3485 * But only if the resulting area is not smaller than what we started with.
3486 */
3487 while (!include)
3488 {
3489 incl(&start_pos);
3490 sol = (curwin->w_cursor.col == 0);
3491 decl(&curwin->w_cursor);
3492 if (what == '{')
3493 while (inindent(1))
3494 {
3495 sol = TRUE;
3496 if (decl(&curwin->w_cursor) != 0)
3497 break;
3498 }
3499#ifdef FEAT_VISUAL
3500 /*
3501 * In Visual mode, when the resulting area is not bigger than what we
3502 * started with, extend it to the next block, and then exclude again.
3503 */
3504 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3505 && VIsual_active)
3506 {
3507 curwin->w_cursor = old_start;
3508 decl(&curwin->w_cursor);
3509 if ((pos = findmatch(NULL, what)) == NULL)
3510 {
3511 curwin->w_cursor = old_pos;
3512 return FAIL;
3513 }
3514 start_pos = *pos;
3515 curwin->w_cursor = *pos;
3516 if ((end_pos = findmatch(NULL, other)) == NULL)
3517 {
3518 curwin->w_cursor = old_pos;
3519 return FAIL;
3520 }
3521 curwin->w_cursor = *end_pos;
3522 }
3523 else
3524#endif
3525 break;
3526 }
3527
3528#ifdef FEAT_VISUAL
3529 if (VIsual_active)
3530 {
3531 if (*p_sel == 'e')
3532 ++curwin->w_cursor.col;
3533 if (sol)
3534 inc(&curwin->w_cursor); /* include the line break */
3535 VIsual = start_pos;
3536 VIsual_mode = 'v';
3537 redraw_curbuf_later(INVERTED); /* update the inversion */
3538 showmode();
3539 }
3540 else
3541#endif
3542 {
3543 oap->start = start_pos;
3544 oap->motion_type = MCHAR;
3545 if (sol)
3546 {
3547 incl(&curwin->w_cursor);
3548 oap->inclusive = FALSE;
3549 }
3550 else
3551 oap->inclusive = TRUE;
3552 }
3553
3554 return OK;
3555}
3556
3557 int
3558current_par(oap, count, include, type)
3559 oparg_T *oap;
3560 long count;
3561 int include; /* TRUE == include white space */
3562 int type; /* 'p' for paragraph, 'S' for section */
3563{
3564 linenr_T start_lnum;
3565 linenr_T end_lnum;
3566 int white_in_front;
3567 int dir;
3568 int start_is_white;
3569 int prev_start_is_white;
3570 int retval = OK;
3571 int do_white = FALSE;
3572 int t;
3573 int i;
3574
3575 if (type == 'S') /* not implemented yet */
3576 return FAIL;
3577
3578 start_lnum = curwin->w_cursor.lnum;
3579
3580#ifdef FEAT_VISUAL
3581 /*
3582 * When visual area is more than one line: extend it.
3583 */
3584 if (VIsual_active && start_lnum != VIsual.lnum)
3585 {
3586extend:
3587 if (start_lnum < VIsual.lnum)
3588 dir = BACKWARD;
3589 else
3590 dir = FORWARD;
3591 for (i = count; --i >= 0; )
3592 {
3593 if (start_lnum ==
3594 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3595 {
3596 retval = FAIL;
3597 break;
3598 }
3599
3600 prev_start_is_white = -1;
3601 for (t = 0; t < 2; ++t)
3602 {
3603 start_lnum += dir;
3604 start_is_white = linewhite(start_lnum);
3605 if (prev_start_is_white == start_is_white)
3606 {
3607 start_lnum -= dir;
3608 break;
3609 }
3610 for (;;)
3611 {
3612 if (start_lnum == (dir == BACKWARD
3613 ? 1 : curbuf->b_ml.ml_line_count))
3614 break;
3615 if (start_is_white != linewhite(start_lnum + dir)
3616 || (!start_is_white
3617 && startPS(start_lnum + (dir > 0
3618 ? 1 : 0), 0, 0)))
3619 break;
3620 start_lnum += dir;
3621 }
3622 if (!include)
3623 break;
3624 if (start_lnum == (dir == BACKWARD
3625 ? 1 : curbuf->b_ml.ml_line_count))
3626 break;
3627 prev_start_is_white = start_is_white;
3628 }
3629 }
3630 curwin->w_cursor.lnum = start_lnum;
3631 curwin->w_cursor.col = 0;
3632 return retval;
3633 }
3634#endif
3635
3636 /*
3637 * First move back to the start_lnum of the paragraph or white lines
3638 */
3639 white_in_front = linewhite(start_lnum);
3640 while (start_lnum > 1)
3641 {
3642 if (white_in_front) /* stop at first white line */
3643 {
3644 if (!linewhite(start_lnum - 1))
3645 break;
3646 }
3647 else /* stop at first non-white line of start of paragraph */
3648 {
3649 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
3650 break;
3651 }
3652 --start_lnum;
3653 }
3654
3655 /*
3656 * Move past the end of any white lines.
3657 */
3658 end_lnum = start_lnum;
3659 while (linewhite(end_lnum) && end_lnum < curbuf->b_ml.ml_line_count)
3660 ++end_lnum;
3661
3662 --end_lnum;
3663 i = count;
3664 if (!include && white_in_front)
3665 --i;
3666 while (i--)
3667 {
3668 if (end_lnum == curbuf->b_ml.ml_line_count)
3669 return FAIL;
3670
3671 if (!include)
3672 do_white = linewhite(end_lnum + 1);
3673
3674 if (include || !do_white)
3675 {
3676 ++end_lnum;
3677 /*
3678 * skip to end of paragraph
3679 */
3680 while (end_lnum < curbuf->b_ml.ml_line_count
3681 && !linewhite(end_lnum + 1)
3682 && !startPS(end_lnum + 1, 0, 0))
3683 ++end_lnum;
3684 }
3685
3686 if (i == 0 && white_in_front && include)
3687 break;
3688
3689 /*
3690 * skip to end of white lines after paragraph
3691 */
3692 if (include || do_white)
3693 while (end_lnum < curbuf->b_ml.ml_line_count
3694 && linewhite(end_lnum + 1))
3695 ++end_lnum;
3696 }
3697
3698 /*
3699 * If there are no empty lines at the end, try to find some empty lines at
3700 * the start (unless that has been done already).
3701 */
3702 if (!white_in_front && !linewhite(end_lnum) && include)
3703 while (start_lnum > 1 && linewhite(start_lnum - 1))
3704 --start_lnum;
3705
3706#ifdef FEAT_VISUAL
3707 if (VIsual_active)
3708 {
3709 /* Problem: when doing "Vipipip" nothing happens in a single white
3710 * line, we get stuck there. Trap this here. */
3711 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
3712 goto extend;
3713 VIsual.lnum = start_lnum;
3714 VIsual_mode = 'V';
3715 redraw_curbuf_later(INVERTED); /* update the inversion */
3716 showmode();
3717 }
3718 else
3719#endif
3720 {
3721 oap->start.lnum = start_lnum;
3722 oap->start.col = 0;
3723 oap->motion_type = MLINE;
3724 }
3725 curwin->w_cursor.lnum = end_lnum;
3726 curwin->w_cursor.col = 0;
3727
3728 return OK;
3729}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003730
3731static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
3732static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
3733
3734/*
3735 * Search quote char from string line[col].
3736 * Quote character escaped by one of the characters in "escape" is not counted
3737 * as a quote.
3738 * Returns column number of "quotechar" or -1 when not found.
3739 */
3740 static int
3741find_next_quote(line, col, quotechar, escape)
3742 char_u *line;
3743 int col;
3744 int quotechar;
3745 char_u *escape; /* escape characters, can be NULL */
3746{
3747 int c;
3748
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003749 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003750 {
3751 c = line[col];
3752 if (c == NUL)
3753 return -1;
3754 else if (escape != NULL && vim_strchr(escape, c))
3755 ++col;
3756 else if (c == quotechar)
3757 break;
3758#ifdef FEAT_MBYTE
3759 if (has_mbyte)
3760 col += (*mb_ptr2len_check)(line + col);
3761 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00003762#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003763 ++col;
3764 }
3765 return col;
3766}
3767
3768/*
3769 * Search backwards in "line" from column "col_start" to find "quotechar".
3770 * Quote character escaped by one of the characters in "escape" is not counted
3771 * as a quote.
3772 * Return the found column or zero.
3773 */
3774 static int
3775find_prev_quote(line, col_start, quotechar, escape)
3776 char_u *line;
3777 int col_start;
3778 int quotechar;
3779 char_u *escape; /* escape characters, can be NULL */
3780{
3781 int n;
3782
3783 while (col_start > 0)
3784 {
3785 --col_start;
3786#ifdef FEAT_MBYTE
3787 col_start -= (*mb_head_off)(line, line + col_start);
3788#endif
3789 n = 0;
3790 if (escape != NULL)
3791 while (col_start - n > 0 && vim_strchr(escape,
3792 line[col_start - n - 1]) != NULL)
3793 ++n;
3794 if (n & 1)
3795 col_start -= n; /* uneven number of escape chars, skip it */
3796 else if (line[col_start] == quotechar)
3797 break;
3798 }
3799 return col_start;
3800}
3801
3802/*
3803 * Find quote under the cursor, cursor at end.
3804 * Returns TRUE if found, else FALSE.
3805 */
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003806/*ARGSUSED*/
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003807 int
3808current_quote(oap, count, include, quotechar)
3809 oparg_T *oap;
Bram Moolenaar5eb86f92004-07-26 12:53:41 +00003810 long count; /* not used */
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003811 int include; /* TRUE == include quote char */
3812 int quotechar; /* Quote character */
3813{
3814 char_u *line = ml_get_curline();
3815 int col_end;
3816 int col_start = curwin->w_cursor.col;
3817 int inclusive = FALSE;
3818#ifdef FEAT_VISUAL
3819 int vis_empty = TRUE; /* Visual selection <= 1 char */
3820 int vis_bef_curs = FALSE; /* Visual starts before cursor */
3821
3822 /* Correct cursor when 'selection' is exclusive */
3823 if (VIsual_active)
3824 {
3825 if (*p_sel == 'e' && vis_bef_curs)
3826 dec_cursor();
3827 vis_empty = equalpos(VIsual, curwin->w_cursor);
3828 vis_bef_curs = lt(VIsual, curwin->w_cursor);
3829 }
3830 if (!vis_empty && line[col_start] == quotechar)
3831 {
3832 /* Already selecting something and on a quote character. Find the
3833 * next quoted string. */
3834 if (vis_bef_curs)
3835 {
3836 /* Assume we are on a closing quote: move to after the next
3837 * opening quote. */
3838 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
3839 if (col_start < 0)
3840 return FALSE;
3841 col_end = find_next_quote(line, col_start + 1, quotechar,
3842 curbuf->b_p_qe);
3843 if (col_end < 0)
3844 {
3845 /* We were on a starting quote perhaps? */
3846 col_end = col_start;
3847 col_start = curwin->w_cursor.col;
3848 }
3849 }
3850 else
3851 {
3852 col_end = find_prev_quote(line, col_start, quotechar, NULL);
3853 if (line[col_end] != quotechar)
3854 return FALSE;
3855 col_start = find_prev_quote(line, col_end, quotechar,
3856 curbuf->b_p_qe);
3857 if (line[col_start] != quotechar)
3858 {
3859 /* We were on an ending quote perhaps? */
3860 col_start = col_end;
3861 col_end = curwin->w_cursor.col;
3862 }
3863 }
3864 }
3865 else
3866#endif
3867
3868 if (line[col_start] == quotechar
3869#ifdef FEAT_VISUAL
3870 || !vis_empty
3871#endif
3872 )
3873 {
3874 int first_col = col_start;
3875
3876#ifdef FEAT_VISUAL
3877 if (!vis_empty)
3878 {
3879 if (vis_bef_curs)
3880 first_col = find_next_quote(line, col_start, quotechar, NULL);
3881 else
3882 first_col = find_prev_quote(line, col_start, quotechar, NULL);
3883 }
3884#endif
3885 /* The cursor is on a quote, we don't know if it's the opening or
3886 * closing quote. Search from the start of the line to find out.
3887 * Also do this when there is a Visual area, a' may leave the cursor
3888 * in between two strings. */
3889 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00003890 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00003891 {
3892 /* Find open quote character. */
3893 col_start = find_next_quote(line, col_start, quotechar, NULL);
3894 if (col_start < 0 || col_start > first_col)
3895 return FALSE;
3896 /* Find close quote character. */
3897 col_end = find_next_quote(line, col_start + 1, quotechar,
3898 curbuf->b_p_qe);
3899 if (col_end < 0)
3900 return FALSE;
3901 /* If is cursor between start and end quote character, it is
3902 * target text object. */
3903 if (col_start <= first_col && first_col <= col_end)
3904 break;
3905 col_start = col_end + 1;
3906 }
3907 }
3908 else
3909 {
3910 /* Search backward for a starting quote. */
3911 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
3912 if (line[col_start] != quotechar)
3913 {
3914 /* No quote before the cursor, look after the cursor. */
3915 col_start = find_next_quote(line, col_start, quotechar, NULL);
3916 if (col_start < 0)
3917 return FALSE;
3918 }
3919
3920 /* Find close quote character. */
3921 col_end = find_next_quote(line, col_start + 1, quotechar,
3922 curbuf->b_p_qe);
3923 if (col_end < 0)
3924 return FALSE;
3925 }
3926
3927 /* When "include" is TRUE, include spaces after closing quote or before
3928 * the starting quote. */
3929 if (include)
3930 {
3931 if (vim_iswhite(line[col_end + 1]))
3932 while (vim_iswhite(line[col_end + 1]))
3933 ++col_end;
3934 else
3935 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
3936 --col_start;
3937 }
3938
3939 /* Set start position */
3940 if (!include)
3941 ++col_start;
3942 curwin->w_cursor.col = col_start;
3943#ifdef FEAT_VISUAL
3944 if (VIsual_active)
3945 {
3946 if (vis_empty)
3947 {
3948 VIsual = curwin->w_cursor;
3949 redraw_curbuf_later(INVERTED);
3950 }
3951 }
3952 else
3953#endif
3954 {
3955 oap->start = curwin->w_cursor;
3956 oap->motion_type = MCHAR;
3957 }
3958
3959 /* Set end position. */
3960 curwin->w_cursor.col = col_end;
3961 if (include && inc_cursor() == 2)
3962 inclusive = TRUE;
3963#ifdef FEAT_VISUAL
3964 if (VIsual_active)
3965 {
3966 if (vis_empty || vis_bef_curs)
3967 {
3968 /* decrement cursor when 'selection' is not exclusive */
3969 if (*p_sel != 'e')
3970 dec_cursor();
3971 }
3972 else
3973 {
3974 /* Cursor is at start of Visual area. */
3975 curwin->w_cursor.col = col_start;
3976 }
3977 if (VIsual_mode == 'V')
3978 {
3979 VIsual_mode = 'v';
3980 redraw_cmdline = TRUE; /* show mode later */
3981 }
3982 }
3983 else
3984#endif
3985 {
3986 /* Set inclusive and other oap's flags. */
3987 oap->inclusive = inclusive;
3988 }
3989
3990 return OK;
3991}
3992
3993#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003994
3995#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
3996 || defined(PROTO)
3997/*
3998 * return TRUE if line 'lnum' is empty or has white chars only.
3999 */
4000 int
4001linewhite(lnum)
4002 linenr_T lnum;
4003{
4004 char_u *p;
4005
4006 p = skipwhite(ml_get(lnum));
4007 return (*p == NUL);
4008}
4009#endif
4010
4011#if defined(FEAT_FIND_ID) || defined(PROTO)
4012/*
4013 * Find identifiers or defines in included files.
4014 * if p_ic && (continue_status & CONT_SOL) then ptr must be in lowercase.
4015 */
4016/*ARGSUSED*/
4017 void
4018find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4019 type, count, action, start_lnum, end_lnum)
4020 char_u *ptr; /* pointer to search pattern */
4021 int dir; /* direction of expansion */
4022 int len; /* length of search pattern */
4023 int whole; /* match whole words only */
4024 int skip_comments; /* don't match inside comments */
4025 int type; /* Type of search; are we looking for a type?
4026 a macro? */
4027 long count;
4028 int action; /* What to do when we find it */
4029 linenr_T start_lnum; /* first line to start searching */
4030 linenr_T end_lnum; /* last line for searching */
4031{
4032 SearchedFile *files; /* Stack of included files */
4033 SearchedFile *bigger; /* When we need more space */
4034 int max_path_depth = 50;
4035 long match_count = 1;
4036
4037 char_u *pat;
4038 char_u *new_fname;
4039 char_u *curr_fname = curbuf->b_fname;
4040 char_u *prev_fname = NULL;
4041 linenr_T lnum;
4042 int depth;
4043 int depth_displayed; /* For type==CHECK_PATH */
4044 int old_files;
4045 int already_searched;
4046 char_u *file_line;
4047 char_u *line;
4048 char_u *p;
4049 char_u save_char;
4050 int define_matched;
4051 regmatch_T regmatch;
4052 regmatch_T incl_regmatch;
4053 regmatch_T def_regmatch;
4054 int matched = FALSE;
4055 int did_show = FALSE;
4056 int found = FALSE;
4057 int i;
4058 char_u *already = NULL;
4059 char_u *startp = NULL;
4060#ifdef RISCOS
4061 int previous_munging = __riscosify_control;
4062#endif
4063#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4064 win_T *curwin_save = NULL;
4065#endif
4066
4067 regmatch.regprog = NULL;
4068 incl_regmatch.regprog = NULL;
4069 def_regmatch.regprog = NULL;
4070
4071 file_line = alloc(LSIZE);
4072 if (file_line == NULL)
4073 return;
4074
4075#ifdef RISCOS
4076 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004077 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004078#endif
4079
4080 if (type != CHECK_PATH && type != FIND_DEFINE
4081#ifdef FEAT_INS_EXPAND
4082 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4083 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
4084 && !(continue_status & CONT_SOL)
4085#endif
4086 )
4087 {
4088 pat = alloc(len + 5);
4089 if (pat == NULL)
4090 goto fpip_end;
4091 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4092 /* ignore case according to p_ic, p_scs and pat */
4093 regmatch.rm_ic = ignorecase(pat);
4094 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4095 vim_free(pat);
4096 if (regmatch.regprog == NULL)
4097 goto fpip_end;
4098 }
4099 if (*curbuf->b_p_inc != NUL || *p_inc != NUL)
4100 {
4101 incl_regmatch.regprog = vim_regcomp(*curbuf->b_p_inc == NUL
4102 ? p_inc : curbuf->b_p_inc, p_magic ? RE_MAGIC : 0);
4103 if (incl_regmatch.regprog == NULL)
4104 goto fpip_end;
4105 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4106 }
4107 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4108 {
4109 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4110 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4111 if (def_regmatch.regprog == NULL)
4112 goto fpip_end;
4113 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4114 }
4115 files = (SearchedFile *)lalloc_clear((long_u)
4116 (max_path_depth * sizeof(SearchedFile)), TRUE);
4117 if (files == NULL)
4118 goto fpip_end;
4119 old_files = max_path_depth;
4120 depth = depth_displayed = -1;
4121
4122 lnum = start_lnum;
4123 if (end_lnum > curbuf->b_ml.ml_line_count)
4124 end_lnum = curbuf->b_ml.ml_line_count;
4125 if (lnum > end_lnum) /* do at least one line */
4126 lnum = end_lnum;
4127 line = ml_get(lnum);
4128
4129 for (;;)
4130 {
4131 if (incl_regmatch.regprog != NULL
4132 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4133 {
4134 new_fname = file_name_in_line(incl_regmatch.endp[0],
4135 0, FNAME_EXP|FNAME_INCL|FNAME_REL, 1L,
4136 curr_fname == curbuf->b_fname
4137 ? curbuf->b_ffname : curr_fname);
4138 already_searched = FALSE;
4139 if (new_fname != NULL)
4140 {
4141 /* Check whether we have already searched in this file */
4142 for (i = 0;; i++)
4143 {
4144 if (i == depth + 1)
4145 i = old_files;
4146 if (i == max_path_depth)
4147 break;
4148 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4149 {
4150 if (type != CHECK_PATH &&
4151 action == ACTION_SHOW_ALL && files[i].matched)
4152 {
4153 msg_putchar('\n'); /* cursor below last one */
4154 if (!got_int) /* don't display if 'q'
4155 typed at "--more--"
4156 mesage */
4157 {
4158 msg_home_replace_hl(new_fname);
4159 MSG_PUTS(_(" (includes previously listed match)"));
4160 prev_fname = NULL;
4161 }
4162 }
4163 vim_free(new_fname);
4164 new_fname = NULL;
4165 already_searched = TRUE;
4166 break;
4167 }
4168 }
4169 }
4170
4171 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4172 || (new_fname == NULL && !already_searched)))
4173 {
4174 if (did_show)
4175 msg_putchar('\n'); /* cursor below last one */
4176 else
4177 {
4178 gotocmdline(TRUE); /* cursor at status line */
4179 MSG_PUTS_TITLE(_("--- Included files "));
4180 if (action != ACTION_SHOW_ALL)
4181 MSG_PUTS_TITLE(_("not found "));
4182 MSG_PUTS_TITLE(_("in path ---\n"));
4183 }
4184 did_show = TRUE;
4185 while (depth_displayed < depth && !got_int)
4186 {
4187 ++depth_displayed;
4188 for (i = 0; i < depth_displayed; i++)
4189 MSG_PUTS(" ");
4190 msg_home_replace(files[depth_displayed].name);
4191 MSG_PUTS(" -->\n");
4192 }
4193 if (!got_int) /* don't display if 'q' typed
4194 for "--more--" message */
4195 {
4196 for (i = 0; i <= depth_displayed; i++)
4197 MSG_PUTS(" ");
4198 if (new_fname != NULL)
4199 {
4200 /* using "new_fname" is more reliable, e.g., when
4201 * 'includeexpr' is set. */
4202 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4203 }
4204 else
4205 {
4206 /*
4207 * Isolate the file name.
4208 * Include the surrounding "" or <> if present.
4209 */
4210 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4211 ;
4212 for (i = 0; vim_isfilec(p[i]); i++)
4213 ;
4214 if (i == 0)
4215 {
4216 /* Nothing found, use the rest of the line. */
4217 p = incl_regmatch.endp[0];
4218 i = STRLEN(p);
4219 }
4220 else
4221 {
4222 if (p[-1] == '"' || p[-1] == '<')
4223 {
4224 --p;
4225 ++i;
4226 }
4227 if (p[i] == '"' || p[i] == '>')
4228 ++i;
4229 }
4230 save_char = p[i];
4231 p[i] = NUL;
4232 msg_outtrans_attr(p, hl_attr(HLF_D));
4233 p[i] = save_char;
4234 }
4235
4236 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4237 {
4238 if (already_searched)
4239 MSG_PUTS(_(" (Already listed)"));
4240 else
4241 MSG_PUTS(_(" NOT FOUND"));
4242 }
4243 }
4244 out_flush(); /* output each line directly */
4245 }
4246
4247 if (new_fname != NULL)
4248 {
4249 /* Push the new file onto the file stack */
4250 if (depth + 1 == old_files)
4251 {
4252 bigger = (SearchedFile *)lalloc((long_u)(
4253 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4254 if (bigger != NULL)
4255 {
4256 for (i = 0; i <= depth; i++)
4257 bigger[i] = files[i];
4258 for (i = depth + 1; i < old_files + max_path_depth; i++)
4259 {
4260 bigger[i].fp = NULL;
4261 bigger[i].name = NULL;
4262 bigger[i].lnum = 0;
4263 bigger[i].matched = FALSE;
4264 }
4265 for (i = old_files; i < max_path_depth; i++)
4266 bigger[i + max_path_depth] = files[i];
4267 old_files += max_path_depth;
4268 max_path_depth *= 2;
4269 vim_free(files);
4270 files = bigger;
4271 }
4272 }
4273 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4274 == NULL)
4275 vim_free(new_fname);
4276 else
4277 {
4278 if (++depth == old_files)
4279 {
4280 /*
4281 * lalloc() for 'bigger' must have failed above. We
4282 * will forget one of our already visited files now.
4283 */
4284 vim_free(files[old_files].name);
4285 ++old_files;
4286 }
4287 files[depth].name = curr_fname = new_fname;
4288 files[depth].lnum = 0;
4289 files[depth].matched = FALSE;
4290#ifdef FEAT_INS_EXPAND
4291 if (action == ACTION_EXPAND)
4292 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004293 vim_snprintf((char*)IObuff, IOSIZE,
4294 _("Scanning included file: %s"),
4295 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004296 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4297 }
4298#endif
4299 }
4300 }
4301 }
4302 else
4303 {
4304 /*
4305 * Check if the line is a define (type == FIND_DEFINE)
4306 */
4307 p = line;
4308search_line:
4309 define_matched = FALSE;
4310 if (def_regmatch.regprog != NULL
4311 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4312 {
4313 /*
4314 * Pattern must be first identifier after 'define', so skip
4315 * to that position before checking for match of pattern. Also
4316 * don't let it match beyond the end of this identifier.
4317 */
4318 p = def_regmatch.endp[0];
4319 while (*p && !vim_iswordc(*p))
4320 p++;
4321 define_matched = TRUE;
4322 }
4323
4324 /*
4325 * Look for a match. Don't do this if we are looking for a
4326 * define and this line didn't match define_prog above.
4327 */
4328 if (def_regmatch.regprog == NULL || define_matched)
4329 {
4330 if (define_matched
4331#ifdef FEAT_INS_EXPAND
4332 || (continue_status & CONT_SOL)
4333#endif
4334 )
4335 {
4336 /* compare the first "len" chars from "ptr" */
4337 startp = skipwhite(p);
4338 if (p_ic)
4339 matched = !MB_STRNICMP(startp, ptr, len);
4340 else
4341 matched = !STRNCMP(startp, ptr, len);
4342 if (matched && define_matched && whole
4343 && vim_iswordc(startp[len]))
4344 matched = FALSE;
4345 }
4346 else if (regmatch.regprog != NULL
4347 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4348 {
4349 matched = TRUE;
4350 startp = regmatch.startp[0];
4351 /*
4352 * Check if the line is not a comment line (unless we are
4353 * looking for a define). A line starting with "# define"
4354 * is not considered to be a comment line.
4355 */
4356 if (!define_matched && skip_comments)
4357 {
4358#ifdef FEAT_COMMENTS
4359 if ((*line != '#' ||
4360 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4361 && get_leader_len(line, NULL, FALSE))
4362 matched = FALSE;
4363
4364 /*
4365 * Also check for a "/ *" or "/ /" before the match.
4366 * Skips lines like "int backwards; / * normal index
4367 * * /" when looking for "normal".
4368 * Note: Doesn't skip "/ *" in comments.
4369 */
4370 p = skipwhite(line);
4371 if (matched
4372 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4373#endif
4374 for (p = line; *p && p < startp; ++p)
4375 {
4376 if (matched
4377 && p[0] == '/'
4378 && (p[1] == '*' || p[1] == '/'))
4379 {
4380 matched = FALSE;
4381 /* After "//" all text is comment */
4382 if (p[1] == '/')
4383 break;
4384 ++p;
4385 }
4386 else if (!matched && p[0] == '*' && p[1] == '/')
4387 {
4388 /* Can find match after "* /". */
4389 matched = TRUE;
4390 ++p;
4391 }
4392 }
4393 }
4394 }
4395 }
4396 }
4397 if (matched)
4398 {
4399#ifdef FEAT_INS_EXPAND
4400 if (action == ACTION_EXPAND)
4401 {
4402 int reuse = 0;
4403 int add_r;
4404 char_u *aux;
4405
4406 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4407 break;
4408 found = TRUE;
4409 aux = p = startp;
4410 if (continue_status & CONT_ADDING)
4411 {
4412 p += completion_length;
4413 if (vim_iswordp(p))
4414 goto exit_matched;
4415 p = find_word_start(p);
4416 }
4417 p = find_word_end(p);
4418 i = (int)(p - aux);
4419
4420 if ((continue_status & CONT_ADDING) && i == completion_length)
4421 {
4422 /* get the next line */
4423 /* IOSIZE > completion_length, so the STRNCPY works */
4424 STRNCPY(IObuff, aux, i);
4425 if (!( depth < 0
4426 && lnum < end_lnum
4427 && (line = ml_get(++lnum)) != NULL)
4428 && !( depth >= 0
4429 && !vim_fgets(line = file_line,
4430 LSIZE, files[depth].fp)))
4431 goto exit_matched;
4432
4433 /* we read a line, set "already" to check this "line" later
4434 * if depth >= 0 we'll increase files[depth].lnum far
4435 * bellow -- Acevedo */
4436 already = aux = p = skipwhite(line);
4437 p = find_word_start(p);
4438 p = find_word_end(p);
4439 if (p > aux)
4440 {
4441 if (*aux != ')' && IObuff[i-1] != TAB)
4442 {
4443 if (IObuff[i-1] != ' ')
4444 IObuff[i++] = ' ';
4445 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4446 if (p_js
4447 && (IObuff[i-2] == '.'
4448 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4449 && (IObuff[i-2] == '?'
4450 || IObuff[i-2] == '!'))))
4451 IObuff[i++] = ' ';
4452 }
4453 /* copy as much as posible of the new word */
4454 if (p - aux >= IOSIZE - i)
4455 p = aux + IOSIZE - i - 1;
4456 STRNCPY(IObuff + i, aux, p - aux);
4457 i += (int)(p - aux);
4458 reuse |= CONT_S_IPOS;
4459 }
4460 IObuff[i] = NUL;
4461 aux = IObuff;
4462
4463 if (i == completion_length)
4464 goto exit_matched;
4465 }
4466
4467 add_r = ins_compl_add_infercase(aux, i,
4468 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4469 dir, reuse);
4470 if (add_r == OK)
4471 /* if dir was BACKWARD then honor it just once */
4472 dir = FORWARD;
4473 else if (add_r == RET_ERROR)
4474 break;
4475 }
4476 else
4477#endif
4478 if (action == ACTION_SHOW_ALL)
4479 {
4480 found = TRUE;
4481 if (!did_show)
4482 gotocmdline(TRUE); /* cursor at status line */
4483 if (curr_fname != prev_fname)
4484 {
4485 if (did_show)
4486 msg_putchar('\n'); /* cursor below last one */
4487 if (!got_int) /* don't display if 'q' typed
4488 at "--more--" mesage */
4489 msg_home_replace_hl(curr_fname);
4490 prev_fname = curr_fname;
4491 }
4492 did_show = TRUE;
4493 if (!got_int)
4494 show_pat_in_path(line, type, TRUE, action,
4495 (depth == -1) ? NULL : files[depth].fp,
4496 (depth == -1) ? &lnum : &files[depth].lnum,
4497 match_count++);
4498
4499 /* Set matched flag for this file and all the ones that
4500 * include it */
4501 for (i = 0; i <= depth; ++i)
4502 files[i].matched = TRUE;
4503 }
4504 else if (--count <= 0)
4505 {
4506 found = TRUE;
4507 if (depth == -1 && lnum == curwin->w_cursor.lnum
4508#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4509 && g_do_tagpreview == 0
4510#endif
4511 )
4512 EMSG(_("E387: Match is on current line"));
4513 else if (action == ACTION_SHOW)
4514 {
4515 show_pat_in_path(line, type, did_show, action,
4516 (depth == -1) ? NULL : files[depth].fp,
4517 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4518 did_show = TRUE;
4519 }
4520 else
4521 {
4522#ifdef FEAT_GUI
4523 need_mouse_correct = TRUE;
4524#endif
4525#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4526 /* ":psearch" uses the preview window */
4527 if (g_do_tagpreview != 0)
4528 {
4529 curwin_save = curwin;
4530 prepare_tagpreview();
4531 }
4532#endif
4533 if (action == ACTION_SPLIT)
4534 {
4535#ifdef FEAT_WINDOWS
4536 if (win_split(0, 0) == FAIL)
4537#endif
4538 break;
4539#ifdef FEAT_SCROLLBIND
4540 curwin->w_p_scb = FALSE;
4541#endif
4542 }
4543 if (depth == -1)
4544 {
4545 /* match in current file */
4546#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4547 if (g_do_tagpreview != 0)
4548 {
4549 if (getfile(0, curwin_save->w_buffer->b_fname,
4550 NULL, TRUE, lnum, FALSE) > 0)
4551 break; /* failed to jump to file */
4552 }
4553 else
4554#endif
4555 setpcmark();
4556 curwin->w_cursor.lnum = lnum;
4557 }
4558 else
4559 {
4560 if (getfile(0, files[depth].name, NULL, TRUE,
4561 files[depth].lnum, FALSE) > 0)
4562 break; /* failed to jump to file */
4563 /* autocommands may have changed the lnum, we don't
4564 * want that here */
4565 curwin->w_cursor.lnum = files[depth].lnum;
4566 }
4567 }
4568 if (action != ACTION_SHOW)
4569 {
4570 curwin->w_cursor.col = (colnr_T) (startp - line);
4571 curwin->w_set_curswant = TRUE;
4572 }
4573
4574#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4575 if (g_do_tagpreview != 0
4576 && curwin != curwin_save && win_valid(curwin_save))
4577 {
4578 /* Return cursor to where we were */
4579 validate_cursor();
4580 redraw_later(VALID);
4581 win_enter(curwin_save, TRUE);
4582 }
4583#endif
4584 break;
4585 }
4586#ifdef FEAT_INS_EXPAND
4587exit_matched:
4588#endif
4589 matched = FALSE;
4590 /* look for other matches in the rest of the line if we
4591 * are not at the end of it already */
4592 if (def_regmatch.regprog == NULL
4593#ifdef FEAT_INS_EXPAND
4594 && action == ACTION_EXPAND
4595 && !(continue_status & CONT_SOL)
4596#endif
4597 && *(p = startp + 1))
4598 goto search_line;
4599 }
4600 line_breakcheck();
4601#ifdef FEAT_INS_EXPAND
4602 if (action == ACTION_EXPAND)
4603 ins_compl_check_keys();
4604 if (got_int || completion_interrupted)
4605#else
4606 if (got_int)
4607#endif
4608 break;
4609
4610 /*
4611 * Read the next line. When reading an included file and encountering
4612 * end-of-file, close the file and continue in the file that included
4613 * it.
4614 */
4615 while (depth >= 0 && !already
4616 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4617 {
4618 fclose(files[depth].fp);
4619 --old_files;
4620 files[old_files].name = files[depth].name;
4621 files[old_files].matched = files[depth].matched;
4622 --depth;
4623 curr_fname = (depth == -1) ? curbuf->b_fname
4624 : files[depth].name;
4625 if (depth < depth_displayed)
4626 depth_displayed = depth;
4627 }
4628 if (depth >= 0) /* we could read the line */
4629 files[depth].lnum++;
4630 else if (!already)
4631 {
4632 if (++lnum > end_lnum)
4633 break;
4634 line = ml_get(lnum);
4635 }
4636 already = NULL;
4637 }
4638 /* End of big for (;;) loop. */
4639
4640 /* Close any files that are still open. */
4641 for (i = 0; i <= depth; i++)
4642 {
4643 fclose(files[i].fp);
4644 vim_free(files[i].name);
4645 }
4646 for (i = old_files; i < max_path_depth; i++)
4647 vim_free(files[i].name);
4648 vim_free(files);
4649
4650 if (type == CHECK_PATH)
4651 {
4652 if (!did_show)
4653 {
4654 if (action != ACTION_SHOW_ALL)
4655 MSG(_("All included files were found"));
4656 else
4657 MSG(_("No included files"));
4658 }
4659 }
4660 else if (!found
4661#ifdef FEAT_INS_EXPAND
4662 && action != ACTION_EXPAND
4663#endif
4664 )
4665 {
4666#ifdef FEAT_INS_EXPAND
4667 if (got_int || completion_interrupted)
4668#else
4669 if (got_int)
4670#endif
4671 EMSG(_(e_interr));
4672 else if (type == FIND_DEFINE)
4673 EMSG(_("E388: Couldn't find definition"));
4674 else
4675 EMSG(_("E389: Couldn't find pattern"));
4676 }
4677 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
4678 msg_end();
4679
4680fpip_end:
4681 vim_free(file_line);
4682 vim_free(regmatch.regprog);
4683 vim_free(incl_regmatch.regprog);
4684 vim_free(def_regmatch.regprog);
4685
4686#ifdef RISCOS
4687 /* Restore previous file munging state. */
4688 __riscosify_control = previous_munging;
4689#endif
4690}
4691
4692 static void
4693show_pat_in_path(line, type, did_show, action, fp, lnum, count)
4694 char_u *line;
4695 int type;
4696 int did_show;
4697 int action;
4698 FILE *fp;
4699 linenr_T *lnum;
4700 long count;
4701{
4702 char_u *p;
4703
4704 if (did_show)
4705 msg_putchar('\n'); /* cursor below last one */
4706 else
4707 gotocmdline(TRUE); /* cursor at status line */
4708 if (got_int) /* 'q' typed at "--more--" message */
4709 return;
4710 for (;;)
4711 {
4712 p = line + STRLEN(line) - 1;
4713 if (fp != NULL)
4714 {
4715 /* We used fgets(), so get rid of newline at end */
4716 if (p >= line && *p == '\n')
4717 --p;
4718 if (p >= line && *p == '\r')
4719 --p;
4720 *(p + 1) = NUL;
4721 }
4722 if (action == ACTION_SHOW_ALL)
4723 {
4724 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
4725 msg_puts(IObuff);
4726 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
4727 /* Highlight line numbers */
4728 msg_puts_attr(IObuff, hl_attr(HLF_N));
4729 MSG_PUTS(" ");
4730 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00004731 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732 out_flush(); /* show one line at a time */
4733
4734 /* Definition continues until line that doesn't end with '\' */
4735 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
4736 break;
4737
4738 if (fp != NULL)
4739 {
4740 if (vim_fgets(line, LSIZE, fp)) /* end of file */
4741 break;
4742 ++*lnum;
4743 }
4744 else
4745 {
4746 if (++*lnum > curbuf->b_ml.ml_line_count)
4747 break;
4748 line = ml_get(*lnum);
4749 }
4750 msg_putchar('\n');
4751 }
4752}
4753#endif
4754
4755#ifdef FEAT_VIMINFO
4756 int
4757read_viminfo_search_pattern(virp, force)
4758 vir_T *virp;
4759 int force;
4760{
4761 char_u *lp;
4762 int idx = -1;
4763 int magic = FALSE;
4764 int no_scs = FALSE;
4765 int off_line = FALSE;
4766 int off_end = FALSE;
4767 long off = 0;
4768 int setlast = FALSE;
4769#ifdef FEAT_SEARCH_EXTRA
4770 static int hlsearch_on = FALSE;
4771#endif
4772 char_u *val;
4773
4774 /*
4775 * Old line types:
4776 * "/pat", "&pat": search/subst. pat
4777 * "~/pat", "~&pat": last used search/subst. pat
4778 * New line types:
4779 * "~h", "~H": hlsearch highlighting off/on
4780 * "~<magic><smartcase><line><end><off><last><which>pat"
4781 * <magic>: 'm' off, 'M' on
4782 * <smartcase>: 's' off, 'S' on
4783 * <line>: 'L' line offset, 'l' char offset
4784 * <end>: 'E' from end, 'e' from start
4785 * <off>: decimal, offset
4786 * <last>: '~' last used pattern
4787 * <which>: '/' search pat, '&' subst. pat
4788 */
4789 lp = virp->vir_line;
4790 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
4791 {
4792 if (lp[1] == 'M') /* magic on */
4793 magic = TRUE;
4794 if (lp[2] == 's')
4795 no_scs = TRUE;
4796 if (lp[3] == 'L')
4797 off_line = TRUE;
4798 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00004799 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004800 lp += 5;
4801 off = getdigits(&lp);
4802 }
4803 if (lp[0] == '~') /* use this pattern for last-used pattern */
4804 {
4805 setlast = TRUE;
4806 lp++;
4807 }
4808 if (lp[0] == '/')
4809 idx = RE_SEARCH;
4810 else if (lp[0] == '&')
4811 idx = RE_SUBST;
4812#ifdef FEAT_SEARCH_EXTRA
4813 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
4814 hlsearch_on = FALSE;
4815 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
4816 hlsearch_on = TRUE;
4817#endif
4818 if (idx >= 0)
4819 {
4820 if (force || spats[idx].pat == NULL)
4821 {
4822 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
4823 TRUE);
4824 if (val != NULL)
4825 {
4826 set_last_search_pat(val, idx, magic, setlast);
4827 vim_free(val);
4828 spats[idx].no_scs = no_scs;
4829 spats[idx].off.line = off_line;
4830 spats[idx].off.end = off_end;
4831 spats[idx].off.off = off;
4832#ifdef FEAT_SEARCH_EXTRA
4833 if (setlast)
4834 no_hlsearch = !hlsearch_on;
4835#endif
4836 }
4837 }
4838 }
4839 return viminfo_readline(virp);
4840}
4841
4842 void
4843write_viminfo_search_pattern(fp)
4844 FILE *fp;
4845{
4846 if (get_viminfo_parameter('/') != 0)
4847 {
4848#ifdef FEAT_SEARCH_EXTRA
4849 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
4850 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
4851#endif
4852 wvsp_one(fp, RE_SEARCH, "", '/');
4853 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
4854 }
4855}
4856
4857 static void
4858wvsp_one(fp, idx, s, sc)
4859 FILE *fp; /* file to write to */
4860 int idx; /* spats[] index */
4861 char *s; /* search pat */
4862 int sc; /* dir char */
4863{
4864 if (spats[idx].pat != NULL)
4865 {
4866 fprintf(fp, "\n# Last %sSearch Pattern:\n~", s);
4867 /* off.dir is not stored, it's reset to forward */
4868 fprintf(fp, "%c%c%c%c%ld%s%c",
4869 spats[idx].magic ? 'M' : 'm', /* magic */
4870 spats[idx].no_scs ? 's' : 'S', /* smartcase */
4871 spats[idx].off.line ? 'L' : 'l', /* line offset */
4872 spats[idx].off.end ? 'E' : 'e', /* offset from end */
4873 spats[idx].off.off, /* offset */
4874 last_idx == idx ? "~" : "", /* last used pat */
4875 sc);
4876 viminfo_writestring(fp, spats[idx].pat);
4877 }
4878}
4879#endif /* FEAT_VIMINFO */