Update credits
[MacVim.git] / src / search.c
blob59fa322340e577342006e856cb399636397ba142
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 if (options & SEARCH_START)
551 extra_col = 0;
552 #ifdef FEAT_MBYTE
553 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
554 else if (has_mbyte && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
555 && pos->col < MAXCOL - 2)
557 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
558 if (*ptr == NUL)
559 extra_col = 1;
560 else
561 extra_col = (*mb_ptr2len)(ptr);
563 #endif
564 else
565 extra_col = 1;
568 * find the string
570 called_emsg = FALSE;
571 do /* loop for count */
573 start_pos = *pos; /* remember start pos for detecting no match */
574 found = 0; /* default: not found */
575 at_first_line = TRUE; /* default: start in first line */
576 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
578 pos->lnum = 1;
579 pos->col = 0;
580 at_first_line = FALSE; /* not in first line now */
584 * Start searching in current line, unless searching backwards and
585 * we're in column 0.
586 * If we are searching backwards, in column 0, and not including the
587 * current position, gain some efficiency by skipping back a line.
588 * Otherwise begin the search in the current line.
590 if (dir == BACKWARD && start_pos.col == 0
591 && (options & SEARCH_START) == 0)
593 lnum = pos->lnum - 1;
594 at_first_line = FALSE;
596 else
597 lnum = pos->lnum;
599 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
601 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
602 lnum += dir, at_first_line = FALSE)
604 /* Stop after checking "stop_lnum", if it's set. */
605 if (stop_lnum != 0 && (dir == FORWARD
606 ? lnum > stop_lnum : lnum < stop_lnum))
607 break;
608 #ifdef FEAT_RELTIME
609 /* Stop after passing the "tm" time limit. */
610 if (tm != NULL && profile_passed_limit(tm))
611 break;
612 #endif
615 * Look for a match somewhere in line "lnum".
617 nmatched = vim_regexec_multi(&regmatch, win, buf,
618 lnum, (colnr_T)0,
619 #ifdef FEAT_RELTIME
621 #else
622 NULL
623 #endif
625 /* Abort searching on an error (e.g., out of stack). */
626 if (called_emsg)
627 break;
628 if (nmatched > 0)
630 /* match may actually be in another line when using \zs */
631 matchpos = regmatch.startpos[0];
632 endpos = regmatch.endpos[0];
633 #ifdef FEAT_EVAL
634 submatch = first_submatch(&regmatch);
635 #endif
636 /* "lnum" may be past end of buffer for "\n\zs". */
637 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
638 ptr = (char_u *)"";
639 else
640 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
643 * Forward search in the first line: match should be after
644 * the start position. If not, continue at the end of the
645 * match (this is vi compatible) or on the next char.
647 if (dir == FORWARD && at_first_line)
649 match_ok = TRUE;
651 * When the match starts in a next line it's certainly
652 * past the start position.
653 * When match lands on a NUL the cursor will be put
654 * one back afterwards, compare with that position,
655 * otherwise "/$" will get stuck on end of line.
657 while (matchpos.lnum == 0
658 && ((options & SEARCH_END)
659 ? (nmatched == 1
660 && (int)endpos.col - 1
661 < (int)start_pos.col + extra_col)
662 : ((int)matchpos.col
663 - (ptr[matchpos.col] == NUL)
664 < (int)start_pos.col + extra_col)))
667 * If vi-compatible searching, continue at the end
668 * of the match, otherwise continue one position
669 * forward.
671 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
673 if (nmatched > 1)
675 /* end is in next line, thus no match in
676 * this line */
677 match_ok = FALSE;
678 break;
680 matchcol = endpos.col;
681 /* for empty match: advance one char */
682 if (matchcol == matchpos.col
683 && ptr[matchcol] != NUL)
685 #ifdef FEAT_MBYTE
686 if (has_mbyte)
687 matchcol +=
688 (*mb_ptr2len)(ptr + matchcol);
689 else
690 #endif
691 ++matchcol;
694 else
696 matchcol = matchpos.col;
697 if (ptr[matchcol] != NUL)
699 #ifdef FEAT_MBYTE
700 if (has_mbyte)
701 matchcol += (*mb_ptr2len)(ptr
702 + matchcol);
703 else
704 #endif
705 ++matchcol;
708 if (ptr[matchcol] == NUL
709 || (nmatched = vim_regexec_multi(&regmatch,
710 win, buf, lnum + matchpos.lnum,
711 matchcol,
712 #ifdef FEAT_RELTIME
714 #else
715 NULL
716 #endif
717 )) == 0)
719 match_ok = FALSE;
720 break;
722 matchpos = regmatch.startpos[0];
723 endpos = regmatch.endpos[0];
724 # ifdef FEAT_EVAL
725 submatch = first_submatch(&regmatch);
726 # endif
728 /* Need to get the line pointer again, a
729 * multi-line search may have made it invalid. */
730 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
732 if (!match_ok)
733 continue;
735 if (dir == BACKWARD)
738 * Now, if there are multiple matches on this line,
739 * we have to get the last one. Or the last one before
740 * the cursor, if we're on that line.
741 * When putting the new cursor at the end, compare
742 * relative to the end of the match.
744 match_ok = FALSE;
745 for (;;)
747 /* Remember a position that is before the start
748 * position, we use it if it's the last match in
749 * the line. Always accept a position after
750 * wrapping around. */
751 if (loop
752 || ((options & SEARCH_END)
753 ? (lnum + regmatch.endpos[0].lnum
754 < start_pos.lnum
755 || (lnum + regmatch.endpos[0].lnum
756 == start_pos.lnum
757 && (int)regmatch.endpos[0].col - 1
758 + extra_col
759 <= (int)start_pos.col))
760 : (lnum + regmatch.startpos[0].lnum
761 < start_pos.lnum
762 || (lnum + regmatch.startpos[0].lnum
763 == start_pos.lnum
764 && (int)regmatch.startpos[0].col
765 + extra_col
766 <= (int)start_pos.col))))
768 match_ok = TRUE;
769 matchpos = regmatch.startpos[0];
770 endpos = regmatch.endpos[0];
771 # ifdef FEAT_EVAL
772 submatch = first_submatch(&regmatch);
773 # endif
775 else
776 break;
779 * We found a valid match, now check if there is
780 * another one after it.
781 * If vi-compatible searching, continue at the end
782 * of the match, otherwise continue one position
783 * forward.
785 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
787 if (nmatched > 1)
788 break;
789 matchcol = endpos.col;
790 /* for empty match: advance one char */
791 if (matchcol == matchpos.col
792 && ptr[matchcol] != NUL)
794 #ifdef FEAT_MBYTE
795 if (has_mbyte)
796 matchcol +=
797 (*mb_ptr2len)(ptr + matchcol);
798 else
799 #endif
800 ++matchcol;
803 else
805 /* Stop when the match is in a next line. */
806 if (matchpos.lnum > 0)
807 break;
808 matchcol = matchpos.col;
809 if (ptr[matchcol] != NUL)
811 #ifdef FEAT_MBYTE
812 if (has_mbyte)
813 matchcol +=
814 (*mb_ptr2len)(ptr + matchcol);
815 else
816 #endif
817 ++matchcol;
820 if (ptr[matchcol] == NUL
821 || (nmatched = vim_regexec_multi(&regmatch,
822 win, buf, lnum + matchpos.lnum,
823 matchcol,
824 #ifdef FEAT_RELTIME
826 #else
827 NULL
828 #endif
829 )) == 0)
830 break;
832 /* Need to get the line pointer again, a
833 * multi-line search may have made it invalid. */
834 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
838 * If there is only a match after the cursor, skip
839 * this match.
841 if (!match_ok)
842 continue;
845 /* With the SEARCH_END option move to the last character
846 * of the match. Don't do it for an empty match, end
847 * should be same as start then. */
848 if (options & SEARCH_END && !(options & SEARCH_NOOF)
849 && !(matchpos.lnum == endpos.lnum
850 && matchpos.col == endpos.col))
852 /* For a match in the first column, set the position
853 * on the NUL in the previous line. */
854 pos->lnum = lnum + endpos.lnum;
855 pos->col = endpos.col;
856 if (endpos.col == 0)
858 if (pos->lnum > 1) /* just in case */
860 --pos->lnum;
861 pos->col = (colnr_T)STRLEN(ml_get_buf(buf,
862 pos->lnum, FALSE));
865 else
867 --pos->col;
868 #ifdef FEAT_MBYTE
869 if (has_mbyte
870 && pos->lnum <= buf->b_ml.ml_line_count)
872 ptr = ml_get_buf(buf, pos->lnum, FALSE);
873 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
875 #endif
878 else
880 pos->lnum = lnum + matchpos.lnum;
881 pos->col = matchpos.col;
883 #ifdef FEAT_VIRTUALEDIT
884 pos->coladd = 0;
885 #endif
886 found = 1;
888 /* Set variables used for 'incsearch' highlighting. */
889 search_match_lines = endpos.lnum - matchpos.lnum;
890 search_match_endcol = endpos.col;
891 break;
893 line_breakcheck(); /* stop if ctrl-C typed */
894 if (got_int)
895 break;
897 #ifdef FEAT_SEARCH_EXTRA
898 /* Cancel searching if a character was typed. Used for
899 * 'incsearch'. Don't check too often, that would slowdown
900 * searching too much. */
901 if ((options & SEARCH_PEEK)
902 && ((lnum - pos->lnum) & 0x3f) == 0
903 && char_avail())
905 break_loop = TRUE;
906 break;
908 #endif
910 if (loop && lnum == start_pos.lnum)
911 break; /* if second loop, stop where started */
913 at_first_line = FALSE;
916 * Stop the search if wrapscan isn't set, "stop_lnum" is
917 * specified, after an interrupt, after a match and after looping
918 * twice.
920 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
921 || break_loop || found || loop)
922 break;
925 * If 'wrapscan' is set we continue at the other end of the file.
926 * If 'shortmess' does not contain 's', we give a message.
927 * This message is also remembered in keep_msg for when the screen
928 * is redrawn. The keep_msg is cleared whenever another message is
929 * written.
931 if (dir == BACKWARD) /* start second loop at the other end */
932 lnum = buf->b_ml.ml_line_count;
933 else
934 lnum = 1;
935 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
936 give_warning((char_u *)_(dir == BACKWARD
937 ? top_bot_msg : bot_top_msg), TRUE);
939 if (got_int || called_emsg || break_loop)
940 break;
942 while (--count > 0 && found); /* stop after count matches or no match */
944 vim_free(regmatch.regprog);
946 called_emsg |= save_called_emsg;
948 if (!found) /* did not find it */
950 if (got_int)
951 EMSG(_(e_interr));
952 else if ((options & SEARCH_MSG) == SEARCH_MSG)
954 if (p_ws)
955 EMSG2(_(e_patnotf2), mr_pattern);
956 else if (lnum == 0)
957 EMSG2(_("E384: search hit TOP without match for: %s"),
958 mr_pattern);
959 else
960 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
961 mr_pattern);
963 return FAIL;
966 /* A pattern like "\n\zs" may go past the last line. */
967 if (pos->lnum > buf->b_ml.ml_line_count)
969 pos->lnum = buf->b_ml.ml_line_count;
970 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
971 if (pos->col > 0)
972 --pos->col;
975 return submatch + 1;
978 #ifdef FEAT_EVAL
980 * Return the number of the first subpat that matched.
982 static int
983 first_submatch(rp)
984 regmmatch_T *rp;
986 int submatch;
988 for (submatch = 1; ; ++submatch)
990 if (rp->startpos[submatch].lnum >= 0)
991 break;
992 if (submatch == 9)
994 submatch = 0;
995 break;
998 return submatch;
1000 #endif
1003 * Highest level string search function.
1004 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
1005 * If 'dirc' is 0: use previous dir.
1006 * If 'pat' is NULL or empty : use previous string.
1007 * If 'options & SEARCH_REV' : go in reverse of previous dir.
1008 * If 'options & SEARCH_ECHO': echo the search command and handle options
1009 * If 'options & SEARCH_MSG' : may give error message
1010 * If 'options & SEARCH_OPT' : interpret optional flags
1011 * If 'options & SEARCH_HIS' : put search pattern in history
1012 * If 'options & SEARCH_NOOF': don't add offset to position
1013 * If 'options & SEARCH_MARK': set previous context mark
1014 * If 'options & SEARCH_KEEP': keep previous search pattern
1015 * If 'options & SEARCH_START': accept match at curpos itself
1016 * If 'options & SEARCH_PEEK': check for typed char, cancel search
1018 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
1019 * makes the movement linewise without moving the match position.
1021 * return 0 for failure, 1 for found, 2 for found and line offset added
1024 do_search(oap, dirc, pat, count, options, tm)
1025 oparg_T *oap; /* can be NULL */
1026 int dirc; /* '/' or '?' */
1027 char_u *pat;
1028 long count;
1029 int options;
1030 proftime_T *tm; /* timeout limit or NULL */
1032 pos_T pos; /* position of the last match */
1033 char_u *searchstr;
1034 struct soffset old_off;
1035 int retval; /* Return value */
1036 char_u *p;
1037 long c;
1038 char_u *dircp;
1039 char_u *strcopy = NULL;
1040 char_u *ps;
1043 * A line offset is not remembered, this is vi compatible.
1045 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1047 spats[0].off.line = FALSE;
1048 spats[0].off.off = 0;
1052 * Save the values for when (options & SEARCH_KEEP) is used.
1053 * (there is no "if ()" around this because gcc wants them initialized)
1055 old_off = spats[0].off;
1057 pos = curwin->w_cursor; /* start searching at the cursor position */
1060 * Find out the direction of the search.
1062 if (dirc == 0)
1063 dirc = spats[0].off.dir;
1064 else
1065 spats[0].off.dir = dirc;
1066 if (options & SEARCH_REV)
1068 #ifdef WIN32
1069 /* There is a bug in the Visual C++ 2.2 compiler which means that
1070 * dirc always ends up being '/' */
1071 dirc = (dirc == '/') ? '?' : '/';
1072 #else
1073 if (dirc == '/')
1074 dirc = '?';
1075 else
1076 dirc = '/';
1077 #endif
1080 #ifdef FEAT_FOLDING
1081 /* If the cursor is in a closed fold, don't find another match in the same
1082 * fold. */
1083 if (dirc == '/')
1085 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1086 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1088 else
1090 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1091 pos.col = 0;
1093 #endif
1095 #ifdef FEAT_SEARCH_EXTRA
1097 * Turn 'hlsearch' highlighting back on.
1099 if (no_hlsearch && !(options & SEARCH_KEEP))
1101 redraw_all_later(SOME_VALID);
1102 no_hlsearch = FALSE;
1104 #endif
1107 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1109 for (;;)
1111 searchstr = pat;
1112 dircp = NULL;
1113 /* use previous pattern */
1114 if (pat == NULL || *pat == NUL || *pat == dirc)
1116 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1118 EMSG(_(e_noprevre));
1119 retval = 0;
1120 goto end_do_search;
1122 /* make search_regcomp() use spats[RE_SEARCH].pat */
1123 searchstr = (char_u *)"";
1126 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1129 * Find end of regular expression.
1130 * If there is a matching '/' or '?', toss it.
1132 ps = strcopy;
1133 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1134 if (strcopy != ps)
1136 /* made a copy of "pat" to change "\?" to "?" */
1137 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
1138 pat = strcopy;
1139 searchstr = strcopy;
1141 if (*p == dirc)
1143 dircp = p; /* remember where we put the NUL */
1144 *p++ = NUL;
1146 spats[0].off.line = FALSE;
1147 spats[0].off.end = FALSE;
1148 spats[0].off.off = 0;
1150 * Check for a line offset or a character offset.
1151 * For get_address (echo off) we don't check for a character
1152 * offset, because it is meaningless and the 's' could be a
1153 * substitute command.
1155 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1156 spats[0].off.line = TRUE;
1157 else if ((options & SEARCH_OPT) &&
1158 (*p == 'e' || *p == 's' || *p == 'b'))
1160 if (*p == 'e') /* end */
1161 spats[0].off.end = SEARCH_END;
1162 ++p;
1164 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1166 /* 'nr' or '+nr' or '-nr' */
1167 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1168 spats[0].off.off = atol((char *)p);
1169 else if (*p == '-') /* single '-' */
1170 spats[0].off.off = -1;
1171 else /* single '+' */
1172 spats[0].off.off = 1;
1173 ++p;
1174 while (VIM_ISDIGIT(*p)) /* skip number */
1175 ++p;
1178 /* compute length of search command for get_address() */
1179 searchcmdlen += (int)(p - pat);
1181 pat = p; /* put pat after search command */
1184 if ((options & SEARCH_ECHO) && messaging()
1185 && !cmd_silent && msg_silent == 0)
1187 char_u *msgbuf;
1188 char_u *trunc;
1190 if (*searchstr == NUL)
1191 p = spats[last_idx].pat;
1192 else
1193 p = searchstr;
1194 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1195 if (msgbuf != NULL)
1197 msgbuf[0] = dirc;
1198 #ifdef FEAT_MBYTE
1199 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1201 /* Use a space to draw the composing char on. */
1202 msgbuf[1] = ' ';
1203 STRCPY(msgbuf + 2, p);
1205 else
1206 #endif
1207 STRCPY(msgbuf + 1, p);
1208 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1210 p = msgbuf + STRLEN(msgbuf);
1211 *p++ = dirc;
1212 if (spats[0].off.end)
1213 *p++ = 'e';
1214 else if (!spats[0].off.line)
1215 *p++ = 's';
1216 if (spats[0].off.off > 0 || spats[0].off.line)
1217 *p++ = '+';
1218 if (spats[0].off.off != 0 || spats[0].off.line)
1219 sprintf((char *)p, "%ld", spats[0].off.off);
1220 else
1221 *p = NUL;
1224 msg_start();
1225 trunc = msg_strtrunc(msgbuf, FALSE);
1227 #ifdef FEAT_RIGHTLEFT
1228 /* The search pattern could be shown on the right in rightleft
1229 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1230 * it would be blanked out again very soon. Show it on the
1231 * left, but do reverse the text. */
1232 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1234 char_u *r;
1236 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1237 if (r != NULL)
1239 vim_free(trunc);
1240 trunc = r;
1243 #endif
1244 if (trunc != NULL)
1246 msg_outtrans(trunc);
1247 vim_free(trunc);
1249 else
1250 msg_outtrans(msgbuf);
1251 msg_clr_eos();
1252 msg_check();
1253 vim_free(msgbuf);
1255 gotocmdline(FALSE);
1256 out_flush();
1257 msg_nowait = TRUE; /* don't wait for this message */
1262 * If there is a character offset, subtract it from the current
1263 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
1264 * Skip this if pos.col is near MAXCOL (closed fold).
1265 * This is not done for a line offset, because then we would not be vi
1266 * compatible.
1268 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
1270 if (spats[0].off.off > 0)
1272 for (c = spats[0].off.off; c; --c)
1273 if (decl(&pos) == -1)
1274 break;
1275 if (c) /* at start of buffer */
1277 pos.lnum = 0; /* allow lnum == 0 here */
1278 pos.col = MAXCOL;
1281 else
1283 for (c = spats[0].off.off; c; ++c)
1284 if (incl(&pos) == -1)
1285 break;
1286 if (c) /* at end of buffer */
1288 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1289 pos.col = 0;
1294 #ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1295 if (p_altkeymap && curwin->w_p_rl)
1296 lrFswap(searchstr,0);
1297 #endif
1299 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1300 searchstr, count, spats[0].off.end + (options &
1301 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1302 + SEARCH_MSG + SEARCH_START
1303 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
1304 RE_LAST, (linenr_T)0, tm);
1306 if (dircp != NULL)
1307 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1308 if (c == FAIL)
1310 retval = 0;
1311 goto end_do_search;
1313 if (spats[0].off.end && oap != NULL)
1314 oap->inclusive = TRUE; /* 'e' includes last character */
1316 retval = 1; /* pattern found */
1319 * Add character and/or line offset
1321 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
1323 if (spats[0].off.line) /* Add the offset to the line number. */
1325 c = pos.lnum + spats[0].off.off;
1326 if (c < 1)
1327 pos.lnum = 1;
1328 else if (c > curbuf->b_ml.ml_line_count)
1329 pos.lnum = curbuf->b_ml.ml_line_count;
1330 else
1331 pos.lnum = c;
1332 pos.col = 0;
1334 retval = 2; /* pattern found, line offset added */
1336 else if (pos.col < MAXCOL - 2) /* just in case */
1338 /* to the right, check for end of file */
1339 if (spats[0].off.off > 0)
1341 for (c = spats[0].off.off; c; --c)
1342 if (incl(&pos) == -1)
1343 break;
1345 /* to the left, check for start of file */
1346 else
1348 if ((c = pos.col + spats[0].off.off) >= 0)
1349 pos.col = c;
1350 else
1351 for (c = spats[0].off.off; c; ++c)
1352 if (decl(&pos) == -1)
1353 break;
1359 * The search command can be followed by a ';' to do another search.
1360 * For example: "/pat/;/foo/+3;?bar"
1361 * This is like doing another search command, except:
1362 * - The remembered direction '/' or '?' is from the first search.
1363 * - When an error happens the cursor isn't moved at all.
1364 * Don't do this when called by get_address() (it handles ';' itself).
1366 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1367 break;
1369 dirc = *++pat;
1370 if (dirc != '?' && dirc != '/')
1372 retval = 0;
1373 EMSG(_("E386: Expected '?' or '/' after ';'"));
1374 goto end_do_search;
1376 ++pat;
1379 if (options & SEARCH_MARK)
1380 setpcmark();
1381 curwin->w_cursor = pos;
1382 curwin->w_set_curswant = TRUE;
1384 end_do_search:
1385 if (options & SEARCH_KEEP)
1386 spats[0].off = old_off;
1387 vim_free(strcopy);
1389 return retval;
1392 #if defined(FEAT_INS_EXPAND) || defined(PROTO)
1394 * search_for_exact_line(buf, pos, dir, pat)
1396 * Search for a line starting with the given pattern (ignoring leading
1397 * white-space), starting from pos and going in direction dir. pos will
1398 * contain the position of the match found. Blank lines match only if
1399 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1400 * Return OK for success, or FAIL if no line found.
1403 search_for_exact_line(buf, pos, dir, pat)
1404 buf_T *buf;
1405 pos_T *pos;
1406 int dir;
1407 char_u *pat;
1409 linenr_T start = 0;
1410 char_u *ptr;
1411 char_u *p;
1413 if (buf->b_ml.ml_line_count == 0)
1414 return FAIL;
1415 for (;;)
1417 pos->lnum += dir;
1418 if (pos->lnum < 1)
1420 if (p_ws)
1422 pos->lnum = buf->b_ml.ml_line_count;
1423 if (!shortmess(SHM_SEARCH))
1424 give_warning((char_u *)_(top_bot_msg), TRUE);
1426 else
1428 pos->lnum = 1;
1429 break;
1432 else if (pos->lnum > buf->b_ml.ml_line_count)
1434 if (p_ws)
1436 pos->lnum = 1;
1437 if (!shortmess(SHM_SEARCH))
1438 give_warning((char_u *)_(bot_top_msg), TRUE);
1440 else
1442 pos->lnum = 1;
1443 break;
1446 if (pos->lnum == start)
1447 break;
1448 if (start == 0)
1449 start = pos->lnum;
1450 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1451 p = skipwhite(ptr);
1452 pos->col = (colnr_T) (p - ptr);
1454 /* when adding lines the matching line may be empty but it is not
1455 * ignored because we are interested in the next line -- Acevedo */
1456 if ((compl_cont_status & CONT_ADDING)
1457 && !(compl_cont_status & CONT_SOL))
1459 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1460 return OK;
1462 else if (*p != NUL) /* ignore empty lines */
1463 { /* expanding lines or words */
1464 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1465 : STRNCMP(p, pat, compl_length)) == 0)
1466 return OK;
1469 return FAIL;
1471 #endif /* FEAT_INS_EXPAND */
1474 * Character Searches
1478 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1479 * position of the character, otherwise move to just before the char.
1480 * Do this "cap->count1" times.
1481 * Return FAIL or OK.
1484 searchc(cap, t_cmd)
1485 cmdarg_T *cap;
1486 int t_cmd;
1488 int c = cap->nchar; /* char to search for */
1489 int dir = cap->arg; /* TRUE for searching forward */
1490 long count = cap->count1; /* repeat count */
1491 static int lastc = NUL; /* last character searched for */
1492 static int lastcdir; /* last direction of character search */
1493 static int last_t_cmd; /* last search t_cmd */
1494 int col;
1495 char_u *p;
1496 int len;
1497 #ifdef FEAT_MBYTE
1498 static char_u bytes[MB_MAXBYTES];
1499 static int bytelen = 1; /* >1 for multi-byte char */
1500 #endif
1502 if (c != NUL) /* normal search: remember args for repeat */
1504 if (!KeyStuffed) /* don't remember when redoing */
1506 lastc = c;
1507 lastcdir = dir;
1508 last_t_cmd = t_cmd;
1509 #ifdef FEAT_MBYTE
1510 bytelen = (*mb_char2bytes)(c, bytes);
1511 if (cap->ncharC1 != 0)
1513 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1514 if (cap->ncharC2 != 0)
1515 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1517 #endif
1520 else /* repeat previous search */
1522 if (lastc == NUL)
1523 return FAIL;
1524 if (dir) /* repeat in opposite direction */
1525 dir = -lastcdir;
1526 else
1527 dir = lastcdir;
1528 t_cmd = last_t_cmd;
1529 c = lastc;
1530 /* For multi-byte re-use last bytes[] and bytelen. */
1533 if (dir == BACKWARD)
1534 cap->oap->inclusive = FALSE;
1535 else
1536 cap->oap->inclusive = TRUE;
1538 p = ml_get_curline();
1539 col = curwin->w_cursor.col;
1540 len = (int)STRLEN(p);
1542 while (count--)
1544 #ifdef FEAT_MBYTE
1545 if (has_mbyte)
1547 for (;;)
1549 if (dir > 0)
1551 col += (*mb_ptr2len)(p + col);
1552 if (col >= len)
1553 return FAIL;
1555 else
1557 if (col == 0)
1558 return FAIL;
1559 col -= (*mb_head_off)(p, p + col - 1) + 1;
1561 if (bytelen == 1)
1563 if (p[col] == c)
1564 break;
1566 else
1568 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1569 break;
1573 else
1574 #endif
1576 for (;;)
1578 if ((col += dir) < 0 || col >= len)
1579 return FAIL;
1580 if (p[col] == c)
1581 break;
1586 if (t_cmd)
1588 /* backup to before the character (possibly double-byte) */
1589 col -= dir;
1590 #ifdef FEAT_MBYTE
1591 if (has_mbyte)
1593 if (dir < 0)
1594 /* Landed on the search char which is bytelen long */
1595 col += bytelen - 1;
1596 else
1597 /* To previous char, which may be multi-byte. */
1598 col -= (*mb_head_off)(p, p + col);
1600 #endif
1602 curwin->w_cursor.col = col;
1604 return OK;
1608 * "Other" Searches
1612 * findmatch - find the matching paren or brace
1614 * Improvement over vi: Braces inside quotes are ignored.
1616 pos_T *
1617 findmatch(oap, initc)
1618 oparg_T *oap;
1619 int initc;
1621 return findmatchlimit(oap, initc, 0, 0);
1625 * Return TRUE if the character before "linep[col]" equals "ch".
1626 * Return FALSE if "col" is zero.
1627 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1628 * is NULL.
1629 * Handles multibyte string correctly.
1631 static int
1632 check_prevcol(linep, col, ch, prevcol)
1633 char_u *linep;
1634 int col;
1635 int ch;
1636 int *prevcol;
1638 --col;
1639 #ifdef FEAT_MBYTE
1640 if (col > 0 && has_mbyte)
1641 col -= (*mb_head_off)(linep, linep + col);
1642 #endif
1643 if (prevcol)
1644 *prevcol = col;
1645 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1649 * findmatchlimit -- find the matching paren or brace, if it exists within
1650 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1651 * the edge of the file.
1653 * "initc" is the character to find a match for. NUL means to find the
1654 * character at or after the cursor.
1656 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1657 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1658 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1659 * FM_SKIPCOMM skip comments (not implemented yet!)
1661 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1662 * NULL
1665 pos_T *
1666 findmatchlimit(oap, initc, flags, maxtravel)
1667 oparg_T *oap;
1668 int initc;
1669 int flags;
1670 int maxtravel;
1672 static pos_T pos; /* current search position */
1673 int findc = 0; /* matching brace */
1674 int c;
1675 int count = 0; /* cumulative number of braces */
1676 int backwards = FALSE; /* init for gcc */
1677 int inquote = FALSE; /* TRUE when inside quotes */
1678 char_u *linep; /* pointer to current line */
1679 char_u *ptr;
1680 int do_quotes; /* check for quotes in current line */
1681 int at_start; /* do_quotes value at start position */
1682 int hash_dir = 0; /* Direction searched for # things */
1683 int comment_dir = 0; /* Direction searched for comments */
1684 pos_T match_pos; /* Where last slash-star was found */
1685 int start_in_quotes; /* start position is in quotes */
1686 int traveled = 0; /* how far we've searched so far */
1687 int ignore_cend = FALSE; /* ignore comment end */
1688 int cpo_match; /* vi compatible matching */
1689 int cpo_bsl; /* don't recognize backslashes */
1690 int match_escaped = 0; /* search for escaped match */
1691 int dir; /* Direction to search */
1692 int comment_col = MAXCOL; /* start of / / comment */
1693 #ifdef FEAT_LISP
1694 int lispcomm = FALSE; /* inside of Lisp-style comment */
1695 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1696 #endif
1698 pos = curwin->w_cursor;
1699 linep = ml_get(pos.lnum);
1701 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1702 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1704 /* Direction to search when initc is '/', '*' or '#' */
1705 if (flags & FM_BACKWARD)
1706 dir = BACKWARD;
1707 else if (flags & FM_FORWARD)
1708 dir = FORWARD;
1709 else
1710 dir = 0;
1713 * if initc given, look in the table for the matching character
1714 * '/' and '*' are special cases: look for start or end of comment.
1715 * When '/' is used, we ignore running backwards into an star-slash, for
1716 * "[*" command, we just want to find any comment.
1718 if (initc == '/' || initc == '*')
1720 comment_dir = dir;
1721 if (initc == '/')
1722 ignore_cend = TRUE;
1723 backwards = (dir == FORWARD) ? FALSE : TRUE;
1724 initc = NUL;
1726 else if (initc != '#' && initc != NUL)
1728 /* 'matchpairs' is "x:y,x:y" */
1729 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1731 if (*ptr == initc)
1733 findc = initc;
1734 initc = ptr[2];
1735 backwards = TRUE;
1736 break;
1738 ptr += 2;
1739 if (*ptr == initc)
1741 findc = initc;
1742 initc = ptr[-2];
1743 backwards = FALSE;
1744 break;
1746 if (ptr[1] != ',')
1747 break;
1749 if (!findc) /* invalid initc! */
1750 return NULL;
1753 * Either initc is '#', or no initc was given and we need to look under the
1754 * cursor.
1756 else
1758 if (initc == '#')
1760 hash_dir = dir;
1762 else
1765 * initc was not given, must look for something to match under
1766 * or near the cursor.
1767 * Only check for special things when 'cpo' doesn't have '%'.
1769 if (!cpo_match)
1771 /* Are we before or at #if, #else etc.? */
1772 ptr = skipwhite(linep);
1773 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1775 ptr = skipwhite(ptr + 1);
1776 if ( STRNCMP(ptr, "if", 2) == 0
1777 || STRNCMP(ptr, "endif", 5) == 0
1778 || STRNCMP(ptr, "el", 2) == 0)
1779 hash_dir = 1;
1782 /* Are we on a comment? */
1783 else if (linep[pos.col] == '/')
1785 if (linep[pos.col + 1] == '*')
1787 comment_dir = FORWARD;
1788 backwards = FALSE;
1789 pos.col++;
1791 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1793 comment_dir = BACKWARD;
1794 backwards = TRUE;
1795 pos.col--;
1798 else if (linep[pos.col] == '*')
1800 if (linep[pos.col + 1] == '/')
1802 comment_dir = BACKWARD;
1803 backwards = TRUE;
1805 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1807 comment_dir = FORWARD;
1808 backwards = FALSE;
1814 * If we are not on a comment or the # at the start of a line, then
1815 * look for brace anywhere on this line after the cursor.
1817 if (!hash_dir && !comment_dir)
1820 * Find the brace under or after the cursor.
1821 * If beyond the end of the line, use the last character in
1822 * the line.
1824 if (linep[pos.col] == NUL && pos.col)
1825 --pos.col;
1826 for (;;)
1828 initc = linep[pos.col];
1829 if (initc == NUL)
1830 break;
1832 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1834 if (*ptr == initc)
1836 findc = ptr[2];
1837 backwards = FALSE;
1838 break;
1840 ptr += 2;
1841 if (*ptr == initc)
1843 findc = ptr[-2];
1844 backwards = TRUE;
1845 break;
1847 if (!*++ptr)
1848 break;
1850 if (findc)
1851 break;
1852 #ifdef FEAT_MBYTE
1853 if (has_mbyte)
1854 pos.col += (*mb_ptr2len)(linep + pos.col);
1855 else
1856 #endif
1857 ++pos.col;
1859 if (!findc)
1861 /* no brace in the line, maybe use " #if" then */
1862 if (!cpo_match && *skipwhite(linep) == '#')
1863 hash_dir = 1;
1864 else
1865 return NULL;
1867 else if (!cpo_bsl)
1869 int col, bslcnt = 0;
1871 /* Set "match_escaped" if there are an odd number of
1872 * backslashes. */
1873 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1874 bslcnt++;
1875 match_escaped = (bslcnt & 1);
1879 if (hash_dir)
1882 * Look for matching #if, #else, #elif, or #endif
1884 if (oap != NULL)
1885 oap->motion_type = MLINE; /* Linewise for this case only */
1886 if (initc != '#')
1888 ptr = skipwhite(skipwhite(linep) + 1);
1889 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1890 hash_dir = 1;
1891 else if (STRNCMP(ptr, "endif", 5) == 0)
1892 hash_dir = -1;
1893 else
1894 return NULL;
1896 pos.col = 0;
1897 while (!got_int)
1899 if (hash_dir > 0)
1901 if (pos.lnum == curbuf->b_ml.ml_line_count)
1902 break;
1904 else if (pos.lnum == 1)
1905 break;
1906 pos.lnum += hash_dir;
1907 linep = ml_get(pos.lnum);
1908 line_breakcheck(); /* check for CTRL-C typed */
1909 ptr = skipwhite(linep);
1910 if (*ptr != '#')
1911 continue;
1912 pos.col = (colnr_T) (ptr - linep);
1913 ptr = skipwhite(ptr + 1);
1914 if (hash_dir > 0)
1916 if (STRNCMP(ptr, "if", 2) == 0)
1917 count++;
1918 else if (STRNCMP(ptr, "el", 2) == 0)
1920 if (count == 0)
1921 return &pos;
1923 else if (STRNCMP(ptr, "endif", 5) == 0)
1925 if (count == 0)
1926 return &pos;
1927 count--;
1930 else
1932 if (STRNCMP(ptr, "if", 2) == 0)
1934 if (count == 0)
1935 return &pos;
1936 count--;
1938 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1940 if (count == 0)
1941 return &pos;
1943 else if (STRNCMP(ptr, "endif", 5) == 0)
1944 count++;
1947 return NULL;
1951 #ifdef FEAT_RIGHTLEFT
1952 /* This is just guessing: when 'rightleft' is set, search for a matching
1953 * paren/brace in the other direction. */
1954 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1955 backwards = !backwards;
1956 #endif
1958 do_quotes = -1;
1959 start_in_quotes = MAYBE;
1960 clearpos(&match_pos);
1962 /* backward search: Check if this line contains a single-line comment */
1963 if ((backwards && comment_dir)
1964 #ifdef FEAT_LISP
1965 || lisp
1966 #endif
1968 comment_col = check_linecomment(linep);
1969 #ifdef FEAT_LISP
1970 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1971 lispcomm = TRUE; /* find match inside this comment */
1972 #endif
1973 while (!got_int)
1976 * Go to the next position, forward or backward. We could use
1977 * inc() and dec() here, but that is much slower
1979 if (backwards)
1981 #ifdef FEAT_LISP
1982 /* char to match is inside of comment, don't search outside */
1983 if (lispcomm && pos.col < (colnr_T)comment_col)
1984 break;
1985 #endif
1986 if (pos.col == 0) /* at start of line, go to prev. one */
1988 if (pos.lnum == 1) /* start of file */
1989 break;
1990 --pos.lnum;
1992 if (maxtravel > 0 && ++traveled > maxtravel)
1993 break;
1995 linep = ml_get(pos.lnum);
1996 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1997 do_quotes = -1;
1998 line_breakcheck();
2000 /* Check if this line contains a single-line comment */
2001 if (comment_dir
2002 #ifdef FEAT_LISP
2003 || lisp
2004 #endif
2006 comment_col = check_linecomment(linep);
2007 #ifdef FEAT_LISP
2008 /* skip comment */
2009 if (lisp && comment_col != MAXCOL)
2010 pos.col = comment_col;
2011 #endif
2013 else
2015 --pos.col;
2016 #ifdef FEAT_MBYTE
2017 if (has_mbyte)
2018 pos.col -= (*mb_head_off)(linep, linep + pos.col);
2019 #endif
2022 else /* forward search */
2024 if (linep[pos.col] == NUL
2025 /* at end of line, go to next one */
2026 #ifdef FEAT_LISP
2027 /* don't search for match in comment */
2028 || (lisp && comment_col != MAXCOL
2029 && pos.col == (colnr_T)comment_col)
2030 #endif
2033 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
2034 #ifdef FEAT_LISP
2035 /* line is exhausted and comment with it,
2036 * don't search for match in code */
2037 || lispcomm
2038 #endif
2040 break;
2041 ++pos.lnum;
2043 if (maxtravel && traveled++ > maxtravel)
2044 break;
2046 linep = ml_get(pos.lnum);
2047 pos.col = 0;
2048 do_quotes = -1;
2049 line_breakcheck();
2050 #ifdef FEAT_LISP
2051 if (lisp) /* find comment pos in new line */
2052 comment_col = check_linecomment(linep);
2053 #endif
2055 else
2057 #ifdef FEAT_MBYTE
2058 if (has_mbyte)
2059 pos.col += (*mb_ptr2len)(linep + pos.col);
2060 else
2061 #endif
2062 ++pos.col;
2067 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2069 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2070 (linep[0] == '{' || linep[0] == '}'))
2072 if (linep[0] == findc && count == 0) /* match! */
2073 return &pos;
2074 break; /* out of scope */
2077 if (comment_dir)
2079 /* Note: comments do not nest, and we ignore quotes in them */
2080 /* TODO: ignore comment brackets inside strings */
2081 if (comment_dir == FORWARD)
2083 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2085 pos.col++;
2086 return &pos;
2089 else /* Searching backwards */
2092 * A comment may contain / * or / /, it may also start or end
2093 * with / * /. Ignore a / * after / /.
2095 if (pos.col == 0)
2096 continue;
2097 else if ( linep[pos.col - 1] == '/'
2098 && linep[pos.col] == '*'
2099 && (int)pos.col < comment_col)
2101 count++;
2102 match_pos = pos;
2103 match_pos.col--;
2105 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2107 if (count > 0)
2108 pos = match_pos;
2109 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2110 && (int)pos.col <= comment_col)
2111 pos.col -= 2;
2112 else if (ignore_cend)
2113 continue;
2114 else
2115 return NULL;
2116 return &pos;
2119 continue;
2123 * If smart matching ('cpoptions' does not contain '%'), braces inside
2124 * of quotes are ignored, but only if there is an even number of
2125 * quotes in the line.
2127 if (cpo_match)
2128 do_quotes = 0;
2129 else if (do_quotes == -1)
2132 * Count the number of quotes in the line, skipping \" and '"'.
2133 * Watch out for "\\".
2135 at_start = do_quotes;
2136 for (ptr = linep; *ptr; ++ptr)
2138 if (ptr == linep + pos.col + backwards)
2139 at_start = (do_quotes & 1);
2140 if (*ptr == '"'
2141 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2142 ++do_quotes;
2143 if (*ptr == '\\' && ptr[1] != NUL)
2144 ++ptr;
2146 do_quotes &= 1; /* result is 1 with even number of quotes */
2149 * If we find an uneven count, check current line and previous
2150 * one for a '\' at the end.
2152 if (!do_quotes)
2154 inquote = FALSE;
2155 if (ptr[-1] == '\\')
2157 do_quotes = 1;
2158 if (start_in_quotes == MAYBE)
2160 /* Do we need to use at_start here? */
2161 inquote = TRUE;
2162 start_in_quotes = TRUE;
2164 else if (backwards)
2165 inquote = TRUE;
2167 if (pos.lnum > 1)
2169 ptr = ml_get(pos.lnum - 1);
2170 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2172 do_quotes = 1;
2173 if (start_in_quotes == MAYBE)
2175 inquote = at_start;
2176 if (inquote)
2177 start_in_quotes = TRUE;
2179 else if (!backwards)
2180 inquote = TRUE;
2183 /* ml_get() only keeps one line, need to get linep again */
2184 linep = ml_get(pos.lnum);
2188 if (start_in_quotes == MAYBE)
2189 start_in_quotes = FALSE;
2192 * If 'smartmatch' is set:
2193 * Things inside quotes are ignored by setting 'inquote'. If we
2194 * find a quote without a preceding '\' invert 'inquote'. At the
2195 * end of a line not ending in '\' we reset 'inquote'.
2197 * In lines with an uneven number of quotes (without preceding '\')
2198 * we do not know which part to ignore. Therefore we only set
2199 * inquote if the number of quotes in a line is even, unless this
2200 * line or the previous one ends in a '\'. Complicated, isn't it?
2202 switch (c = linep[pos.col])
2204 case NUL:
2205 /* at end of line without trailing backslash, reset inquote */
2206 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2208 inquote = FALSE;
2209 start_in_quotes = FALSE;
2211 break;
2213 case '"':
2214 /* a quote that is preceded with an odd number of backslashes is
2215 * ignored */
2216 if (do_quotes)
2218 int col;
2220 for (col = pos.col - 1; col >= 0; --col)
2221 if (linep[col] != '\\')
2222 break;
2223 if ((((int)pos.col - 1 - col) & 1) == 0)
2225 inquote = !inquote;
2226 start_in_quotes = FALSE;
2229 break;
2232 * If smart matching ('cpoptions' does not contain '%'):
2233 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2234 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2235 * skipped, there is never a brace in them.
2236 * Ignore this when finding matches for `'.
2238 case '\'':
2239 if (!cpo_match && initc != '\'' && findc != '\'')
2241 if (backwards)
2243 if (pos.col > 1)
2245 if (linep[pos.col - 2] == '\'')
2247 pos.col -= 2;
2248 break;
2250 else if (linep[pos.col - 2] == '\\' &&
2251 pos.col > 2 && linep[pos.col - 3] == '\'')
2253 pos.col -= 3;
2254 break;
2258 else if (linep[pos.col + 1]) /* forward search */
2260 if (linep[pos.col + 1] == '\\' &&
2261 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2263 pos.col += 3;
2264 break;
2266 else if (linep[pos.col + 2] == '\'')
2268 pos.col += 2;
2269 break;
2273 /* FALLTHROUGH */
2275 default:
2276 #ifdef FEAT_LISP
2278 * For Lisp skip over backslashed (), {} and [].
2279 * (actually, we skip #\( et al)
2281 if (curbuf->b_p_lisp
2282 && vim_strchr((char_u *)"(){}[]", c) != NULL
2283 && pos.col > 1
2284 && check_prevcol(linep, pos.col, '\\', NULL)
2285 && check_prevcol(linep, pos.col - 1, '#', NULL))
2286 break;
2287 #endif
2289 /* Check for match outside of quotes, and inside of
2290 * quotes when the start is also inside of quotes. */
2291 if ((!inquote || start_in_quotes == TRUE)
2292 && (c == initc || c == findc))
2294 int col, bslcnt = 0;
2296 if (!cpo_bsl)
2298 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2299 bslcnt++;
2301 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2302 * what we expect. */
2303 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2305 if (c == initc)
2306 count++;
2307 else
2309 if (count == 0)
2310 return &pos;
2311 count--;
2318 if (comment_dir == BACKWARD && count > 0)
2320 pos = match_pos;
2321 return &pos;
2323 return (pos_T *)NULL; /* never found it */
2327 * Check if line[] contains a / / comment.
2328 * Return MAXCOL if not, otherwise return the column.
2329 * TODO: skip strings.
2331 static int
2332 check_linecomment(line)
2333 char_u *line;
2335 char_u *p;
2337 p = line;
2338 #ifdef FEAT_LISP
2339 /* skip Lispish one-line comments */
2340 if (curbuf->b_p_lisp)
2342 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2344 int instr = FALSE; /* inside of string */
2346 p = line; /* scan from start */
2347 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
2349 if (*p == '"')
2351 if (instr)
2353 if (*(p - 1) != '\\') /* skip escaped quote */
2354 instr = FALSE;
2356 else if (p == line || ((p - line) >= 2
2357 /* skip #\" form */
2358 && *(p - 1) != '\\' && *(p - 2) != '#'))
2359 instr = TRUE;
2361 else if (!instr && ((p - line) < 2
2362 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2363 break; /* found! */
2364 ++p;
2367 else
2368 p = NULL;
2370 else
2371 #endif
2372 while ((p = vim_strchr(p, '/')) != NULL)
2374 /* accept a double /, unless it's preceded with * and followed by *,
2375 * because * / / * is an end and start of a C comment */
2376 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
2377 break;
2378 ++p;
2381 if (p == NULL)
2382 return MAXCOL;
2383 return (int)(p - line);
2387 * Move cursor briefly to character matching the one under the cursor.
2388 * Used for Insert mode and "r" command.
2389 * Show the match only if it is visible on the screen.
2390 * If there isn't a match, then beep.
2392 void
2393 showmatch(c)
2394 int c; /* char to show match for */
2396 pos_T *lpos, save_cursor;
2397 pos_T mpos;
2398 colnr_T vcol;
2399 long save_so;
2400 long save_siso;
2401 #ifdef CURSOR_SHAPE
2402 int save_state;
2403 #endif
2404 colnr_T save_dollar_vcol;
2405 char_u *p;
2408 * Only show match for chars in the 'matchpairs' option.
2410 /* 'matchpairs' is "x:y,x:y" */
2411 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2413 #ifdef FEAT_RIGHTLEFT
2414 if (*p == c && (curwin->w_p_rl ^ p_ri))
2415 break;
2416 #endif
2417 p += 2;
2418 if (*p == c
2419 #ifdef FEAT_RIGHTLEFT
2420 && !(curwin->w_p_rl ^ p_ri)
2421 #endif
2423 break;
2424 if (p[1] != ',')
2425 return;
2428 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2429 vim_beep();
2430 else if (lpos->lnum >= curwin->w_topline)
2432 if (!curwin->w_p_wrap)
2433 getvcol(curwin, lpos, NULL, &vcol, NULL);
2434 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2435 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2437 mpos = *lpos; /* save the pos, update_screen() may change it */
2438 save_cursor = curwin->w_cursor;
2439 save_so = p_so;
2440 save_siso = p_siso;
2441 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2442 * stop displaying the "$". */
2443 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2444 dollar_vcol = 0;
2445 ++curwin->w_virtcol; /* do display ')' just before "$" */
2446 update_screen(VALID); /* show the new char first */
2448 save_dollar_vcol = dollar_vcol;
2449 #ifdef CURSOR_SHAPE
2450 save_state = State;
2451 State = SHOWMATCH;
2452 ui_cursor_shape(); /* may show different cursor shape */
2453 #endif
2454 curwin->w_cursor = mpos; /* move to matching char */
2455 p_so = 0; /* don't use 'scrolloff' here */
2456 p_siso = 0; /* don't use 'sidescrolloff' here */
2457 showruler(FALSE);
2458 setcursor();
2459 cursor_on(); /* make sure that the cursor is shown */
2460 out_flush();
2461 #ifdef FEAT_GUI
2462 if (gui.in_use)
2464 gui_update_cursor(TRUE, FALSE);
2465 gui_mch_flush();
2467 #endif
2468 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2469 * which resets it if the matching position is in a previous line
2470 * and has a higher column number. */
2471 dollar_vcol = save_dollar_vcol;
2474 * brief pause, unless 'm' is present in 'cpo' and a character is
2475 * available.
2477 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2478 ui_delay(p_mat * 100L, TRUE);
2479 else if (!char_avail())
2480 ui_delay(p_mat * 100L, FALSE);
2481 curwin->w_cursor = save_cursor; /* restore cursor position */
2482 p_so = save_so;
2483 p_siso = save_siso;
2484 #ifdef CURSOR_SHAPE
2485 State = save_state;
2486 ui_cursor_shape(); /* may show different cursor shape */
2487 #endif
2493 * findsent(dir, count) - Find the start of the next sentence in direction
2494 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
2495 * space or a line break. Also stop at an empty line.
2496 * Return OK if the next sentence was found.
2499 findsent(dir, count)
2500 int dir;
2501 long count;
2503 pos_T pos, tpos;
2504 int c;
2505 int (*func) __ARGS((pos_T *));
2506 int startlnum;
2507 int noskip = FALSE; /* do not skip blanks */
2508 int cpo_J;
2509 int found_dot;
2511 pos = curwin->w_cursor;
2512 if (dir == FORWARD)
2513 func = incl;
2514 else
2515 func = decl;
2517 while (count--)
2520 * if on an empty line, skip upto a non-empty line
2522 if (gchar_pos(&pos) == NUL)
2525 if ((*func)(&pos) == -1)
2526 break;
2527 while (gchar_pos(&pos) == NUL);
2528 if (dir == FORWARD)
2529 goto found;
2532 * if on the start of a paragraph or a section and searching forward,
2533 * go to the next line
2535 else if (dir == FORWARD && pos.col == 0 &&
2536 startPS(pos.lnum, NUL, FALSE))
2538 if (pos.lnum == curbuf->b_ml.ml_line_count)
2539 return FAIL;
2540 ++pos.lnum;
2541 goto found;
2543 else if (dir == BACKWARD)
2544 decl(&pos);
2546 /* go back to the previous non-blank char */
2547 found_dot = FALSE;
2548 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2549 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2551 if (vim_strchr((char_u *)".!?", c) != NULL)
2553 /* Only skip over a '.', '!' and '?' once. */
2554 if (found_dot)
2555 break;
2556 found_dot = TRUE;
2558 if (decl(&pos) == -1)
2559 break;
2560 /* when going forward: Stop in front of empty line */
2561 if (lineempty(pos.lnum) && dir == FORWARD)
2563 incl(&pos);
2564 goto found;
2568 /* remember the line where the search started */
2569 startlnum = pos.lnum;
2570 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2572 for (;;) /* find end of sentence */
2574 c = gchar_pos(&pos);
2575 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2577 if (dir == BACKWARD && pos.lnum != startlnum)
2578 ++pos.lnum;
2579 break;
2581 if (c == '.' || c == '!' || c == '?')
2583 tpos = pos;
2585 if ((c = inc(&tpos)) == -1)
2586 break;
2587 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2588 != NULL);
2589 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2590 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2591 && gchar_pos(&tpos) == ' ')))
2593 pos = tpos;
2594 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2595 inc(&pos);
2596 break;
2599 if ((*func)(&pos) == -1)
2601 if (count)
2602 return FAIL;
2603 noskip = TRUE;
2604 break;
2607 found:
2608 /* skip white space */
2609 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2610 if (incl(&pos) == -1)
2611 break;
2614 setpcmark();
2615 curwin->w_cursor = pos;
2616 return OK;
2620 * Find the next paragraph or section in direction 'dir'.
2621 * Paragraphs are currently supposed to be separated by empty lines.
2622 * If 'what' is NUL we go to the next paragraph.
2623 * If 'what' is '{' or '}' we go to the next section.
2624 * If 'both' is TRUE also stop at '}'.
2625 * Return TRUE if the next paragraph or section was found.
2628 findpar(pincl, dir, count, what, both)
2629 int *pincl; /* Return: TRUE if last char is to be included */
2630 int dir;
2631 long count;
2632 int what;
2633 int both;
2635 linenr_T curr;
2636 int did_skip; /* TRUE after separating lines have been skipped */
2637 int first; /* TRUE on first line */
2638 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
2639 #ifdef FEAT_FOLDING
2640 linenr_T fold_first; /* first line of a closed fold */
2641 linenr_T fold_last; /* last line of a closed fold */
2642 int fold_skipped; /* TRUE if a closed fold was skipped this
2643 iteration */
2644 #endif
2646 curr = curwin->w_cursor.lnum;
2648 while (count--)
2650 did_skip = FALSE;
2651 for (first = TRUE; ; first = FALSE)
2653 if (*ml_get(curr) != NUL)
2654 did_skip = TRUE;
2656 #ifdef FEAT_FOLDING
2657 /* skip folded lines */
2658 fold_skipped = FALSE;
2659 if (first && hasFolding(curr, &fold_first, &fold_last))
2661 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2662 fold_skipped = TRUE;
2664 #endif
2666 /* POSIX has it's own ideas of what a paragraph boundary is and it
2667 * doesn't match historical Vi: It also stops at a "{" in the
2668 * first column and at an empty line. */
2669 if (!first && did_skip && (startPS(curr, what, both)
2670 || (posix && what == NUL && *ml_get(curr) == '{')))
2671 break;
2673 #ifdef FEAT_FOLDING
2674 if (fold_skipped)
2675 curr -= dir;
2676 #endif
2677 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2679 if (count)
2680 return FALSE;
2681 curr -= dir;
2682 break;
2686 setpcmark();
2687 if (both && *ml_get(curr) == '}') /* include line with '}' */
2688 ++curr;
2689 curwin->w_cursor.lnum = curr;
2690 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2692 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2694 --curwin->w_cursor.col;
2695 *pincl = TRUE;
2698 else
2699 curwin->w_cursor.col = 0;
2700 return TRUE;
2704 * check if the string 's' is a nroff macro that is in option 'opt'
2706 static int
2707 inmacro(opt, s)
2708 char_u *opt;
2709 char_u *s;
2711 char_u *macro;
2713 for (macro = opt; macro[0]; ++macro)
2715 /* Accept two characters in the option being equal to two characters
2716 * in the line. A space in the option matches with a space in the
2717 * line or the line having ended. */
2718 if ( (macro[0] == s[0]
2719 || (macro[0] == ' '
2720 && (s[0] == NUL || s[0] == ' ')))
2721 && (macro[1] == s[1]
2722 || ((macro[1] == NUL || macro[1] == ' ')
2723 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2724 break;
2725 ++macro;
2726 if (macro[0] == NUL)
2727 break;
2729 return (macro[0] != NUL);
2733 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2734 * If 'para' is '{' or '}' only check for sections.
2735 * If 'both' is TRUE also stop at '}'
2738 startPS(lnum, para, both)
2739 linenr_T lnum;
2740 int para;
2741 int both;
2743 char_u *s;
2745 s = ml_get(lnum);
2746 if (*s == para || *s == '\f' || (both && *s == '}'))
2747 return TRUE;
2748 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2749 (!para && inmacro(p_para, s + 1))))
2750 return TRUE;
2751 return FALSE;
2755 * The following routines do the word searches performed by the 'w', 'W',
2756 * 'b', 'B', 'e', and 'E' commands.
2760 * To perform these searches, characters are placed into one of three
2761 * classes, and transitions between classes determine word boundaries.
2763 * The classes are:
2765 * 0 - white space
2766 * 1 - punctuation
2767 * 2 or higher - keyword characters (letters, digits and underscore)
2770 static int cls_bigword; /* TRUE for "W", "B" or "E" */
2773 * cls() - returns the class of character at curwin->w_cursor
2775 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2776 * from class 2 and higher are reported as class 1 since only white space
2777 * boundaries are of interest.
2779 static int
2780 cls()
2782 int c;
2784 c = gchar_cursor();
2785 #ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2786 if (p_altkeymap && c == F_BLANK)
2787 return 0;
2788 #endif
2789 if (c == ' ' || c == '\t' || c == NUL)
2790 return 0;
2791 #ifdef FEAT_MBYTE
2792 if (enc_dbcs != 0 && c > 0xFF)
2794 /* If cls_bigword, report multi-byte chars as class 1. */
2795 if (enc_dbcs == DBCS_KOR && cls_bigword)
2796 return 1;
2798 /* process code leading/trailing bytes */
2799 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2801 if (enc_utf8)
2803 c = utf_class(c);
2804 if (c != 0 && cls_bigword)
2805 return 1;
2806 return c;
2808 #endif
2810 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2811 if (cls_bigword)
2812 return 1;
2814 if (vim_iswordc(c))
2815 return 2;
2816 return 1;
2821 * fwd_word(count, type, eol) - move forward one word
2823 * Returns FAIL if the cursor was already at the end of the file.
2824 * If eol is TRUE, last word stops at end of line (for operators).
2827 fwd_word(count, bigword, eol)
2828 long count;
2829 int bigword; /* "W", "E" or "B" */
2830 int eol;
2832 int sclass; /* starting class */
2833 int i;
2834 int last_line;
2836 #ifdef FEAT_VIRTUALEDIT
2837 curwin->w_cursor.coladd = 0;
2838 #endif
2839 cls_bigword = bigword;
2840 while (--count >= 0)
2842 #ifdef FEAT_FOLDING
2843 /* When inside a range of folded lines, move to the last char of the
2844 * last line. */
2845 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2846 coladvance((colnr_T)MAXCOL);
2847 #endif
2848 sclass = cls();
2851 * We always move at least one character, unless on the last
2852 * character in the buffer.
2854 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2855 i = inc_cursor();
2856 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2857 return FAIL;
2858 if (i >= 1 && eol && count == 0) /* started at last char in line */
2859 return OK;
2862 * Go one char past end of current word (if any)
2864 if (sclass != 0)
2865 while (cls() == sclass)
2867 i = inc_cursor();
2868 if (i == -1 || (i >= 1 && eol && count == 0))
2869 return OK;
2873 * go to next non-white
2875 while (cls() == 0)
2878 * We'll stop if we land on a blank line
2880 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2881 break;
2883 i = inc_cursor();
2884 if (i == -1 || (i >= 1 && eol && count == 0))
2885 return OK;
2888 return OK;
2892 * bck_word() - move backward 'count' words
2894 * If stop is TRUE and we are already on the start of a word, move one less.
2896 * Returns FAIL if top of the file was reached.
2899 bck_word(count, bigword, stop)
2900 long count;
2901 int bigword;
2902 int stop;
2904 int sclass; /* starting class */
2906 #ifdef FEAT_VIRTUALEDIT
2907 curwin->w_cursor.coladd = 0;
2908 #endif
2909 cls_bigword = bigword;
2910 while (--count >= 0)
2912 #ifdef FEAT_FOLDING
2913 /* When inside a range of folded lines, move to the first char of the
2914 * first line. */
2915 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2916 curwin->w_cursor.col = 0;
2917 #endif
2918 sclass = cls();
2919 if (dec_cursor() == -1) /* started at start of file */
2920 return FAIL;
2922 if (!stop || sclass == cls() || sclass == 0)
2925 * Skip white space before the word.
2926 * Stop on an empty line.
2928 while (cls() == 0)
2930 if (curwin->w_cursor.col == 0
2931 && lineempty(curwin->w_cursor.lnum))
2932 goto finished;
2933 if (dec_cursor() == -1) /* hit start of file, stop here */
2934 return OK;
2938 * Move backward to start of this word.
2940 if (skip_chars(cls(), BACKWARD))
2941 return OK;
2944 inc_cursor(); /* overshot - forward one */
2945 finished:
2946 stop = FALSE;
2948 return OK;
2952 * end_word() - move to the end of the word
2954 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2955 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2956 * motion crosses blank lines. When the real vi crosses a blank line in an
2957 * 'e' motion, the cursor is placed on the FIRST character of the next
2958 * non-blank line. The 'E' command, however, works correctly. Since this
2959 * appears to be a bug, I have not duplicated it here.
2961 * Returns FAIL if end of the file was reached.
2963 * If stop is TRUE and we are already on the end of a word, move one less.
2964 * If empty is TRUE stop on an empty line.
2967 end_word(count, bigword, stop, empty)
2968 long count;
2969 int bigword;
2970 int stop;
2971 int empty;
2973 int sclass; /* starting class */
2975 #ifdef FEAT_VIRTUALEDIT
2976 curwin->w_cursor.coladd = 0;
2977 #endif
2978 cls_bigword = bigword;
2979 while (--count >= 0)
2981 #ifdef FEAT_FOLDING
2982 /* When inside a range of folded lines, move to the last char of the
2983 * last line. */
2984 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2985 coladvance((colnr_T)MAXCOL);
2986 #endif
2987 sclass = cls();
2988 if (inc_cursor() == -1)
2989 return FAIL;
2992 * If we're in the middle of a word, we just have to move to the end
2993 * of it.
2995 if (cls() == sclass && sclass != 0)
2998 * Move forward to end of the current word
3000 if (skip_chars(sclass, FORWARD))
3001 return FAIL;
3003 else if (!stop || sclass == 0)
3006 * We were at the end of a word. Go to the end of the next word.
3007 * First skip white space, if 'empty' is TRUE, stop at empty line.
3009 while (cls() == 0)
3011 if (empty && curwin->w_cursor.col == 0
3012 && lineempty(curwin->w_cursor.lnum))
3013 goto finished;
3014 if (inc_cursor() == -1) /* hit end of file, stop here */
3015 return FAIL;
3019 * Move forward to the end of this word.
3021 if (skip_chars(cls(), FORWARD))
3022 return FAIL;
3024 dec_cursor(); /* overshot - one char backward */
3025 finished:
3026 stop = FALSE; /* we move only one word less */
3028 return OK;
3032 * Move back to the end of the word.
3034 * Returns FAIL if start of the file was reached.
3037 bckend_word(count, bigword, eol)
3038 long count;
3039 int bigword; /* TRUE for "B" */
3040 int eol; /* TRUE: stop at end of line. */
3042 int sclass; /* starting class */
3043 int i;
3045 #ifdef FEAT_VIRTUALEDIT
3046 curwin->w_cursor.coladd = 0;
3047 #endif
3048 cls_bigword = bigword;
3049 while (--count >= 0)
3051 sclass = cls();
3052 if ((i = dec_cursor()) == -1)
3053 return FAIL;
3054 if (eol && i == 1)
3055 return OK;
3058 * Move backward to before the start of this word.
3060 if (sclass != 0)
3062 while (cls() == sclass)
3063 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3064 return OK;
3068 * Move backward to end of the previous word
3070 while (cls() == 0)
3072 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3073 break;
3074 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3075 return OK;
3078 return OK;
3082 * Skip a row of characters of the same class.
3083 * Return TRUE when end-of-file reached, FALSE otherwise.
3085 static int
3086 skip_chars(cclass, dir)
3087 int cclass;
3088 int dir;
3090 while (cls() == cclass)
3091 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3092 return TRUE;
3093 return FALSE;
3096 #ifdef FEAT_TEXTOBJ
3098 * Go back to the start of the word or the start of white space
3100 static void
3101 back_in_line()
3103 int sclass; /* starting class */
3105 sclass = cls();
3106 for (;;)
3108 if (curwin->w_cursor.col == 0) /* stop at start of line */
3109 break;
3110 dec_cursor();
3111 if (cls() != sclass) /* stop at start of word */
3113 inc_cursor();
3114 break;
3119 static void
3120 find_first_blank(posp)
3121 pos_T *posp;
3123 int c;
3125 while (decl(posp) != -1)
3127 c = gchar_pos(posp);
3128 if (!vim_iswhite(c))
3130 incl(posp);
3131 break;
3137 * Skip count/2 sentences and count/2 separating white spaces.
3139 static void
3140 findsent_forward(count, at_start_sent)
3141 long count;
3142 int at_start_sent; /* cursor is at start of sentence */
3144 while (count--)
3146 findsent(FORWARD, 1L);
3147 if (at_start_sent)
3148 find_first_blank(&curwin->w_cursor);
3149 if (count == 0 || at_start_sent)
3150 decl(&curwin->w_cursor);
3151 at_start_sent = !at_start_sent;
3156 * Find word under cursor, cursor at end.
3157 * Used while an operator is pending, and in Visual mode.
3160 current_word(oap, count, include, bigword)
3161 oparg_T *oap;
3162 long count;
3163 int include; /* TRUE: include word and white space */
3164 int bigword; /* FALSE == word, TRUE == WORD */
3166 pos_T start_pos;
3167 pos_T pos;
3168 int inclusive = TRUE;
3169 int include_white = FALSE;
3171 cls_bigword = bigword;
3172 clearpos(&start_pos);
3174 #ifdef FEAT_VISUAL
3175 /* Correct cursor when 'selection' is exclusive */
3176 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3177 dec_cursor();
3180 * When Visual mode is not active, or when the VIsual area is only one
3181 * character, select the word and/or white space under the cursor.
3183 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3184 #endif
3187 * Go to start of current word or white space.
3189 back_in_line();
3190 start_pos = curwin->w_cursor;
3193 * If the start is on white space, and white space should be included
3194 * (" word"), or start is not on white space, and white space should
3195 * not be included ("word"), find end of word.
3197 if ((cls() == 0) == include)
3199 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3200 return FAIL;
3202 else
3205 * If the start is not on white space, and white space should be
3206 * included ("word "), or start is on white space and white
3207 * space should not be included (" "), find start of word.
3208 * If we end up in the first column of the next line (single char
3209 * word) back up to end of the line.
3211 fwd_word(1L, bigword, TRUE);
3212 if (curwin->w_cursor.col == 0)
3213 decl(&curwin->w_cursor);
3214 else
3215 oneleft();
3217 if (include)
3218 include_white = TRUE;
3221 #ifdef FEAT_VISUAL
3222 if (VIsual_active)
3224 /* should do something when inclusive == FALSE ! */
3225 VIsual = start_pos;
3226 redraw_curbuf_later(INVERTED); /* update the inversion */
3228 else
3229 #endif
3231 oap->start = start_pos;
3232 oap->motion_type = MCHAR;
3234 --count;
3238 * When count is still > 0, extend with more objects.
3240 while (count > 0)
3242 inclusive = TRUE;
3243 #ifdef FEAT_VISUAL
3244 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3247 * In Visual mode, with cursor at start: move cursor back.
3249 if (decl(&curwin->w_cursor) == -1)
3250 return FAIL;
3251 if (include != (cls() != 0))
3253 if (bck_word(1L, bigword, TRUE) == FAIL)
3254 return FAIL;
3256 else
3258 if (bckend_word(1L, bigword, TRUE) == FAIL)
3259 return FAIL;
3260 (void)incl(&curwin->w_cursor);
3263 else
3264 #endif
3267 * Move cursor forward one word and/or white area.
3269 if (incl(&curwin->w_cursor) == -1)
3270 return FAIL;
3271 if (include != (cls() == 0))
3273 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
3274 return FAIL;
3276 * If end is just past a new-line, we don't want to include
3277 * the first character on the line.
3278 * Put cursor on last char of white.
3280 if (oneleft() == FAIL)
3281 inclusive = FALSE;
3283 else
3285 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3286 return FAIL;
3289 --count;
3292 if (include_white && (cls() != 0
3293 || (curwin->w_cursor.col == 0 && !inclusive)))
3296 * If we don't include white space at the end, move the start
3297 * to include some white space there. This makes "daw" work
3298 * better on the last word in a sentence (and "2daw" on last-but-one
3299 * word). Also when "2daw" deletes "word." at the end of the line
3300 * (cursor is at start of next line).
3301 * But don't delete white space at start of line (indent).
3303 pos = curwin->w_cursor; /* save cursor position */
3304 curwin->w_cursor = start_pos;
3305 if (oneleft() == OK)
3307 back_in_line();
3308 if (cls() == 0 && curwin->w_cursor.col > 0)
3310 #ifdef FEAT_VISUAL
3311 if (VIsual_active)
3312 VIsual = curwin->w_cursor;
3313 else
3314 #endif
3315 oap->start = curwin->w_cursor;
3318 curwin->w_cursor = pos; /* put cursor back at end */
3321 #ifdef FEAT_VISUAL
3322 if (VIsual_active)
3324 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3325 inc_cursor();
3326 if (VIsual_mode == 'V')
3328 VIsual_mode = 'v';
3329 redraw_cmdline = TRUE; /* show mode later */
3332 else
3333 #endif
3334 oap->inclusive = inclusive;
3336 return OK;
3340 * Find sentence(s) under the cursor, cursor at end.
3341 * When Visual active, extend it by one or more sentences.
3344 current_sent(oap, count, include)
3345 oparg_T *oap;
3346 long count;
3347 int include;
3349 pos_T start_pos;
3350 pos_T pos;
3351 int start_blank;
3352 int c;
3353 int at_start_sent;
3354 long ncount;
3356 start_pos = curwin->w_cursor;
3357 pos = start_pos;
3358 findsent(FORWARD, 1L); /* Find start of next sentence. */
3360 #ifdef FEAT_VISUAL
3362 * When visual area is bigger than one character: Extend it.
3364 if (VIsual_active && !equalpos(start_pos, VIsual))
3366 extend:
3367 if (lt(start_pos, VIsual))
3370 * Cursor at start of Visual area.
3371 * Find out where we are:
3372 * - in the white space before a sentence
3373 * - in a sentence or just after it
3374 * - at the start of a sentence
3376 at_start_sent = TRUE;
3377 decl(&pos);
3378 while (lt(pos, curwin->w_cursor))
3380 c = gchar_pos(&pos);
3381 if (!vim_iswhite(c))
3383 at_start_sent = FALSE;
3384 break;
3386 incl(&pos);
3388 if (!at_start_sent)
3390 findsent(BACKWARD, 1L);
3391 if (equalpos(curwin->w_cursor, start_pos))
3392 at_start_sent = TRUE; /* exactly at start of sentence */
3393 else
3394 /* inside a sentence, go to its end (start of next) */
3395 findsent(FORWARD, 1L);
3397 if (include) /* "as" gets twice as much as "is" */
3398 count *= 2;
3399 while (count--)
3401 if (at_start_sent)
3402 find_first_blank(&curwin->w_cursor);
3403 c = gchar_cursor();
3404 if (!at_start_sent || (!include && !vim_iswhite(c)))
3405 findsent(BACKWARD, 1L);
3406 at_start_sent = !at_start_sent;
3409 else
3412 * Cursor at end of Visual area.
3413 * Find out where we are:
3414 * - just before a sentence
3415 * - just before or in the white space before a sentence
3416 * - in a sentence
3418 incl(&pos);
3419 at_start_sent = TRUE;
3420 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3422 at_start_sent = FALSE;
3423 while (lt(pos, curwin->w_cursor))
3425 c = gchar_pos(&pos);
3426 if (!vim_iswhite(c))
3428 at_start_sent = TRUE;
3429 break;
3431 incl(&pos);
3433 if (at_start_sent) /* in the sentence */
3434 findsent(BACKWARD, 1L);
3435 else /* in/before white before a sentence */
3436 curwin->w_cursor = start_pos;
3439 if (include) /* "as" gets twice as much as "is" */
3440 count *= 2;
3441 findsent_forward(count, at_start_sent);
3442 if (*p_sel == 'e')
3443 ++curwin->w_cursor.col;
3445 return OK;
3447 #endif
3450 * If cursor started on blank, check if it is just before the start of the
3451 * next sentence.
3453 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3454 incl(&pos);
3455 if (equalpos(pos, curwin->w_cursor))
3457 start_blank = TRUE;
3458 find_first_blank(&start_pos); /* go back to first blank */
3460 else
3462 start_blank = FALSE;
3463 findsent(BACKWARD, 1L);
3464 start_pos = curwin->w_cursor;
3466 if (include)
3467 ncount = count * 2;
3468 else
3470 ncount = count;
3471 if (start_blank)
3472 --ncount;
3474 if (ncount > 0)
3475 findsent_forward(ncount, TRUE);
3476 else
3477 decl(&curwin->w_cursor);
3479 if (include)
3482 * If the blank in front of the sentence is included, exclude the
3483 * blanks at the end of the sentence, go back to the first blank.
3484 * If there are no trailing blanks, try to include leading blanks.
3486 if (start_blank)
3488 find_first_blank(&curwin->w_cursor);
3489 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3490 if (vim_iswhite(c))
3491 decl(&curwin->w_cursor);
3493 else if (c = gchar_cursor(), !vim_iswhite(c))
3494 find_first_blank(&start_pos);
3497 #ifdef FEAT_VISUAL
3498 if (VIsual_active)
3500 /* avoid getting stuck with "is" on a single space before a sent. */
3501 if (equalpos(start_pos, curwin->w_cursor))
3502 goto extend;
3503 if (*p_sel == 'e')
3504 ++curwin->w_cursor.col;
3505 VIsual = start_pos;
3506 VIsual_mode = 'v';
3507 redraw_curbuf_later(INVERTED); /* update the inversion */
3509 else
3510 #endif
3512 /* include a newline after the sentence, if there is one */
3513 if (incl(&curwin->w_cursor) == -1)
3514 oap->inclusive = TRUE;
3515 else
3516 oap->inclusive = FALSE;
3517 oap->start = start_pos;
3518 oap->motion_type = MCHAR;
3520 return OK;
3524 * Find block under the cursor, cursor at end.
3525 * "what" and "other" are two matching parenthesis/paren/etc.
3528 current_block(oap, count, include, what, other)
3529 oparg_T *oap;
3530 long count;
3531 int include; /* TRUE == include white space */
3532 int what; /* '(', '{', etc. */
3533 int other; /* ')', '}', etc. */
3535 pos_T old_pos;
3536 pos_T *pos = NULL;
3537 pos_T start_pos;
3538 pos_T *end_pos;
3539 pos_T old_start, old_end;
3540 char_u *save_cpo;
3541 int sol = FALSE; /* '{' at start of line */
3543 old_pos = curwin->w_cursor;
3544 old_end = curwin->w_cursor; /* remember where we started */
3545 old_start = old_end;
3548 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3550 #ifdef FEAT_VISUAL
3551 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3552 #endif
3554 setpcmark();
3555 if (what == '{') /* ignore indent */
3556 while (inindent(1))
3557 if (inc_cursor() != 0)
3558 break;
3559 if (gchar_cursor() == what)
3560 /* cursor on '(' or '{', move cursor just after it */
3561 ++curwin->w_cursor.col;
3563 #ifdef FEAT_VISUAL
3564 else if (lt(VIsual, curwin->w_cursor))
3566 old_start = VIsual;
3567 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3569 else
3570 old_end = VIsual;
3571 #endif
3574 * Search backwards for unclosed '(', '{', etc..
3575 * Put this position in start_pos.
3576 * Ignore quotes here.
3578 save_cpo = p_cpo;
3579 p_cpo = (char_u *)"%";
3580 while (count-- > 0)
3582 if ((pos = findmatch(NULL, what)) == NULL)
3583 break;
3584 curwin->w_cursor = *pos;
3585 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3587 p_cpo = save_cpo;
3590 * Search for matching ')', '}', etc.
3591 * Put this position in curwin->w_cursor.
3593 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3595 curwin->w_cursor = old_pos;
3596 return FAIL;
3598 curwin->w_cursor = *end_pos;
3601 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3602 * If the ending '}' is only preceded by indent, skip that indent.
3603 * But only if the resulting area is not smaller than what we started with.
3605 while (!include)
3607 incl(&start_pos);
3608 sol = (curwin->w_cursor.col == 0);
3609 decl(&curwin->w_cursor);
3610 if (what == '{')
3611 while (inindent(1))
3613 sol = TRUE;
3614 if (decl(&curwin->w_cursor) != 0)
3615 break;
3617 #ifdef FEAT_VISUAL
3619 * In Visual mode, when the resulting area is not bigger than what we
3620 * started with, extend it to the next block, and then exclude again.
3622 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3623 && VIsual_active)
3625 curwin->w_cursor = old_start;
3626 decl(&curwin->w_cursor);
3627 if ((pos = findmatch(NULL, what)) == NULL)
3629 curwin->w_cursor = old_pos;
3630 return FAIL;
3632 start_pos = *pos;
3633 curwin->w_cursor = *pos;
3634 if ((end_pos = findmatch(NULL, other)) == NULL)
3636 curwin->w_cursor = old_pos;
3637 return FAIL;
3639 curwin->w_cursor = *end_pos;
3641 else
3642 #endif
3643 break;
3646 #ifdef FEAT_VISUAL
3647 if (VIsual_active)
3649 if (*p_sel == 'e')
3650 ++curwin->w_cursor.col;
3651 if (sol && gchar_cursor() != NUL)
3652 inc(&curwin->w_cursor); /* include the line break */
3653 VIsual = start_pos;
3654 VIsual_mode = 'v';
3655 redraw_curbuf_later(INVERTED); /* update the inversion */
3656 showmode();
3658 else
3659 #endif
3661 oap->start = start_pos;
3662 oap->motion_type = MCHAR;
3663 oap->inclusive = FALSE;
3664 if (sol)
3665 incl(&curwin->w_cursor);
3666 else if (ltoreq(start_pos, curwin->w_cursor))
3667 /* Include the character under the cursor. */
3668 oap->inclusive = TRUE;
3669 else
3670 /* End is before the start (no text in between <>, [], etc.): don't
3671 * operate on any text. */
3672 curwin->w_cursor = start_pos;
3675 return OK;
3678 static int in_html_tag __ARGS((int));
3681 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3682 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3684 static int
3685 in_html_tag(end_tag)
3686 int end_tag;
3688 char_u *line = ml_get_curline();
3689 char_u *p;
3690 int c;
3691 int lc = NUL;
3692 pos_T pos;
3694 #ifdef FEAT_MBYTE
3695 if (enc_dbcs)
3697 char_u *lp = NULL;
3699 /* We search forward until the cursor, because searching backwards is
3700 * very slow for DBCS encodings. */
3701 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3702 if (*p == '>' || *p == '<')
3704 lc = *p;
3705 lp = p;
3707 if (*p != '<') /* check for '<' under cursor */
3709 if (lc != '<')
3710 return FALSE;
3711 p = lp;
3714 else
3715 #endif
3717 for (p = line + curwin->w_cursor.col; p > line; )
3719 if (*p == '<') /* find '<' under/before cursor */
3720 break;
3721 mb_ptr_back(line, p);
3722 if (*p == '>') /* find '>' before cursor */
3723 break;
3725 if (*p != '<')
3726 return FALSE;
3729 pos.lnum = curwin->w_cursor.lnum;
3730 pos.col = (colnr_T)(p - line);
3732 mb_ptr_adv(p);
3733 if (end_tag)
3734 /* check that there is a '/' after the '<' */
3735 return *p == '/';
3737 /* check that there is no '/' after the '<' */
3738 if (*p == '/')
3739 return FALSE;
3741 /* check that the matching '>' is not preceded by '/' */
3742 for (;;)
3744 if (inc(&pos) < 0)
3745 return FALSE;
3746 c = *ml_get_pos(&pos);
3747 if (c == '>')
3748 break;
3749 lc = c;
3751 return lc != '/';
3755 * Find tag block under the cursor, cursor at end.
3758 current_tagblock(oap, count_arg, include)
3759 oparg_T *oap;
3760 long count_arg;
3761 int include; /* TRUE == include white space */
3763 long count = count_arg;
3764 long n;
3765 pos_T old_pos;
3766 pos_T start_pos;
3767 pos_T end_pos;
3768 pos_T old_start, old_end;
3769 char_u *spat, *epat;
3770 char_u *p;
3771 char_u *cp;
3772 int len;
3773 int r;
3774 int do_include = include;
3775 int save_p_ws = p_ws;
3776 int retval = FAIL;
3778 p_ws = FALSE;
3780 old_pos = curwin->w_cursor;
3781 old_end = curwin->w_cursor; /* remember where we started */
3782 old_start = old_end;
3783 #ifdef FEAT_VISUAL
3784 if (!VIsual_active || *p_sel == 'e')
3785 #endif
3786 decl(&old_end); /* old_end is inclusive */
3789 * If we start on "<aaa>" select that block.
3791 #ifdef FEAT_VISUAL
3792 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3793 #endif
3795 setpcmark();
3797 /* ignore indent */
3798 while (inindent(1))
3799 if (inc_cursor() != 0)
3800 break;
3802 if (in_html_tag(FALSE))
3804 /* cursor on start tag, move to its '>' */
3805 while (*ml_get_cursor() != '>')
3806 if (inc_cursor() < 0)
3807 break;
3809 else if (in_html_tag(TRUE))
3811 /* cursor on end tag, move to just before it */
3812 while (*ml_get_cursor() != '<')
3813 if (dec_cursor() < 0)
3814 break;
3815 dec_cursor();
3816 old_end = curwin->w_cursor;
3819 #ifdef FEAT_VISUAL
3820 else if (lt(VIsual, curwin->w_cursor))
3822 old_start = VIsual;
3823 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3825 else
3826 old_end = VIsual;
3827 #endif
3829 again:
3831 * Search backwards for unclosed "<aaa>".
3832 * Put this position in start_pos.
3834 for (n = 0; n < count; ++n)
3836 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
3837 (char_u *)"",
3838 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
3839 NULL, (linenr_T)0, 0L) <= 0)
3841 curwin->w_cursor = old_pos;
3842 goto theend;
3845 start_pos = curwin->w_cursor;
3848 * Search for matching "</aaa>". First isolate the "aaa".
3850 inc_cursor();
3851 p = ml_get_cursor();
3852 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3854 len = (int)(cp - p);
3855 if (len == 0)
3857 curwin->w_cursor = old_pos;
3858 goto theend;
3860 spat = alloc(len + 29);
3861 epat = alloc(len + 9);
3862 if (spat == NULL || epat == NULL)
3864 vim_free(spat);
3865 vim_free(epat);
3866 curwin->w_cursor = old_pos;
3867 goto theend;
3869 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3870 sprintf((char *)epat, "</%.*s>\\c", len, p);
3872 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
3873 0, NULL, (linenr_T)0, 0L);
3875 vim_free(spat);
3876 vim_free(epat);
3878 if (r < 1 || lt(curwin->w_cursor, old_end))
3880 /* Can't find other end or it's before the previous end. Could be a
3881 * HTML tag that doesn't have a matching end. Search backwards for
3882 * another starting tag. */
3883 count = 1;
3884 curwin->w_cursor = start_pos;
3885 goto again;
3888 if (do_include || r < 1)
3890 /* Include up to the '>'. */
3891 while (*ml_get_cursor() != '>')
3892 if (inc_cursor() < 0)
3893 break;
3895 else
3897 /* Exclude the '<' of the end tag. */
3898 if (*ml_get_cursor() == '<')
3899 dec_cursor();
3901 end_pos = curwin->w_cursor;
3903 if (!do_include)
3905 /* Exclude the start tag. */
3906 curwin->w_cursor = start_pos;
3907 while (inc_cursor() >= 0)
3908 if (*ml_get_cursor() == '>')
3910 inc_cursor();
3911 start_pos = curwin->w_cursor;
3912 break;
3914 curwin->w_cursor = end_pos;
3916 /* If we now have the same text as before reset "do_include" and try
3917 * again. */
3918 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
3920 do_include = TRUE;
3921 curwin->w_cursor = old_start;
3922 count = count_arg;
3923 goto again;
3927 #ifdef FEAT_VISUAL
3928 if (VIsual_active)
3930 /* If the end is before the start there is no text between tags, select
3931 * the char under the cursor. */
3932 if (lt(end_pos, start_pos))
3933 curwin->w_cursor = start_pos;
3934 else if (*p_sel == 'e')
3935 ++curwin->w_cursor.col;
3936 VIsual = start_pos;
3937 VIsual_mode = 'v';
3938 redraw_curbuf_later(INVERTED); /* update the inversion */
3939 showmode();
3941 else
3942 #endif
3944 oap->start = start_pos;
3945 oap->motion_type = MCHAR;
3946 if (lt(end_pos, start_pos))
3948 /* End is before the start: there is no text between tags; operate
3949 * on an empty area. */
3950 curwin->w_cursor = start_pos;
3951 oap->inclusive = FALSE;
3953 else
3954 oap->inclusive = TRUE;
3956 retval = OK;
3958 theend:
3959 p_ws = save_p_ws;
3960 return retval;
3964 current_par(oap, count, include, type)
3965 oparg_T *oap;
3966 long count;
3967 int include; /* TRUE == include white space */
3968 int type; /* 'p' for paragraph, 'S' for section */
3970 linenr_T start_lnum;
3971 linenr_T end_lnum;
3972 int white_in_front;
3973 int dir;
3974 int start_is_white;
3975 int prev_start_is_white;
3976 int retval = OK;
3977 int do_white = FALSE;
3978 int t;
3979 int i;
3981 if (type == 'S') /* not implemented yet */
3982 return FAIL;
3984 start_lnum = curwin->w_cursor.lnum;
3986 #ifdef FEAT_VISUAL
3988 * When visual area is more than one line: extend it.
3990 if (VIsual_active && start_lnum != VIsual.lnum)
3992 extend:
3993 if (start_lnum < VIsual.lnum)
3994 dir = BACKWARD;
3995 else
3996 dir = FORWARD;
3997 for (i = count; --i >= 0; )
3999 if (start_lnum ==
4000 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
4002 retval = FAIL;
4003 break;
4006 prev_start_is_white = -1;
4007 for (t = 0; t < 2; ++t)
4009 start_lnum += dir;
4010 start_is_white = linewhite(start_lnum);
4011 if (prev_start_is_white == start_is_white)
4013 start_lnum -= dir;
4014 break;
4016 for (;;)
4018 if (start_lnum == (dir == BACKWARD
4019 ? 1 : curbuf->b_ml.ml_line_count))
4020 break;
4021 if (start_is_white != linewhite(start_lnum + dir)
4022 || (!start_is_white
4023 && startPS(start_lnum + (dir > 0
4024 ? 1 : 0), 0, 0)))
4025 break;
4026 start_lnum += dir;
4028 if (!include)
4029 break;
4030 if (start_lnum == (dir == BACKWARD
4031 ? 1 : curbuf->b_ml.ml_line_count))
4032 break;
4033 prev_start_is_white = start_is_white;
4036 curwin->w_cursor.lnum = start_lnum;
4037 curwin->w_cursor.col = 0;
4038 return retval;
4040 #endif
4043 * First move back to the start_lnum of the paragraph or white lines
4045 white_in_front = linewhite(start_lnum);
4046 while (start_lnum > 1)
4048 if (white_in_front) /* stop at first white line */
4050 if (!linewhite(start_lnum - 1))
4051 break;
4053 else /* stop at first non-white line of start of paragraph */
4055 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4056 break;
4058 --start_lnum;
4062 * Move past the end of any white lines.
4064 end_lnum = start_lnum;
4065 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4066 ++end_lnum;
4068 --end_lnum;
4069 i = count;
4070 if (!include && white_in_front)
4071 --i;
4072 while (i--)
4074 if (end_lnum == curbuf->b_ml.ml_line_count)
4075 return FAIL;
4077 if (!include)
4078 do_white = linewhite(end_lnum + 1);
4080 if (include || !do_white)
4082 ++end_lnum;
4084 * skip to end of paragraph
4086 while (end_lnum < curbuf->b_ml.ml_line_count
4087 && !linewhite(end_lnum + 1)
4088 && !startPS(end_lnum + 1, 0, 0))
4089 ++end_lnum;
4092 if (i == 0 && white_in_front && include)
4093 break;
4096 * skip to end of white lines after paragraph
4098 if (include || do_white)
4099 while (end_lnum < curbuf->b_ml.ml_line_count
4100 && linewhite(end_lnum + 1))
4101 ++end_lnum;
4105 * If there are no empty lines at the end, try to find some empty lines at
4106 * the start (unless that has been done already).
4108 if (!white_in_front && !linewhite(end_lnum) && include)
4109 while (start_lnum > 1 && linewhite(start_lnum - 1))
4110 --start_lnum;
4112 #ifdef FEAT_VISUAL
4113 if (VIsual_active)
4115 /* Problem: when doing "Vipipip" nothing happens in a single white
4116 * line, we get stuck there. Trap this here. */
4117 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4118 goto extend;
4119 VIsual.lnum = start_lnum;
4120 VIsual_mode = 'V';
4121 redraw_curbuf_later(INVERTED); /* update the inversion */
4122 showmode();
4124 else
4125 #endif
4127 oap->start.lnum = start_lnum;
4128 oap->start.col = 0;
4129 oap->motion_type = MLINE;
4131 curwin->w_cursor.lnum = end_lnum;
4132 curwin->w_cursor.col = 0;
4134 return OK;
4137 static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4138 static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4141 * Search quote char from string line[col].
4142 * Quote character escaped by one of the characters in "escape" is not counted
4143 * as a quote.
4144 * Returns column number of "quotechar" or -1 when not found.
4146 static int
4147 find_next_quote(line, col, quotechar, escape)
4148 char_u *line;
4149 int col;
4150 int quotechar;
4151 char_u *escape; /* escape characters, can be NULL */
4153 int c;
4155 for (;;)
4157 c = line[col];
4158 if (c == NUL)
4159 return -1;
4160 else if (escape != NULL && vim_strchr(escape, c))
4161 ++col;
4162 else if (c == quotechar)
4163 break;
4164 #ifdef FEAT_MBYTE
4165 if (has_mbyte)
4166 col += (*mb_ptr2len)(line + col);
4167 else
4168 #endif
4169 ++col;
4171 return col;
4175 * Search backwards in "line" from column "col_start" to find "quotechar".
4176 * Quote character escaped by one of the characters in "escape" is not counted
4177 * as a quote.
4178 * Return the found column or zero.
4180 static int
4181 find_prev_quote(line, col_start, quotechar, escape)
4182 char_u *line;
4183 int col_start;
4184 int quotechar;
4185 char_u *escape; /* escape characters, can be NULL */
4187 int n;
4189 while (col_start > 0)
4191 --col_start;
4192 #ifdef FEAT_MBYTE
4193 col_start -= (*mb_head_off)(line, line + col_start);
4194 #endif
4195 n = 0;
4196 if (escape != NULL)
4197 while (col_start - n > 0 && vim_strchr(escape,
4198 line[col_start - n - 1]) != NULL)
4199 ++n;
4200 if (n & 1)
4201 col_start -= n; /* uneven number of escape chars, skip it */
4202 else if (line[col_start] == quotechar)
4203 break;
4205 return col_start;
4209 * Find quote under the cursor, cursor at end.
4210 * Returns TRUE if found, else FALSE.
4213 current_quote(oap, count, include, quotechar)
4214 oparg_T *oap;
4215 long count;
4216 int include; /* TRUE == include quote char */
4217 int quotechar; /* Quote character */
4219 char_u *line = ml_get_curline();
4220 int col_end;
4221 int col_start = curwin->w_cursor.col;
4222 int inclusive = FALSE;
4223 #ifdef FEAT_VISUAL
4224 int vis_empty = TRUE; /* Visual selection <= 1 char */
4225 int vis_bef_curs = FALSE; /* Visual starts before cursor */
4226 int inside_quotes = FALSE; /* Looks like "i'" done before */
4227 int selected_quote = FALSE; /* Has quote inside selection */
4228 int i;
4230 /* Correct cursor when 'selection' is exclusive */
4231 if (VIsual_active)
4233 vis_bef_curs = lt(VIsual, curwin->w_cursor);
4234 if (*p_sel == 'e' && vis_bef_curs)
4235 dec_cursor();
4236 vis_empty = equalpos(VIsual, curwin->w_cursor);
4239 if (!vis_empty)
4241 /* Check if the existing selection exactly spans the text inside
4242 * quotes. */
4243 if (vis_bef_curs)
4245 inside_quotes = VIsual.col > 0
4246 && line[VIsual.col - 1] == quotechar
4247 && line[curwin->w_cursor.col] != NUL
4248 && line[curwin->w_cursor.col + 1] == quotechar;
4249 i = VIsual.col;
4250 col_end = curwin->w_cursor.col;
4252 else
4254 inside_quotes = curwin->w_cursor.col > 0
4255 && line[curwin->w_cursor.col - 1] == quotechar
4256 && line[VIsual.col] != NUL
4257 && line[VIsual.col + 1] == quotechar;
4258 i = curwin->w_cursor.col;
4259 col_end = VIsual.col;
4262 /* Find out if we have a quote in the selection. */
4263 while (i <= col_end)
4264 if (line[i++] == quotechar)
4266 selected_quote = TRUE;
4267 break;
4271 if (!vis_empty && line[col_start] == quotechar)
4273 /* Already selecting something and on a quote character. Find the
4274 * next quoted string. */
4275 if (vis_bef_curs)
4277 /* Assume we are on a closing quote: move to after the next
4278 * opening quote. */
4279 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4280 if (col_start < 0)
4281 return FALSE;
4282 col_end = find_next_quote(line, col_start + 1, quotechar,
4283 curbuf->b_p_qe);
4284 if (col_end < 0)
4286 /* We were on a starting quote perhaps? */
4287 col_end = col_start;
4288 col_start = curwin->w_cursor.col;
4291 else
4293 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4294 if (line[col_end] != quotechar)
4295 return FALSE;
4296 col_start = find_prev_quote(line, col_end, quotechar,
4297 curbuf->b_p_qe);
4298 if (line[col_start] != quotechar)
4300 /* We were on an ending quote perhaps? */
4301 col_start = col_end;
4302 col_end = curwin->w_cursor.col;
4306 else
4307 #endif
4309 if (line[col_start] == quotechar
4310 #ifdef FEAT_VISUAL
4311 || !vis_empty
4312 #endif
4315 int first_col = col_start;
4317 #ifdef FEAT_VISUAL
4318 if (!vis_empty)
4320 if (vis_bef_curs)
4321 first_col = find_next_quote(line, col_start, quotechar, NULL);
4322 else
4323 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4325 #endif
4326 /* The cursor is on a quote, we don't know if it's the opening or
4327 * closing quote. Search from the start of the line to find out.
4328 * Also do this when there is a Visual area, a' may leave the cursor
4329 * in between two strings. */
4330 col_start = 0;
4331 for (;;)
4333 /* Find open quote character. */
4334 col_start = find_next_quote(line, col_start, quotechar, NULL);
4335 if (col_start < 0 || col_start > first_col)
4336 return FALSE;
4337 /* Find close quote character. */
4338 col_end = find_next_quote(line, col_start + 1, quotechar,
4339 curbuf->b_p_qe);
4340 if (col_end < 0)
4341 return FALSE;
4342 /* If is cursor between start and end quote character, it is
4343 * target text object. */
4344 if (col_start <= first_col && first_col <= col_end)
4345 break;
4346 col_start = col_end + 1;
4349 else
4351 /* Search backward for a starting quote. */
4352 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4353 if (line[col_start] != quotechar)
4355 /* No quote before the cursor, look after the cursor. */
4356 col_start = find_next_quote(line, col_start, quotechar, NULL);
4357 if (col_start < 0)
4358 return FALSE;
4361 /* Find close quote character. */
4362 col_end = find_next_quote(line, col_start + 1, quotechar,
4363 curbuf->b_p_qe);
4364 if (col_end < 0)
4365 return FALSE;
4368 /* When "include" is TRUE, include spaces after closing quote or before
4369 * the starting quote. */
4370 if (include)
4372 if (vim_iswhite(line[col_end + 1]))
4373 while (vim_iswhite(line[col_end + 1]))
4374 ++col_end;
4375 else
4376 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4377 --col_start;
4380 /* Set start position. After vi" another i" must include the ".
4381 * For v2i" include the quotes. */
4382 if (!include && count < 2
4383 #ifdef FEAT_VISUAL
4384 && (vis_empty || !inside_quotes)
4385 #endif
4387 ++col_start;
4388 curwin->w_cursor.col = col_start;
4389 #ifdef FEAT_VISUAL
4390 if (VIsual_active)
4392 /* Set the start of the Visual area when the Visual area was empty, we
4393 * were just inside quotes or the Visual area didn't start at a quote
4394 * and didn't include a quote.
4396 if (vis_empty
4397 || (vis_bef_curs
4398 && !selected_quote
4399 && (inside_quotes
4400 || (line[VIsual.col] != quotechar
4401 && (VIsual.col == 0
4402 || line[VIsual.col - 1] != quotechar)))))
4404 VIsual = curwin->w_cursor;
4405 redraw_curbuf_later(INVERTED);
4408 else
4409 #endif
4411 oap->start = curwin->w_cursor;
4412 oap->motion_type = MCHAR;
4415 /* Set end position. */
4416 curwin->w_cursor.col = col_end;
4417 if ((include || count > 1
4418 #ifdef FEAT_VISUAL
4419 /* After vi" another i" must include the ". */
4420 || (!vis_empty && inside_quotes)
4421 #endif
4422 ) && inc_cursor() == 2)
4423 inclusive = TRUE;
4424 #ifdef FEAT_VISUAL
4425 if (VIsual_active)
4427 if (vis_empty || vis_bef_curs)
4429 /* decrement cursor when 'selection' is not exclusive */
4430 if (*p_sel != 'e')
4431 dec_cursor();
4433 else
4435 /* Cursor is at start of Visual area. Set the end of the Visual
4436 * area when it was just inside quotes or it didn't end at a
4437 * quote. */
4438 if (inside_quotes
4439 || (!selected_quote
4440 && line[VIsual.col] != quotechar
4441 && (line[VIsual.col] == NUL
4442 || line[VIsual.col + 1] != quotechar)))
4444 dec_cursor();
4445 VIsual = curwin->w_cursor;
4447 curwin->w_cursor.col = col_start;
4449 if (VIsual_mode == 'V')
4451 VIsual_mode = 'v';
4452 redraw_cmdline = TRUE; /* show mode later */
4455 else
4456 #endif
4458 /* Set inclusive and other oap's flags. */
4459 oap->inclusive = inclusive;
4462 return OK;
4465 #endif /* FEAT_TEXTOBJ */
4467 #if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4468 || defined(PROTO)
4470 * return TRUE if line 'lnum' is empty or has white chars only.
4473 linewhite(lnum)
4474 linenr_T lnum;
4476 char_u *p;
4478 p = skipwhite(ml_get(lnum));
4479 return (*p == NUL);
4481 #endif
4483 #if defined(FEAT_FIND_ID) || defined(PROTO)
4485 * Find identifiers or defines in included files.
4486 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
4488 /*ARGSUSED*/
4489 void
4490 find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4491 type, count, action, start_lnum, end_lnum)
4492 char_u *ptr; /* pointer to search pattern */
4493 int dir; /* direction of expansion */
4494 int len; /* length of search pattern */
4495 int whole; /* match whole words only */
4496 int skip_comments; /* don't match inside comments */
4497 int type; /* Type of search; are we looking for a type?
4498 a macro? */
4499 long count;
4500 int action; /* What to do when we find it */
4501 linenr_T start_lnum; /* first line to start searching */
4502 linenr_T end_lnum; /* last line for searching */
4504 SearchedFile *files; /* Stack of included files */
4505 SearchedFile *bigger; /* When we need more space */
4506 int max_path_depth = 50;
4507 long match_count = 1;
4509 char_u *pat;
4510 char_u *new_fname;
4511 char_u *curr_fname = curbuf->b_fname;
4512 char_u *prev_fname = NULL;
4513 linenr_T lnum;
4514 int depth;
4515 int depth_displayed; /* For type==CHECK_PATH */
4516 int old_files;
4517 int already_searched;
4518 char_u *file_line;
4519 char_u *line;
4520 char_u *p;
4521 char_u save_char;
4522 int define_matched;
4523 regmatch_T regmatch;
4524 regmatch_T incl_regmatch;
4525 regmatch_T def_regmatch;
4526 int matched = FALSE;
4527 int did_show = FALSE;
4528 int found = FALSE;
4529 int i;
4530 char_u *already = NULL;
4531 char_u *startp = NULL;
4532 char_u *inc_opt = NULL;
4533 #ifdef RISCOS
4534 int previous_munging = __riscosify_control;
4535 #endif
4536 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4537 win_T *curwin_save = NULL;
4538 #endif
4540 regmatch.regprog = NULL;
4541 incl_regmatch.regprog = NULL;
4542 def_regmatch.regprog = NULL;
4544 file_line = alloc(LSIZE);
4545 if (file_line == NULL)
4546 return;
4548 #ifdef RISCOS
4549 /* UnixLib knows best how to munge c file names - turn munging back on. */
4550 int __riscosify_control = 0;
4551 #endif
4553 if (type != CHECK_PATH && type != FIND_DEFINE
4554 #ifdef FEAT_INS_EXPAND
4555 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4556 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
4557 && !(compl_cont_status & CONT_SOL)
4558 #endif
4561 pat = alloc(len + 5);
4562 if (pat == NULL)
4563 goto fpip_end;
4564 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4565 /* ignore case according to p_ic, p_scs and pat */
4566 regmatch.rm_ic = ignorecase(pat);
4567 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4568 vim_free(pat);
4569 if (regmatch.regprog == NULL)
4570 goto fpip_end;
4572 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4573 if (*inc_opt != NUL)
4575 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
4576 if (incl_regmatch.regprog == NULL)
4577 goto fpip_end;
4578 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4580 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4582 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4583 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4584 if (def_regmatch.regprog == NULL)
4585 goto fpip_end;
4586 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4588 files = (SearchedFile *)lalloc_clear((long_u)
4589 (max_path_depth * sizeof(SearchedFile)), TRUE);
4590 if (files == NULL)
4591 goto fpip_end;
4592 old_files = max_path_depth;
4593 depth = depth_displayed = -1;
4595 lnum = start_lnum;
4596 if (end_lnum > curbuf->b_ml.ml_line_count)
4597 end_lnum = curbuf->b_ml.ml_line_count;
4598 if (lnum > end_lnum) /* do at least one line */
4599 lnum = end_lnum;
4600 line = ml_get(lnum);
4602 for (;;)
4604 if (incl_regmatch.regprog != NULL
4605 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4607 char_u *p_fname = (curr_fname == curbuf->b_fname)
4608 ? curbuf->b_ffname : curr_fname;
4610 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4611 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4612 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
4613 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
4614 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4615 else
4616 /* Use text after match with 'include'. */
4617 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
4618 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
4619 already_searched = FALSE;
4620 if (new_fname != NULL)
4622 /* Check whether we have already searched in this file */
4623 for (i = 0;; i++)
4625 if (i == depth + 1)
4626 i = old_files;
4627 if (i == max_path_depth)
4628 break;
4629 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4631 if (type != CHECK_PATH &&
4632 action == ACTION_SHOW_ALL && files[i].matched)
4634 msg_putchar('\n'); /* cursor below last one */
4635 if (!got_int) /* don't display if 'q'
4636 typed at "--more--"
4637 mesage */
4639 msg_home_replace_hl(new_fname);
4640 MSG_PUTS(_(" (includes previously listed match)"));
4641 prev_fname = NULL;
4644 vim_free(new_fname);
4645 new_fname = NULL;
4646 already_searched = TRUE;
4647 break;
4652 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4653 || (new_fname == NULL && !already_searched)))
4655 if (did_show)
4656 msg_putchar('\n'); /* cursor below last one */
4657 else
4659 gotocmdline(TRUE); /* cursor at status line */
4660 MSG_PUTS_TITLE(_("--- Included files "));
4661 if (action != ACTION_SHOW_ALL)
4662 MSG_PUTS_TITLE(_("not found "));
4663 MSG_PUTS_TITLE(_("in path ---\n"));
4665 did_show = TRUE;
4666 while (depth_displayed < depth && !got_int)
4668 ++depth_displayed;
4669 for (i = 0; i < depth_displayed; i++)
4670 MSG_PUTS(" ");
4671 msg_home_replace(files[depth_displayed].name);
4672 MSG_PUTS(" -->\n");
4674 if (!got_int) /* don't display if 'q' typed
4675 for "--more--" message */
4677 for (i = 0; i <= depth_displayed; i++)
4678 MSG_PUTS(" ");
4679 if (new_fname != NULL)
4681 /* using "new_fname" is more reliable, e.g., when
4682 * 'includeexpr' is set. */
4683 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4685 else
4688 * Isolate the file name.
4689 * Include the surrounding "" or <> if present.
4691 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4693 for (i = 0; vim_isfilec(p[i]); i++)
4695 if (i == 0)
4697 /* Nothing found, use the rest of the line. */
4698 p = incl_regmatch.endp[0];
4699 i = (int)STRLEN(p);
4701 else
4703 if (p[-1] == '"' || p[-1] == '<')
4705 --p;
4706 ++i;
4708 if (p[i] == '"' || p[i] == '>')
4709 ++i;
4711 save_char = p[i];
4712 p[i] = NUL;
4713 msg_outtrans_attr(p, hl_attr(HLF_D));
4714 p[i] = save_char;
4717 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4719 if (already_searched)
4720 MSG_PUTS(_(" (Already listed)"));
4721 else
4722 MSG_PUTS(_(" NOT FOUND"));
4725 out_flush(); /* output each line directly */
4728 if (new_fname != NULL)
4730 /* Push the new file onto the file stack */
4731 if (depth + 1 == old_files)
4733 bigger = (SearchedFile *)lalloc((long_u)(
4734 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4735 if (bigger != NULL)
4737 for (i = 0; i <= depth; i++)
4738 bigger[i] = files[i];
4739 for (i = depth + 1; i < old_files + max_path_depth; i++)
4741 bigger[i].fp = NULL;
4742 bigger[i].name = NULL;
4743 bigger[i].lnum = 0;
4744 bigger[i].matched = FALSE;
4746 for (i = old_files; i < max_path_depth; i++)
4747 bigger[i + max_path_depth] = files[i];
4748 old_files += max_path_depth;
4749 max_path_depth *= 2;
4750 vim_free(files);
4751 files = bigger;
4754 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4755 == NULL)
4756 vim_free(new_fname);
4757 else
4759 if (++depth == old_files)
4762 * lalloc() for 'bigger' must have failed above. We
4763 * will forget one of our already visited files now.
4765 vim_free(files[old_files].name);
4766 ++old_files;
4768 files[depth].name = curr_fname = new_fname;
4769 files[depth].lnum = 0;
4770 files[depth].matched = FALSE;
4771 #ifdef FEAT_INS_EXPAND
4772 if (action == ACTION_EXPAND)
4774 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
4775 vim_snprintf((char*)IObuff, IOSIZE,
4776 _("Scanning included file: %s"),
4777 (char *)new_fname);
4778 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4780 else
4781 #endif
4782 if (p_verbose >= 5)
4784 verbose_enter();
4785 smsg((char_u *)_("Searching included file %s"),
4786 (char *)new_fname);
4787 verbose_leave();
4793 else
4796 * Check if the line is a define (type == FIND_DEFINE)
4798 p = line;
4799 search_line:
4800 define_matched = FALSE;
4801 if (def_regmatch.regprog != NULL
4802 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4805 * Pattern must be first identifier after 'define', so skip
4806 * to that position before checking for match of pattern. Also
4807 * don't let it match beyond the end of this identifier.
4809 p = def_regmatch.endp[0];
4810 while (*p && !vim_iswordc(*p))
4811 p++;
4812 define_matched = TRUE;
4816 * Look for a match. Don't do this if we are looking for a
4817 * define and this line didn't match define_prog above.
4819 if (def_regmatch.regprog == NULL || define_matched)
4821 if (define_matched
4822 #ifdef FEAT_INS_EXPAND
4823 || (compl_cont_status & CONT_SOL)
4824 #endif
4827 /* compare the first "len" chars from "ptr" */
4828 startp = skipwhite(p);
4829 if (p_ic)
4830 matched = !MB_STRNICMP(startp, ptr, len);
4831 else
4832 matched = !STRNCMP(startp, ptr, len);
4833 if (matched && define_matched && whole
4834 && vim_iswordc(startp[len]))
4835 matched = FALSE;
4837 else if (regmatch.regprog != NULL
4838 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4840 matched = TRUE;
4841 startp = regmatch.startp[0];
4843 * Check if the line is not a comment line (unless we are
4844 * looking for a define). A line starting with "# define"
4845 * is not considered to be a comment line.
4847 if (!define_matched && skip_comments)
4849 #ifdef FEAT_COMMENTS
4850 if ((*line != '#' ||
4851 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4852 && get_leader_len(line, NULL, FALSE))
4853 matched = FALSE;
4856 * Also check for a "/ *" or "/ /" before the match.
4857 * Skips lines like "int backwards; / * normal index
4858 * * /" when looking for "normal".
4859 * Note: Doesn't skip "/ *" in comments.
4861 p = skipwhite(line);
4862 if (matched
4863 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4864 #endif
4865 for (p = line; *p && p < startp; ++p)
4867 if (matched
4868 && p[0] == '/'
4869 && (p[1] == '*' || p[1] == '/'))
4871 matched = FALSE;
4872 /* After "//" all text is comment */
4873 if (p[1] == '/')
4874 break;
4875 ++p;
4877 else if (!matched && p[0] == '*' && p[1] == '/')
4879 /* Can find match after "* /". */
4880 matched = TRUE;
4881 ++p;
4888 if (matched)
4890 #ifdef FEAT_INS_EXPAND
4891 if (action == ACTION_EXPAND)
4893 int reuse = 0;
4894 int add_r;
4895 char_u *aux;
4897 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4898 break;
4899 found = TRUE;
4900 aux = p = startp;
4901 if (compl_cont_status & CONT_ADDING)
4903 p += compl_length;
4904 if (vim_iswordp(p))
4905 goto exit_matched;
4906 p = find_word_start(p);
4908 p = find_word_end(p);
4909 i = (int)(p - aux);
4911 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
4913 /* IOSIZE > compl_length, so the STRNCPY works */
4914 STRNCPY(IObuff, aux, i);
4916 /* Get the next line: when "depth" < 0 from the current
4917 * buffer, otherwise from the included file. Jump to
4918 * exit_matched when past the last line. */
4919 if (depth < 0)
4921 if (lnum >= end_lnum)
4922 goto exit_matched;
4923 line = ml_get(++lnum);
4925 else if (vim_fgets(line = file_line,
4926 LSIZE, files[depth].fp))
4927 goto exit_matched;
4929 /* we read a line, set "already" to check this "line" later
4930 * if depth >= 0 we'll increase files[depth].lnum far
4931 * bellow -- Acevedo */
4932 already = aux = p = skipwhite(line);
4933 p = find_word_start(p);
4934 p = find_word_end(p);
4935 if (p > aux)
4937 if (*aux != ')' && IObuff[i-1] != TAB)
4939 if (IObuff[i-1] != ' ')
4940 IObuff[i++] = ' ';
4941 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4942 if (p_js
4943 && (IObuff[i-2] == '.'
4944 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4945 && (IObuff[i-2] == '?'
4946 || IObuff[i-2] == '!'))))
4947 IObuff[i++] = ' ';
4949 /* copy as much as posible of the new word */
4950 if (p - aux >= IOSIZE - i)
4951 p = aux + IOSIZE - i - 1;
4952 STRNCPY(IObuff + i, aux, p - aux);
4953 i += (int)(p - aux);
4954 reuse |= CONT_S_IPOS;
4956 IObuff[i] = NUL;
4957 aux = IObuff;
4959 if (i == compl_length)
4960 goto exit_matched;
4963 add_r = ins_compl_add_infercase(aux, i, p_ic,
4964 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4965 dir, reuse);
4966 if (add_r == OK)
4967 /* if dir was BACKWARD then honor it just once */
4968 dir = FORWARD;
4969 else if (add_r == FAIL)
4970 break;
4972 else
4973 #endif
4974 if (action == ACTION_SHOW_ALL)
4976 found = TRUE;
4977 if (!did_show)
4978 gotocmdline(TRUE); /* cursor at status line */
4979 if (curr_fname != prev_fname)
4981 if (did_show)
4982 msg_putchar('\n'); /* cursor below last one */
4983 if (!got_int) /* don't display if 'q' typed
4984 at "--more--" mesage */
4985 msg_home_replace_hl(curr_fname);
4986 prev_fname = curr_fname;
4988 did_show = TRUE;
4989 if (!got_int)
4990 show_pat_in_path(line, type, TRUE, action,
4991 (depth == -1) ? NULL : files[depth].fp,
4992 (depth == -1) ? &lnum : &files[depth].lnum,
4993 match_count++);
4995 /* Set matched flag for this file and all the ones that
4996 * include it */
4997 for (i = 0; i <= depth; ++i)
4998 files[i].matched = TRUE;
5000 else if (--count <= 0)
5002 found = TRUE;
5003 if (depth == -1 && lnum == curwin->w_cursor.lnum
5004 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5005 && g_do_tagpreview == 0
5006 #endif
5008 EMSG(_("E387: Match is on current line"));
5009 else if (action == ACTION_SHOW)
5011 show_pat_in_path(line, type, did_show, action,
5012 (depth == -1) ? NULL : files[depth].fp,
5013 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
5014 did_show = TRUE;
5016 else
5018 #ifdef FEAT_GUI
5019 need_mouse_correct = TRUE;
5020 #endif
5021 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5022 /* ":psearch" uses the preview window */
5023 if (g_do_tagpreview != 0)
5025 curwin_save = curwin;
5026 prepare_tagpreview(TRUE);
5028 #endif
5029 if (action == ACTION_SPLIT)
5031 #ifdef FEAT_WINDOWS
5032 if (win_split(0, 0) == FAIL)
5033 #endif
5034 break;
5035 #ifdef FEAT_SCROLLBIND
5036 curwin->w_p_scb = FALSE;
5037 #endif
5039 if (depth == -1)
5041 /* match in current file */
5042 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5043 if (g_do_tagpreview != 0)
5045 if (getfile(0, curwin_save->w_buffer->b_fname,
5046 NULL, TRUE, lnum, FALSE) > 0)
5047 break; /* failed to jump to file */
5049 else
5050 #endif
5051 setpcmark();
5052 curwin->w_cursor.lnum = lnum;
5054 else
5056 if (getfile(0, files[depth].name, NULL, TRUE,
5057 files[depth].lnum, FALSE) > 0)
5058 break; /* failed to jump to file */
5059 /* autocommands may have changed the lnum, we don't
5060 * want that here */
5061 curwin->w_cursor.lnum = files[depth].lnum;
5064 if (action != ACTION_SHOW)
5066 curwin->w_cursor.col = (colnr_T) (startp - line);
5067 curwin->w_set_curswant = TRUE;
5070 #if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5071 if (g_do_tagpreview != 0
5072 && curwin != curwin_save && win_valid(curwin_save))
5074 /* Return cursor to where we were */
5075 validate_cursor();
5076 redraw_later(VALID);
5077 win_enter(curwin_save, TRUE);
5079 #endif
5080 break;
5082 #ifdef FEAT_INS_EXPAND
5083 exit_matched:
5084 #endif
5085 matched = FALSE;
5086 /* look for other matches in the rest of the line if we
5087 * are not at the end of it already */
5088 if (def_regmatch.regprog == NULL
5089 #ifdef FEAT_INS_EXPAND
5090 && action == ACTION_EXPAND
5091 && !(compl_cont_status & CONT_SOL)
5092 #endif
5093 && *(p = startp + 1))
5094 goto search_line;
5096 line_breakcheck();
5097 #ifdef FEAT_INS_EXPAND
5098 if (action == ACTION_EXPAND)
5099 ins_compl_check_keys(30);
5100 if (got_int || compl_interrupted)
5101 #else
5102 if (got_int)
5103 #endif
5104 break;
5107 * Read the next line. When reading an included file and encountering
5108 * end-of-file, close the file and continue in the file that included
5109 * it.
5111 while (depth >= 0 && !already
5112 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5114 fclose(files[depth].fp);
5115 --old_files;
5116 files[old_files].name = files[depth].name;
5117 files[old_files].matched = files[depth].matched;
5118 --depth;
5119 curr_fname = (depth == -1) ? curbuf->b_fname
5120 : files[depth].name;
5121 if (depth < depth_displayed)
5122 depth_displayed = depth;
5124 if (depth >= 0) /* we could read the line */
5125 files[depth].lnum++;
5126 else if (!already)
5128 if (++lnum > end_lnum)
5129 break;
5130 line = ml_get(lnum);
5132 already = NULL;
5134 /* End of big for (;;) loop. */
5136 /* Close any files that are still open. */
5137 for (i = 0; i <= depth; i++)
5139 fclose(files[i].fp);
5140 vim_free(files[i].name);
5142 for (i = old_files; i < max_path_depth; i++)
5143 vim_free(files[i].name);
5144 vim_free(files);
5146 if (type == CHECK_PATH)
5148 if (!did_show)
5150 if (action != ACTION_SHOW_ALL)
5151 MSG(_("All included files were found"));
5152 else
5153 MSG(_("No included files"));
5156 else if (!found
5157 #ifdef FEAT_INS_EXPAND
5158 && action != ACTION_EXPAND
5159 #endif
5162 #ifdef FEAT_INS_EXPAND
5163 if (got_int || compl_interrupted)
5164 #else
5165 if (got_int)
5166 #endif
5167 EMSG(_(e_interr));
5168 else if (type == FIND_DEFINE)
5169 EMSG(_("E388: Couldn't find definition"));
5170 else
5171 EMSG(_("E389: Couldn't find pattern"));
5173 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5174 msg_end();
5176 fpip_end:
5177 vim_free(file_line);
5178 vim_free(regmatch.regprog);
5179 vim_free(incl_regmatch.regprog);
5180 vim_free(def_regmatch.regprog);
5182 #ifdef RISCOS
5183 /* Restore previous file munging state. */
5184 __riscosify_control = previous_munging;
5185 #endif
5188 static void
5189 show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5190 char_u *line;
5191 int type;
5192 int did_show;
5193 int action;
5194 FILE *fp;
5195 linenr_T *lnum;
5196 long count;
5198 char_u *p;
5200 if (did_show)
5201 msg_putchar('\n'); /* cursor below last one */
5202 else if (!msg_silent)
5203 gotocmdline(TRUE); /* cursor at status line */
5204 if (got_int) /* 'q' typed at "--more--" message */
5205 return;
5206 for (;;)
5208 p = line + STRLEN(line) - 1;
5209 if (fp != NULL)
5211 /* We used fgets(), so get rid of newline at end */
5212 if (p >= line && *p == '\n')
5213 --p;
5214 if (p >= line && *p == '\r')
5215 --p;
5216 *(p + 1) = NUL;
5218 if (action == ACTION_SHOW_ALL)
5220 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5221 msg_puts(IObuff);
5222 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5223 /* Highlight line numbers */
5224 msg_puts_attr(IObuff, hl_attr(HLF_N));
5225 MSG_PUTS(" ");
5227 msg_prt_line(line, FALSE);
5228 out_flush(); /* show one line at a time */
5230 /* Definition continues until line that doesn't end with '\' */
5231 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5232 break;
5234 if (fp != NULL)
5236 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5237 break;
5238 ++*lnum;
5240 else
5242 if (++*lnum > curbuf->b_ml.ml_line_count)
5243 break;
5244 line = ml_get(*lnum);
5246 msg_putchar('\n');
5249 #endif
5251 #ifdef FEAT_VIMINFO
5253 read_viminfo_search_pattern(virp, force)
5254 vir_T *virp;
5255 int force;
5257 char_u *lp;
5258 int idx = -1;
5259 int magic = FALSE;
5260 int no_scs = FALSE;
5261 int off_line = FALSE;
5262 int off_end = 0;
5263 long off = 0;
5264 int setlast = FALSE;
5265 #ifdef FEAT_SEARCH_EXTRA
5266 static int hlsearch_on = FALSE;
5267 #endif
5268 char_u *val;
5271 * Old line types:
5272 * "/pat", "&pat": search/subst. pat
5273 * "~/pat", "~&pat": last used search/subst. pat
5274 * New line types:
5275 * "~h", "~H": hlsearch highlighting off/on
5276 * "~<magic><smartcase><line><end><off><last><which>pat"
5277 * <magic>: 'm' off, 'M' on
5278 * <smartcase>: 's' off, 'S' on
5279 * <line>: 'L' line offset, 'l' char offset
5280 * <end>: 'E' from end, 'e' from start
5281 * <off>: decimal, offset
5282 * <last>: '~' last used pattern
5283 * <which>: '/' search pat, '&' subst. pat
5285 lp = virp->vir_line;
5286 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5288 if (lp[1] == 'M') /* magic on */
5289 magic = TRUE;
5290 if (lp[2] == 's')
5291 no_scs = TRUE;
5292 if (lp[3] == 'L')
5293 off_line = TRUE;
5294 if (lp[4] == 'E')
5295 off_end = SEARCH_END;
5296 lp += 5;
5297 off = getdigits(&lp);
5299 if (lp[0] == '~') /* use this pattern for last-used pattern */
5301 setlast = TRUE;
5302 lp++;
5304 if (lp[0] == '/')
5305 idx = RE_SEARCH;
5306 else if (lp[0] == '&')
5307 idx = RE_SUBST;
5308 #ifdef FEAT_SEARCH_EXTRA
5309 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5310 hlsearch_on = FALSE;
5311 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5312 hlsearch_on = TRUE;
5313 #endif
5314 if (idx >= 0)
5316 if (force || spats[idx].pat == NULL)
5318 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5319 TRUE);
5320 if (val != NULL)
5322 set_last_search_pat(val, idx, magic, setlast);
5323 vim_free(val);
5324 spats[idx].no_scs = no_scs;
5325 spats[idx].off.line = off_line;
5326 spats[idx].off.end = off_end;
5327 spats[idx].off.off = off;
5328 #ifdef FEAT_SEARCH_EXTRA
5329 if (setlast)
5330 no_hlsearch = !hlsearch_on;
5331 #endif
5335 return viminfo_readline(virp);
5338 void
5339 write_viminfo_search_pattern(fp)
5340 FILE *fp;
5342 if (get_viminfo_parameter('/') != 0)
5344 #ifdef FEAT_SEARCH_EXTRA
5345 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5346 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5347 #endif
5348 wvsp_one(fp, RE_SEARCH, "", '/');
5349 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5353 static void
5354 wvsp_one(fp, idx, s, sc)
5355 FILE *fp; /* file to write to */
5356 int idx; /* spats[] index */
5357 char *s; /* search pat */
5358 int sc; /* dir char */
5360 if (spats[idx].pat != NULL)
5362 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
5363 /* off.dir is not stored, it's reset to forward */
5364 fprintf(fp, "%c%c%c%c%ld%s%c",
5365 spats[idx].magic ? 'M' : 'm', /* magic */
5366 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5367 spats[idx].off.line ? 'L' : 'l', /* line offset */
5368 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5369 spats[idx].off.off, /* offset */
5370 last_idx == idx ? "~" : "", /* last used pat */
5371 sc);
5372 viminfo_writestring(fp, spats[idx].pat);
5375 #endif /* FEAT_VIMINFO */