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