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