Merge branch 'vim'
[MacVim.git] / src / search.c
bloba25816fda8c4b1afefc31c62341531d0e02779d6
1 /* vi:set ts=8 sts=4 sw=4:
3 * VIM - Vi IMproved by Bram Moolenaar
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
13 #include "vim.h"
15 static void save_re_pat __ARGS((int idx, char_u *pat, int magic));
16 #ifdef FEAT_EVAL
17 static int first_submatch __ARGS((regmmatch_T *rp));
18 #endif
19 static int check_prevcol __ARGS((char_u *linep, int col, int ch, int *prevcol));
20 static int inmacro __ARGS((char_u *, char_u *));
21 static int check_linecomment __ARGS((char_u *line));
22 static int cls __ARGS((void));
23 static int skip_chars __ARGS((int, int));
24 #ifdef FEAT_TEXTOBJ
25 static void back_in_line __ARGS((void));
26 static void find_first_blank __ARGS((pos_T *));
27 static void findsent_forward __ARGS((long count, int at_start_sent));
28 #endif
29 #ifdef FEAT_FIND_ID
30 static void show_pat_in_path __ARGS((char_u *, int,
31 int, int, FILE *, linenr_T *, long));
32 #endif
33 #ifdef FEAT_VIMINFO
34 static void wvsp_one __ARGS((FILE *fp, int idx, char *s, int sc));
35 #endif
38 * This file contains various searching-related routines. These fall into
39 * three groups:
40 * 1. string searches (for /, ?, n, and N)
41 * 2. character searches within a single line (for f, F, t, T, etc)
42 * 3. "other" kinds of searches like the '%' command, and 'word' searches.
46 * String searches
48 * The string search functions are divided into two levels:
49 * lowest: searchit(); uses an pos_T for starting position and found match.
50 * Highest: do_search(); uses curwin->w_cursor; calls searchit().
52 * The last search pattern is remembered for repeating the same search.
53 * This pattern is shared between the :g, :s, ? and / commands.
54 * This is in search_regcomp().
56 * The actual string matching is done using a heavily modified version of
57 * Henry Spencer's regular expression library. See regexp.c.
60 /* The offset for a search command is store in a soff struct */
61 /* Note: only spats[0].off is really used */
62 struct soffset
64 int dir; /* search direction */
65 int line; /* search has line offset */
66 int end; /* search set cursor at end */
67 long off; /* line or char offset */
70 /* A search pattern and its attributes are stored in a spat struct */
71 struct spat
73 char_u *pat; /* the pattern (in allocated memory) or NULL */
74 int magic; /* magicness of the pattern */
75 int no_scs; /* no smarcase for this pattern */
76 struct soffset off;
80 * Two search patterns are remembered: One for the :substitute command and
81 * one for other searches. last_idx points to the one that was used the last
82 * time.
84 static struct spat spats[2] =
86 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}}, /* last used search pat */
87 {NULL, TRUE, FALSE, {'/', 0, 0, 0L}} /* last used substitute pat */
90 static int last_idx = 0; /* index in spats[] for RE_LAST */
92 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
93 /* copy of spats[], for keeping the search patterns while executing autocmds */
94 static struct spat saved_spats[2];
95 static int saved_last_idx = 0;
96 # ifdef FEAT_SEARCH_EXTRA
97 static int saved_no_hlsearch = 0;
98 # endif
99 #endif
101 static char_u *mr_pattern = NULL; /* pattern used by search_regcomp() */
102 #ifdef FEAT_RIGHTLEFT
103 static int mr_pattern_alloced = FALSE; /* mr_pattern was allocated */
104 #endif
106 #ifdef FEAT_FIND_ID
108 * Type used by find_pattern_in_path() to remember which included files have
109 * been searched already.
111 typedef struct SearchedFile
113 FILE *fp; /* File pointer */
114 char_u *name; /* Full name of file */
115 linenr_T lnum; /* Line we were up to in file */
116 int matched; /* Found a match in this file */
117 } SearchedFile;
118 #endif
121 * translate search pattern for vim_regcomp()
123 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
124 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
125 * pat_save == RE_BOTH: save pat in both patterns (:global command)
126 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
127 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
128 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
129 * options & SEARCH_HIS: put search string in history
130 * options & SEARCH_KEEP: keep previous search pattern
132 * returns FAIL if failed, OK otherwise.
135 search_regcomp(pat, pat_save, pat_use, options, regmatch)
136 char_u *pat;
137 int pat_save;
138 int pat_use;
139 int options;
140 regmmatch_T *regmatch; /* return: pattern and ignore-case flag */
142 int magic;
143 int i;
145 rc_did_emsg = FALSE;
146 magic = p_magic;
149 * If no pattern given, use a previously defined pattern.
151 if (pat == NULL || *pat == NUL)
153 if (pat_use == RE_LAST)
154 i = last_idx;
155 else
156 i = pat_use;
157 if (spats[i].pat == NULL) /* pattern was never defined */
159 if (pat_use == RE_SUBST)
160 EMSG(_(e_nopresub));
161 else
162 EMSG(_(e_noprevre));
163 rc_did_emsg = TRUE;
164 return FAIL;
166 pat = spats[i].pat;
167 magic = spats[i].magic;
168 no_smartcase = spats[i].no_scs;
170 #ifdef FEAT_CMDHIST
171 else if (options & SEARCH_HIS) /* put new pattern in history */
172 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
173 #endif
175 #ifdef FEAT_RIGHTLEFT
176 if (mr_pattern_alloced)
178 vim_free(mr_pattern);
179 mr_pattern_alloced = FALSE;
182 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
184 char_u *rev_pattern;
186 rev_pattern = reverse_text(pat);
187 if (rev_pattern == NULL)
188 mr_pattern = pat; /* out of memory, keep normal pattern. */
189 else
191 mr_pattern = rev_pattern;
192 mr_pattern_alloced = TRUE;
195 else
196 #endif
197 mr_pattern = pat;
200 * Save the currently used pattern in the appropriate place,
201 * unless the pattern should not be remembered.
203 if (!(options & SEARCH_KEEP))
205 /* search or global command */
206 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
207 save_re_pat(RE_SEARCH, pat, magic);
208 /* substitute or global command */
209 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
210 save_re_pat(RE_SUBST, pat, magic);
213 regmatch->rmm_ic = ignorecase(pat);
214 regmatch->rmm_maxcol = 0;
215 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
216 if (regmatch->regprog == NULL)
217 return FAIL;
218 return OK;
222 * Get search pattern used by search_regcomp().
224 char_u *
225 get_search_pat()
227 return mr_pattern;
230 #if defined(FEAT_RIGHTLEFT) || defined(PROTO)
232 * Reverse text into allocated memory.
233 * Returns the allocated string, NULL when out of memory.
235 char_u *
236 reverse_text(s)
237 char_u *s;
239 unsigned len;
240 unsigned s_i, rev_i;
241 char_u *rev;
244 * Reverse the pattern.
246 len = (unsigned)STRLEN(s);
247 rev = alloc(len + 1);
248 if (rev != NULL)
250 rev_i = len;
251 for (s_i = 0; s_i < len; ++s_i)
253 # ifdef FEAT_MBYTE
254 if (has_mbyte)
256 int mb_len;
258 mb_len = (*mb_ptr2len)(s + s_i);
259 rev_i -= mb_len;
260 mch_memmove(rev + rev_i, s + s_i, mb_len);
261 s_i += mb_len - 1;
263 else
264 # endif
265 rev[--rev_i] = s[s_i];
268 rev[len] = NUL;
270 return rev;
272 #endif
274 static void
275 save_re_pat(idx, pat, magic)
276 int idx;
277 char_u *pat;
278 int magic;
280 if (spats[idx].pat != pat)
282 #if FEAT_GUI_MACVIM
283 if (RE_SEARCH == idx)
284 gui_macvim_add_to_find_pboard(pat);
285 #endif
286 vim_free(spats[idx].pat);
287 spats[idx].pat = vim_strsave(pat);
288 spats[idx].magic = magic;
289 spats[idx].no_scs = no_smartcase;
290 last_idx = idx;
291 #ifdef FEAT_SEARCH_EXTRA
292 /* If 'hlsearch' set and search pat changed: need redraw. */
293 if (p_hls)
294 redraw_all_later(SOME_VALID);
295 no_hlsearch = FALSE;
296 #endif
300 #if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
302 * Save the search patterns, so they can be restored later.
303 * Used before/after executing autocommands and user functions.
305 static int save_level = 0;
307 void
308 save_search_patterns()
310 if (save_level++ == 0)
312 saved_spats[0] = spats[0];
313 if (spats[0].pat != NULL)
314 saved_spats[0].pat = vim_strsave(spats[0].pat);
315 saved_spats[1] = spats[1];
316 if (spats[1].pat != NULL)
317 saved_spats[1].pat = vim_strsave(spats[1].pat);
318 saved_last_idx = last_idx;
319 # ifdef FEAT_SEARCH_EXTRA
320 saved_no_hlsearch = no_hlsearch;
321 # endif
325 void
326 restore_search_patterns()
328 if (--save_level == 0)
330 vim_free(spats[0].pat);
331 spats[0] = saved_spats[0];
332 vim_free(spats[1].pat);
333 spats[1] = saved_spats[1];
334 last_idx = saved_last_idx;
335 # ifdef FEAT_SEARCH_EXTRA
336 no_hlsearch = saved_no_hlsearch;
337 # endif
340 #endif
342 #if defined(EXITFREE) || defined(PROTO)
343 void
344 free_search_patterns()
346 vim_free(spats[0].pat);
347 vim_free(spats[1].pat);
349 #endif
352 * Return TRUE when case should be ignored for search pattern "pat".
353 * Uses the 'ignorecase' and 'smartcase' options.
356 ignorecase(pat)
357 char_u *pat;
359 char_u *p;
360 int ic;
362 ic = p_ic;
363 if (ic && !no_smartcase && p_scs
364 #ifdef FEAT_INS_EXPAND
365 && !(ctrl_x_mode && curbuf->b_p_inf)
366 #endif
369 /* don't ignore case if pattern has uppercase */
370 for (p = pat; *p; )
372 #ifdef FEAT_MBYTE
373 int l;
375 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
377 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
379 ic = FALSE;
380 break;
382 p += l;
384 else
385 #endif
386 if (*p == '\\' && p[1] != NUL) /* skip "\S" et al. */
387 p += 2;
388 else if (isupper(*p))
390 ic = FALSE;
391 break;
393 else
394 ++p;
397 no_smartcase = FALSE;
399 return ic;
402 char_u *
403 last_search_pat()
405 return spats[last_idx].pat;
409 * Reset search direction to forward. For "gd" and "gD" commands.
411 void
412 reset_search_dir()
414 spats[0].off.dir = '/';
417 #if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
419 * Set the last search pattern. For ":let @/ =" and viminfo.
420 * Also set the saved search pattern, so that this works in an autocommand.
422 void
423 set_last_search_pat(s, idx, magic, setlast)
424 char_u *s;
425 int idx;
426 int magic;
427 int setlast;
429 #if FEAT_GUI_MACVIM
430 if (RE_SEARCH == idx)
431 gui_macvim_add_to_find_pboard(s);
432 #endif
434 vim_free(spats[idx].pat);
435 /* An empty string means that nothing should be matched. */
436 if (*s == NUL)
437 spats[idx].pat = NULL;
438 else
439 spats[idx].pat = vim_strsave(s);
440 spats[idx].magic = magic;
441 spats[idx].no_scs = FALSE;
442 spats[idx].off.dir = '/';
443 spats[idx].off.line = FALSE;
444 spats[idx].off.end = FALSE;
445 spats[idx].off.off = 0;
446 if (setlast)
447 last_idx = idx;
448 if (save_level)
450 vim_free(saved_spats[idx].pat);
451 saved_spats[idx] = spats[0];
452 if (spats[idx].pat == NULL)
453 saved_spats[idx].pat = NULL;
454 else
455 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
456 saved_last_idx = last_idx;
458 # ifdef FEAT_SEARCH_EXTRA
459 /* If 'hlsearch' set and search pat changed: need redraw. */
460 if (p_hls && idx == last_idx && !no_hlsearch)
461 redraw_all_later(SOME_VALID);
462 # endif
464 #endif
466 #ifdef FEAT_SEARCH_EXTRA
468 * Get a regexp program for the last used search pattern.
469 * This is used for highlighting all matches in a window.
470 * Values returned in regmatch->regprog and regmatch->rmm_ic.
472 void
473 last_pat_prog(regmatch)
474 regmmatch_T *regmatch;
476 if (spats[last_idx].pat == NULL)
478 regmatch->regprog = NULL;
479 return;
481 ++emsg_off; /* So it doesn't beep if bad expr */
482 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
483 --emsg_off;
485 #endif
488 * lowest level search function.
489 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
490 * Start at position 'pos' and return the found position in 'pos'.
492 * if (options & SEARCH_MSG) == 0 don't give any messages
493 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
494 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
495 * if (options & SEARCH_HIS) put search pattern in history
496 * if (options & SEARCH_END) return position at end of match
497 * if (options & SEARCH_START) accept match at pos itself
498 * if (options & SEARCH_KEEP) keep previous search pattern
499 * if (options & SEARCH_FOLD) match only once in a closed fold
500 * if (options & SEARCH_PEEK) check for typed char, cancel search
502 * Return FAIL (zero) for failure, non-zero for success.
503 * When FEAT_EVAL is defined, returns the index of the first matching
504 * subpattern plus one; one if there was none.
506 /*ARGSUSED*/
508 searchit(win, buf, pos, dir, pat, count, options, pat_use, stop_lnum, tm)
509 win_T *win; /* window to search in; can be NULL for a
510 buffer without a window! */
511 buf_T *buf;
512 pos_T *pos;
513 int dir;
514 char_u *pat;
515 long count;
516 int options;
517 int pat_use; /* which pattern to use when "pat" is empty */
518 linenr_T stop_lnum; /* stop after this line number when != 0 */
519 proftime_T *tm; /* timeout limit or NULL */
521 int found;
522 linenr_T lnum; /* no init to shut up Apollo cc */
523 regmmatch_T regmatch;
524 char_u *ptr;
525 colnr_T matchcol;
526 lpos_T endpos;
527 lpos_T matchpos;
528 int loop;
529 pos_T start_pos;
530 int at_first_line;
531 int extra_col;
532 int match_ok;
533 long nmatched;
534 int submatch = 0;
535 int save_called_emsg = called_emsg;
536 #ifdef FEAT_SEARCH_EXTRA
537 int break_loop = FALSE;
538 #else
539 # define break_loop FALSE
540 #endif
542 if (search_regcomp(pat, RE_SEARCH, pat_use,
543 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
545 if ((options & SEARCH_MSG) && !rc_did_emsg)
546 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
547 return FAIL;
550 /* When not accepting a match at the start position set "extra_col" to a
551 * non-zero value. Don't do that when starting at MAXCOL, since MAXCOL +
552 * 1 is zero. */
553 if ((options & SEARCH_START) || pos->col == MAXCOL)
554 extra_col = 0;
555 #ifdef FEAT_MBYTE
556 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
557 else if (has_mbyte && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
558 && pos->col < MAXCOL - 2)
560 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
561 if (*ptr == NUL)
562 extra_col = 1;
563 else
564 extra_col = (*mb_ptr2len)(ptr);
566 #endif
567 else
568 extra_col = 1;
571 * find the string
573 called_emsg = FALSE;
574 do /* loop for count */
576 start_pos = *pos; /* remember start pos for detecting no match */
577 found = 0; /* default: not found */
578 at_first_line = TRUE; /* default: start in first line */
579 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
581 pos->lnum = 1;
582 pos->col = 0;
583 at_first_line = FALSE; /* not in first line now */
587 * Start searching in current line, unless searching backwards and
588 * we're in column 0.
589 * If we are searching backwards, in column 0, and not including the
590 * current position, gain some efficiency by skipping back a line.
591 * Otherwise begin the search in the current line.
593 if (dir == BACKWARD && start_pos.col == 0
594 && (options & SEARCH_START) == 0)
596 lnum = pos->lnum - 1;
597 at_first_line = FALSE;
599 else
600 lnum = pos->lnum;
602 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
604 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
605 lnum += dir, at_first_line = FALSE)
607 /* Stop after checking "stop_lnum", if it's set. */
608 if (stop_lnum != 0 && (dir == FORWARD
609 ? lnum > stop_lnum : lnum < stop_lnum))
610 break;
611 #ifdef FEAT_RELTIME
612 /* Stop after passing the "tm" time limit. */
613 if (tm != NULL && profile_passed_limit(tm))
614 break;
615 #endif
618 * Look for a match somewhere in line "lnum".
620 nmatched = vim_regexec_multi(&regmatch, win, buf,
621 lnum, (colnr_T)0,
622 #ifdef FEAT_RELTIME
624 #else
625 NULL
626 #endif
628 /* Abort searching on an error (e.g., out of stack). */
629 if (called_emsg)
630 break;
631 if (nmatched > 0)
633 /* match may actually be in another line when using \zs */
634 matchpos = regmatch.startpos[0];
635 endpos = regmatch.endpos[0];
636 #ifdef FEAT_EVAL
637 submatch = first_submatch(&regmatch);
638 #endif
639 /* "lnum" may be past end of buffer for "\n\zs". */
640 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
641 ptr = (char_u *)"";
642 else
643 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
646 * Forward search in the first line: match should be after
647 * the start position. If not, continue at the end of the
648 * match (this is vi compatible) or on the next char.
650 if (dir == FORWARD && at_first_line)
652 match_ok = TRUE;
654 * When the match starts in a next line it's certainly
655 * past the start position.
656 * When match lands on a NUL the cursor will be put
657 * one back afterwards, compare with that position,
658 * otherwise "/$" will get stuck on end of line.
660 while (matchpos.lnum == 0
661 && ((options & SEARCH_END)
662 ? (nmatched == 1
663 && (int)endpos.col - 1
664 < (int)start_pos.col + extra_col)
665 : ((int)matchpos.col
666 - (ptr[matchpos.col] == NUL)
667 < (int)start_pos.col + extra_col)))
670 * If vi-compatible searching, continue at the end
671 * of the match, otherwise continue one position
672 * forward.
674 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
676 if (nmatched > 1)
678 /* end is in next line, thus no match in
679 * this line */
680 match_ok = FALSE;
681 break;
683 matchcol = endpos.col;
684 /* for empty match: advance one char */
685 if (matchcol == matchpos.col
686 && ptr[matchcol] != NUL)
688 #ifdef FEAT_MBYTE
689 if (has_mbyte)
690 matchcol +=
691 (*mb_ptr2len)(ptr + matchcol);
692 else
693 #endif
694 ++matchcol;
697 else
699 matchcol = matchpos.col;
700 if (ptr[matchcol] != NUL)
702 #ifdef FEAT_MBYTE
703 if (has_mbyte)
704 matchcol += (*mb_ptr2len)(ptr
705 + matchcol);
706 else
707 #endif
708 ++matchcol;
711 if (ptr[matchcol] == NUL
712 || (nmatched = vim_regexec_multi(&regmatch,
713 win, buf, lnum + matchpos.lnum,
714 matchcol,
715 #ifdef FEAT_RELTIME
717 #else
718 NULL
719 #endif
720 )) == 0)
722 match_ok = FALSE;
723 break;
725 matchpos = regmatch.startpos[0];
726 endpos = regmatch.endpos[0];
727 # ifdef FEAT_EVAL
728 submatch = first_submatch(&regmatch);
729 # endif
731 /* Need to get the line pointer again, a
732 * multi-line search may have made it invalid. */
733 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
735 if (!match_ok)
736 continue;
738 if (dir == BACKWARD)
741 * Now, if there are multiple matches on this line,
742 * we have to get the last one. Or the last one before
743 * the cursor, if we're on that line.
744 * When putting the new cursor at the end, compare
745 * relative to the end of the match.
747 match_ok = FALSE;
748 for (;;)
750 /* Remember a position that is before the start
751 * position, we use it if it's the last match in
752 * the line. Always accept a position after
753 * wrapping around. */
754 if (loop
755 || ((options & SEARCH_END)
756 ? (lnum + regmatch.endpos[0].lnum
757 < start_pos.lnum
758 || (lnum + regmatch.endpos[0].lnum
759 == start_pos.lnum
760 && (int)regmatch.endpos[0].col - 1
761 + extra_col
762 <= (int)start_pos.col))
763 : (lnum + regmatch.startpos[0].lnum
764 < start_pos.lnum
765 || (lnum + regmatch.startpos[0].lnum
766 == start_pos.lnum
767 && (int)regmatch.startpos[0].col
768 + extra_col
769 <= (int)start_pos.col))))
771 match_ok = TRUE;
772 matchpos = regmatch.startpos[0];
773 endpos = regmatch.endpos[0];
774 # ifdef FEAT_EVAL
775 submatch = first_submatch(&regmatch);
776 # endif
778 else
779 break;
782 * We found a valid match, now check if there is
783 * another one after it.
784 * If vi-compatible searching, continue at the end
785 * of the match, otherwise continue one position
786 * forward.
788 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
790 if (nmatched > 1)
791 break;
792 matchcol = endpos.col;
793 /* for empty match: advance one char */
794 if (matchcol == matchpos.col
795 && ptr[matchcol] != NUL)
797 #ifdef FEAT_MBYTE
798 if (has_mbyte)
799 matchcol +=
800 (*mb_ptr2len)(ptr + matchcol);
801 else
802 #endif
803 ++matchcol;
806 else
808 /* Stop when the match is in a next line. */
809 if (matchpos.lnum > 0)
810 break;
811 matchcol = matchpos.col;
812 if (ptr[matchcol] != NUL)
814 #ifdef FEAT_MBYTE
815 if (has_mbyte)
816 matchcol +=
817 (*mb_ptr2len)(ptr + matchcol);
818 else
819 #endif
820 ++matchcol;
823 if (ptr[matchcol] == NUL
824 || (nmatched = vim_regexec_multi(&regmatch,
825 win, buf, lnum + matchpos.lnum,
826 matchcol,
827 #ifdef FEAT_RELTIME
829 #else
830 NULL
831 #endif
832 )) == 0)
833 break;
835 /* Need to get the line pointer again, a
836 * multi-line search may have made it invalid. */
837 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
841 * If there is only a match after the cursor, skip
842 * this match.
844 if (!match_ok)
845 continue;
848 /* With the SEARCH_END option move to the last character
849 * of the match. Don't do it for an empty match, end
850 * should be same as start then. */
851 if (options & SEARCH_END && !(options & SEARCH_NOOF)
852 && !(matchpos.lnum == endpos.lnum
853 && matchpos.col == endpos.col))
855 /* For a match in the first column, set the position
856 * on the NUL in the previous line. */
857 pos->lnum = lnum + endpos.lnum;
858 pos->col = endpos.col;
859 if (endpos.col == 0)
861 if (pos->lnum > 1) /* just in case */
863 --pos->lnum;
864 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
865 pos->lnum, FALSE));
868 else
870 --pos->col;
871 #ifdef FEAT_MBYTE
872 if (has_mbyte
873 && pos->lnum <= buf->b_ml.ml_line_count)
875 ptr = ml_get_buf(buf, pos->lnum, FALSE);
876 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
878 #endif
881 else
883 pos->lnum = lnum + matchpos.lnum;
884 pos->col = matchpos.col;
886 #ifdef FEAT_VIRTUALEDIT
887 pos->coladd = 0;
888 #endif
889 found = 1;
891 /* Set variables used for 'incsearch' highlighting. */
892 search_match_lines = endpos.lnum - matchpos.lnum;
893 search_match_endcol = endpos.col;
894 break;
896 line_breakcheck(); /* stop if ctrl-C typed */
897 if (got_int)
898 break;
900 #ifdef FEAT_SEARCH_EXTRA
901 /* Cancel searching if a character was typed. Used for
902 * 'incsearch'. Don't check too often, that would slowdown
903 * searching too much. */
904 if ((options & SEARCH_PEEK)
905 && ((lnum - pos->lnum) & 0x3f) == 0
906 && char_avail())
908 break_loop = TRUE;
909 break;
911 #endif
913 if (loop && lnum == start_pos.lnum)
914 break; /* if second loop, stop where started */
916 at_first_line = FALSE;
919 * Stop the search if wrapscan isn't set, "stop_lnum" is
920 * specified, after an interrupt, after a match and after looping
921 * twice.
923 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
924 || break_loop || found || loop)
925 break;
928 * If 'wrapscan' is set we continue at the other end of the file.
929 * If 'shortmess' does not contain 's', we give a message.
930 * This message is also remembered in keep_msg for when the screen
931 * is redrawn. The keep_msg is cleared whenever another message is
932 * written.
934 if (dir == BACKWARD) /* start second loop at the other end */
935 lnum = buf->b_ml.ml_line_count;
936 else
937 lnum = 1;
938 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
939 give_warning((char_u *)_(dir == BACKWARD
940 ? top_bot_msg : bot_top_msg), TRUE);
942 if (got_int || called_emsg || break_loop)
943 break;
945 while (--count > 0 && found); /* stop after count matches or no match */
947 vim_free(regmatch.regprog);
949 called_emsg |= save_called_emsg;
951 if (!found) /* did not find it */
953 if (got_int)
954 EMSG(_(e_interr));
955 else if ((options & SEARCH_MSG) == SEARCH_MSG)
957 if (p_ws)
958 EMSG2(_(e_patnotf2), mr_pattern);
959 else if (lnum == 0)
960 EMSG2(_("E384: search hit TOP without match for: %s"),
961 mr_pattern);
962 else
963 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
964 mr_pattern);
966 return FAIL;
969 /* A pattern like "\n\zs" may go past the last line. */
970 if (pos->lnum > buf->b_ml.ml_line_count)
972 pos->lnum = buf->b_ml.ml_line_count;
973 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
974 if (pos->col > 0)
975 --pos->col;
978 return submatch + 1;
981 #ifdef FEAT_EVAL
983 * Return the number of the first subpat that matched.
985 static int
986 first_submatch(rp)
987 regmmatch_T *rp;
989 int submatch;
991 for (submatch = 1; ; ++submatch)
993 if (rp->startpos[submatch].lnum >= 0)
994 break;
995 if (submatch == 9)
997 submatch = 0;
998 break;
1001 return submatch;
1003 #endif
1006 * Highest level string search function.
1007 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
1008 * If 'dirc' is 0: use previous dir.
1009 * If 'pat' is NULL or empty : use previous string.
1010 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1011 * If 'options & SEARCH_ECHO': echo the search command and handle options
1012 * If 'options & SEARCH_MSG' : may give error message
1013 * If 'options & SEARCH_OPT' : interpret optional flags
1014 * If 'options & SEARCH_HIS' : put search pattern in history
1015 * If 'options & SEARCH_NOOF': don't add offset to position
1016 * If 'options & SEARCH_MARK': set previous context mark
1017 * If 'options & SEARCH_KEEP': keep previous search pattern
1018 * If 'options & SEARCH_START': accept match at curpos itself
1019 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1021 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1022 * makes the movement linewise without moving the match position.
1024 * return 0 for failure, 1 for found, 2 for found and line offset added
1027 do_search(oap, dirc, pat, count, options, tm)
1028 oparg_T *oap; /* can be NULL */
1029 int dirc; /* '/' or '?' */
1030 char_u *pat;
1031 long count;
1032 int options;
1033 proftime_T *tm; /* timeout limit or NULL */
1035 pos_T pos; /* position of the last match */
1036 char_u *searchstr;
1037 struct soffset old_off;
1038 int retval; /* Return value */
1039 char_u *p;
1040 long c;
1041 char_u *dircp;
1042 char_u *strcopy = NULL;
1043 char_u *ps;
1046 * A line offset is not remembered, this is vi compatible.
1048 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1050 spats[0].off.line = FALSE;
1051 spats[0].off.off = 0;
1055 * Save the values for when (options & SEARCH_KEEP) is used.
1056 * (there is no "if ()" around this because gcc wants them initialized)
1058 old_off = spats[0].off;
1060 pos = curwin->w_cursor; /* start searching at the cursor position */
1063 * Find out the direction of the search.
1065 if (dirc == 0)
1066 dirc = spats[0].off.dir;
1067 else
1068 spats[0].off.dir = dirc;
1069 if (options & SEARCH_REV)
1071 #ifdef WIN32
1072 /* There is a bug in the Visual C++ 2.2 compiler which means that
1073 * dirc always ends up being '/' */
1074 dirc = (dirc == '/') ? '?' : '/';
1075 #else
1076 if (dirc == '/')
1077 dirc = '?';
1078 else
1079 dirc = '/';
1080 #endif
1083 #ifdef FEAT_FOLDING
1084 /* If the cursor is in a closed fold, don't find another match in the same
1085 * fold. */
1086 if (dirc == '/')
1088 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1089 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1091 else
1093 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1094 pos.col = 0;
1096 #endif
1098 #ifdef FEAT_SEARCH_EXTRA
1100 * Turn 'hlsearch' highlighting back on.
1102 if (no_hlsearch && !(options & SEARCH_KEEP))
1104 redraw_all_later(SOME_VALID);
1105 no_hlsearch = FALSE;
1107 #endif
1110 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1112 for (;;)
1114 searchstr = pat;
1115 dircp = NULL;
1116 /* use previous pattern */
1117 if (pat == NULL || *pat == NUL || *pat == dirc)
1119 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1121 EMSG(_(e_noprevre));
1122 retval = 0;
1123 goto end_do_search;
1125 /* make search_regcomp() use spats[RE_SEARCH].pat */
1126 searchstr = (char_u *)"";
1129 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1132 * Find end of regular expression.
1133 * If there is a matching '/' or '?', toss it.
1135 ps = strcopy;
1136 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1137 if (strcopy != ps)
1139 /* made a copy of "pat" to change "\?" to "?" */
1140 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
1141 pat = strcopy;
1142 searchstr = strcopy;
1144 if (*p == dirc)
1146 dircp = p; /* remember where we put the NUL */
1147 *p++ = NUL;
1149 spats[0].off.line = FALSE;
1150 spats[0].off.end = FALSE;
1151 spats[0].off.off = 0;
1153 * Check for a line offset or a character offset.
1154 * For get_address (echo off) we don't check for a character
1155 * offset, because it is meaningless and the 's' could be a
1156 * substitute command.
1158 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1159 spats[0].off.line = TRUE;
1160 else if ((options & SEARCH_OPT) &&
1161 (*p == 'e' || *p == 's' || *p == 'b'))
1163 if (*p == 'e') /* end */
1164 spats[0].off.end = SEARCH_END;
1165 ++p;
1167 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1169 /* 'nr' or '+nr' or '-nr' */
1170 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1171 spats[0].off.off = atol((char *)p);
1172 else if (*p == '-') /* single '-' */
1173 spats[0].off.off = -1;
1174 else /* single '+' */
1175 spats[0].off.off = 1;
1176 ++p;
1177 while (VIM_ISDIGIT(*p)) /* skip number */
1178 ++p;
1181 /* compute length of search command for get_address() */
1182 searchcmdlen += (int)(p - pat);
1184 pat = p; /* put pat after search command */
1187 if ((options & SEARCH_ECHO) && messaging()
1188 && !cmd_silent && msg_silent == 0)
1190 char_u *msgbuf;
1191 char_u *trunc;
1193 if (*searchstr == NUL)
1194 p = spats[last_idx].pat;
1195 else
1196 p = searchstr;
1197 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1198 if (msgbuf != NULL)
1200 msgbuf[0] = dirc;
1201 #ifdef FEAT_MBYTE
1202 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1204 /* Use a space to draw the composing char on. */
1205 msgbuf[1] = ' ';
1206 STRCPY(msgbuf + 2, p);
1208 else
1209 #endif
1210 STRCPY(msgbuf + 1, p);
1211 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1213 p = msgbuf + STRLEN(msgbuf);
1214 *p++ = dirc;
1215 if (spats[0].off.end)
1216 *p++ = 'e';
1217 else if (!spats[0].off.line)
1218 *p++ = 's';
1219 if (spats[0].off.off > 0 || spats[0].off.line)
1220 *p++ = '+';
1221 if (spats[0].off.off != 0 || spats[0].off.line)
1222 sprintf((char *)p, "%ld", spats[0].off.off);
1223 else
1224 *p = NUL;
1227 msg_start();
1228 trunc = msg_strtrunc(msgbuf, FALSE);
1230 #ifdef FEAT_RIGHTLEFT
1231 /* The search pattern could be shown on the right in rightleft
1232 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1233 * it would be blanked out again very soon. Show it on the
1234 * left, but do reverse the text. */
1235 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1237 char_u *r;
1239 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1240 if (r != NULL)
1242 vim_free(trunc);
1243 trunc = r;
1246 #endif
1247 if (trunc != NULL)
1249 msg_outtrans(trunc);
1250 vim_free(trunc);
1252 else
1253 msg_outtrans(msgbuf);
1254 msg_clr_eos();
1255 msg_check();
1256 vim_free(msgbuf);
1258 gotocmdline(FALSE);
1259 out_flush();
1260 msg_nowait = TRUE; /* don't wait for this message */
1265 * If there is a character offset, subtract it from the current
1266 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
1267 * Skip this if pos.col is near MAXCOL (closed fold).
1268 * This is not done for a line offset, because then we would not be vi
1269 * compatible.
1271 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
1273 if (spats[0].off.off > 0)
1275 for (c = spats[0].off.off; c; --c)
1276 if (decl(&pos) == -1)
1277 break;
1278 if (c) /* at start of buffer */
1280 pos.lnum = 0; /* allow lnum == 0 here */
1281 pos.col = MAXCOL;
1284 else
1286 for (c = spats[0].off.off; c; ++c)
1287 if (incl(&pos) == -1)
1288 break;
1289 if (c) /* at end of buffer */
1291 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1292 pos.col = 0;
1297 #ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1298 if (p_altkeymap && curwin->w_p_rl)
1299 lrFswap(searchstr,0);
1300 #endif
1302 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1303 searchstr, count, spats[0].off.end + (options &
1304 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1305 + SEARCH_MSG + SEARCH_START
1306 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
1307 RE_LAST, (linenr_T)0, tm);
1309 if (dircp != NULL)
1310 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1311 if (c == FAIL)
1313 retval = 0;
1314 goto end_do_search;
1316 if (spats[0].off.end && oap != NULL)
1317 oap->inclusive = TRUE; /* 'e' includes last character */
1319 retval = 1; /* pattern found */
1322 * Add character and/or line offset
1324 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
1326 if (spats[0].off.line) /* Add the offset to the line number. */
1328 c = pos.lnum + spats[0].off.off;
1329 if (c < 1)
1330 pos.lnum = 1;
1331 else if (c > curbuf->b_ml.ml_line_count)
1332 pos.lnum = curbuf->b_ml.ml_line_count;
1333 else
1334 pos.lnum = c;
1335 pos.col = 0;
1337 retval = 2; /* pattern found, line offset added */
1339 else if (pos.col < MAXCOL - 2) /* just in case */
1341 /* to the right, check for end of file */
1342 if (spats[0].off.off > 0)
1344 for (c = spats[0].off.off; c; --c)
1345 if (incl(&pos) == -1)
1346 break;
1348 /* to the left, check for start of file */
1349 else
1351 if ((c = pos.col + spats[0].off.off) >= 0)
1352 pos.col = c;
1353 else
1354 for (c = spats[0].off.off; c; ++c)
1355 if (decl(&pos) == -1)
1356 break;
1362 * The search command can be followed by a ';' to do another search.
1363 * For example: "/pat/;/foo/+3;?bar"
1364 * This is like doing another search command, except:
1365 * - The remembered direction '/' or '?' is from the first search.
1366 * - When an error happens the cursor isn't moved at all.
1367 * Don't do this when called by get_address() (it handles ';' itself).
1369 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1370 break;
1372 dirc = *++pat;
1373 if (dirc != '?' && dirc != '/')
1375 retval = 0;
1376 EMSG(_("E386: Expected '?' or '/' after ';'"));
1377 goto end_do_search;
1379 ++pat;
1382 if (options & SEARCH_MARK)
1383 setpcmark();
1384 curwin->w_cursor = pos;
1385 curwin->w_set_curswant = TRUE;
1387 end_do_search:
1388 if (options & SEARCH_KEEP)
1389 spats[0].off = old_off;
1390 vim_free(strcopy);
1392 return retval;
1395 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1397 * search_for_exact_line(buf, pos, dir, pat)
1399 * Search for a line starting with the given pattern (ignoring leading
1400 * white-space), starting from pos and going in direction dir. pos will
1401 * contain the position of the match found. Blank lines match only if
1402 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1403 * Return OK for success, or FAIL if no line found.
1406 search_for_exact_line(buf, pos, dir, pat)
1407 buf_T *buf;
1408 pos_T *pos;
1409 int dir;
1410 char_u *pat;
1412 linenr_T start = 0;
1413 char_u *ptr;
1414 char_u *p;
1416 if (buf->b_ml.ml_line_count == 0)
1417 return FAIL;
1418 for (;;)
1420 pos->lnum += dir;
1421 if (pos->lnum < 1)
1423 if (p_ws)
1425 pos->lnum = buf->b_ml.ml_line_count;
1426 if (!shortmess(SHM_SEARCH))
1427 give_warning((char_u *)_(top_bot_msg), TRUE);
1429 else
1431 pos->lnum = 1;
1432 break;
1435 else if (pos->lnum > buf->b_ml.ml_line_count)
1437 if (p_ws)
1439 pos->lnum = 1;
1440 if (!shortmess(SHM_SEARCH))
1441 give_warning((char_u *)_(bot_top_msg), TRUE);
1443 else
1445 pos->lnum = 1;
1446 break;
1449 if (pos->lnum == start)
1450 break;
1451 if (start == 0)
1452 start = pos->lnum;
1453 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1454 p = skipwhite(ptr);
1455 pos->col = (colnr_T) (p - ptr);
1457 /* when adding lines the matching line may be empty but it is not
1458 * ignored because we are interested in the next line -- Acevedo */
1459 if ((compl_cont_status & CONT_ADDING)
1460 && !(compl_cont_status & CONT_SOL))
1462 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1463 return OK;
1465 else if (*p != NUL) /* ignore empty lines */
1466 { /* expanding lines or words */
1467 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1468 : STRNCMP(p, pat, compl_length)) == 0)
1469 return OK;
1472 return FAIL;
1474 #endif /* FEAT_INS_EXPAND */
1477 * Character Searches
1481 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1482 * position of the character, otherwise move to just before the char.
1483 * Do this "cap->count1" times.
1484 * Return FAIL or OK.
1487 searchc(cap, t_cmd)
1488 cmdarg_T *cap;
1489 int t_cmd;
1491 int c = cap->nchar; /* char to search for */
1492 int dir = cap->arg; /* TRUE for searching forward */
1493 long count = cap->count1; /* repeat count */
1494 static int lastc = NUL; /* last character searched for */
1495 static int lastcdir; /* last direction of character search */
1496 static int last_t_cmd; /* last search t_cmd */
1497 int col;
1498 char_u *p;
1499 int len;
1500 #ifdef FEAT_MBYTE
1501 static char_u bytes[MB_MAXBYTES];
1502 static int bytelen = 1; /* >1 for multi-byte char */
1503 #endif
1505 if (c != NUL) /* normal search: remember args for repeat */
1507 if (!KeyStuffed) /* don't remember when redoing */
1509 lastc = c;
1510 lastcdir = dir;
1511 last_t_cmd = t_cmd;
1512 #ifdef FEAT_MBYTE
1513 bytelen = (*mb_char2bytes)(c, bytes);
1514 if (cap->ncharC1 != 0)
1516 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1517 if (cap->ncharC2 != 0)
1518 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1520 #endif
1523 else /* repeat previous search */
1525 if (lastc == NUL)
1526 return FAIL;
1527 if (dir) /* repeat in opposite direction */
1528 dir = -lastcdir;
1529 else
1530 dir = lastcdir;
1531 t_cmd = last_t_cmd;
1532 c = lastc;
1533 /* For multi-byte re-use last bytes[] and bytelen. */
1536 if (dir == BACKWARD)
1537 cap->oap->inclusive = FALSE;
1538 else
1539 cap->oap->inclusive = TRUE;
1541 p = ml_get_curline();
1542 col = curwin->w_cursor.col;
1543 len = (int)STRLEN(p);
1545 while (count--)
1547 #ifdef FEAT_MBYTE
1548 if (has_mbyte)
1550 for (;;)
1552 if (dir > 0)
1554 col += (*mb_ptr2len)(p + col);
1555 if (col >= len)
1556 return FAIL;
1558 else
1560 if (col == 0)
1561 return FAIL;
1562 col -= (*mb_head_off)(p, p + col - 1) + 1;
1564 if (bytelen == 1)
1566 if (p[col] == c)
1567 break;
1569 else
1571 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1572 break;
1576 else
1577 #endif
1579 for (;;)
1581 if ((col += dir) < 0 || col >= len)
1582 return FAIL;
1583 if (p[col] == c)
1584 break;
1589 if (t_cmd)
1591 /* backup to before the character (possibly double-byte) */
1592 col -= dir;
1593 #ifdef FEAT_MBYTE
1594 if (has_mbyte)
1596 if (dir < 0)
1597 /* Landed on the search char which is bytelen long */
1598 col += bytelen - 1;
1599 else
1600 /* To previous char, which may be multi-byte. */
1601 col -= (*mb_head_off)(p, p + col);
1603 #endif
1605 curwin->w_cursor.col = col;
1607 return OK;
1611 * "Other" Searches
1615 * findmatch - find the matching paren or brace
1617 * Improvement over vi: Braces inside quotes are ignored.
1619 pos_T *
1620 findmatch(oap, initc)
1621 oparg_T *oap;
1622 int initc;
1624 return findmatchlimit(oap, initc, 0, 0);
1628 * Return TRUE if the character before "linep[col]" equals "ch".
1629 * Return FALSE if "col" is zero.
1630 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1631 * is NULL.
1632 * Handles multibyte string correctly.
1634 static int
1635 check_prevcol(linep, col, ch, prevcol)
1636 char_u *linep;
1637 int col;
1638 int ch;
1639 int *prevcol;
1641 --col;
1642 #ifdef FEAT_MBYTE
1643 if (col > 0 && has_mbyte)
1644 col -= (*mb_head_off)(linep, linep + col);
1645 #endif
1646 if (prevcol)
1647 *prevcol = col;
1648 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1652 * findmatchlimit -- find the matching paren or brace, if it exists within
1653 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1654 * the edge of the file.
1656 * "initc" is the character to find a match for. NUL means to find the
1657 * character at or after the cursor.
1659 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1660 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1661 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1662 * FM_SKIPCOMM skip comments (not implemented yet!)
1664 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1665 * NULL
1668 pos_T *
1669 findmatchlimit(oap, initc, flags, maxtravel)
1670 oparg_T *oap;
1671 int initc;
1672 int flags;
1673 int maxtravel;
1675 static pos_T pos; /* current search position */
1676 int findc = 0; /* matching brace */
1677 int c;
1678 int count = 0; /* cumulative number of braces */
1679 int backwards = FALSE; /* init for gcc */
1680 int inquote = FALSE; /* TRUE when inside quotes */
1681 char_u *linep; /* pointer to current line */
1682 char_u *ptr;
1683 int do_quotes; /* check for quotes in current line */
1684 int at_start; /* do_quotes value at start position */
1685 int hash_dir = 0; /* Direction searched for # things */
1686 int comment_dir = 0; /* Direction searched for comments */
1687 pos_T match_pos; /* Where last slash-star was found */
1688 int start_in_quotes; /* start position is in quotes */
1689 int traveled = 0; /* how far we've searched so far */
1690 int ignore_cend = FALSE; /* ignore comment end */
1691 int cpo_match; /* vi compatible matching */
1692 int cpo_bsl; /* don't recognize backslashes */
1693 int match_escaped = 0; /* search for escaped match */
1694 int dir; /* Direction to search */
1695 int comment_col = MAXCOL; /* start of / / comment */
1696 #ifdef FEAT_LISP
1697 int lispcomm = FALSE; /* inside of Lisp-style comment */
1698 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1699 #endif
1701 pos = curwin->w_cursor;
1702 linep = ml_get(pos.lnum);
1704 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1705 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1707 /* Direction to search when initc is '/', '*' or '#' */
1708 if (flags & FM_BACKWARD)
1709 dir = BACKWARD;
1710 else if (flags & FM_FORWARD)
1711 dir = FORWARD;
1712 else
1713 dir = 0;
1716 * if initc given, look in the table for the matching character
1717 * '/' and '*' are special cases: look for start or end of comment.
1718 * When '/' is used, we ignore running backwards into an star-slash, for
1719 * "[*" command, we just want to find any comment.
1721 if (initc == '/' || initc == '*')
1723 comment_dir = dir;
1724 if (initc == '/')
1725 ignore_cend = TRUE;
1726 backwards = (dir == FORWARD) ? FALSE : TRUE;
1727 initc = NUL;
1729 else if (initc != '#' && initc != NUL)
1731 /* 'matchpairs' is "x:y,x:y" */
1732 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1734 if (*ptr == initc)
1736 findc = initc;
1737 initc = ptr[2];
1738 backwards = TRUE;
1739 break;
1741 ptr += 2;
1742 if (*ptr == initc)
1744 findc = initc;
1745 initc = ptr[-2];
1746 backwards = FALSE;
1747 break;
1749 if (ptr[1] != ',')
1750 break;
1752 if (!findc) /* invalid initc! */
1753 return NULL;
1756 * Either initc is '#', or no initc was given and we need to look under the
1757 * cursor.
1759 else
1761 if (initc == '#')
1763 hash_dir = dir;
1765 else
1768 * initc was not given, must look for something to match under
1769 * or near the cursor.
1770 * Only check for special things when 'cpo' doesn't have '%'.
1772 if (!cpo_match)
1774 /* Are we before or at #if, #else etc.? */
1775 ptr = skipwhite(linep);
1776 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1778 ptr = skipwhite(ptr + 1);
1779 if ( STRNCMP(ptr, "if", 2) == 0
1780 || STRNCMP(ptr, "endif", 5) == 0
1781 || STRNCMP(ptr, "el", 2) == 0)
1782 hash_dir = 1;
1785 /* Are we on a comment? */
1786 else if (linep[pos.col] == '/')
1788 if (linep[pos.col + 1] == '*')
1790 comment_dir = FORWARD;
1791 backwards = FALSE;
1792 pos.col++;
1794 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1796 comment_dir = BACKWARD;
1797 backwards = TRUE;
1798 pos.col--;
1801 else if (linep[pos.col] == '*')
1803 if (linep[pos.col + 1] == '/')
1805 comment_dir = BACKWARD;
1806 backwards = TRUE;
1808 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1810 comment_dir = FORWARD;
1811 backwards = FALSE;
1817 * If we are not on a comment or the # at the start of a line, then
1818 * look for brace anywhere on this line after the cursor.
1820 if (!hash_dir && !comment_dir)
1823 * Find the brace under or after the cursor.
1824 * If beyond the end of the line, use the last character in
1825 * the line.
1827 if (linep[pos.col] == NUL && pos.col)
1828 --pos.col;
1829 for (;;)
1831 initc = linep[pos.col];
1832 if (initc == NUL)
1833 break;
1835 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1837 if (*ptr == initc)
1839 findc = ptr[2];
1840 backwards = FALSE;
1841 break;
1843 ptr += 2;
1844 if (*ptr == initc)
1846 findc = ptr[-2];
1847 backwards = TRUE;
1848 break;
1850 if (!*++ptr)
1851 break;
1853 if (findc)
1854 break;
1855 #ifdef FEAT_MBYTE
1856 if (has_mbyte)
1857 pos.col += (*mb_ptr2len)(linep + pos.col);
1858 else
1859 #endif
1860 ++pos.col;
1862 if (!findc)
1864 /* no brace in the line, maybe use " #if" then */
1865 if (!cpo_match && *skipwhite(linep) == '#')
1866 hash_dir = 1;
1867 else
1868 return NULL;
1870 else if (!cpo_bsl)
1872 int col, bslcnt = 0;
1874 /* Set "match_escaped" if there are an odd number of
1875 * backslashes. */
1876 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1877 bslcnt++;
1878 match_escaped = (bslcnt & 1);
1882 if (hash_dir)
1885 * Look for matching #if, #else, #elif, or #endif
1887 if (oap != NULL)
1888 oap->motion_type = MLINE; /* Linewise for this case only */
1889 if (initc != '#')
1891 ptr = skipwhite(skipwhite(linep) + 1);
1892 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1893 hash_dir = 1;
1894 else if (STRNCMP(ptr, "endif", 5) == 0)
1895 hash_dir = -1;
1896 else
1897 return NULL;
1899 pos.col = 0;
1900 while (!got_int)
1902 if (hash_dir > 0)
1904 if (pos.lnum == curbuf->b_ml.ml_line_count)
1905 break;
1907 else if (pos.lnum == 1)
1908 break;
1909 pos.lnum += hash_dir;
1910 linep = ml_get(pos.lnum);
1911 line_breakcheck(); /* check for CTRL-C typed */
1912 ptr = skipwhite(linep);
1913 if (*ptr != '#')
1914 continue;
1915 pos.col = (colnr_T) (ptr - linep);
1916 ptr = skipwhite(ptr + 1);
1917 if (hash_dir > 0)
1919 if (STRNCMP(ptr, "if", 2) == 0)
1920 count++;
1921 else if (STRNCMP(ptr, "el", 2) == 0)
1923 if (count == 0)
1924 return &pos;
1926 else if (STRNCMP(ptr, "endif", 5) == 0)
1928 if (count == 0)
1929 return &pos;
1930 count--;
1933 else
1935 if (STRNCMP(ptr, "if", 2) == 0)
1937 if (count == 0)
1938 return &pos;
1939 count--;
1941 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1943 if (count == 0)
1944 return &pos;
1946 else if (STRNCMP(ptr, "endif", 5) == 0)
1947 count++;
1950 return NULL;
1954 #ifdef FEAT_RIGHTLEFT
1955 /* This is just guessing: when 'rightleft' is set, search for a matching
1956 * paren/brace in the other direction. */
1957 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1958 backwards = !backwards;
1959 #endif
1961 do_quotes = -1;
1962 start_in_quotes = MAYBE;
1963 clearpos(&match_pos);
1965 /* backward search: Check if this line contains a single-line comment */
1966 if ((backwards && comment_dir)
1967 #ifdef FEAT_LISP
1968 || lisp
1969 #endif
1971 comment_col = check_linecomment(linep);
1972 #ifdef FEAT_LISP
1973 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1974 lispcomm = TRUE; /* find match inside this comment */
1975 #endif
1976 while (!got_int)
1979 * Go to the next position, forward or backward. We could use
1980 * inc() and dec() here, but that is much slower
1982 if (backwards)
1984 #ifdef FEAT_LISP
1985 /* char to match is inside of comment, don't search outside */
1986 if (lispcomm && pos.col < (colnr_T)comment_col)
1987 break;
1988 #endif
1989 if (pos.col == 0) /* at start of line, go to prev. one */
1991 if (pos.lnum == 1) /* start of file */
1992 break;
1993 --pos.lnum;
1995 if (maxtravel > 0 && ++traveled > maxtravel)
1996 break;
1998 linep = ml_get(pos.lnum);
1999 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
2000 do_quotes = -1;
2001 line_breakcheck();
2003 /* Check if this line contains a single-line comment */
2004 if (comment_dir
2005 #ifdef FEAT_LISP
2006 || lisp
2007 #endif
2009 comment_col = check_linecomment(linep);
2010 #ifdef FEAT_LISP
2011 /* skip comment */
2012 if (lisp && comment_col != MAXCOL)
2013 pos.col = comment_col;
2014 #endif
2016 else
2018 --pos.col;
2019 #ifdef FEAT_MBYTE
2020 if (has_mbyte)
2021 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2022 #endif
2025 else /* forward search */
2027 if (linep[pos.col] == NUL
2028 /* at end of line, go to next one */
2029 #ifdef FEAT_LISP
2030 /* don't search for match in comment */
2031 || (lisp && comment_col != MAXCOL
2032 && pos.col == (colnr_T)comment_col)
2033 #endif
2036 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2037 #ifdef FEAT_LISP
2038 /* line is exhausted and comment with it,
2039 * don't search for match in code */
2040 || lispcomm
2041 #endif
2043 break;
2044 ++pos.lnum;
2046 if (maxtravel && traveled++ > maxtravel)
2047 break;
2049 linep = ml_get(pos.lnum);
2050 pos.col = 0;
2051 do_quotes = -1;
2052 line_breakcheck();
2053 #ifdef FEAT_LISP
2054 if (lisp) /* find comment pos in new line */
2055 comment_col = check_linecomment(linep);
2056 #endif
2058 else
2060 #ifdef FEAT_MBYTE
2061 if (has_mbyte)
2062 pos.col += (*mb_ptr2len)(linep + pos.col);
2063 else
2064 #endif
2065 ++pos.col;
2070 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2072 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2073 (linep[0] == '{' || linep[0] == '}'))
2075 if (linep[0] == findc && count == 0) /* match! */
2076 return &pos;
2077 break; /* out of scope */
2080 if (comment_dir)
2082 /* Note: comments do not nest, and we ignore quotes in them */
2083 /* TODO: ignore comment brackets inside strings */
2084 if (comment_dir == FORWARD)
2086 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2088 pos.col++;
2089 return &pos;
2092 else /* Searching backwards */
2095 * A comment may contain / * or / /, it may also start or end
2096 * with / * /. Ignore a / * after / /.
2098 if (pos.col == 0)
2099 continue;
2100 else if ( linep[pos.col - 1] == '/'
2101 && linep[pos.col] == '*'
2102 && (int)pos.col < comment_col)
2104 count++;
2105 match_pos = pos;
2106 match_pos.col--;
2108 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2110 if (count > 0)
2111 pos = match_pos;
2112 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2113 && (int)pos.col <= comment_col)
2114 pos.col -= 2;
2115 else if (ignore_cend)
2116 continue;
2117 else
2118 return NULL;
2119 return &pos;
2122 continue;
2126 * If smart matching ('cpoptions' does not contain '%'), braces inside
2127 * of quotes are ignored, but only if there is an even number of
2128 * quotes in the line.
2130 if (cpo_match)
2131 do_quotes = 0;
2132 else if (do_quotes == -1)
2135 * Count the number of quotes in the line, skipping \" and '"'.
2136 * Watch out for "\\".
2138 at_start = do_quotes;
2139 for (ptr = linep; *ptr; ++ptr)
2141 if (ptr == linep + pos.col + backwards)
2142 at_start = (do_quotes & 1);
2143 if (*ptr == '"'
2144 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2145 ++do_quotes;
2146 if (*ptr == '\\' && ptr[1] != NUL)
2147 ++ptr;
2149 do_quotes &= 1; /* result is 1 with even number of quotes */
2152 * If we find an uneven count, check current line and previous
2153 * one for a '\' at the end.
2155 if (!do_quotes)
2157 inquote = FALSE;
2158 if (ptr[-1] == '\\')
2160 do_quotes = 1;
2161 if (start_in_quotes == MAYBE)
2163 /* Do we need to use at_start here? */
2164 inquote = TRUE;
2165 start_in_quotes = TRUE;
2167 else if (backwards)
2168 inquote = TRUE;
2170 if (pos.lnum > 1)
2172 ptr = ml_get(pos.lnum - 1);
2173 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2175 do_quotes = 1;
2176 if (start_in_quotes == MAYBE)
2178 inquote = at_start;
2179 if (inquote)
2180 start_in_quotes = TRUE;
2182 else if (!backwards)
2183 inquote = TRUE;
2186 /* ml_get() only keeps one line, need to get linep again */
2187 linep = ml_get(pos.lnum);
2191 if (start_in_quotes == MAYBE)
2192 start_in_quotes = FALSE;
2195 * If 'smartmatch' is set:
2196 * Things inside quotes are ignored by setting 'inquote'. If we
2197 * find a quote without a preceding '\' invert 'inquote'. At the
2198 * end of a line not ending in '\' we reset 'inquote'.
2200 * In lines with an uneven number of quotes (without preceding '\')
2201 * we do not know which part to ignore. Therefore we only set
2202 * inquote if the number of quotes in a line is even, unless this
2203 * line or the previous one ends in a '\'. Complicated, isn't it?
2205 switch (c = linep[pos.col])
2207 case NUL:
2208 /* at end of line without trailing backslash, reset inquote */
2209 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2211 inquote = FALSE;
2212 start_in_quotes = FALSE;
2214 break;
2216 case '"':
2217 /* a quote that is preceded with an odd number of backslashes is
2218 * ignored */
2219 if (do_quotes)
2221 int col;
2223 for (col = pos.col - 1; col >= 0; --col)
2224 if (linep[col] != '\\')
2225 break;
2226 if ((((int)pos.col - 1 - col) & 1) == 0)
2228 inquote = !inquote;
2229 start_in_quotes = FALSE;
2232 break;
2235 * If smart matching ('cpoptions' does not contain '%'):
2236 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2237 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2238 * skipped, there is never a brace in them.
2239 * Ignore this when finding matches for `'.
2241 case '\'':
2242 if (!cpo_match && initc != '\'' && findc != '\'')
2244 if (backwards)
2246 if (pos.col > 1)
2248 if (linep[pos.col - 2] == '\'')
2250 pos.col -= 2;
2251 break;
2253 else if (linep[pos.col - 2] == '\\' &&
2254 pos.col > 2 && linep[pos.col - 3] == '\'')
2256 pos.col -= 3;
2257 break;
2261 else if (linep[pos.col + 1]) /* forward search */
2263 if (linep[pos.col + 1] == '\\' &&
2264 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2266 pos.col += 3;
2267 break;
2269 else if (linep[pos.col + 2] == '\'')
2271 pos.col += 2;
2272 break;
2276 /* FALLTHROUGH */
2278 default:
2279 #ifdef FEAT_LISP
2281 * For Lisp skip over backslashed (), {} and [].
2282 * (actually, we skip #\( et al)
2284 if (curbuf->b_p_lisp
2285 && vim_strchr((char_u *)"(){}[]", c) != NULL
2286 && pos.col > 1
2287 && check_prevcol(linep, pos.col, '\\', NULL)
2288 && check_prevcol(linep, pos.col - 1, '#', NULL))
2289 break;
2290 #endif
2292 /* Check for match outside of quotes, and inside of
2293 * quotes when the start is also inside of quotes. */
2294 if ((!inquote || start_in_quotes == TRUE)
2295 && (c == initc || c == findc))
2297 int col, bslcnt = 0;
2299 if (!cpo_bsl)
2301 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2302 bslcnt++;
2304 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2305 * what we expect. */
2306 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2308 if (c == initc)
2309 count++;
2310 else
2312 if (count == 0)
2313 return &pos;
2314 count--;
2321 if (comment_dir == BACKWARD && count > 0)
2323 pos = match_pos;
2324 return &pos;
2326 return (pos_T *)NULL; /* never found it */
2330 * Check if line[] contains a / / comment.
2331 * Return MAXCOL if not, otherwise return the column.
2332 * TODO: skip strings.
2334 static int
2335 check_linecomment(line)
2336 char_u *line;
2338 char_u *p;
2340 p = line;
2341 #ifdef FEAT_LISP
2342 /* skip Lispish one-line comments */
2343 if (curbuf->b_p_lisp)
2345 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2347 int instr = FALSE; /* inside of string */
2349 p = line; /* scan from start */
2350 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
2352 if (*p == '"')
2354 if (instr)
2356 if (*(p - 1) != '\\') /* skip escaped quote */
2357 instr = FALSE;
2359 else if (p == line || ((p - line) >= 2
2360 /* skip #\" form */
2361 && *(p - 1) != '\\' && *(p - 2) != '#'))
2362 instr = TRUE;
2364 else if (!instr && ((p - line) < 2
2365 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2366 break; /* found! */
2367 ++p;
2370 else
2371 p = NULL;
2373 else
2374 #endif
2375 while ((p = vim_strchr(p, '/')) != NULL)
2377 /* accept a double /, unless it's preceded with * and followed by *,
2378 * because * / / * is an end and start of a C comment */
2379 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
2380 break;
2381 ++p;
2384 if (p == NULL)
2385 return MAXCOL;
2386 return (int)(p - line);
2390 * Move cursor briefly to character matching the one under the cursor.
2391 * Used for Insert mode and "r" command.
2392 * Show the match only if it is visible on the screen.
2393 * If there isn't a match, then beep.
2395 void
2396 showmatch(c)
2397 int c; /* char to show match for */
2399 pos_T *lpos, save_cursor;
2400 pos_T mpos;
2401 colnr_T vcol;
2402 long save_so;
2403 long save_siso;
2404 #ifdef CURSOR_SHAPE
2405 int save_state;
2406 #endif
2407 colnr_T save_dollar_vcol;
2408 char_u *p;
2411 * Only show match for chars in the 'matchpairs' option.
2413 /* 'matchpairs' is "x:y,x:y" */
2414 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2416 #ifdef FEAT_RIGHTLEFT
2417 if (*p == c && (curwin->w_p_rl ^ p_ri))
2418 break;
2419 #endif
2420 p += 2;
2421 if (*p == c
2422 #ifdef FEAT_RIGHTLEFT
2423 && !(curwin->w_p_rl ^ p_ri)
2424 #endif
2426 break;
2427 if (p[1] != ',')
2428 return;
2431 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2432 vim_beep();
2433 else if (lpos->lnum >= curwin->w_topline)
2435 if (!curwin->w_p_wrap)
2436 getvcol(curwin, lpos, NULL, &vcol, NULL);
2437 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2438 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2440 mpos = *lpos; /* save the pos, update_screen() may change it */
2441 save_cursor = curwin->w_cursor;
2442 save_so = p_so;
2443 save_siso = p_siso;
2444 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2445 * stop displaying the "$". */
2446 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2447 dollar_vcol = 0;
2448 ++curwin->w_virtcol; /* do display ')' just before "$" */
2449 update_screen(VALID); /* show the new char first */
2451 save_dollar_vcol = dollar_vcol;
2452 #ifdef CURSOR_SHAPE
2453 save_state = State;
2454 State = SHOWMATCH;
2455 ui_cursor_shape(); /* may show different cursor shape */
2456 #endif
2457 curwin->w_cursor = mpos; /* move to matching char */
2458 p_so = 0; /* don't use 'scrolloff' here */
2459 p_siso = 0; /* don't use 'sidescrolloff' here */
2460 showruler(FALSE);
2461 setcursor();
2462 cursor_on(); /* make sure that the cursor is shown */
2463 out_flush();
2464 #ifdef FEAT_GUI
2465 if (gui.in_use)
2467 gui_update_cursor(TRUE, FALSE);
2468 gui_mch_flush();
2470 #endif
2471 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2472 * which resets it if the matching position is in a previous line
2473 * and has a higher column number. */
2474 dollar_vcol = save_dollar_vcol;
2477 * brief pause, unless 'm' is present in 'cpo' and a character is
2478 * available.
2480 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2481 ui_delay(p_mat * 100L, TRUE);
2482 else if (!char_avail())
2483 ui_delay(p_mat * 100L, FALSE);
2484 curwin->w_cursor = save_cursor; /* restore cursor position */
2485 p_so = save_so;
2486 p_siso = save_siso;
2487 #ifdef CURSOR_SHAPE
2488 State = save_state;
2489 ui_cursor_shape(); /* may show different cursor shape */
2490 #endif
2496 * findsent(dir, count) - Find the start of the next sentence in direction
2497 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
2498 * space or a line break. Also stop at an empty line.
2499 * Return OK if the next sentence was found.
2502 findsent(dir, count)
2503 int dir;
2504 long count;
2506 pos_T pos, tpos;
2507 int c;
2508 int (*func) __ARGS((pos_T *));
2509 int startlnum;
2510 int noskip = FALSE; /* do not skip blanks */
2511 int cpo_J;
2512 int found_dot;
2514 pos = curwin->w_cursor;
2515 if (dir == FORWARD)
2516 func = incl;
2517 else
2518 func = decl;
2520 while (count--)
2523 * if on an empty line, skip upto a non-empty line
2525 if (gchar_pos(&pos) == NUL)
2528 if ((*func)(&pos) == -1)
2529 break;
2530 while (gchar_pos(&pos) == NUL);
2531 if (dir == FORWARD)
2532 goto found;
2535 * if on the start of a paragraph or a section and searching forward,
2536 * go to the next line
2538 else if (dir == FORWARD && pos.col == 0 &&
2539 startPS(pos.lnum, NUL, FALSE))
2541 if (pos.lnum == curbuf->b_ml.ml_line_count)
2542 return FAIL;
2543 ++pos.lnum;
2544 goto found;
2546 else if (dir == BACKWARD)
2547 decl(&pos);
2549 /* go back to the previous non-blank char */
2550 found_dot = FALSE;
2551 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2552 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2554 if (vim_strchr((char_u *)".!?", c) != NULL)
2556 /* Only skip over a '.', '!' and '?' once. */
2557 if (found_dot)
2558 break;
2559 found_dot = TRUE;
2561 if (decl(&pos) == -1)
2562 break;
2563 /* when going forward: Stop in front of empty line */
2564 if (lineempty(pos.lnum) && dir == FORWARD)
2566 incl(&pos);
2567 goto found;
2571 /* remember the line where the search started */
2572 startlnum = pos.lnum;
2573 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2575 for (;;) /* find end of sentence */
2577 c = gchar_pos(&pos);
2578 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2580 if (dir == BACKWARD && pos.lnum != startlnum)
2581 ++pos.lnum;
2582 break;
2584 if (c == '.' || c == '!' || c == '?')
2586 tpos = pos;
2588 if ((c = inc(&tpos)) == -1)
2589 break;
2590 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2591 != NULL);
2592 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2593 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2594 && gchar_pos(&tpos) == ' ')))
2596 pos = tpos;
2597 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2598 inc(&pos);
2599 break;
2602 if ((*func)(&pos) == -1)
2604 if (count)
2605 return FAIL;
2606 noskip = TRUE;
2607 break;
2610 found:
2611 /* skip white space */
2612 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2613 if (incl(&pos) == -1)
2614 break;
2617 setpcmark();
2618 curwin->w_cursor = pos;
2619 return OK;
2623 * Find the next paragraph or section in direction 'dir'.
2624 * Paragraphs are currently supposed to be separated by empty lines.
2625 * If 'what' is NUL we go to the next paragraph.
2626 * If 'what' is '{' or '}' we go to the next section.
2627 * If 'both' is TRUE also stop at '}'.
2628 * Return TRUE if the next paragraph or section was found.
2631 findpar(pincl, dir, count, what, both)
2632 int *pincl; /* Return: TRUE if last char is to be included */
2633 int dir;
2634 long count;
2635 int what;
2636 int both;
2638 linenr_T curr;
2639 int did_skip; /* TRUE after separating lines have been skipped */
2640 int first; /* TRUE on first line */
2641 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
2642 #ifdef FEAT_FOLDING
2643 linenr_T fold_first; /* first line of a closed fold */
2644 linenr_T fold_last; /* last line of a closed fold */
2645 int fold_skipped; /* TRUE if a closed fold was skipped this
2646 iteration */
2647 #endif
2649 curr = curwin->w_cursor.lnum;
2651 while (count--)
2653 did_skip = FALSE;
2654 for (first = TRUE; ; first = FALSE)
2656 if (*ml_get(curr) != NUL)
2657 did_skip = TRUE;
2659 #ifdef FEAT_FOLDING
2660 /* skip folded lines */
2661 fold_skipped = FALSE;
2662 if (first && hasFolding(curr, &fold_first, &fold_last))
2664 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2665 fold_skipped = TRUE;
2667 #endif
2669 /* POSIX has it's own ideas of what a paragraph boundary is and it
2670 * doesn't match historical Vi: It also stops at a "{" in the
2671 * first column and at an empty line. */
2672 if (!first && did_skip && (startPS(curr, what, both)
2673 || (posix && what == NUL && *ml_get(curr) == '{')))
2674 break;
2676 #ifdef FEAT_FOLDING
2677 if (fold_skipped)
2678 curr -= dir;
2679 #endif
2680 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2682 if (count)
2683 return FALSE;
2684 curr -= dir;
2685 break;
2689 setpcmark();
2690 if (both && *ml_get(curr) == '}') /* include line with '}' */
2691 ++curr;
2692 curwin->w_cursor.lnum = curr;
2693 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2695 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2697 --curwin->w_cursor.col;
2698 *pincl = TRUE;
2701 else
2702 curwin->w_cursor.col = 0;
2703 return TRUE;
2707 * check if the string 's' is a nroff macro that is in option 'opt'
2709 static int
2710 inmacro(opt, s)
2711 char_u *opt;
2712 char_u *s;
2714 char_u *macro;
2716 for (macro = opt; macro[0]; ++macro)
2718 /* Accept two characters in the option being equal to two characters
2719 * in the line. A space in the option matches with a space in the
2720 * line or the line having ended. */
2721 if ( (macro[0] == s[0]
2722 || (macro[0] == ' '
2723 && (s[0] == NUL || s[0] == ' ')))
2724 && (macro[1] == s[1]
2725 || ((macro[1] == NUL || macro[1] == ' ')
2726 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2727 break;
2728 ++macro;
2729 if (macro[0] == NUL)
2730 break;
2732 return (macro[0] != NUL);
2736 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2737 * If 'para' is '{' or '}' only check for sections.
2738 * If 'both' is TRUE also stop at '}'
2741 startPS(lnum, para, both)
2742 linenr_T lnum;
2743 int para;
2744 int both;
2746 char_u *s;
2748 s = ml_get(lnum);
2749 if (*s == para || *s == '\f' || (both && *s == '}'))
2750 return TRUE;
2751 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2752 (!para && inmacro(p_para, s + 1))))
2753 return TRUE;
2754 return FALSE;
2758 * The following routines do the word searches performed by the 'w', 'W',
2759 * 'b', 'B', 'e', and 'E' commands.
2763 * To perform these searches, characters are placed into one of three
2764 * classes, and transitions between classes determine word boundaries.
2766 * The classes are:
2768 * 0 - white space
2769 * 1 - punctuation
2770 * 2 or higher - keyword characters (letters, digits and underscore)
2773 static int cls_bigword; /* TRUE for "W", "B" or "E" */
2776 * cls() - returns the class of character at curwin->w_cursor
2778 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2779 * from class 2 and higher are reported as class 1 since only white space
2780 * boundaries are of interest.
2782 static int
2783 cls()
2785 int c;
2787 c = gchar_cursor();
2788 #ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2789 if (p_altkeymap && c == F_BLANK)
2790 return 0;
2791 #endif
2792 if (c == ' ' || c == '\t' || c == NUL)
2793 return 0;
2794 #ifdef FEAT_MBYTE
2795 if (enc_dbcs != 0 && c > 0xFF)
2797 /* If cls_bigword, report multi-byte chars as class 1. */
2798 if (enc_dbcs == DBCS_KOR && cls_bigword)
2799 return 1;
2801 /* process code leading/trailing bytes */
2802 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2804 if (enc_utf8)
2806 c = utf_class(c);
2807 if (c != 0 && cls_bigword)
2808 return 1;
2809 return c;
2811 #endif
2813 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2814 if (cls_bigword)
2815 return 1;
2817 if (vim_iswordc(c))
2818 return 2;
2819 return 1;
2824 * fwd_word(count, type, eol) - move forward one word
2826 * Returns FAIL if the cursor was already at the end of the file.
2827 * If eol is TRUE, last word stops at end of line (for operators).
2830 fwd_word(count, bigword, eol)
2831 long count;
2832 int bigword; /* "W", "E" or "B" */
2833 int eol;
2835 int sclass; /* starting class */
2836 int i;
2837 int last_line;
2839 #ifdef FEAT_VIRTUALEDIT
2840 curwin->w_cursor.coladd = 0;
2841 #endif
2842 cls_bigword = bigword;
2843 while (--count >= 0)
2845 #ifdef FEAT_FOLDING
2846 /* When inside a range of folded lines, move to the last char of the
2847 * last line. */
2848 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2849 coladvance((colnr_T)MAXCOL);
2850 #endif
2851 sclass = cls();
2854 * We always move at least one character, unless on the last
2855 * character in the buffer.
2857 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2858 i = inc_cursor();
2859 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2860 return FAIL;
2861 if (i >= 1 && eol && count == 0) /* started at last char in line */
2862 return OK;
2865 * Go one char past end of current word (if any)
2867 if (sclass != 0)
2868 while (cls() == sclass)
2870 i = inc_cursor();
2871 if (i == -1 || (i >= 1 && eol && count == 0))
2872 return OK;
2876 * go to next non-white
2878 while (cls() == 0)
2881 * We'll stop if we land on a blank line
2883 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2884 break;
2886 i = inc_cursor();
2887 if (i == -1 || (i >= 1 && eol && count == 0))
2888 return OK;
2891 return OK;
2895 * bck_word() - move backward 'count' words
2897 * If stop is TRUE and we are already on the start of a word, move one less.
2899 * Returns FAIL if top of the file was reached.
2902 bck_word(count, bigword, stop)
2903 long count;
2904 int bigword;
2905 int stop;
2907 int sclass; /* starting class */
2909 #ifdef FEAT_VIRTUALEDIT
2910 curwin->w_cursor.coladd = 0;
2911 #endif
2912 cls_bigword = bigword;
2913 while (--count >= 0)
2915 #ifdef FEAT_FOLDING
2916 /* When inside a range of folded lines, move to the first char of the
2917 * first line. */
2918 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2919 curwin->w_cursor.col = 0;
2920 #endif
2921 sclass = cls();
2922 if (dec_cursor() == -1) /* started at start of file */
2923 return FAIL;
2925 if (!stop || sclass == cls() || sclass == 0)
2928 * Skip white space before the word.
2929 * Stop on an empty line.
2931 while (cls() == 0)
2933 if (curwin->w_cursor.col == 0
2934 && lineempty(curwin->w_cursor.lnum))
2935 goto finished;
2936 if (dec_cursor() == -1) /* hit start of file, stop here */
2937 return OK;
2941 * Move backward to start of this word.
2943 if (skip_chars(cls(), BACKWARD))
2944 return OK;
2947 inc_cursor(); /* overshot - forward one */
2948 finished:
2949 stop = FALSE;
2951 return OK;
2955 * end_word() - move to the end of the word
2957 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2958 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2959 * motion crosses blank lines. When the real vi crosses a blank line in an
2960 * 'e' motion, the cursor is placed on the FIRST character of the next
2961 * non-blank line. The 'E' command, however, works correctly. Since this
2962 * appears to be a bug, I have not duplicated it here.
2964 * Returns FAIL if end of the file was reached.
2966 * If stop is TRUE and we are already on the end of a word, move one less.
2967 * If empty is TRUE stop on an empty line.
2970 end_word(count, bigword, stop, empty)
2971 long count;
2972 int bigword;
2973 int stop;
2974 int empty;
2976 int sclass; /* starting class */
2978 #ifdef FEAT_VIRTUALEDIT
2979 curwin->w_cursor.coladd = 0;
2980 #endif
2981 cls_bigword = bigword;
2982 while (--count >= 0)
2984 #ifdef FEAT_FOLDING
2985 /* When inside a range of folded lines, move to the last char of the
2986 * last line. */
2987 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2988 coladvance((colnr_T)MAXCOL);
2989 #endif
2990 sclass = cls();
2991 if (inc_cursor() == -1)
2992 return FAIL;
2995 * If we're in the middle of a word, we just have to move to the end
2996 * of it.
2998 if (cls() == sclass && sclass != 0)
3001 * Move forward to end of the current word
3003 if (skip_chars(sclass, FORWARD))
3004 return FAIL;
3006 else if (!stop || sclass == 0)
3009 * We were at the end of a word. Go to the end of the next word.
3010 * First skip white space, if 'empty' is TRUE, stop at empty line.
3012 while (cls() == 0)
3014 if (empty && curwin->w_cursor.col == 0
3015 && lineempty(curwin->w_cursor.lnum))
3016 goto finished;
3017 if (inc_cursor() == -1) /* hit end of file, stop here */
3018 return FAIL;
3022 * Move forward to the end of this word.
3024 if (skip_chars(cls(), FORWARD))
3025 return FAIL;
3027 dec_cursor(); /* overshot - one char backward */
3028 finished:
3029 stop = FALSE; /* we move only one word less */
3031 return OK;
3035 * Move back to the end of the word.
3037 * Returns FAIL if start of the file was reached.
3040 bckend_word(count, bigword, eol)
3041 long count;
3042 int bigword; /* TRUE for "B" */
3043 int eol; /* TRUE: stop at end of line. */
3045 int sclass; /* starting class */
3046 int i;
3048 #ifdef FEAT_VIRTUALEDIT
3049 curwin->w_cursor.coladd = 0;
3050 #endif
3051 cls_bigword = bigword;
3052 while (--count >= 0)
3054 sclass = cls();
3055 if ((i = dec_cursor()) == -1)
3056 return FAIL;
3057 if (eol && i == 1)
3058 return OK;
3061 * Move backward to before the start of this word.
3063 if (sclass != 0)
3065 while (cls() == sclass)
3066 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3067 return OK;
3071 * Move backward to end of the previous word
3073 while (cls() == 0)
3075 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3076 break;
3077 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3078 return OK;
3081 return OK;
3085 * Skip a row of characters of the same class.
3086 * Return TRUE when end-of-file reached, FALSE otherwise.
3088 static int
3089 skip_chars(cclass, dir)
3090 int cclass;
3091 int dir;
3093 while (cls() == cclass)
3094 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3095 return TRUE;
3096 return FALSE;
3099 #ifdef FEAT_TEXTOBJ
3101 * Go back to the start of the word or the start of white space
3103 static void
3104 back_in_line()
3106 int sclass; /* starting class */
3108 sclass = cls();
3109 for (;;)
3111 if (curwin->w_cursor.col == 0) /* stop at start of line */
3112 break;
3113 dec_cursor();
3114 if (cls() != sclass) /* stop at start of word */
3116 inc_cursor();
3117 break;
3122 static void
3123 find_first_blank(posp)
3124 pos_T *posp;
3126 int c;
3128 while (decl(posp) != -1)
3130 c = gchar_pos(posp);
3131 if (!vim_iswhite(c))
3133 incl(posp);
3134 break;
3140 * Skip count/2 sentences and count/2 separating white spaces.
3142 static void
3143 findsent_forward(count, at_start_sent)
3144 long count;
3145 int at_start_sent; /* cursor is at start of sentence */
3147 while (count--)
3149 findsent(FORWARD, 1L);
3150 if (at_start_sent)
3151 find_first_blank(&curwin->w_cursor);
3152 if (count == 0 || at_start_sent)
3153 decl(&curwin->w_cursor);
3154 at_start_sent = !at_start_sent;
3159 * Find word under cursor, cursor at end.
3160 * Used while an operator is pending, and in Visual mode.
3163 current_word(oap, count, include, bigword)
3164 oparg_T *oap;
3165 long count;
3166 int include; /* TRUE: include word and white space */
3167 int bigword; /* FALSE == word, TRUE == WORD */
3169 pos_T start_pos;
3170 pos_T pos;
3171 int inclusive = TRUE;
3172 int include_white = FALSE;
3174 cls_bigword = bigword;
3175 clearpos(&start_pos);
3177 #ifdef FEAT_VISUAL
3178 /* Correct cursor when 'selection' is exclusive */
3179 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3180 dec_cursor();
3183 * When Visual mode is not active, or when the VIsual area is only one
3184 * character, select the word and/or white space under the cursor.
3186 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3187 #endif
3190 * Go to start of current word or white space.
3192 back_in_line();
3193 start_pos = curwin->w_cursor;
3196 * If the start is on white space, and white space should be included
3197 * (" word"), or start is not on white space, and white space should
3198 * not be included ("word"), find end of word.
3200 if ((cls() == 0) == include)
3202 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3203 return FAIL;
3205 else
3208 * If the start is not on white space, and white space should be
3209 * included ("word "), or start is on white space and white
3210 * space should not be included (" "), find start of word.
3211 * If we end up in the first column of the next line (single char
3212 * word) back up to end of the line.
3214 fwd_word(1L, bigword, TRUE);
3215 if (curwin->w_cursor.col == 0)
3216 decl(&curwin->w_cursor);
3217 else
3218 oneleft();
3220 if (include)
3221 include_white = TRUE;
3224 #ifdef FEAT_VISUAL
3225 if (VIsual_active)
3227 /* should do something when inclusive == FALSE ! */
3228 VIsual = start_pos;
3229 redraw_curbuf_later(INVERTED); /* update the inversion */
3231 else
3232 #endif
3234 oap->start = start_pos;
3235 oap->motion_type = MCHAR;
3237 --count;
3241 * When count is still > 0, extend with more objects.
3243 while (count > 0)
3245 inclusive = TRUE;
3246 #ifdef FEAT_VISUAL
3247 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3250 * In Visual mode, with cursor at start: move cursor back.
3252 if (decl(&curwin->w_cursor) == -1)
3253 return FAIL;
3254 if (include != (cls() != 0))
3256 if (bck_word(1L, bigword, TRUE) == FAIL)
3257 return FAIL;
3259 else
3261 if (bckend_word(1L, bigword, TRUE) == FAIL)
3262 return FAIL;
3263 (void)incl(&curwin->w_cursor);
3266 else
3267 #endif
3270 * Move cursor forward one word and/or white area.
3272 if (incl(&curwin->w_cursor) == -1)
3273 return FAIL;
3274 if (include != (cls() == 0))
3276 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
3277 return FAIL;
3279 * If end is just past a new-line, we don't want to include
3280 * the first character on the line.
3281 * Put cursor on last char of white.
3283 if (oneleft() == FAIL)
3284 inclusive = FALSE;
3286 else
3288 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3289 return FAIL;
3292 --count;
3295 if (include_white && (cls() != 0
3296 || (curwin->w_cursor.col == 0 && !inclusive)))
3299 * If we don't include white space at the end, move the start
3300 * to include some white space there. This makes "daw" work
3301 * better on the last word in a sentence (and "2daw" on last-but-one
3302 * word). Also when "2daw" deletes "word." at the end of the line
3303 * (cursor is at start of next line).
3304 * But don't delete white space at start of line (indent).
3306 pos = curwin->w_cursor; /* save cursor position */
3307 curwin->w_cursor = start_pos;
3308 if (oneleft() == OK)
3310 back_in_line();
3311 if (cls() == 0 && curwin->w_cursor.col > 0)
3313 #ifdef FEAT_VISUAL
3314 if (VIsual_active)
3315 VIsual = curwin->w_cursor;
3316 else
3317 #endif
3318 oap->start = curwin->w_cursor;
3321 curwin->w_cursor = pos; /* put cursor back at end */
3324 #ifdef FEAT_VISUAL
3325 if (VIsual_active)
3327 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3328 inc_cursor();
3329 if (VIsual_mode == 'V')
3331 VIsual_mode = 'v';
3332 redraw_cmdline = TRUE; /* show mode later */
3335 else
3336 #endif
3337 oap->inclusive = inclusive;
3339 return OK;
3343 * Find sentence(s) under the cursor, cursor at end.
3344 * When Visual active, extend it by one or more sentences.
3347 current_sent(oap, count, include)
3348 oparg_T *oap;
3349 long count;
3350 int include;
3352 pos_T start_pos;
3353 pos_T pos;
3354 int start_blank;
3355 int c;
3356 int at_start_sent;
3357 long ncount;
3359 start_pos = curwin->w_cursor;
3360 pos = start_pos;
3361 findsent(FORWARD, 1L); /* Find start of next sentence. */
3363 #ifdef FEAT_VISUAL
3365 * When visual area is bigger than one character: Extend it.
3367 if (VIsual_active && !equalpos(start_pos, VIsual))
3369 extend:
3370 if (lt(start_pos, VIsual))
3373 * Cursor at start of Visual area.
3374 * Find out where we are:
3375 * - in the white space before a sentence
3376 * - in a sentence or just after it
3377 * - at the start of a sentence
3379 at_start_sent = TRUE;
3380 decl(&pos);
3381 while (lt(pos, curwin->w_cursor))
3383 c = gchar_pos(&pos);
3384 if (!vim_iswhite(c))
3386 at_start_sent = FALSE;
3387 break;
3389 incl(&pos);
3391 if (!at_start_sent)
3393 findsent(BACKWARD, 1L);
3394 if (equalpos(curwin->w_cursor, start_pos))
3395 at_start_sent = TRUE; /* exactly at start of sentence */
3396 else
3397 /* inside a sentence, go to its end (start of next) */
3398 findsent(FORWARD, 1L);
3400 if (include) /* "as" gets twice as much as "is" */
3401 count *= 2;
3402 while (count--)
3404 if (at_start_sent)
3405 find_first_blank(&curwin->w_cursor);
3406 c = gchar_cursor();
3407 if (!at_start_sent || (!include && !vim_iswhite(c)))
3408 findsent(BACKWARD, 1L);
3409 at_start_sent = !at_start_sent;
3412 else
3415 * Cursor at end of Visual area.
3416 * Find out where we are:
3417 * - just before a sentence
3418 * - just before or in the white space before a sentence
3419 * - in a sentence
3421 incl(&pos);
3422 at_start_sent = TRUE;
3423 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3425 at_start_sent = FALSE;
3426 while (lt(pos, curwin->w_cursor))
3428 c = gchar_pos(&pos);
3429 if (!vim_iswhite(c))
3431 at_start_sent = TRUE;
3432 break;
3434 incl(&pos);
3436 if (at_start_sent) /* in the sentence */
3437 findsent(BACKWARD, 1L);
3438 else /* in/before white before a sentence */
3439 curwin->w_cursor = start_pos;
3442 if (include) /* "as" gets twice as much as "is" */
3443 count *= 2;
3444 findsent_forward(count, at_start_sent);
3445 if (*p_sel == 'e')
3446 ++curwin->w_cursor.col;
3448 return OK;
3450 #endif
3453 * If cursor started on blank, check if it is just before the start of the
3454 * next sentence.
3456 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3457 incl(&pos);
3458 if (equalpos(pos, curwin->w_cursor))
3460 start_blank = TRUE;
3461 find_first_blank(&start_pos); /* go back to first blank */
3463 else
3465 start_blank = FALSE;
3466 findsent(BACKWARD, 1L);
3467 start_pos = curwin->w_cursor;
3469 if (include)
3470 ncount = count * 2;
3471 else
3473 ncount = count;
3474 if (start_blank)
3475 --ncount;
3477 if (ncount > 0)
3478 findsent_forward(ncount, TRUE);
3479 else
3480 decl(&curwin->w_cursor);
3482 if (include)
3485 * If the blank in front of the sentence is included, exclude the
3486 * blanks at the end of the sentence, go back to the first blank.
3487 * If there are no trailing blanks, try to include leading blanks.
3489 if (start_blank)
3491 find_first_blank(&curwin->w_cursor);
3492 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3493 if (vim_iswhite(c))
3494 decl(&curwin->w_cursor);
3496 else if (c = gchar_cursor(), !vim_iswhite(c))
3497 find_first_blank(&start_pos);
3500 #ifdef FEAT_VISUAL
3501 if (VIsual_active)
3503 /* avoid getting stuck with "is" on a single space before a sent. */
3504 if (equalpos(start_pos, curwin->w_cursor))
3505 goto extend;
3506 if (*p_sel == 'e')
3507 ++curwin->w_cursor.col;
3508 VIsual = start_pos;
3509 VIsual_mode = 'v';
3510 redraw_curbuf_later(INVERTED); /* update the inversion */
3512 else
3513 #endif
3515 /* include a newline after the sentence, if there is one */
3516 if (incl(&curwin->w_cursor) == -1)
3517 oap->inclusive = TRUE;
3518 else
3519 oap->inclusive = FALSE;
3520 oap->start = start_pos;
3521 oap->motion_type = MCHAR;
3523 return OK;
3527 * Find block under the cursor, cursor at end.
3528 * "what" and "other" are two matching parenthesis/paren/etc.
3531 current_block(oap, count, include, what, other)
3532 oparg_T *oap;
3533 long count;
3534 int include; /* TRUE == include white space */
3535 int what; /* '(', '{', etc. */
3536 int other; /* ')', '}', etc. */
3538 pos_T old_pos;
3539 pos_T *pos = NULL;
3540 pos_T start_pos;
3541 pos_T *end_pos;
3542 pos_T old_start, old_end;
3543 char_u *save_cpo;
3544 int sol = FALSE; /* '{' at start of line */
3546 old_pos = curwin->w_cursor;
3547 old_end = curwin->w_cursor; /* remember where we started */
3548 old_start = old_end;
3551 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3553 #ifdef FEAT_VISUAL
3554 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3555 #endif
3557 setpcmark();
3558 if (what == '{') /* ignore indent */
3559 while (inindent(1))
3560 if (inc_cursor() != 0)
3561 break;
3562 if (gchar_cursor() == what)
3563 /* cursor on '(' or '{', move cursor just after it */
3564 ++curwin->w_cursor.col;
3566 #ifdef FEAT_VISUAL
3567 else if (lt(VIsual, curwin->w_cursor))
3569 old_start = VIsual;
3570 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3572 else
3573 old_end = VIsual;
3574 #endif
3577 * Search backwards for unclosed '(', '{', etc..
3578 * Put this position in start_pos.
3579 * Ignore quotes here.
3581 save_cpo = p_cpo;
3582 p_cpo = (char_u *)"%";
3583 while (count-- > 0)
3585 if ((pos = findmatch(NULL, what)) == NULL)
3586 break;
3587 curwin->w_cursor = *pos;
3588 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3590 p_cpo = save_cpo;
3593 * Search for matching ')', '}', etc.
3594 * Put this position in curwin->w_cursor.
3596 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3598 curwin->w_cursor = old_pos;
3599 return FAIL;
3601 curwin->w_cursor = *end_pos;
3604 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3605 * If the ending '}' is only preceded by indent, skip that indent.
3606 * But only if the resulting area is not smaller than what we started with.
3608 while (!include)
3610 incl(&start_pos);
3611 sol = (curwin->w_cursor.col == 0);
3612 decl(&curwin->w_cursor);
3613 if (what == '{')
3614 while (inindent(1))
3616 sol = TRUE;
3617 if (decl(&curwin->w_cursor) != 0)
3618 break;
3620 #ifdef FEAT_VISUAL
3622 * In Visual mode, when the resulting area is not bigger than what we
3623 * started with, extend it to the next block, and then exclude again.
3625 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3626 && VIsual_active)
3628 curwin->w_cursor = old_start;
3629 decl(&curwin->w_cursor);
3630 if ((pos = findmatch(NULL, what)) == NULL)
3632 curwin->w_cursor = old_pos;
3633 return FAIL;
3635 start_pos = *pos;
3636 curwin->w_cursor = *pos;
3637 if ((end_pos = findmatch(NULL, other)) == NULL)
3639 curwin->w_cursor = old_pos;
3640 return FAIL;
3642 curwin->w_cursor = *end_pos;
3644 else
3645 #endif
3646 break;
3649 #ifdef FEAT_VISUAL
3650 if (VIsual_active)
3652 if (*p_sel == 'e')
3653 ++curwin->w_cursor.col;
3654 if (sol && gchar_cursor() != NUL)
3655 inc(&curwin->w_cursor); /* include the line break */
3656 VIsual = start_pos;
3657 VIsual_mode = 'v';
3658 redraw_curbuf_later(INVERTED); /* update the inversion */
3659 showmode();
3661 else
3662 #endif
3664 oap->start = start_pos;
3665 oap->motion_type = MCHAR;
3666 oap->inclusive = FALSE;
3667 if (sol)
3668 incl(&curwin->w_cursor);
3669 else if (ltoreq(start_pos, curwin->w_cursor))
3670 /* Include the character under the cursor. */
3671 oap->inclusive = TRUE;
3672 else
3673 /* End is before the start (no text in between <>, [], etc.): don't
3674 * operate on any text. */
3675 curwin->w_cursor = start_pos;
3678 return OK;
3681 static int in_html_tag __ARGS((int));
3684 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3685 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3687 static int
3688 in_html_tag(end_tag)
3689 int end_tag;
3691 char_u *line = ml_get_curline();
3692 char_u *p;
3693 int c;
3694 int lc = NUL;
3695 pos_T pos;
3697 #ifdef FEAT_MBYTE
3698 if (enc_dbcs)
3700 char_u *lp = NULL;
3702 /* We search forward until the cursor, because searching backwards is
3703 * very slow for DBCS encodings. */
3704 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3705 if (*p == '>' || *p == '<')
3707 lc = *p;
3708 lp = p;
3710 if (*p != '<') /* check for '<' under cursor */
3712 if (lc != '<')
3713 return FALSE;
3714 p = lp;
3717 else
3718 #endif
3720 for (p = line + curwin->w_cursor.col; p > line; )
3722 if (*p == '<') /* find '<' under/before cursor */
3723 break;
3724 mb_ptr_back(line, p);
3725 if (*p == '>') /* find '>' before cursor */
3726 break;
3728 if (*p != '<')
3729 return FALSE;
3732 pos.lnum = curwin->w_cursor.lnum;
3733 pos.col = (colnr_T)(p - line);
3735 mb_ptr_adv(p);
3736 if (end_tag)
3737 /* check that there is a '/' after the '<' */
3738 return *p == '/';
3740 /* check that there is no '/' after the '<' */
3741 if (*p == '/')
3742 return FALSE;
3744 /* check that the matching '>' is not preceded by '/' */
3745 for (;;)
3747 if (inc(&pos) < 0)
3748 return FALSE;
3749 c = *ml_get_pos(&pos);
3750 if (c == '>')
3751 break;
3752 lc = c;
3754 return lc != '/';
3758 * Find tag block under the cursor, cursor at end.
3761 current_tagblock(oap, count_arg, include)
3762 oparg_T *oap;
3763 long count_arg;
3764 int include; /* TRUE == include white space */
3766 long count = count_arg;
3767 long n;
3768 pos_T old_pos;
3769 pos_T start_pos;
3770 pos_T end_pos;
3771 pos_T old_start, old_end;
3772 char_u *spat, *epat;
3773 char_u *p;
3774 char_u *cp;
3775 int len;
3776 int r;
3777 int do_include = include;
3778 int save_p_ws = p_ws;
3779 int retval = FAIL;
3781 p_ws = FALSE;
3783 old_pos = curwin->w_cursor;
3784 old_end = curwin->w_cursor; /* remember where we started */
3785 old_start = old_end;
3786 #ifdef FEAT_VISUAL
3787 if (!VIsual_active || *p_sel == 'e')
3788 #endif
3789 decl(&old_end); /* old_end is inclusive */
3792 * If we start on "<aaa>" select that block.
3794 #ifdef FEAT_VISUAL
3795 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3796 #endif
3798 setpcmark();
3800 /* ignore indent */
3801 while (inindent(1))
3802 if (inc_cursor() != 0)
3803 break;
3805 if (in_html_tag(FALSE))
3807 /* cursor on start tag, move to its '>' */
3808 while (*ml_get_cursor() != '>')
3809 if (inc_cursor() < 0)
3810 break;
3812 else if (in_html_tag(TRUE))
3814 /* cursor on end tag, move to just before it */
3815 while (*ml_get_cursor() != '<')
3816 if (dec_cursor() < 0)
3817 break;
3818 dec_cursor();
3819 old_end = curwin->w_cursor;
3822 #ifdef FEAT_VISUAL
3823 else if (lt(VIsual, curwin->w_cursor))
3825 old_start = VIsual;
3826 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3828 else
3829 old_end = VIsual;
3830 #endif
3832 again:
3834 * Search backwards for unclosed "<aaa>".
3835 * Put this position in start_pos.
3837 for (n = 0; n < count; ++n)
3839 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
3840 (char_u *)"",
3841 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
3842 NULL, (linenr_T)0, 0L) <= 0)
3844 curwin->w_cursor = old_pos;
3845 goto theend;
3848 start_pos = curwin->w_cursor;
3851 * Search for matching "</aaa>". First isolate the "aaa".
3853 inc_cursor();
3854 p = ml_get_cursor();
3855 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3857 len = (int)(cp - p);
3858 if (len == 0)
3860 curwin->w_cursor = old_pos;
3861 goto theend;
3863 spat = alloc(len + 29);
3864 epat = alloc(len + 9);
3865 if (spat == NULL || epat == NULL)
3867 vim_free(spat);
3868 vim_free(epat);
3869 curwin->w_cursor = old_pos;
3870 goto theend;
3872 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3873 sprintf((char *)epat, "</%.*s>\\c", len, p);
3875 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
3876 0, NULL, (linenr_T)0, 0L);
3878 vim_free(spat);
3879 vim_free(epat);
3881 if (r < 1 || lt(curwin->w_cursor, old_end))
3883 /* Can't find other end or it's before the previous end. Could be a
3884 * HTML tag that doesn't have a matching end. Search backwards for
3885 * another starting tag. */
3886 count = 1;
3887 curwin->w_cursor = start_pos;
3888 goto again;
3891 if (do_include || r < 1)
3893 /* Include up to the '>'. */
3894 while (*ml_get_cursor() != '>')
3895 if (inc_cursor() < 0)
3896 break;
3898 else
3900 /* Exclude the '<' of the end tag. */
3901 if (*ml_get_cursor() == '<')
3902 dec_cursor();
3904 end_pos = curwin->w_cursor;
3906 if (!do_include)
3908 /* Exclude the start tag. */
3909 curwin->w_cursor = start_pos;
3910 while (inc_cursor() >= 0)
3911 if (*ml_get_cursor() == '>')
3913 inc_cursor();
3914 start_pos = curwin->w_cursor;
3915 break;
3917 curwin->w_cursor = end_pos;
3919 /* If we now have the same text as before reset "do_include" and try
3920 * again. */
3921 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
3923 do_include = TRUE;
3924 curwin->w_cursor = old_start;
3925 count = count_arg;
3926 goto again;
3930 #ifdef FEAT_VISUAL
3931 if (VIsual_active)
3933 /* If the end is before the start there is no text between tags, select
3934 * the char under the cursor. */
3935 if (lt(end_pos, start_pos))
3936 curwin->w_cursor = start_pos;
3937 else if (*p_sel == 'e')
3938 ++curwin->w_cursor.col;
3939 VIsual = start_pos;
3940 VIsual_mode = 'v';
3941 redraw_curbuf_later(INVERTED); /* update the inversion */
3942 showmode();
3944 else
3945 #endif
3947 oap->start = start_pos;
3948 oap->motion_type = MCHAR;
3949 if (lt(end_pos, start_pos))
3951 /* End is before the start: there is no text between tags; operate
3952 * on an empty area. */
3953 curwin->w_cursor = start_pos;
3954 oap->inclusive = FALSE;
3956 else
3957 oap->inclusive = TRUE;
3959 retval = OK;
3961 theend:
3962 p_ws = save_p_ws;
3963 return retval;
3967 current_par(oap, count, include, type)
3968 oparg_T *oap;
3969 long count;
3970 int include; /* TRUE == include white space */
3971 int type; /* 'p' for paragraph, 'S' for section */
3973 linenr_T start_lnum;
3974 linenr_T end_lnum;
3975 int white_in_front;
3976 int dir;
3977 int start_is_white;
3978 int prev_start_is_white;
3979 int retval = OK;
3980 int do_white = FALSE;
3981 int t;
3982 int i;
3984 if (type == 'S') /* not implemented yet */
3985 return FAIL;
3987 start_lnum = curwin->w_cursor.lnum;
3989 #ifdef FEAT_VISUAL
3991 * When visual area is more than one line: extend it.
3993 if (VIsual_active && start_lnum != VIsual.lnum)
3995 extend:
3996 if (start_lnum < VIsual.lnum)
3997 dir = BACKWARD;
3998 else
3999 dir = FORWARD;
4000 for (i = count; --i >= 0; )
4002 if (start_lnum ==
4003 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4005 retval = FAIL;
4006 break;
4009 prev_start_is_white = -1;
4010 for (t = 0; t < 2; ++t)
4012 start_lnum += dir;
4013 start_is_white = linewhite(start_lnum);
4014 if (prev_start_is_white == start_is_white)
4016 start_lnum -= dir;
4017 break;
4019 for (;;)
4021 if (start_lnum == (dir == BACKWARD
4022 ? 1 : curbuf->b_ml.ml_line_count))
4023 break;
4024 if (start_is_white != linewhite(start_lnum + dir)
4025 || (!start_is_white
4026 && startPS(start_lnum + (dir > 0
4027 ? 1 : 0), 0, 0)))
4028 break;
4029 start_lnum += dir;
4031 if (!include)
4032 break;
4033 if (start_lnum == (dir == BACKWARD
4034 ? 1 : curbuf->b_ml.ml_line_count))
4035 break;
4036 prev_start_is_white = start_is_white;
4039 curwin->w_cursor.lnum = start_lnum;
4040 curwin->w_cursor.col = 0;
4041 return retval;
4043 #endif
4046 * First move back to the start_lnum of the paragraph or white lines
4048 white_in_front = linewhite(start_lnum);
4049 while (start_lnum > 1)
4051 if (white_in_front) /* stop at first white line */
4053 if (!linewhite(start_lnum - 1))
4054 break;
4056 else /* stop at first non-white line of start of paragraph */
4058 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4059 break;
4061 --start_lnum;
4065 * Move past the end of any white lines.
4067 end_lnum = start_lnum;
4068 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4069 ++end_lnum;
4071 --end_lnum;
4072 i = count;
4073 if (!include && white_in_front)
4074 --i;
4075 while (i--)
4077 if (end_lnum == curbuf->b_ml.ml_line_count)
4078 return FAIL;
4080 if (!include)
4081 do_white = linewhite(end_lnum + 1);
4083 if (include || !do_white)
4085 ++end_lnum;
4087 * skip to end of paragraph
4089 while (end_lnum < curbuf->b_ml.ml_line_count
4090 && !linewhite(end_lnum + 1)
4091 && !startPS(end_lnum + 1, 0, 0))
4092 ++end_lnum;
4095 if (i == 0 && white_in_front && include)
4096 break;
4099 * skip to end of white lines after paragraph
4101 if (include || do_white)
4102 while (end_lnum < curbuf->b_ml.ml_line_count
4103 && linewhite(end_lnum + 1))
4104 ++end_lnum;
4108 * If there are no empty lines at the end, try to find some empty lines at
4109 * the start (unless that has been done already).
4111 if (!white_in_front && !linewhite(end_lnum) && include)
4112 while (start_lnum > 1 && linewhite(start_lnum - 1))
4113 --start_lnum;
4115 #ifdef FEAT_VISUAL
4116 if (VIsual_active)
4118 /* Problem: when doing "Vipipip" nothing happens in a single white
4119 * line, we get stuck there. Trap this here. */
4120 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4121 goto extend;
4122 VIsual.lnum = start_lnum;
4123 VIsual_mode = 'V';
4124 redraw_curbuf_later(INVERTED); /* update the inversion */
4125 showmode();
4127 else
4128 #endif
4130 oap->start.lnum = start_lnum;
4131 oap->start.col = 0;
4132 oap->motion_type = MLINE;
4134 curwin->w_cursor.lnum = end_lnum;
4135 curwin->w_cursor.col = 0;
4137 return OK;
4140 static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4141 static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4144 * Search quote char from string line[col].
4145 * Quote character escaped by one of the characters in "escape" is not counted
4146 * as a quote.
4147 * Returns column number of "quotechar" or -1 when not found.
4149 static int
4150 find_next_quote(line, col, quotechar, escape)
4151 char_u *line;
4152 int col;
4153 int quotechar;
4154 char_u *escape; /* escape characters, can be NULL */
4156 int c;
4158 for (;;)
4160 c = line[col];
4161 if (c == NUL)
4162 return -1;
4163 else if (escape != NULL && vim_strchr(escape, c))
4164 ++col;
4165 else if (c == quotechar)
4166 break;
4167 #ifdef FEAT_MBYTE
4168 if (has_mbyte)
4169 col += (*mb_ptr2len)(line + col);
4170 else
4171 #endif
4172 ++col;
4174 return col;
4178 * Search backwards in "line" from column "col_start" to find "quotechar".
4179 * Quote character escaped by one of the characters in "escape" is not counted
4180 * as a quote.
4181 * Return the found column or zero.
4183 static int
4184 find_prev_quote(line, col_start, quotechar, escape)
4185 char_u *line;
4186 int col_start;
4187 int quotechar;
4188 char_u *escape; /* escape characters, can be NULL */
4190 int n;
4192 while (col_start > 0)
4194 --col_start;
4195 #ifdef FEAT_MBYTE
4196 col_start -= (*mb_head_off)(line, line + col_start);
4197 #endif
4198 n = 0;
4199 if (escape != NULL)
4200 while (col_start - n > 0 && vim_strchr(escape,
4201 line[col_start - n - 1]) != NULL)
4202 ++n;
4203 if (n & 1)
4204 col_start -= n; /* uneven number of escape chars, skip it */
4205 else if (line[col_start] == quotechar)
4206 break;
4208 return col_start;
4212 * Find quote under the cursor, cursor at end.
4213 * Returns TRUE if found, else FALSE.
4216 current_quote(oap, count, include, quotechar)
4217 oparg_T *oap;
4218 long count;
4219 int include; /* TRUE == include quote char */
4220 int quotechar; /* Quote character */
4222 char_u *line = ml_get_curline();
4223 int col_end;
4224 int col_start = curwin->w_cursor.col;
4225 int inclusive = FALSE;
4226 #ifdef FEAT_VISUAL
4227 int vis_empty = TRUE; /* Visual selection <= 1 char */
4228 int vis_bef_curs = FALSE; /* Visual starts before cursor */
4229 int inside_quotes = FALSE; /* Looks like "i'" done before */
4230 int selected_quote = FALSE; /* Has quote inside selection */
4231 int i;
4233 /* Correct cursor when 'selection' is exclusive */
4234 if (VIsual_active)
4236 vis_bef_curs = lt(VIsual, curwin->w_cursor);
4237 if (*p_sel == 'e' && vis_bef_curs)
4238 dec_cursor();
4239 vis_empty = equalpos(VIsual, curwin->w_cursor);
4242 if (!vis_empty)
4244 /* Check if the existing selection exactly spans the text inside
4245 * quotes. */
4246 if (vis_bef_curs)
4248 inside_quotes = VIsual.col > 0
4249 && line[VIsual.col - 1] == quotechar
4250 && line[curwin->w_cursor.col] != NUL
4251 && line[curwin->w_cursor.col + 1] == quotechar;
4252 i = VIsual.col;
4253 col_end = curwin->w_cursor.col;
4255 else
4257 inside_quotes = curwin->w_cursor.col > 0
4258 && line[curwin->w_cursor.col - 1] == quotechar
4259 && line[VIsual.col] != NUL
4260 && line[VIsual.col + 1] == quotechar;
4261 i = curwin->w_cursor.col;
4262 col_end = VIsual.col;
4265 /* Find out if we have a quote in the selection. */
4266 while (i <= col_end)
4267 if (line[i++] == quotechar)
4269 selected_quote = TRUE;
4270 break;
4274 if (!vis_empty && line[col_start] == quotechar)
4276 /* Already selecting something and on a quote character. Find the
4277 * next quoted string. */
4278 if (vis_bef_curs)
4280 /* Assume we are on a closing quote: move to after the next
4281 * opening quote. */
4282 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4283 if (col_start < 0)
4284 return FALSE;
4285 col_end = find_next_quote(line, col_start + 1, quotechar,
4286 curbuf->b_p_qe);
4287 if (col_end < 0)
4289 /* We were on a starting quote perhaps? */
4290 col_end = col_start;
4291 col_start = curwin->w_cursor.col;
4294 else
4296 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4297 if (line[col_end] != quotechar)
4298 return FALSE;
4299 col_start = find_prev_quote(line, col_end, quotechar,
4300 curbuf->b_p_qe);
4301 if (line[col_start] != quotechar)
4303 /* We were on an ending quote perhaps? */
4304 col_start = col_end;
4305 col_end = curwin->w_cursor.col;
4309 else
4310 #endif
4312 if (line[col_start] == quotechar
4313 #ifdef FEAT_VISUAL
4314 || !vis_empty
4315 #endif
4318 int first_col = col_start;
4320 #ifdef FEAT_VISUAL
4321 if (!vis_empty)
4323 if (vis_bef_curs)
4324 first_col = find_next_quote(line, col_start, quotechar, NULL);
4325 else
4326 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4328 #endif
4329 /* The cursor is on a quote, we don't know if it's the opening or
4330 * closing quote. Search from the start of the line to find out.
4331 * Also do this when there is a Visual area, a' may leave the cursor
4332 * in between two strings. */
4333 col_start = 0;
4334 for (;;)
4336 /* Find open quote character. */
4337 col_start = find_next_quote(line, col_start, quotechar, NULL);
4338 if (col_start < 0 || col_start > first_col)
4339 return FALSE;
4340 /* Find close quote character. */
4341 col_end = find_next_quote(line, col_start + 1, quotechar,
4342 curbuf->b_p_qe);
4343 if (col_end < 0)
4344 return FALSE;
4345 /* If is cursor between start and end quote character, it is
4346 * target text object. */
4347 if (col_start <= first_col && first_col <= col_end)
4348 break;
4349 col_start = col_end + 1;
4352 else
4354 /* Search backward for a starting quote. */
4355 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4356 if (line[col_start] != quotechar)
4358 /* No quote before the cursor, look after the cursor. */
4359 col_start = find_next_quote(line, col_start, quotechar, NULL);
4360 if (col_start < 0)
4361 return FALSE;
4364 /* Find close quote character. */
4365 col_end = find_next_quote(line, col_start + 1, quotechar,
4366 curbuf->b_p_qe);
4367 if (col_end < 0)
4368 return FALSE;
4371 /* When "include" is TRUE, include spaces after closing quote or before
4372 * the starting quote. */
4373 if (include)
4375 if (vim_iswhite(line[col_end + 1]))
4376 while (vim_iswhite(line[col_end + 1]))
4377 ++col_end;
4378 else
4379 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4380 --col_start;
4383 /* Set start position. After vi" another i" must include the ".
4384 * For v2i" include the quotes. */
4385 if (!include && count < 2
4386 #ifdef FEAT_VISUAL
4387 && (vis_empty || !inside_quotes)
4388 #endif
4390 ++col_start;
4391 curwin->w_cursor.col = col_start;
4392 #ifdef FEAT_VISUAL
4393 if (VIsual_active)
4395 /* Set the start of the Visual area when the Visual area was empty, we
4396 * were just inside quotes or the Visual area didn't start at a quote
4397 * and didn't include a quote.
4399 if (vis_empty
4400 || (vis_bef_curs
4401 && !selected_quote
4402 && (inside_quotes
4403 || (line[VIsual.col] != quotechar
4404 && (VIsual.col == 0
4405 || line[VIsual.col - 1] != quotechar)))))
4407 VIsual = curwin->w_cursor;
4408 redraw_curbuf_later(INVERTED);
4411 else
4412 #endif
4414 oap->start = curwin->w_cursor;
4415 oap->motion_type = MCHAR;
4418 /* Set end position. */
4419 curwin->w_cursor.col = col_end;
4420 if ((include || count > 1
4421 #ifdef FEAT_VISUAL
4422 /* After vi" another i" must include the ". */
4423 || (!vis_empty && inside_quotes)
4424 #endif
4425 ) && inc_cursor() == 2)
4426 inclusive = TRUE;
4427 #ifdef FEAT_VISUAL
4428 if (VIsual_active)
4430 if (vis_empty || vis_bef_curs)
4432 /* decrement cursor when 'selection' is not exclusive */
4433 if (*p_sel != 'e')
4434 dec_cursor();
4436 else
4438 /* Cursor is at start of Visual area. Set the end of the Visual
4439 * area when it was just inside quotes or it didn't end at a
4440 * quote. */
4441 if (inside_quotes
4442 || (!selected_quote
4443 && line[VIsual.col] != quotechar
4444 && (line[VIsual.col] == NUL
4445 || line[VIsual.col + 1] != quotechar)))
4447 dec_cursor();
4448 VIsual = curwin->w_cursor;
4450 curwin->w_cursor.col = col_start;
4452 if (VIsual_mode == 'V')
4454 VIsual_mode = 'v';
4455 redraw_cmdline = TRUE; /* show mode later */
4458 else
4459 #endif
4461 /* Set inclusive and other oap's flags. */
4462 oap->inclusive = inclusive;
4465 return OK;
4468 #endif /* FEAT_TEXTOBJ */
4470 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4471 || defined(PROTO)
4473 * return TRUE if line 'lnum' is empty or has white chars only.
4476 linewhite(lnum)
4477 linenr_T lnum;
4479 char_u *p;
4481 p = skipwhite(ml_get(lnum));
4482 return (*p == NUL);
4484 #endif
4486 #if defined(FEAT_FIND_ID) || defined(PROTO)
4488 * Find identifiers or defines in included files.
4489 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
4491 /*ARGSUSED*/
4492 void
4493 find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4494 type, count, action, start_lnum, end_lnum)
4495 char_u *ptr; /* pointer to search pattern */
4496 int dir; /* direction of expansion */
4497 int len; /* length of search pattern */
4498 int whole; /* match whole words only */
4499 int skip_comments; /* don't match inside comments */
4500 int type; /* Type of search; are we looking for a type?
4501 a macro? */
4502 long count;
4503 int action; /* What to do when we find it */
4504 linenr_T start_lnum; /* first line to start searching */
4505 linenr_T end_lnum; /* last line for searching */
4507 SearchedFile *files; /* Stack of included files */
4508 SearchedFile *bigger; /* When we need more space */
4509 int max_path_depth = 50;
4510 long match_count = 1;
4512 char_u *pat;
4513 char_u *new_fname;
4514 char_u *curr_fname = curbuf->b_fname;
4515 char_u *prev_fname = NULL;
4516 linenr_T lnum;
4517 int depth;
4518 int depth_displayed; /* For type==CHECK_PATH */
4519 int old_files;
4520 int already_searched;
4521 char_u *file_line;
4522 char_u *line;
4523 char_u *p;
4524 char_u save_char;
4525 int define_matched;
4526 regmatch_T regmatch;
4527 regmatch_T incl_regmatch;
4528 regmatch_T def_regmatch;
4529 int matched = FALSE;
4530 int did_show = FALSE;
4531 int found = FALSE;
4532 int i;
4533 char_u *already = NULL;
4534 char_u *startp = NULL;
4535 char_u *inc_opt = NULL;
4536 #ifdef RISCOS
4537 int previous_munging = __riscosify_control;
4538 #endif
4539 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4540 win_T *curwin_save = NULL;
4541 #endif
4543 regmatch.regprog = NULL;
4544 incl_regmatch.regprog = NULL;
4545 def_regmatch.regprog = NULL;
4547 file_line = alloc(LSIZE);
4548 if (file_line == NULL)
4549 return;
4551 #ifdef RISCOS
4552 /* UnixLib knows best how to munge c file names - turn munging back on. */
4553 int __riscosify_control = 0;
4554 #endif
4556 if (type != CHECK_PATH && type != FIND_DEFINE
4557 #ifdef FEAT_INS_EXPAND
4558 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4559 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
4560 && !(compl_cont_status & CONT_SOL)
4561 #endif
4564 pat = alloc(len + 5);
4565 if (pat == NULL)
4566 goto fpip_end;
4567 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4568 /* ignore case according to p_ic, p_scs and pat */
4569 regmatch.rm_ic = ignorecase(pat);
4570 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4571 vim_free(pat);
4572 if (regmatch.regprog == NULL)
4573 goto fpip_end;
4575 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4576 if (*inc_opt != NUL)
4578 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
4579 if (incl_regmatch.regprog == NULL)
4580 goto fpip_end;
4581 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4583 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4585 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4586 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4587 if (def_regmatch.regprog == NULL)
4588 goto fpip_end;
4589 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4591 files = (SearchedFile *)lalloc_clear((long_u)
4592 (max_path_depth * sizeof(SearchedFile)), TRUE);
4593 if (files == NULL)
4594 goto fpip_end;
4595 old_files = max_path_depth;
4596 depth = depth_displayed = -1;
4598 lnum = start_lnum;
4599 if (end_lnum > curbuf->b_ml.ml_line_count)
4600 end_lnum = curbuf->b_ml.ml_line_count;
4601 if (lnum > end_lnum) /* do at least one line */
4602 lnum = end_lnum;
4603 line = ml_get(lnum);
4605 for (;;)
4607 if (incl_regmatch.regprog != NULL
4608 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4610 char_u *p_fname = (curr_fname == curbuf->b_fname)
4611 ? curbuf->b_ffname : curr_fname;
4613 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4614 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4615 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
4616 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
4617 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4618 else
4619 /* Use text after match with 'include'. */
4620 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
4621 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
4622 already_searched = FALSE;
4623 if (new_fname != NULL)
4625 /* Check whether we have already searched in this file */
4626 for (i = 0;; i++)
4628 if (i == depth + 1)
4629 i = old_files;
4630 if (i == max_path_depth)
4631 break;
4632 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4634 if (type != CHECK_PATH &&
4635 action == ACTION_SHOW_ALL && files[i].matched)
4637 msg_putchar('\n'); /* cursor below last one */
4638 if (!got_int) /* don't display if 'q'
4639 typed at "--more--"
4640 mesage */
4642 msg_home_replace_hl(new_fname);
4643 MSG_PUTS(_(" (includes previously listed match)"));
4644 prev_fname = NULL;
4647 vim_free(new_fname);
4648 new_fname = NULL;
4649 already_searched = TRUE;
4650 break;
4655 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4656 || (new_fname == NULL && !already_searched)))
4658 if (did_show)
4659 msg_putchar('\n'); /* cursor below last one */
4660 else
4662 gotocmdline(TRUE); /* cursor at status line */
4663 MSG_PUTS_TITLE(_("--- Included files "));
4664 if (action != ACTION_SHOW_ALL)
4665 MSG_PUTS_TITLE(_("not found "));
4666 MSG_PUTS_TITLE(_("in path ---\n"));
4668 did_show = TRUE;
4669 while (depth_displayed < depth && !got_int)
4671 ++depth_displayed;
4672 for (i = 0; i < depth_displayed; i++)
4673 MSG_PUTS(" ");
4674 msg_home_replace(files[depth_displayed].name);
4675 MSG_PUTS(" -->\n");
4677 if (!got_int) /* don't display if 'q' typed
4678 for "--more--" message */
4680 for (i = 0; i <= depth_displayed; i++)
4681 MSG_PUTS(" ");
4682 if (new_fname != NULL)
4684 /* using "new_fname" is more reliable, e.g., when
4685 * 'includeexpr' is set. */
4686 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4688 else
4691 * Isolate the file name.
4692 * Include the surrounding "" or <> if present.
4694 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4696 for (i = 0; vim_isfilec(p[i]); i++)
4698 if (i == 0)
4700 /* Nothing found, use the rest of the line. */
4701 p = incl_regmatch.endp[0];
4702 i = (int)STRLEN(p);
4704 else
4706 if (p[-1] == '"' || p[-1] == '<')
4708 --p;
4709 ++i;
4711 if (p[i] == '"' || p[i] == '>')
4712 ++i;
4714 save_char = p[i];
4715 p[i] = NUL;
4716 msg_outtrans_attr(p, hl_attr(HLF_D));
4717 p[i] = save_char;
4720 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4722 if (already_searched)
4723 MSG_PUTS(_(" (Already listed)"));
4724 else
4725 MSG_PUTS(_(" NOT FOUND"));
4728 out_flush(); /* output each line directly */
4731 if (new_fname != NULL)
4733 /* Push the new file onto the file stack */
4734 if (depth + 1 == old_files)
4736 bigger = (SearchedFile *)lalloc((long_u)(
4737 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4738 if (bigger != NULL)
4740 for (i = 0; i <= depth; i++)
4741 bigger[i] = files[i];
4742 for (i = depth + 1; i < old_files + max_path_depth; i++)
4744 bigger[i].fp = NULL;
4745 bigger[i].name = NULL;
4746 bigger[i].lnum = 0;
4747 bigger[i].matched = FALSE;
4749 for (i = old_files; i < max_path_depth; i++)
4750 bigger[i + max_path_depth] = files[i];
4751 old_files += max_path_depth;
4752 max_path_depth *= 2;
4753 vim_free(files);
4754 files = bigger;
4757 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4758 == NULL)
4759 vim_free(new_fname);
4760 else
4762 if (++depth == old_files)
4765 * lalloc() for 'bigger' must have failed above. We
4766 * will forget one of our already visited files now.
4768 vim_free(files[old_files].name);
4769 ++old_files;
4771 files[depth].name = curr_fname = new_fname;
4772 files[depth].lnum = 0;
4773 files[depth].matched = FALSE;
4774 #ifdef FEAT_INS_EXPAND
4775 if (action == ACTION_EXPAND)
4777 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
4778 vim_snprintf((char*)IObuff, IOSIZE,
4779 _("Scanning included file: %s"),
4780 (char *)new_fname);
4781 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4783 else
4784 #endif
4785 if (p_verbose >= 5)
4787 verbose_enter();
4788 smsg((char_u *)_("Searching included file %s"),
4789 (char *)new_fname);
4790 verbose_leave();
4796 else
4799 * Check if the line is a define (type == FIND_DEFINE)
4801 p = line;
4802 search_line:
4803 define_matched = FALSE;
4804 if (def_regmatch.regprog != NULL
4805 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4808 * Pattern must be first identifier after 'define', so skip
4809 * to that position before checking for match of pattern. Also
4810 * don't let it match beyond the end of this identifier.
4812 p = def_regmatch.endp[0];
4813 while (*p && !vim_iswordc(*p))
4814 p++;
4815 define_matched = TRUE;
4819 * Look for a match. Don't do this if we are looking for a
4820 * define and this line didn't match define_prog above.
4822 if (def_regmatch.regprog == NULL || define_matched)
4824 if (define_matched
4825 #ifdef FEAT_INS_EXPAND
4826 || (compl_cont_status & CONT_SOL)
4827 #endif
4830 /* compare the first "len" chars from "ptr" */
4831 startp = skipwhite(p);
4832 if (p_ic)
4833 matched = !MB_STRNICMP(startp, ptr, len);
4834 else
4835 matched = !STRNCMP(startp, ptr, len);
4836 if (matched && define_matched && whole
4837 && vim_iswordc(startp[len]))
4838 matched = FALSE;
4840 else if (regmatch.regprog != NULL
4841 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4843 matched = TRUE;
4844 startp = regmatch.startp[0];
4846 * Check if the line is not a comment line (unless we are
4847 * looking for a define). A line starting with "# define"
4848 * is not considered to be a comment line.
4850 if (!define_matched && skip_comments)
4852 #ifdef FEAT_COMMENTS
4853 if ((*line != '#' ||
4854 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4855 && get_leader_len(line, NULL, FALSE))
4856 matched = FALSE;
4859 * Also check for a "/ *" or "/ /" before the match.
4860 * Skips lines like "int backwards; / * normal index
4861 * * /" when looking for "normal".
4862 * Note: Doesn't skip "/ *" in comments.
4864 p = skipwhite(line);
4865 if (matched
4866 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4867 #endif
4868 for (p = line; *p && p < startp; ++p)
4870 if (matched
4871 && p[0] == '/'
4872 && (p[1] == '*' || p[1] == '/'))
4874 matched = FALSE;
4875 /* After "//" all text is comment */
4876 if (p[1] == '/')
4877 break;
4878 ++p;
4880 else if (!matched && p[0] == '*' && p[1] == '/')
4882 /* Can find match after "* /". */
4883 matched = TRUE;
4884 ++p;
4891 if (matched)
4893 #ifdef FEAT_INS_EXPAND
4894 if (action == ACTION_EXPAND)
4896 int reuse = 0;
4897 int add_r;
4898 char_u *aux;
4900 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4901 break;
4902 found = TRUE;
4903 aux = p = startp;
4904 if (compl_cont_status & CONT_ADDING)
4906 p += compl_length;
4907 if (vim_iswordp(p))
4908 goto exit_matched;
4909 p = find_word_start(p);
4911 p = find_word_end(p);
4912 i = (int)(p - aux);
4914 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
4916 /* IOSIZE > compl_length, so the STRNCPY works */
4917 STRNCPY(IObuff, aux, i);
4919 /* Get the next line: when "depth" < 0 from the current
4920 * buffer, otherwise from the included file. Jump to
4921 * exit_matched when past the last line. */
4922 if (depth < 0)
4924 if (lnum >= end_lnum)
4925 goto exit_matched;
4926 line = ml_get(++lnum);
4928 else if (vim_fgets(line = file_line,
4929 LSIZE, files[depth].fp))
4930 goto exit_matched;
4932 /* we read a line, set "already" to check this "line" later
4933 * if depth >= 0 we'll increase files[depth].lnum far
4934 * bellow -- Acevedo */
4935 already = aux = p = skipwhite(line);
4936 p = find_word_start(p);
4937 p = find_word_end(p);
4938 if (p > aux)
4940 if (*aux != ')' && IObuff[i-1] != TAB)
4942 if (IObuff[i-1] != ' ')
4943 IObuff[i++] = ' ';
4944 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4945 if (p_js
4946 && (IObuff[i-2] == '.'
4947 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4948 && (IObuff[i-2] == '?'
4949 || IObuff[i-2] == '!'))))
4950 IObuff[i++] = ' ';
4952 /* copy as much as posible of the new word */
4953 if (p - aux >= IOSIZE - i)
4954 p = aux + IOSIZE - i - 1;
4955 STRNCPY(IObuff + i, aux, p - aux);
4956 i += (int)(p - aux);
4957 reuse |= CONT_S_IPOS;
4959 IObuff[i] = NUL;
4960 aux = IObuff;
4962 if (i == compl_length)
4963 goto exit_matched;
4966 add_r = ins_compl_add_infercase(aux, i, p_ic,
4967 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4968 dir, reuse);
4969 if (add_r == OK)
4970 /* if dir was BACKWARD then honor it just once */
4971 dir = FORWARD;
4972 else if (add_r == FAIL)
4973 break;
4975 else
4976 #endif
4977 if (action == ACTION_SHOW_ALL)
4979 found = TRUE;
4980 if (!did_show)
4981 gotocmdline(TRUE); /* cursor at status line */
4982 if (curr_fname != prev_fname)
4984 if (did_show)
4985 msg_putchar('\n'); /* cursor below last one */
4986 if (!got_int) /* don't display if 'q' typed
4987 at "--more--" mesage */
4988 msg_home_replace_hl(curr_fname);
4989 prev_fname = curr_fname;
4991 did_show = TRUE;
4992 if (!got_int)
4993 show_pat_in_path(line, type, TRUE, action,
4994 (depth == -1) ? NULL : files[depth].fp,
4995 (depth == -1) ? &lnum : &files[depth].lnum,
4996 match_count++);
4998 /* Set matched flag for this file and all the ones that
4999 * include it */
5000 for (i = 0; i <= depth; ++i)
5001 files[i].matched = TRUE;
5003 else if (--count <= 0)
5005 found = TRUE;
5006 if (depth == -1 && lnum == curwin->w_cursor.lnum
5007 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5008 && g_do_tagpreview == 0
5009 #endif
5011 EMSG(_("E387: Match is on current line"));
5012 else if (action == ACTION_SHOW)
5014 show_pat_in_path(line, type, did_show, action,
5015 (depth == -1) ? NULL : files[depth].fp,
5016 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5017 did_show = TRUE;
5019 else
5021 #ifdef FEAT_GUI
5022 need_mouse_correct = TRUE;
5023 #endif
5024 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5025 /* ":psearch" uses the preview window */
5026 if (g_do_tagpreview != 0)
5028 curwin_save = curwin;
5029 prepare_tagpreview(TRUE);
5031 #endif
5032 if (action == ACTION_SPLIT)
5034 #ifdef FEAT_WINDOWS
5035 if (win_split(0, 0) == FAIL)
5036 #endif
5037 break;
5038 #ifdef FEAT_SCROLLBIND
5039 curwin->w_p_scb = FALSE;
5040 #endif
5042 if (depth == -1)
5044 /* match in current file */
5045 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5046 if (g_do_tagpreview != 0)
5048 if (getfile(0, curwin_save->w_buffer->b_fname,
5049 NULL, TRUE, lnum, FALSE) > 0)
5050 break; /* failed to jump to file */
5052 else
5053 #endif
5054 setpcmark();
5055 curwin->w_cursor.lnum = lnum;
5057 else
5059 if (getfile(0, files[depth].name, NULL, TRUE,
5060 files[depth].lnum, FALSE) > 0)
5061 break; /* failed to jump to file */
5062 /* autocommands may have changed the lnum, we don't
5063 * want that here */
5064 curwin->w_cursor.lnum = files[depth].lnum;
5067 if (action != ACTION_SHOW)
5069 curwin->w_cursor.col = (colnr_T) (startp - line);
5070 curwin->w_set_curswant = TRUE;
5073 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5074 if (g_do_tagpreview != 0
5075 && curwin != curwin_save && win_valid(curwin_save))
5077 /* Return cursor to where we were */
5078 validate_cursor();
5079 redraw_later(VALID);
5080 win_enter(curwin_save, TRUE);
5082 #endif
5083 break;
5085 #ifdef FEAT_INS_EXPAND
5086 exit_matched:
5087 #endif
5088 matched = FALSE;
5089 /* look for other matches in the rest of the line if we
5090 * are not at the end of it already */
5091 if (def_regmatch.regprog == NULL
5092 #ifdef FEAT_INS_EXPAND
5093 && action == ACTION_EXPAND
5094 && !(compl_cont_status & CONT_SOL)
5095 #endif
5096 && *(p = startp + 1))
5097 goto search_line;
5099 line_breakcheck();
5100 #ifdef FEAT_INS_EXPAND
5101 if (action == ACTION_EXPAND)
5102 ins_compl_check_keys(30);
5103 if (got_int || compl_interrupted)
5104 #else
5105 if (got_int)
5106 #endif
5107 break;
5110 * Read the next line. When reading an included file and encountering
5111 * end-of-file, close the file and continue in the file that included
5112 * it.
5114 while (depth >= 0 && !already
5115 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5117 fclose(files[depth].fp);
5118 --old_files;
5119 files[old_files].name = files[depth].name;
5120 files[old_files].matched = files[depth].matched;
5121 --depth;
5122 curr_fname = (depth == -1) ? curbuf->b_fname
5123 : files[depth].name;
5124 if (depth < depth_displayed)
5125 depth_displayed = depth;
5127 if (depth >= 0) /* we could read the line */
5128 files[depth].lnum++;
5129 else if (!already)
5131 if (++lnum > end_lnum)
5132 break;
5133 line = ml_get(lnum);
5135 already = NULL;
5137 /* End of big for (;;) loop. */
5139 /* Close any files that are still open. */
5140 for (i = 0; i <= depth; i++)
5142 fclose(files[i].fp);
5143 vim_free(files[i].name);
5145 for (i = old_files; i < max_path_depth; i++)
5146 vim_free(files[i].name);
5147 vim_free(files);
5149 if (type == CHECK_PATH)
5151 if (!did_show)
5153 if (action != ACTION_SHOW_ALL)
5154 MSG(_("All included files were found"));
5155 else
5156 MSG(_("No included files"));
5159 else if (!found
5160 #ifdef FEAT_INS_EXPAND
5161 && action != ACTION_EXPAND
5162 #endif
5165 #ifdef FEAT_INS_EXPAND
5166 if (got_int || compl_interrupted)
5167 #else
5168 if (got_int)
5169 #endif
5170 EMSG(_(e_interr));
5171 else if (type == FIND_DEFINE)
5172 EMSG(_("E388: Couldn't find definition"));
5173 else
5174 EMSG(_("E389: Couldn't find pattern"));
5176 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5177 msg_end();
5179 fpip_end:
5180 vim_free(file_line);
5181 vim_free(regmatch.regprog);
5182 vim_free(incl_regmatch.regprog);
5183 vim_free(def_regmatch.regprog);
5185 #ifdef RISCOS
5186 /* Restore previous file munging state. */
5187 __riscosify_control = previous_munging;
5188 #endif
5191 static void
5192 show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5193 char_u *line;
5194 int type;
5195 int did_show;
5196 int action;
5197 FILE *fp;
5198 linenr_T *lnum;
5199 long count;
5201 char_u *p;
5203 if (did_show)
5204 msg_putchar('\n'); /* cursor below last one */
5205 else if (!msg_silent)
5206 gotocmdline(TRUE); /* cursor at status line */
5207 if (got_int) /* 'q' typed at "--more--" message */
5208 return;
5209 for (;;)
5211 p = line + STRLEN(line) - 1;
5212 if (fp != NULL)
5214 /* We used fgets(), so get rid of newline at end */
5215 if (p >= line && *p == '\n')
5216 --p;
5217 if (p >= line && *p == '\r')
5218 --p;
5219 *(p + 1) = NUL;
5221 if (action == ACTION_SHOW_ALL)
5223 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5224 msg_puts(IObuff);
5225 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5226 /* Highlight line numbers */
5227 msg_puts_attr(IObuff, hl_attr(HLF_N));
5228 MSG_PUTS(" ");
5230 msg_prt_line(line, FALSE);
5231 out_flush(); /* show one line at a time */
5233 /* Definition continues until line that doesn't end with '\' */
5234 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5235 break;
5237 if (fp != NULL)
5239 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5240 break;
5241 ++*lnum;
5243 else
5245 if (++*lnum > curbuf->b_ml.ml_line_count)
5246 break;
5247 line = ml_get(*lnum);
5249 msg_putchar('\n');
5252 #endif
5254 #ifdef FEAT_VIMINFO
5256 read_viminfo_search_pattern(virp, force)
5257 vir_T *virp;
5258 int force;
5260 char_u *lp;
5261 int idx = -1;
5262 int magic = FALSE;
5263 int no_scs = FALSE;
5264 int off_line = FALSE;
5265 int off_end = 0;
5266 long off = 0;
5267 int setlast = FALSE;
5268 #ifdef FEAT_SEARCH_EXTRA
5269 static int hlsearch_on = FALSE;
5270 #endif
5271 char_u *val;
5274 * Old line types:
5275 * "/pat", "&pat": search/subst. pat
5276 * "~/pat", "~&pat": last used search/subst. pat
5277 * New line types:
5278 * "~h", "~H": hlsearch highlighting off/on
5279 * "~<magic><smartcase><line><end><off><last><which>pat"
5280 * <magic>: 'm' off, 'M' on
5281 * <smartcase>: 's' off, 'S' on
5282 * <line>: 'L' line offset, 'l' char offset
5283 * <end>: 'E' from end, 'e' from start
5284 * <off>: decimal, offset
5285 * <last>: '~' last used pattern
5286 * <which>: '/' search pat, '&' subst. pat
5288 lp = virp->vir_line;
5289 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5291 if (lp[1] == 'M') /* magic on */
5292 magic = TRUE;
5293 if (lp[2] == 's')
5294 no_scs = TRUE;
5295 if (lp[3] == 'L')
5296 off_line = TRUE;
5297 if (lp[4] == 'E')
5298 off_end = SEARCH_END;
5299 lp += 5;
5300 off = getdigits(&lp);
5302 if (lp[0] == '~') /* use this pattern for last-used pattern */
5304 setlast = TRUE;
5305 lp++;
5307 if (lp[0] == '/')
5308 idx = RE_SEARCH;
5309 else if (lp[0] == '&')
5310 idx = RE_SUBST;
5311 #ifdef FEAT_SEARCH_EXTRA
5312 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5313 hlsearch_on = FALSE;
5314 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5315 hlsearch_on = TRUE;
5316 #endif
5317 if (idx >= 0)
5319 if (force || spats[idx].pat == NULL)
5321 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5322 TRUE);
5323 if (val != NULL)
5325 set_last_search_pat(val, idx, magic, setlast);
5326 vim_free(val);
5327 spats[idx].no_scs = no_scs;
5328 spats[idx].off.line = off_line;
5329 spats[idx].off.end = off_end;
5330 spats[idx].off.off = off;
5331 #ifdef FEAT_SEARCH_EXTRA
5332 if (setlast)
5333 no_hlsearch = !hlsearch_on;
5334 #endif
5338 return viminfo_readline(virp);
5341 void
5342 write_viminfo_search_pattern(fp)
5343 FILE *fp;
5345 if (get_viminfo_parameter('/') != 0)
5347 #ifdef FEAT_SEARCH_EXTRA
5348 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5349 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5350 #endif
5351 wvsp_one(fp, RE_SEARCH, "", '/');
5352 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5356 static void
5357 wvsp_one(fp, idx, s, sc)
5358 FILE *fp; /* file to write to */
5359 int idx; /* spats[] index */
5360 char *s; /* search pat */
5361 int sc; /* dir char */
5363 if (spats[idx].pat != NULL)
5365 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
5366 /* off.dir is not stored, it's reset to forward */
5367 fprintf(fp, "%c%c%c%c%ld%s%c",
5368 spats[idx].magic ? 'M' : 'm', /* magic */
5369 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5370 spats[idx].off.line ? 'L' : 'l', /* line offset */
5371 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5372 spats[idx].off.off, /* offset */
5373 last_idx == idx ? "~" : "", /* last used pat */
5374 sc);
5375 viminfo_writestring(fp, spats[idx].pat);
5378 #endif /* FEAT_VIMINFO */