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