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