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