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