blob: e92985a8c3be5f5946042adc7cb219b1e348213b [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 Moolenaar32466aa2006-02-24 23:53:04 +0000605 /* Line me be past end of buffer for "\n\zs". */
606 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
607 ptr = (char_u *)"";
608 else
609 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000610
611 /*
612 * Forward search in the first line: match should be after
613 * the start position. If not, continue at the end of the
614 * match (this is vi compatible) or on the next char.
615 */
616 if (dir == FORWARD && at_first_line)
617 {
618 match_ok = TRUE;
619 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000620 * When the match starts in a next line it's certainly
621 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000622 * When match lands on a NUL the cursor will be put
623 * one back afterwards, compare with that position,
624 * otherwise "/$" will get stuck on end of line.
625 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000626 while (matchpos.lnum == 0
627 && ((options & SEARCH_END)
628 ? (nmatched == 1
629 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000630 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000631 : ((int)matchpos.col
632 - (ptr[matchpos.col] == NUL)
633 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000634 {
635 /*
636 * If vi-compatible searching, continue at the end
637 * of the match, otherwise continue one position
638 * forward.
639 */
640 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
641 {
642 if (nmatched > 1)
643 {
644 /* end is in next line, thus no match in
645 * this line */
646 match_ok = FALSE;
647 break;
648 }
649 matchcol = endpos.col;
650 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000651 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000652 && ptr[matchcol] != NUL)
653 {
654#ifdef FEAT_MBYTE
655 if (has_mbyte)
656 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000657 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000658 else
659#endif
660 ++matchcol;
661 }
662 }
663 else
664 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000665 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000666 if (ptr[matchcol] != NUL)
667 {
668#ifdef FEAT_MBYTE
669 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000670 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000671 + matchcol);
672 else
673#endif
674 ++matchcol;
675 }
676 }
677 if (ptr[matchcol] == NUL
678 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000679 win, buf, lnum + matchpos.lnum,
680 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000681 {
682 match_ok = FALSE;
683 break;
684 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000685 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000686 endpos = regmatch.endpos[0];
687# ifdef FEAT_EVAL
688 submatch = first_submatch(&regmatch);
689# endif
690
691 /* Need to get the line pointer again, a
692 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000693 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000694 }
695 if (!match_ok)
696 continue;
697 }
698 if (dir == BACKWARD)
699 {
700 /*
701 * Now, if there are multiple matches on this line,
702 * we have to get the last one. Or the last one before
703 * the cursor, if we're on that line.
704 * When putting the new cursor at the end, compare
705 * relative to the end of the match.
706 */
707 match_ok = FALSE;
708 for (;;)
709 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000710 /* Remember a position that is before the start
711 * position, we use it if it's the last match in
712 * the line. Always accept a position after
713 * wrapping around. */
714 if (loop
715 || ((options & SEARCH_END)
716 ? (lnum + regmatch.endpos[0].lnum
717 < start_pos.lnum
718 || (lnum + regmatch.endpos[0].lnum
719 == start_pos.lnum
720 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000721 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000722 <= (int)start_pos.col))
723 : (lnum + regmatch.startpos[0].lnum
724 < start_pos.lnum
725 || (lnum + regmatch.startpos[0].lnum
726 == start_pos.lnum
727 && (int)regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000728 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000729 <= (int)start_pos.col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000730 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000731 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000732 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000733 endpos = regmatch.endpos[0];
734# ifdef FEAT_EVAL
735 submatch = first_submatch(&regmatch);
736# endif
737 }
738 else
739 break;
740
741 /*
742 * We found a valid match, now check if there is
743 * another one after it.
744 * If vi-compatible searching, continue at the end
745 * of the match, otherwise continue one position
746 * forward.
747 */
748 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
749 {
750 if (nmatched > 1)
751 break;
752 matchcol = endpos.col;
753 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000754 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000755 && ptr[matchcol] != NUL)
756 {
757#ifdef FEAT_MBYTE
758 if (has_mbyte)
759 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000760 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000761 else
762#endif
763 ++matchcol;
764 }
765 }
766 else
767 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000768 /* Stop when the match is in a next line. */
769 if (matchpos.lnum > 0)
770 break;
771 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000772 if (ptr[matchcol] != NUL)
773 {
774#ifdef FEAT_MBYTE
775 if (has_mbyte)
776 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000777 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000778 else
779#endif
780 ++matchcol;
781 }
782 }
783 if (ptr[matchcol] == NUL
784 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000785 win, buf, lnum + matchpos.lnum,
786 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000787 break;
788
789 /* Need to get the line pointer again, a
790 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000791 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000792 }
793
794 /*
795 * If there is only a match after the cursor, skip
796 * this match.
797 */
798 if (!match_ok)
799 continue;
800 }
801
802 if (options & SEARCH_END && !(options & SEARCH_NOOF))
803 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000804 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000805 pos->col = endpos.col - 1;
806 }
807 else
808 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000809 pos->lnum = lnum + matchpos.lnum;
810 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000811 }
812#ifdef FEAT_VIRTUALEDIT
813 pos->coladd = 0;
814#endif
815 found = 1;
816
817 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000818 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000819 search_match_endcol = endpos.col;
820 break;
821 }
822 line_breakcheck(); /* stop if ctrl-C typed */
823 if (got_int)
824 break;
825
826#ifdef FEAT_SEARCH_EXTRA
827 /* Cancel searching if a character was typed. Used for
828 * 'incsearch'. Don't check too often, that would slowdown
829 * searching too much. */
830 if ((options & SEARCH_PEEK)
831 && ((lnum - pos->lnum) & 0x3f) == 0
832 && char_avail())
833 {
834 break_loop = TRUE;
835 break;
836 }
837#endif
838
839 if (loop && lnum == start_pos.lnum)
840 break; /* if second loop, stop where started */
841 }
842 at_first_line = FALSE;
843
844 /*
845 * Stop the search if wrapscan isn't set, after an interrupt,
846 * after a match and after looping twice.
847 */
848 if (!p_ws || got_int || called_emsg || break_loop || found || loop)
849 break;
850
851 /*
852 * If 'wrapscan' is set we continue at the other end of the file.
853 * If 'shortmess' does not contain 's', we give a message.
854 * This message is also remembered in keep_msg for when the screen
855 * is redrawn. The keep_msg is cleared whenever another message is
856 * written.
857 */
858 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000859 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000860 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000861 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +0000862 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
863 give_warning((char_u *)_(dir == BACKWARD
864 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000865 }
866 if (got_int || called_emsg || break_loop)
867 break;
868 }
869 while (--count > 0 && found); /* stop after count matches or no match */
870
871 vim_free(regmatch.regprog);
872
Bram Moolenaar280f1262006-01-30 00:14:18 +0000873 called_emsg |= save_called_emsg;
874
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 if (!found) /* did not find it */
876 {
877 if (got_int)
878 EMSG(_(e_interr));
879 else if ((options & SEARCH_MSG) == SEARCH_MSG)
880 {
881 if (p_ws)
882 EMSG2(_(e_patnotf2), mr_pattern);
883 else if (lnum == 0)
884 EMSG2(_("E384: search hit TOP without match for: %s"),
885 mr_pattern);
886 else
887 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
888 mr_pattern);
889 }
890 return FAIL;
891 }
892
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000893 /* A pattern like "\n\zs" may go past the last line. */
894 if (pos->lnum > buf->b_ml.ml_line_count)
895 {
896 pos->lnum = buf->b_ml.ml_line_count;
897 pos->col = STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
898 if (pos->col > 0)
899 --pos->col;
900 }
901
Bram Moolenaar071d4272004-06-13 20:20:40 +0000902 return submatch + 1;
903}
904
905#ifdef FEAT_EVAL
906/*
907 * Return the number of the first subpat that matched.
908 */
909 static int
910first_submatch(rp)
911 regmmatch_T *rp;
912{
913 int submatch;
914
915 for (submatch = 1; ; ++submatch)
916 {
917 if (rp->startpos[submatch].lnum >= 0)
918 break;
919 if (submatch == 9)
920 {
921 submatch = 0;
922 break;
923 }
924 }
925 return submatch;
926}
927#endif
928
929/*
930 * Highest level string search function.
931 * Search for the 'count'th occurence of pattern 'pat' in direction 'dirc'
932 * If 'dirc' is 0: use previous dir.
933 * If 'pat' is NULL or empty : use previous string.
934 * If 'options & SEARCH_REV' : go in reverse of previous dir.
935 * If 'options & SEARCH_ECHO': echo the search command and handle options
936 * If 'options & SEARCH_MSG' : may give error message
937 * If 'options & SEARCH_OPT' : interpret optional flags
938 * If 'options & SEARCH_HIS' : put search pattern in history
939 * If 'options & SEARCH_NOOF': don't add offset to position
940 * If 'options & SEARCH_MARK': set previous context mark
941 * If 'options & SEARCH_KEEP': keep previous search pattern
942 * If 'options & SEARCH_START': accept match at curpos itself
943 * If 'options & SEARCH_PEEK': check for typed char, cancel search
944 *
945 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
946 * makes the movement linewise without moving the match position.
947 *
948 * return 0 for failure, 1 for found, 2 for found and line offset added
949 */
950 int
951do_search(oap, dirc, pat, count, options)
952 oparg_T *oap; /* can be NULL */
953 int dirc; /* '/' or '?' */
954 char_u *pat;
955 long count;
956 int options;
957{
958 pos_T pos; /* position of the last match */
959 char_u *searchstr;
960 struct soffset old_off;
961 int retval; /* Return value */
962 char_u *p;
963 long c;
964 char_u *dircp;
965 char_u *strcopy = NULL;
966 char_u *ps;
967
968 /*
969 * A line offset is not remembered, this is vi compatible.
970 */
971 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
972 {
973 spats[0].off.line = FALSE;
974 spats[0].off.off = 0;
975 }
976
977 /*
978 * Save the values for when (options & SEARCH_KEEP) is used.
979 * (there is no "if ()" around this because gcc wants them initialized)
980 */
981 old_off = spats[0].off;
982
983 pos = curwin->w_cursor; /* start searching at the cursor position */
984
985 /*
986 * Find out the direction of the search.
987 */
988 if (dirc == 0)
989 dirc = spats[0].off.dir;
990 else
991 spats[0].off.dir = dirc;
992 if (options & SEARCH_REV)
993 {
994#ifdef WIN32
995 /* There is a bug in the Visual C++ 2.2 compiler which means that
996 * dirc always ends up being '/' */
997 dirc = (dirc == '/') ? '?' : '/';
998#else
999 if (dirc == '/')
1000 dirc = '?';
1001 else
1002 dirc = '/';
1003#endif
1004 }
1005
1006#ifdef FEAT_FOLDING
1007 /* If the cursor is in a closed fold, don't find another match in the same
1008 * fold. */
1009 if (dirc == '/')
1010 {
1011 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1012 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1013 }
1014 else
1015 {
1016 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1017 pos.col = 0;
1018 }
1019#endif
1020
1021#ifdef FEAT_SEARCH_EXTRA
1022 /*
1023 * Turn 'hlsearch' highlighting back on.
1024 */
1025 if (no_hlsearch && !(options & SEARCH_KEEP))
1026 {
1027 redraw_all_later(NOT_VALID);
1028 no_hlsearch = FALSE;
1029 }
1030#endif
1031
1032 /*
1033 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1034 */
1035 for (;;)
1036 {
1037 searchstr = pat;
1038 dircp = NULL;
1039 /* use previous pattern */
1040 if (pat == NULL || *pat == NUL || *pat == dirc)
1041 {
1042 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1043 {
1044 EMSG(_(e_noprevre));
1045 retval = 0;
1046 goto end_do_search;
1047 }
1048 /* make search_regcomp() use spats[RE_SEARCH].pat */
1049 searchstr = (char_u *)"";
1050 }
1051
1052 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1053 {
1054 /*
1055 * Find end of regular expression.
1056 * If there is a matching '/' or '?', toss it.
1057 */
1058 ps = strcopy;
1059 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1060 if (strcopy != ps)
1061 {
1062 /* made a copy of "pat" to change "\?" to "?" */
1063 searchcmdlen += STRLEN(pat) - STRLEN(strcopy);
1064 pat = strcopy;
1065 searchstr = strcopy;
1066 }
1067 if (*p == dirc)
1068 {
1069 dircp = p; /* remember where we put the NUL */
1070 *p++ = NUL;
1071 }
1072 spats[0].off.line = FALSE;
1073 spats[0].off.end = FALSE;
1074 spats[0].off.off = 0;
1075 /*
1076 * Check for a line offset or a character offset.
1077 * For get_address (echo off) we don't check for a character
1078 * offset, because it is meaningless and the 's' could be a
1079 * substitute command.
1080 */
1081 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1082 spats[0].off.line = TRUE;
1083 else if ((options & SEARCH_OPT) &&
1084 (*p == 'e' || *p == 's' || *p == 'b'))
1085 {
1086 if (*p == 'e') /* end */
1087 spats[0].off.end = SEARCH_END;
1088 ++p;
1089 }
1090 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1091 {
1092 /* 'nr' or '+nr' or '-nr' */
1093 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1094 spats[0].off.off = atol((char *)p);
1095 else if (*p == '-') /* single '-' */
1096 spats[0].off.off = -1;
1097 else /* single '+' */
1098 spats[0].off.off = 1;
1099 ++p;
1100 while (VIM_ISDIGIT(*p)) /* skip number */
1101 ++p;
1102 }
1103
1104 /* compute length of search command for get_address() */
1105 searchcmdlen += (int)(p - pat);
1106
1107 pat = p; /* put pat after search command */
1108 }
1109
1110 if ((options & SEARCH_ECHO) && messaging()
1111 && !cmd_silent && msg_silent == 0)
1112 {
1113 char_u *msgbuf;
1114 char_u *trunc;
1115
1116 if (*searchstr == NUL)
1117 p = spats[last_idx].pat;
1118 else
1119 p = searchstr;
1120 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1121 if (msgbuf != NULL)
1122 {
1123 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001124#ifdef FEAT_MBYTE
1125 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1126 {
1127 /* Use a space to draw the composing char on. */
1128 msgbuf[1] = ' ';
1129 STRCPY(msgbuf + 2, p);
1130 }
1131 else
1132#endif
1133 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001134 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1135 {
1136 p = msgbuf + STRLEN(msgbuf);
1137 *p++ = dirc;
1138 if (spats[0].off.end)
1139 *p++ = 'e';
1140 else if (!spats[0].off.line)
1141 *p++ = 's';
1142 if (spats[0].off.off > 0 || spats[0].off.line)
1143 *p++ = '+';
1144 if (spats[0].off.off != 0 || spats[0].off.line)
1145 sprintf((char *)p, "%ld", spats[0].off.off);
1146 else
1147 *p = NUL;
1148 }
1149
1150 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001151 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001152
1153#ifdef FEAT_RIGHTLEFT
1154 /* The search pattern could be shown on the right in rightleft
1155 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1156 * it would be blanked out again very soon. Show it on the
1157 * left, but do reverse the text. */
1158 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1159 {
1160 char_u *r;
1161
1162 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1163 if (r != NULL)
1164 {
1165 vim_free(trunc);
1166 trunc = r;
1167 }
1168 }
1169#endif
1170 if (trunc != NULL)
1171 {
1172 msg_outtrans(trunc);
1173 vim_free(trunc);
1174 }
1175 else
1176 msg_outtrans(msgbuf);
1177 msg_clr_eos();
1178 msg_check();
1179 vim_free(msgbuf);
1180
1181 gotocmdline(FALSE);
1182 out_flush();
1183 msg_nowait = TRUE; /* don't wait for this message */
1184 }
1185 }
1186
1187 /*
1188 * If there is a character offset, subtract it from the current
1189 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001190 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001191 * This is not done for a line offset, because then we would not be vi
1192 * compatible.
1193 */
Bram Moolenaared203462004-06-16 11:19:22 +00001194 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001195 {
1196 if (spats[0].off.off > 0)
1197 {
1198 for (c = spats[0].off.off; c; --c)
1199 if (decl(&pos) == -1)
1200 break;
1201 if (c) /* at start of buffer */
1202 {
1203 pos.lnum = 0; /* allow lnum == 0 here */
1204 pos.col = MAXCOL;
1205 }
1206 }
1207 else
1208 {
1209 for (c = spats[0].off.off; c; ++c)
1210 if (incl(&pos) == -1)
1211 break;
1212 if (c) /* at end of buffer */
1213 {
1214 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1215 pos.col = 0;
1216 }
1217 }
1218 }
1219
1220#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1221 if (p_altkeymap && curwin->w_p_rl)
1222 lrFswap(searchstr,0);
1223#endif
1224
1225 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1226 searchstr, count, spats[0].off.end + (options &
1227 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1228 + SEARCH_MSG + SEARCH_START
1229 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
1230 RE_LAST);
1231
1232 if (dircp != NULL)
1233 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1234 if (c == FAIL)
1235 {
1236 retval = 0;
1237 goto end_do_search;
1238 }
1239 if (spats[0].off.end && oap != NULL)
1240 oap->inclusive = TRUE; /* 'e' includes last character */
1241
1242 retval = 1; /* pattern found */
1243
1244 /*
1245 * Add character and/or line offset
1246 */
1247 if (!(options & SEARCH_NOOF) || *pat == ';')
1248 {
1249 if (spats[0].off.line) /* Add the offset to the line number. */
1250 {
1251 c = pos.lnum + spats[0].off.off;
1252 if (c < 1)
1253 pos.lnum = 1;
1254 else if (c > curbuf->b_ml.ml_line_count)
1255 pos.lnum = curbuf->b_ml.ml_line_count;
1256 else
1257 pos.lnum = c;
1258 pos.col = 0;
1259
1260 retval = 2; /* pattern found, line offset added */
1261 }
Bram Moolenaared203462004-06-16 11:19:22 +00001262 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001263 {
1264 /* to the right, check for end of file */
1265 if (spats[0].off.off > 0)
1266 {
1267 for (c = spats[0].off.off; c; --c)
1268 if (incl(&pos) == -1)
1269 break;
1270 }
1271 /* to the left, check for start of file */
1272 else
1273 {
1274 if ((c = pos.col + spats[0].off.off) >= 0)
1275 pos.col = c;
1276 else
1277 for (c = spats[0].off.off; c; ++c)
1278 if (decl(&pos) == -1)
1279 break;
1280 }
1281 }
1282 }
1283
1284 /*
1285 * The search command can be followed by a ';' to do another search.
1286 * For example: "/pat/;/foo/+3;?bar"
1287 * This is like doing another search command, except:
1288 * - The remembered direction '/' or '?' is from the first search.
1289 * - When an error happens the cursor isn't moved at all.
1290 * Don't do this when called by get_address() (it handles ';' itself).
1291 */
1292 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1293 break;
1294
1295 dirc = *++pat;
1296 if (dirc != '?' && dirc != '/')
1297 {
1298 retval = 0;
1299 EMSG(_("E386: Expected '?' or '/' after ';'"));
1300 goto end_do_search;
1301 }
1302 ++pat;
1303 }
1304
1305 if (options & SEARCH_MARK)
1306 setpcmark();
1307 curwin->w_cursor = pos;
1308 curwin->w_set_curswant = TRUE;
1309
1310end_do_search:
1311 if (options & SEARCH_KEEP)
1312 spats[0].off = old_off;
1313 vim_free(strcopy);
1314
1315 return retval;
1316}
1317
1318#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1319/*
1320 * search_for_exact_line(buf, pos, dir, pat)
1321 *
1322 * Search for a line starting with the given pattern (ignoring leading
1323 * white-space), starting from pos and going in direction dir. pos will
1324 * contain the position of the match found. Blank lines match only if
1325 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1326 * Return OK for success, or FAIL if no line found.
1327 */
1328 int
1329search_for_exact_line(buf, pos, dir, pat)
1330 buf_T *buf;
1331 pos_T *pos;
1332 int dir;
1333 char_u *pat;
1334{
1335 linenr_T start = 0;
1336 char_u *ptr;
1337 char_u *p;
1338
1339 if (buf->b_ml.ml_line_count == 0)
1340 return FAIL;
1341 for (;;)
1342 {
1343 pos->lnum += dir;
1344 if (pos->lnum < 1)
1345 {
1346 if (p_ws)
1347 {
1348 pos->lnum = buf->b_ml.ml_line_count;
1349 if (!shortmess(SHM_SEARCH))
1350 give_warning((char_u *)_(top_bot_msg), TRUE);
1351 }
1352 else
1353 {
1354 pos->lnum = 1;
1355 break;
1356 }
1357 }
1358 else if (pos->lnum > buf->b_ml.ml_line_count)
1359 {
1360 if (p_ws)
1361 {
1362 pos->lnum = 1;
1363 if (!shortmess(SHM_SEARCH))
1364 give_warning((char_u *)_(bot_top_msg), TRUE);
1365 }
1366 else
1367 {
1368 pos->lnum = 1;
1369 break;
1370 }
1371 }
1372 if (pos->lnum == start)
1373 break;
1374 if (start == 0)
1375 start = pos->lnum;
1376 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1377 p = skipwhite(ptr);
1378 pos->col = (colnr_T) (p - ptr);
1379
1380 /* when adding lines the matching line may be empty but it is not
1381 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001382 if ((compl_cont_status & CONT_ADDING)
1383 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001384 {
1385 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1386 return OK;
1387 }
1388 else if (*p != NUL) /* ignore empty lines */
1389 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001390 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1391 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001392 return OK;
1393 }
1394 }
1395 return FAIL;
1396}
1397#endif /* FEAT_INS_EXPAND */
1398
1399/*
1400 * Character Searches
1401 */
1402
1403/*
1404 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1405 * position of the character, otherwise move to just before the char.
1406 * Do this "cap->count1" times.
1407 * Return FAIL or OK.
1408 */
1409 int
1410searchc(cap, t_cmd)
1411 cmdarg_T *cap;
1412 int t_cmd;
1413{
1414 int c = cap->nchar; /* char to search for */
1415 int dir = cap->arg; /* TRUE for searching forward */
1416 long count = cap->count1; /* repeat count */
1417 static int lastc = NUL; /* last character searched for */
1418 static int lastcdir; /* last direction of character search */
1419 static int last_t_cmd; /* last search t_cmd */
1420 int col;
1421 char_u *p;
1422 int len;
1423#ifdef FEAT_MBYTE
1424 static char_u bytes[MB_MAXBYTES];
1425 static int bytelen = 1; /* >1 for multi-byte char */
1426#endif
1427
1428 if (c != NUL) /* normal search: remember args for repeat */
1429 {
1430 if (!KeyStuffed) /* don't remember when redoing */
1431 {
1432 lastc = c;
1433 lastcdir = dir;
1434 last_t_cmd = t_cmd;
1435#ifdef FEAT_MBYTE
1436 bytelen = (*mb_char2bytes)(c, bytes);
1437 if (cap->ncharC1 != 0)
1438 {
1439 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1440 if (cap->ncharC2 != 0)
1441 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1442 }
1443#endif
1444 }
1445 }
1446 else /* repeat previous search */
1447 {
1448 if (lastc == NUL)
1449 return FAIL;
1450 if (dir) /* repeat in opposite direction */
1451 dir = -lastcdir;
1452 else
1453 dir = lastcdir;
1454 t_cmd = last_t_cmd;
1455 c = lastc;
1456 /* For multi-byte re-use last bytes[] and bytelen. */
1457 }
1458
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001459 if (dir == BACKWARD)
1460 cap->oap->inclusive = FALSE;
1461 else
1462 cap->oap->inclusive = TRUE;
1463
Bram Moolenaar071d4272004-06-13 20:20:40 +00001464 p = ml_get_curline();
1465 col = curwin->w_cursor.col;
1466 len = (int)STRLEN(p);
1467
1468 while (count--)
1469 {
1470#ifdef FEAT_MBYTE
1471 if (has_mbyte)
1472 {
1473 for (;;)
1474 {
1475 if (dir > 0)
1476 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001477 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001478 if (col >= len)
1479 return FAIL;
1480 }
1481 else
1482 {
1483 if (col == 0)
1484 return FAIL;
1485 col -= (*mb_head_off)(p, p + col - 1) + 1;
1486 }
1487 if (bytelen == 1)
1488 {
1489 if (p[col] == c)
1490 break;
1491 }
1492 else
1493 {
1494 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1495 break;
1496 }
1497 }
1498 }
1499 else
1500#endif
1501 {
1502 for (;;)
1503 {
1504 if ((col += dir) < 0 || col >= len)
1505 return FAIL;
1506 if (p[col] == c)
1507 break;
1508 }
1509 }
1510 }
1511
1512 if (t_cmd)
1513 {
1514 /* backup to before the character (possibly double-byte) */
1515 col -= dir;
1516#ifdef FEAT_MBYTE
1517 if (has_mbyte)
1518 {
1519 if (dir < 0)
1520 /* Landed on the search char which is bytelen long */
1521 col += bytelen - 1;
1522 else
1523 /* To previous char, which may be multi-byte. */
1524 col -= (*mb_head_off)(p, p + col);
1525 }
1526#endif
1527 }
1528 curwin->w_cursor.col = col;
1529
1530 return OK;
1531}
1532
1533/*
1534 * "Other" Searches
1535 */
1536
1537/*
1538 * findmatch - find the matching paren or brace
1539 *
1540 * Improvement over vi: Braces inside quotes are ignored.
1541 */
1542 pos_T *
1543findmatch(oap, initc)
1544 oparg_T *oap;
1545 int initc;
1546{
1547 return findmatchlimit(oap, initc, 0, 0);
1548}
1549
1550/*
1551 * Return TRUE if the character before "linep[col]" equals "ch".
1552 * Return FALSE if "col" is zero.
1553 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1554 * is NULL.
1555 * Handles multibyte string correctly.
1556 */
1557 static int
1558check_prevcol(linep, col, ch, prevcol)
1559 char_u *linep;
1560 int col;
1561 int ch;
1562 int *prevcol;
1563{
1564 --col;
1565#ifdef FEAT_MBYTE
1566 if (col > 0 && has_mbyte)
1567 col -= (*mb_head_off)(linep, linep + col);
1568#endif
1569 if (prevcol)
1570 *prevcol = col;
1571 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1572}
1573
1574/*
1575 * findmatchlimit -- find the matching paren or brace, if it exists within
1576 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1577 * the edge of the file.
1578 *
1579 * "initc" is the character to find a match for. NUL means to find the
1580 * character at or after the cursor.
1581 *
1582 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1583 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1584 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1585 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001586 *
1587 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1588 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001589 */
1590
1591 pos_T *
1592findmatchlimit(oap, initc, flags, maxtravel)
1593 oparg_T *oap;
1594 int initc;
1595 int flags;
1596 int maxtravel;
1597{
1598 static pos_T pos; /* current search position */
1599 int findc = 0; /* matching brace */
1600 int c;
1601 int count = 0; /* cumulative number of braces */
1602 int backwards = FALSE; /* init for gcc */
1603 int inquote = FALSE; /* TRUE when inside quotes */
1604 char_u *linep; /* pointer to current line */
1605 char_u *ptr;
1606 int do_quotes; /* check for quotes in current line */
1607 int at_start; /* do_quotes value at start position */
1608 int hash_dir = 0; /* Direction searched for # things */
1609 int comment_dir = 0; /* Direction searched for comments */
1610 pos_T match_pos; /* Where last slash-star was found */
1611 int start_in_quotes; /* start position is in quotes */
1612 int traveled = 0; /* how far we've searched so far */
1613 int ignore_cend = FALSE; /* ignore comment end */
1614 int cpo_match; /* vi compatible matching */
1615 int cpo_bsl; /* don't recognize backslashes */
1616 int match_escaped = 0; /* search for escaped match */
1617 int dir; /* Direction to search */
1618 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001619#ifdef FEAT_LISP
1620 int lispcomm = FALSE; /* inside of Lisp-style comment */
1621 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1622#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001623
1624 pos = curwin->w_cursor;
1625 linep = ml_get(pos.lnum);
1626
1627 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1628 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1629
1630 /* Direction to search when initc is '/', '*' or '#' */
1631 if (flags & FM_BACKWARD)
1632 dir = BACKWARD;
1633 else if (flags & FM_FORWARD)
1634 dir = FORWARD;
1635 else
1636 dir = 0;
1637
1638 /*
1639 * if initc given, look in the table for the matching character
1640 * '/' and '*' are special cases: look for start or end of comment.
1641 * When '/' is used, we ignore running backwards into an star-slash, for
1642 * "[*" command, we just want to find any comment.
1643 */
1644 if (initc == '/' || initc == '*')
1645 {
1646 comment_dir = dir;
1647 if (initc == '/')
1648 ignore_cend = TRUE;
1649 backwards = (dir == FORWARD) ? FALSE : TRUE;
1650 initc = NUL;
1651 }
1652 else if (initc != '#' && initc != NUL)
1653 {
1654 /* 'matchpairs' is "x:y,x:y" */
1655 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1656 {
1657 if (*ptr == initc)
1658 {
1659 findc = initc;
1660 initc = ptr[2];
1661 backwards = TRUE;
1662 break;
1663 }
1664 ptr += 2;
1665 if (*ptr == initc)
1666 {
1667 findc = initc;
1668 initc = ptr[-2];
1669 backwards = FALSE;
1670 break;
1671 }
1672 if (ptr[1] != ',')
1673 break;
1674 }
1675 if (!findc) /* invalid initc! */
1676 return NULL;
1677 }
1678 /*
1679 * Either initc is '#', or no initc was given and we need to look under the
1680 * cursor.
1681 */
1682 else
1683 {
1684 if (initc == '#')
1685 {
1686 hash_dir = dir;
1687 }
1688 else
1689 {
1690 /*
1691 * initc was not given, must look for something to match under
1692 * or near the cursor.
1693 * Only check for special things when 'cpo' doesn't have '%'.
1694 */
1695 if (!cpo_match)
1696 {
1697 /* Are we before or at #if, #else etc.? */
1698 ptr = skipwhite(linep);
1699 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1700 {
1701 ptr = skipwhite(ptr + 1);
1702 if ( STRNCMP(ptr, "if", 2) == 0
1703 || STRNCMP(ptr, "endif", 5) == 0
1704 || STRNCMP(ptr, "el", 2) == 0)
1705 hash_dir = 1;
1706 }
1707
1708 /* Are we on a comment? */
1709 else if (linep[pos.col] == '/')
1710 {
1711 if (linep[pos.col + 1] == '*')
1712 {
1713 comment_dir = FORWARD;
1714 backwards = FALSE;
1715 pos.col++;
1716 }
1717 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1718 {
1719 comment_dir = BACKWARD;
1720 backwards = TRUE;
1721 pos.col--;
1722 }
1723 }
1724 else if (linep[pos.col] == '*')
1725 {
1726 if (linep[pos.col + 1] == '/')
1727 {
1728 comment_dir = BACKWARD;
1729 backwards = TRUE;
1730 }
1731 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1732 {
1733 comment_dir = FORWARD;
1734 backwards = FALSE;
1735 }
1736 }
1737 }
1738
1739 /*
1740 * If we are not on a comment or the # at the start of a line, then
1741 * look for brace anywhere on this line after the cursor.
1742 */
1743 if (!hash_dir && !comment_dir)
1744 {
1745 /*
1746 * Find the brace under or after the cursor.
1747 * If beyond the end of the line, use the last character in
1748 * the line.
1749 */
1750 if (linep[pos.col] == NUL && pos.col)
1751 --pos.col;
1752 for (;;)
1753 {
1754 initc = linep[pos.col];
1755 if (initc == NUL)
1756 break;
1757
1758 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1759 {
1760 if (*ptr == initc)
1761 {
1762 findc = ptr[2];
1763 backwards = FALSE;
1764 break;
1765 }
1766 ptr += 2;
1767 if (*ptr == initc)
1768 {
1769 findc = ptr[-2];
1770 backwards = TRUE;
1771 break;
1772 }
1773 if (!*++ptr)
1774 break;
1775 }
1776 if (findc)
1777 break;
1778#ifdef FEAT_MBYTE
1779 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001780 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001781 else
1782#endif
1783 ++pos.col;
1784 }
1785 if (!findc)
1786 {
1787 /* no brace in the line, maybe use " #if" then */
1788 if (!cpo_match && *skipwhite(linep) == '#')
1789 hash_dir = 1;
1790 else
1791 return NULL;
1792 }
1793 else if (!cpo_bsl)
1794 {
1795 int col, bslcnt = 0;
1796
1797 /* Set "match_escaped" if there are an odd number of
1798 * backslashes. */
1799 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1800 bslcnt++;
1801 match_escaped = (bslcnt & 1);
1802 }
1803 }
1804 }
1805 if (hash_dir)
1806 {
1807 /*
1808 * Look for matching #if, #else, #elif, or #endif
1809 */
1810 if (oap != NULL)
1811 oap->motion_type = MLINE; /* Linewise for this case only */
1812 if (initc != '#')
1813 {
1814 ptr = skipwhite(skipwhite(linep) + 1);
1815 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1816 hash_dir = 1;
1817 else if (STRNCMP(ptr, "endif", 5) == 0)
1818 hash_dir = -1;
1819 else
1820 return NULL;
1821 }
1822 pos.col = 0;
1823 while (!got_int)
1824 {
1825 if (hash_dir > 0)
1826 {
1827 if (pos.lnum == curbuf->b_ml.ml_line_count)
1828 break;
1829 }
1830 else if (pos.lnum == 1)
1831 break;
1832 pos.lnum += hash_dir;
1833 linep = ml_get(pos.lnum);
1834 line_breakcheck(); /* check for CTRL-C typed */
1835 ptr = skipwhite(linep);
1836 if (*ptr != '#')
1837 continue;
1838 pos.col = (colnr_T) (ptr - linep);
1839 ptr = skipwhite(ptr + 1);
1840 if (hash_dir > 0)
1841 {
1842 if (STRNCMP(ptr, "if", 2) == 0)
1843 count++;
1844 else if (STRNCMP(ptr, "el", 2) == 0)
1845 {
1846 if (count == 0)
1847 return &pos;
1848 }
1849 else if (STRNCMP(ptr, "endif", 5) == 0)
1850 {
1851 if (count == 0)
1852 return &pos;
1853 count--;
1854 }
1855 }
1856 else
1857 {
1858 if (STRNCMP(ptr, "if", 2) == 0)
1859 {
1860 if (count == 0)
1861 return &pos;
1862 count--;
1863 }
1864 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1865 {
1866 if (count == 0)
1867 return &pos;
1868 }
1869 else if (STRNCMP(ptr, "endif", 5) == 0)
1870 count++;
1871 }
1872 }
1873 return NULL;
1874 }
1875 }
1876
1877#ifdef FEAT_RIGHTLEFT
1878 /* This is just guessing: when 'rightleft' is set, search for a maching
1879 * paren/brace in the other direction. */
1880 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1881 backwards = !backwards;
1882#endif
1883
1884 do_quotes = -1;
1885 start_in_quotes = MAYBE;
1886 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001887 if ((backwards && comment_dir)
1888#ifdef FEAT_LISP
1889 || lisp
1890#endif
1891 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001892 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001893#ifdef FEAT_LISP
1894 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1895 lispcomm = TRUE; /* find match inside this comment */
1896#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001897 while (!got_int)
1898 {
1899 /*
1900 * Go to the next position, forward or backward. We could use
1901 * inc() and dec() here, but that is much slower
1902 */
1903 if (backwards)
1904 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001905#ifdef FEAT_LISP
1906 /* char to match is inside of comment, don't search outside */
1907 if (lispcomm && pos.col < (colnr_T)comment_col)
1908 break;
1909#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001910 if (pos.col == 0) /* at start of line, go to prev. one */
1911 {
1912 if (pos.lnum == 1) /* start of file */
1913 break;
1914 --pos.lnum;
1915
1916 if (maxtravel && traveled++ > maxtravel)
1917 break;
1918
1919 linep = ml_get(pos.lnum);
1920 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1921 do_quotes = -1;
1922 line_breakcheck();
1923
1924 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001925 if (comment_dir
1926#ifdef FEAT_LISP
1927 || lisp
1928#endif
1929 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001930 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001931#ifdef FEAT_LISP
1932 /* skip comment */
1933 if (lisp && comment_col != MAXCOL)
1934 pos.col = comment_col;
1935#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001936 }
1937 else
1938 {
1939 --pos.col;
1940#ifdef FEAT_MBYTE
1941 if (has_mbyte)
1942 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1943#endif
1944 }
1945 }
1946 else /* forward search */
1947 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001948 if (linep[pos.col] == NUL
1949 /* at end of line, go to next one */
1950#ifdef FEAT_LISP
1951 /* don't search for match in comment */
1952 || (lisp && comment_col != MAXCOL
1953 && pos.col == (colnr_T)comment_col)
1954#endif
1955 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001956 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001957 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
1958#ifdef FEAT_LISP
1959 /* line is exhausted and comment with it,
1960 * don't search for match in code */
1961 || lispcomm
1962#endif
1963 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 break;
1965 ++pos.lnum;
1966
1967 if (maxtravel && traveled++ > maxtravel)
1968 break;
1969
1970 linep = ml_get(pos.lnum);
1971 pos.col = 0;
1972 do_quotes = -1;
1973 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001974#ifdef FEAT_LISP
1975 if (lisp) /* find comment pos in new line */
1976 comment_col = check_linecomment(linep);
1977#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001978 }
1979 else
1980 {
1981#ifdef FEAT_MBYTE
1982 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001983 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001984 else
1985#endif
1986 ++pos.col;
1987 }
1988 }
1989
1990 /*
1991 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
1992 */
1993 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
1994 (linep[0] == '{' || linep[0] == '}'))
1995 {
1996 if (linep[0] == findc && count == 0) /* match! */
1997 return &pos;
1998 break; /* out of scope */
1999 }
2000
2001 if (comment_dir)
2002 {
2003 /* Note: comments do not nest, and we ignore quotes in them */
2004 /* TODO: ignore comment brackets inside strings */
2005 if (comment_dir == FORWARD)
2006 {
2007 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2008 {
2009 pos.col++;
2010 return &pos;
2011 }
2012 }
2013 else /* Searching backwards */
2014 {
2015 /*
2016 * A comment may contain / * or / /, it may also start or end
2017 * with / * /. Ignore a / * after / /.
2018 */
2019 if (pos.col == 0)
2020 continue;
2021 else if ( linep[pos.col - 1] == '/'
2022 && linep[pos.col] == '*'
2023 && (int)pos.col < comment_col)
2024 {
2025 count++;
2026 match_pos = pos;
2027 match_pos.col--;
2028 }
2029 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2030 {
2031 if (count > 0)
2032 pos = match_pos;
2033 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2034 && (int)pos.col <= comment_col)
2035 pos.col -= 2;
2036 else if (ignore_cend)
2037 continue;
2038 else
2039 return NULL;
2040 return &pos;
2041 }
2042 }
2043 continue;
2044 }
2045
2046 /*
2047 * If smart matching ('cpoptions' does not contain '%'), braces inside
2048 * of quotes are ignored, but only if there is an even number of
2049 * quotes in the line.
2050 */
2051 if (cpo_match)
2052 do_quotes = 0;
2053 else if (do_quotes == -1)
2054 {
2055 /*
2056 * Count the number of quotes in the line, skipping \" and '"'.
2057 * Watch out for "\\".
2058 */
2059 at_start = do_quotes;
2060 for (ptr = linep; *ptr; ++ptr)
2061 {
2062 if (ptr == linep + pos.col + backwards)
2063 at_start = (do_quotes & 1);
2064 if (*ptr == '"'
2065 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2066 ++do_quotes;
2067 if (*ptr == '\\' && ptr[1] != NUL)
2068 ++ptr;
2069 }
2070 do_quotes &= 1; /* result is 1 with even number of quotes */
2071
2072 /*
2073 * If we find an uneven count, check current line and previous
2074 * one for a '\' at the end.
2075 */
2076 if (!do_quotes)
2077 {
2078 inquote = FALSE;
2079 if (ptr[-1] == '\\')
2080 {
2081 do_quotes = 1;
2082 if (start_in_quotes == MAYBE)
2083 {
2084 /* Do we need to use at_start here? */
2085 inquote = TRUE;
2086 start_in_quotes = TRUE;
2087 }
2088 else if (backwards)
2089 inquote = TRUE;
2090 }
2091 if (pos.lnum > 1)
2092 {
2093 ptr = ml_get(pos.lnum - 1);
2094 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2095 {
2096 do_quotes = 1;
2097 if (start_in_quotes == MAYBE)
2098 {
2099 inquote = at_start;
2100 if (inquote)
2101 start_in_quotes = TRUE;
2102 }
2103 else if (!backwards)
2104 inquote = TRUE;
2105 }
2106 }
2107 }
2108 }
2109 if (start_in_quotes == MAYBE)
2110 start_in_quotes = FALSE;
2111
2112 /*
2113 * If 'smartmatch' is set:
2114 * Things inside quotes are ignored by setting 'inquote'. If we
2115 * find a quote without a preceding '\' invert 'inquote'. At the
2116 * end of a line not ending in '\' we reset 'inquote'.
2117 *
2118 * In lines with an uneven number of quotes (without preceding '\')
2119 * we do not know which part to ignore. Therefore we only set
2120 * inquote if the number of quotes in a line is even, unless this
2121 * line or the previous one ends in a '\'. Complicated, isn't it?
2122 */
2123 switch (c = linep[pos.col])
2124 {
2125 case NUL:
2126 /* at end of line without trailing backslash, reset inquote */
2127 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2128 {
2129 inquote = FALSE;
2130 start_in_quotes = FALSE;
2131 }
2132 break;
2133
2134 case '"':
2135 /* a quote that is preceded with an odd number of backslashes is
2136 * ignored */
2137 if (do_quotes)
2138 {
2139 int col;
2140
2141 for (col = pos.col - 1; col >= 0; --col)
2142 if (linep[col] != '\\')
2143 break;
2144 if ((((int)pos.col - 1 - col) & 1) == 0)
2145 {
2146 inquote = !inquote;
2147 start_in_quotes = FALSE;
2148 }
2149 }
2150 break;
2151
2152 /*
2153 * If smart matching ('cpoptions' does not contain '%'):
2154 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2155 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2156 * skipped, there is never a brace in them.
2157 * Ignore this when finding matches for `'.
2158 */
2159 case '\'':
2160 if (!cpo_match && initc != '\'' && findc != '\'')
2161 {
2162 if (backwards)
2163 {
2164 if (pos.col > 1)
2165 {
2166 if (linep[pos.col - 2] == '\'')
2167 {
2168 pos.col -= 2;
2169 break;
2170 }
2171 else if (linep[pos.col - 2] == '\\' &&
2172 pos.col > 2 && linep[pos.col - 3] == '\'')
2173 {
2174 pos.col -= 3;
2175 break;
2176 }
2177 }
2178 }
2179 else if (linep[pos.col + 1]) /* forward search */
2180 {
2181 if (linep[pos.col + 1] == '\\' &&
2182 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2183 {
2184 pos.col += 3;
2185 break;
2186 }
2187 else if (linep[pos.col + 2] == '\'')
2188 {
2189 pos.col += 2;
2190 break;
2191 }
2192 }
2193 }
2194 /* FALLTHROUGH */
2195
2196 default:
2197#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002198 /*
2199 * For Lisp skip over backslashed (), {} and [].
2200 * (actually, we skip #\( et al)
2201 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002202 if (curbuf->b_p_lisp
2203 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002204 && pos.col > 1
2205 && check_prevcol(linep, pos.col, '\\', NULL)
2206 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002207 break;
2208#endif
2209
2210 /* Check for match outside of quotes, and inside of
2211 * quotes when the start is also inside of quotes. */
2212 if ((!inquote || start_in_quotes == TRUE)
2213 && (c == initc || c == findc))
2214 {
2215 int col, bslcnt = 0;
2216
2217 if (!cpo_bsl)
2218 {
2219 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2220 bslcnt++;
2221 }
2222 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2223 * what we expect. */
2224 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2225 {
2226 if (c == initc)
2227 count++;
2228 else
2229 {
2230 if (count == 0)
2231 return &pos;
2232 count--;
2233 }
2234 }
2235 }
2236 }
2237 }
2238
2239 if (comment_dir == BACKWARD && count > 0)
2240 {
2241 pos = match_pos;
2242 return &pos;
2243 }
2244 return (pos_T *)NULL; /* never found it */
2245}
2246
2247/*
2248 * Check if line[] contains a / / comment.
2249 * Return MAXCOL if not, otherwise return the column.
2250 * TODO: skip strings.
2251 */
2252 static int
2253check_linecomment(line)
2254 char_u *line;
2255{
2256 char_u *p;
2257
2258 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002259#ifdef FEAT_LISP
2260 /* skip Lispish one-line comments */
2261 if (curbuf->b_p_lisp)
2262 {
2263 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2264 {
2265 int instr = FALSE; /* inside of string */
2266
2267 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002268 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002269 {
2270 if (*p == '"')
2271 {
2272 if (instr)
2273 {
2274 if (*(p - 1) != '\\') /* skip escaped quote */
2275 instr = FALSE;
2276 }
2277 else if (p == line || ((p - line) >= 2
2278 /* skip #\" form */
2279 && *(p - 1) != '\\' && *(p - 2) != '#'))
2280 instr = TRUE;
2281 }
2282 else if (!instr && ((p - line) < 2
2283 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2284 break; /* found! */
2285 ++p;
2286 }
2287 }
2288 else
2289 p = NULL;
2290 }
2291 else
2292#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002293 while ((p = vim_strchr(p, '/')) != NULL)
2294 {
2295 if (p[1] == '/')
2296 break;
2297 ++p;
2298 }
2299
2300 if (p == NULL)
2301 return MAXCOL;
2302 return (int)(p - line);
2303}
2304
2305/*
2306 * Move cursor briefly to character matching the one under the cursor.
2307 * Used for Insert mode and "r" command.
2308 * Show the match only if it is visible on the screen.
2309 * If there isn't a match, then beep.
2310 */
2311 void
2312showmatch(c)
2313 int c; /* char to show match for */
2314{
2315 pos_T *lpos, save_cursor;
2316 pos_T mpos;
2317 colnr_T vcol;
2318 long save_so;
2319 long save_siso;
2320#ifdef CURSOR_SHAPE
2321 int save_state;
2322#endif
2323 colnr_T save_dollar_vcol;
2324 char_u *p;
2325
2326 /*
2327 * Only show match for chars in the 'matchpairs' option.
2328 */
2329 /* 'matchpairs' is "x:y,x:y" */
2330 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2331 {
2332#ifdef FEAT_RIGHTLEFT
2333 if (*p == c && (curwin->w_p_rl ^ p_ri))
2334 break;
2335#endif
2336 p += 2;
2337 if (*p == c
2338#ifdef FEAT_RIGHTLEFT
2339 && !(curwin->w_p_rl ^ p_ri)
2340#endif
2341 )
2342 break;
2343 if (p[1] != ',')
2344 return;
2345 }
2346
2347 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2348 vim_beep();
2349 else if (lpos->lnum >= curwin->w_topline)
2350 {
2351 if (!curwin->w_p_wrap)
2352 getvcol(curwin, lpos, NULL, &vcol, NULL);
2353 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2354 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2355 {
2356 mpos = *lpos; /* save the pos, update_screen() may change it */
2357 save_cursor = curwin->w_cursor;
2358 save_so = p_so;
2359 save_siso = p_siso;
2360 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2361 * stop displaying the "$". */
2362 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2363 dollar_vcol = 0;
2364 ++curwin->w_virtcol; /* do display ')' just before "$" */
2365 update_screen(VALID); /* show the new char first */
2366
2367 save_dollar_vcol = dollar_vcol;
2368#ifdef CURSOR_SHAPE
2369 save_state = State;
2370 State = SHOWMATCH;
2371 ui_cursor_shape(); /* may show different cursor shape */
2372#endif
2373 curwin->w_cursor = mpos; /* move to matching char */
2374 p_so = 0; /* don't use 'scrolloff' here */
2375 p_siso = 0; /* don't use 'sidescrolloff' here */
2376 showruler(FALSE);
2377 setcursor();
2378 cursor_on(); /* make sure that the cursor is shown */
2379 out_flush();
2380#ifdef FEAT_GUI
2381 if (gui.in_use)
2382 {
2383 gui_update_cursor(TRUE, FALSE);
2384 gui_mch_flush();
2385 }
2386#endif
2387 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2388 * which resets it if the matching position is in a previous line
2389 * and has a higher column number. */
2390 dollar_vcol = save_dollar_vcol;
2391
2392 /*
2393 * brief pause, unless 'm' is present in 'cpo' and a character is
2394 * available.
2395 */
2396 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2397 ui_delay(p_mat * 100L, TRUE);
2398 else if (!char_avail())
2399 ui_delay(p_mat * 100L, FALSE);
2400 curwin->w_cursor = save_cursor; /* restore cursor position */
2401 p_so = save_so;
2402 p_siso = save_siso;
2403#ifdef CURSOR_SHAPE
2404 State = save_state;
2405 ui_cursor_shape(); /* may show different cursor shape */
2406#endif
2407 }
2408 }
2409}
2410
2411/*
2412 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002413 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002414 * space or a line break. Also stop at an empty line.
2415 * Return OK if the next sentence was found.
2416 */
2417 int
2418findsent(dir, count)
2419 int dir;
2420 long count;
2421{
2422 pos_T pos, tpos;
2423 int c;
2424 int (*func) __ARGS((pos_T *));
2425 int startlnum;
2426 int noskip = FALSE; /* do not skip blanks */
2427 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002428 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002429
2430 pos = curwin->w_cursor;
2431 if (dir == FORWARD)
2432 func = incl;
2433 else
2434 func = decl;
2435
2436 while (count--)
2437 {
2438 /*
2439 * if on an empty line, skip upto a non-empty line
2440 */
2441 if (gchar_pos(&pos) == NUL)
2442 {
2443 do
2444 if ((*func)(&pos) == -1)
2445 break;
2446 while (gchar_pos(&pos) == NUL);
2447 if (dir == FORWARD)
2448 goto found;
2449 }
2450 /*
2451 * if on the start of a paragraph or a section and searching forward,
2452 * go to the next line
2453 */
2454 else if (dir == FORWARD && pos.col == 0 &&
2455 startPS(pos.lnum, NUL, FALSE))
2456 {
2457 if (pos.lnum == curbuf->b_ml.ml_line_count)
2458 return FAIL;
2459 ++pos.lnum;
2460 goto found;
2461 }
2462 else if (dir == BACKWARD)
2463 decl(&pos);
2464
2465 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002466 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002467 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2468 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2469 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002470 if (vim_strchr((char_u *)".!?", c) != NULL)
2471 {
2472 /* Only skip over a '.', '!' and '?' once. */
2473 if (found_dot)
2474 break;
2475 found_dot = TRUE;
2476 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002477 if (decl(&pos) == -1)
2478 break;
2479 /* when going forward: Stop in front of empty line */
2480 if (lineempty(pos.lnum) && dir == FORWARD)
2481 {
2482 incl(&pos);
2483 goto found;
2484 }
2485 }
2486
2487 /* remember the line where the search started */
2488 startlnum = pos.lnum;
2489 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2490
2491 for (;;) /* find end of sentence */
2492 {
2493 c = gchar_pos(&pos);
2494 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2495 {
2496 if (dir == BACKWARD && pos.lnum != startlnum)
2497 ++pos.lnum;
2498 break;
2499 }
2500 if (c == '.' || c == '!' || c == '?')
2501 {
2502 tpos = pos;
2503 do
2504 if ((c = inc(&tpos)) == -1)
2505 break;
2506 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2507 != NULL);
2508 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2509 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2510 && gchar_pos(&tpos) == ' ')))
2511 {
2512 pos = tpos;
2513 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2514 inc(&pos);
2515 break;
2516 }
2517 }
2518 if ((*func)(&pos) == -1)
2519 {
2520 if (count)
2521 return FAIL;
2522 noskip = TRUE;
2523 break;
2524 }
2525 }
2526found:
2527 /* skip white space */
2528 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2529 if (incl(&pos) == -1)
2530 break;
2531 }
2532
2533 setpcmark();
2534 curwin->w_cursor = pos;
2535 return OK;
2536}
2537
2538/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002539 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002540 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002541 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002542 * If 'what' is '{' or '}' we go to the next section.
2543 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002544 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002545 */
2546 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002547findpar(pincl, dir, count, what, both)
2548 int *pincl; /* Return: TRUE if last char is to be included */
2549 int dir;
2550 long count;
2551 int what;
2552 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553{
2554 linenr_T curr;
2555 int did_skip; /* TRUE after separating lines have been skipped */
2556 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002557 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002558#ifdef FEAT_FOLDING
2559 linenr_T fold_first; /* first line of a closed fold */
2560 linenr_T fold_last; /* last line of a closed fold */
2561 int fold_skipped; /* TRUE if a closed fold was skipped this
2562 iteration */
2563#endif
2564
2565 curr = curwin->w_cursor.lnum;
2566
2567 while (count--)
2568 {
2569 did_skip = FALSE;
2570 for (first = TRUE; ; first = FALSE)
2571 {
2572 if (*ml_get(curr) != NUL)
2573 did_skip = TRUE;
2574
2575#ifdef FEAT_FOLDING
2576 /* skip folded lines */
2577 fold_skipped = FALSE;
2578 if (first && hasFolding(curr, &fold_first, &fold_last))
2579 {
2580 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2581 fold_skipped = TRUE;
2582 }
2583#endif
2584
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002585 /* POSIX has it's own ideas of what a paragraph boundary is and it
2586 * doesn't match historical Vi: It also stops at a "{" in the
2587 * first column and at an empty line. */
2588 if (!first && did_skip && (startPS(curr, what, both)
2589 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002590 break;
2591
2592#ifdef FEAT_FOLDING
2593 if (fold_skipped)
2594 curr -= dir;
2595#endif
2596 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2597 {
2598 if (count)
2599 return FALSE;
2600 curr -= dir;
2601 break;
2602 }
2603 }
2604 }
2605 setpcmark();
2606 if (both && *ml_get(curr) == '}') /* include line with '}' */
2607 ++curr;
2608 curwin->w_cursor.lnum = curr;
2609 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2610 {
2611 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2612 {
2613 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002614 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002615 }
2616 }
2617 else
2618 curwin->w_cursor.col = 0;
2619 return TRUE;
2620}
2621
2622/*
2623 * check if the string 's' is a nroff macro that is in option 'opt'
2624 */
2625 static int
2626inmacro(opt, s)
2627 char_u *opt;
2628 char_u *s;
2629{
2630 char_u *macro;
2631
2632 for (macro = opt; macro[0]; ++macro)
2633 {
2634 /* Accept two characters in the option being equal to two characters
2635 * in the line. A space in the option matches with a space in the
2636 * line or the line having ended. */
2637 if ( (macro[0] == s[0]
2638 || (macro[0] == ' '
2639 && (s[0] == NUL || s[0] == ' ')))
2640 && (macro[1] == s[1]
2641 || ((macro[1] == NUL || macro[1] == ' ')
2642 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2643 break;
2644 ++macro;
2645 if (macro[0] == NUL)
2646 break;
2647 }
2648 return (macro[0] != NUL);
2649}
2650
2651/*
2652 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2653 * If 'para' is '{' or '}' only check for sections.
2654 * If 'both' is TRUE also stop at '}'
2655 */
2656 int
2657startPS(lnum, para, both)
2658 linenr_T lnum;
2659 int para;
2660 int both;
2661{
2662 char_u *s;
2663
2664 s = ml_get(lnum);
2665 if (*s == para || *s == '\f' || (both && *s == '}'))
2666 return TRUE;
2667 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2668 (!para && inmacro(p_para, s + 1))))
2669 return TRUE;
2670 return FALSE;
2671}
2672
2673/*
2674 * The following routines do the word searches performed by the 'w', 'W',
2675 * 'b', 'B', 'e', and 'E' commands.
2676 */
2677
2678/*
2679 * To perform these searches, characters are placed into one of three
2680 * classes, and transitions between classes determine word boundaries.
2681 *
2682 * The classes are:
2683 *
2684 * 0 - white space
2685 * 1 - punctuation
2686 * 2 or higher - keyword characters (letters, digits and underscore)
2687 */
2688
2689static int cls_bigword; /* TRUE for "W", "B" or "E" */
2690
2691/*
2692 * cls() - returns the class of character at curwin->w_cursor
2693 *
2694 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2695 * from class 2 and higher are reported as class 1 since only white space
2696 * boundaries are of interest.
2697 */
2698 static int
2699cls()
2700{
2701 int c;
2702
2703 c = gchar_cursor();
2704#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2705 if (p_altkeymap && c == F_BLANK)
2706 return 0;
2707#endif
2708 if (c == ' ' || c == '\t' || c == NUL)
2709 return 0;
2710#ifdef FEAT_MBYTE
2711 if (enc_dbcs != 0 && c > 0xFF)
2712 {
2713 /* If cls_bigword, report multi-byte chars as class 1. */
2714 if (enc_dbcs == DBCS_KOR && cls_bigword)
2715 return 1;
2716
2717 /* process code leading/trailing bytes */
2718 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2719 }
2720 if (enc_utf8)
2721 {
2722 c = utf_class(c);
2723 if (c != 0 && cls_bigword)
2724 return 1;
2725 return c;
2726 }
2727#endif
2728
2729 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2730 if (cls_bigword)
2731 return 1;
2732
2733 if (vim_iswordc(c))
2734 return 2;
2735 return 1;
2736}
2737
2738
2739/*
2740 * fwd_word(count, type, eol) - move forward one word
2741 *
2742 * Returns FAIL if the cursor was already at the end of the file.
2743 * If eol is TRUE, last word stops at end of line (for operators).
2744 */
2745 int
2746fwd_word(count, bigword, eol)
2747 long count;
2748 int bigword; /* "W", "E" or "B" */
2749 int eol;
2750{
2751 int sclass; /* starting class */
2752 int i;
2753 int last_line;
2754
2755#ifdef FEAT_VIRTUALEDIT
2756 curwin->w_cursor.coladd = 0;
2757#endif
2758 cls_bigword = bigword;
2759 while (--count >= 0)
2760 {
2761#ifdef FEAT_FOLDING
2762 /* When inside a range of folded lines, move to the last char of the
2763 * last line. */
2764 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2765 coladvance((colnr_T)MAXCOL);
2766#endif
2767 sclass = cls();
2768
2769 /*
2770 * We always move at least one character, unless on the last
2771 * character in the buffer.
2772 */
2773 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2774 i = inc_cursor();
2775 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2776 return FAIL;
2777 if (i == 1 && eol && count == 0) /* started at last char in line */
2778 return OK;
2779
2780 /*
2781 * Go one char past end of current word (if any)
2782 */
2783 if (sclass != 0)
2784 while (cls() == sclass)
2785 {
2786 i = inc_cursor();
2787 if (i == -1 || (i >= 1 && eol && count == 0))
2788 return OK;
2789 }
2790
2791 /*
2792 * go to next non-white
2793 */
2794 while (cls() == 0)
2795 {
2796 /*
2797 * We'll stop if we land on a blank line
2798 */
2799 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2800 break;
2801
2802 i = inc_cursor();
2803 if (i == -1 || (i >= 1 && eol && count == 0))
2804 return OK;
2805 }
2806 }
2807 return OK;
2808}
2809
2810/*
2811 * bck_word() - move backward 'count' words
2812 *
2813 * If stop is TRUE and we are already on the start of a word, move one less.
2814 *
2815 * Returns FAIL if top of the file was reached.
2816 */
2817 int
2818bck_word(count, bigword, stop)
2819 long count;
2820 int bigword;
2821 int stop;
2822{
2823 int sclass; /* starting class */
2824
2825#ifdef FEAT_VIRTUALEDIT
2826 curwin->w_cursor.coladd = 0;
2827#endif
2828 cls_bigword = bigword;
2829 while (--count >= 0)
2830 {
2831#ifdef FEAT_FOLDING
2832 /* When inside a range of folded lines, move to the first char of the
2833 * first line. */
2834 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2835 curwin->w_cursor.col = 0;
2836#endif
2837 sclass = cls();
2838 if (dec_cursor() == -1) /* started at start of file */
2839 return FAIL;
2840
2841 if (!stop || sclass == cls() || sclass == 0)
2842 {
2843 /*
2844 * Skip white space before the word.
2845 * Stop on an empty line.
2846 */
2847 while (cls() == 0)
2848 {
2849 if (curwin->w_cursor.col == 0
2850 && lineempty(curwin->w_cursor.lnum))
2851 goto finished;
2852 if (dec_cursor() == -1) /* hit start of file, stop here */
2853 return OK;
2854 }
2855
2856 /*
2857 * Move backward to start of this word.
2858 */
2859 if (skip_chars(cls(), BACKWARD))
2860 return OK;
2861 }
2862
2863 inc_cursor(); /* overshot - forward one */
2864finished:
2865 stop = FALSE;
2866 }
2867 return OK;
2868}
2869
2870/*
2871 * end_word() - move to the end of the word
2872 *
2873 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2874 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2875 * motion crosses blank lines. When the real vi crosses a blank line in an
2876 * 'e' motion, the cursor is placed on the FIRST character of the next
2877 * non-blank line. The 'E' command, however, works correctly. Since this
2878 * appears to be a bug, I have not duplicated it here.
2879 *
2880 * Returns FAIL if end of the file was reached.
2881 *
2882 * If stop is TRUE and we are already on the end of a word, move one less.
2883 * If empty is TRUE stop on an empty line.
2884 */
2885 int
2886end_word(count, bigword, stop, empty)
2887 long count;
2888 int bigword;
2889 int stop;
2890 int empty;
2891{
2892 int sclass; /* starting class */
2893
2894#ifdef FEAT_VIRTUALEDIT
2895 curwin->w_cursor.coladd = 0;
2896#endif
2897 cls_bigword = bigword;
2898 while (--count >= 0)
2899 {
2900#ifdef FEAT_FOLDING
2901 /* When inside a range of folded lines, move to the last char of the
2902 * last line. */
2903 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2904 coladvance((colnr_T)MAXCOL);
2905#endif
2906 sclass = cls();
2907 if (inc_cursor() == -1)
2908 return FAIL;
2909
2910 /*
2911 * If we're in the middle of a word, we just have to move to the end
2912 * of it.
2913 */
2914 if (cls() == sclass && sclass != 0)
2915 {
2916 /*
2917 * Move forward to end of the current word
2918 */
2919 if (skip_chars(sclass, FORWARD))
2920 return FAIL;
2921 }
2922 else if (!stop || sclass == 0)
2923 {
2924 /*
2925 * We were at the end of a word. Go to the end of the next word.
2926 * First skip white space, if 'empty' is TRUE, stop at empty line.
2927 */
2928 while (cls() == 0)
2929 {
2930 if (empty && curwin->w_cursor.col == 0
2931 && lineempty(curwin->w_cursor.lnum))
2932 goto finished;
2933 if (inc_cursor() == -1) /* hit end of file, stop here */
2934 return FAIL;
2935 }
2936
2937 /*
2938 * Move forward to the end of this word.
2939 */
2940 if (skip_chars(cls(), FORWARD))
2941 return FAIL;
2942 }
2943 dec_cursor(); /* overshot - one char backward */
2944finished:
2945 stop = FALSE; /* we move only one word less */
2946 }
2947 return OK;
2948}
2949
2950/*
2951 * Move back to the end of the word.
2952 *
2953 * Returns FAIL if start of the file was reached.
2954 */
2955 int
2956bckend_word(count, bigword, eol)
2957 long count;
2958 int bigword; /* TRUE for "B" */
2959 int eol; /* TRUE: stop at end of line. */
2960{
2961 int sclass; /* starting class */
2962 int i;
2963
2964#ifdef FEAT_VIRTUALEDIT
2965 curwin->w_cursor.coladd = 0;
2966#endif
2967 cls_bigword = bigword;
2968 while (--count >= 0)
2969 {
2970 sclass = cls();
2971 if ((i = dec_cursor()) == -1)
2972 return FAIL;
2973 if (eol && i == 1)
2974 return OK;
2975
2976 /*
2977 * Move backward to before the start of this word.
2978 */
2979 if (sclass != 0)
2980 {
2981 while (cls() == sclass)
2982 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2983 return OK;
2984 }
2985
2986 /*
2987 * Move backward to end of the previous word
2988 */
2989 while (cls() == 0)
2990 {
2991 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
2992 break;
2993 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2994 return OK;
2995 }
2996 }
2997 return OK;
2998}
2999
3000/*
3001 * Skip a row of characters of the same class.
3002 * Return TRUE when end-of-file reached, FALSE otherwise.
3003 */
3004 static int
3005skip_chars(cclass, dir)
3006 int cclass;
3007 int dir;
3008{
3009 while (cls() == cclass)
3010 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3011 return TRUE;
3012 return FALSE;
3013}
3014
3015#ifdef FEAT_TEXTOBJ
3016/*
3017 * Go back to the start of the word or the start of white space
3018 */
3019 static void
3020back_in_line()
3021{
3022 int sclass; /* starting class */
3023
3024 sclass = cls();
3025 for (;;)
3026 {
3027 if (curwin->w_cursor.col == 0) /* stop at start of line */
3028 break;
3029 dec_cursor();
3030 if (cls() != sclass) /* stop at start of word */
3031 {
3032 inc_cursor();
3033 break;
3034 }
3035 }
3036}
3037
3038 static void
3039find_first_blank(posp)
3040 pos_T *posp;
3041{
3042 int c;
3043
3044 while (decl(posp) != -1)
3045 {
3046 c = gchar_pos(posp);
3047 if (!vim_iswhite(c))
3048 {
3049 incl(posp);
3050 break;
3051 }
3052 }
3053}
3054
3055/*
3056 * Skip count/2 sentences and count/2 separating white spaces.
3057 */
3058 static void
3059findsent_forward(count, at_start_sent)
3060 long count;
3061 int at_start_sent; /* cursor is at start of sentence */
3062{
3063 while (count--)
3064 {
3065 findsent(FORWARD, 1L);
3066 if (at_start_sent)
3067 find_first_blank(&curwin->w_cursor);
3068 if (count == 0 || at_start_sent)
3069 decl(&curwin->w_cursor);
3070 at_start_sent = !at_start_sent;
3071 }
3072}
3073
3074/*
3075 * Find word under cursor, cursor at end.
3076 * Used while an operator is pending, and in Visual mode.
3077 */
3078 int
3079current_word(oap, count, include, bigword)
3080 oparg_T *oap;
3081 long count;
3082 int include; /* TRUE: include word and white space */
3083 int bigword; /* FALSE == word, TRUE == WORD */
3084{
3085 pos_T start_pos;
3086 pos_T pos;
3087 int inclusive = TRUE;
3088 int include_white = FALSE;
3089
3090 cls_bigword = bigword;
3091
3092#ifdef FEAT_VISUAL
3093 /* Correct cursor when 'selection' is exclusive */
3094 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3095 dec_cursor();
3096
3097 /*
3098 * When Visual mode is not active, or when the VIsual area is only one
3099 * character, select the word and/or white space under the cursor.
3100 */
3101 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3102#endif
3103 {
3104 /*
3105 * Go to start of current word or white space.
3106 */
3107 back_in_line();
3108 start_pos = curwin->w_cursor;
3109
3110 /*
3111 * If the start is on white space, and white space should be included
3112 * (" word"), or start is not on white space, and white space should
3113 * not be included ("word"), find end of word.
3114 */
3115 if ((cls() == 0) == include)
3116 {
3117 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3118 return FAIL;
3119 }
3120 else
3121 {
3122 /*
3123 * If the start is not on white space, and white space should be
3124 * included ("word "), or start is on white space and white
3125 * space should not be included (" "), find start of word.
3126 * If we end up in the first column of the next line (single char
3127 * word) back up to end of the line.
3128 */
3129 fwd_word(1L, bigword, TRUE);
3130 if (curwin->w_cursor.col == 0)
3131 decl(&curwin->w_cursor);
3132 else
3133 oneleft();
3134
3135 if (include)
3136 include_white = TRUE;
3137 }
3138
3139#ifdef FEAT_VISUAL
3140 if (VIsual_active)
3141 {
3142 /* should do something when inclusive == FALSE ! */
3143 VIsual = start_pos;
3144 redraw_curbuf_later(INVERTED); /* update the inversion */
3145 }
3146 else
3147#endif
3148 {
3149 oap->start = start_pos;
3150 oap->motion_type = MCHAR;
3151 }
3152 --count;
3153 }
3154
3155 /*
3156 * When count is still > 0, extend with more objects.
3157 */
3158 while (count > 0)
3159 {
3160 inclusive = TRUE;
3161#ifdef FEAT_VISUAL
3162 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3163 {
3164 /*
3165 * In Visual mode, with cursor at start: move cursor back.
3166 */
3167 if (decl(&curwin->w_cursor) == -1)
3168 return FAIL;
3169 if (include != (cls() != 0))
3170 {
3171 if (bck_word(1L, bigword, TRUE) == FAIL)
3172 return FAIL;
3173 }
3174 else
3175 {
3176 if (bckend_word(1L, bigword, TRUE) == FAIL)
3177 return FAIL;
3178 (void)incl(&curwin->w_cursor);
3179 }
3180 }
3181 else
3182#endif
3183 {
3184 /*
3185 * Move cursor forward one word and/or white area.
3186 */
3187 if (incl(&curwin->w_cursor) == -1)
3188 return FAIL;
3189 if (include != (cls() == 0))
3190 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003191 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003192 return FAIL;
3193 /*
3194 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003195 * the first character on the line.
3196 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003197 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003198 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 inclusive = FALSE;
3200 }
3201 else
3202 {
3203 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3204 return FAIL;
3205 }
3206 }
3207 --count;
3208 }
3209
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003210 if (include_white && (cls() != 0
3211 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003212 {
3213 /*
3214 * If we don't include white space at the end, move the start
3215 * to include some white space there. This makes "daw" work
3216 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003217 * word). Also when "2daw" deletes "word." at the end of the line
3218 * (cursor is at start of next line).
3219 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 */
3221 pos = curwin->w_cursor; /* save cursor position */
3222 curwin->w_cursor = start_pos;
3223 if (oneleft() == OK)
3224 {
3225 back_in_line();
3226 if (cls() == 0 && curwin->w_cursor.col > 0)
3227 {
3228#ifdef FEAT_VISUAL
3229 if (VIsual_active)
3230 VIsual = curwin->w_cursor;
3231 else
3232#endif
3233 oap->start = curwin->w_cursor;
3234 }
3235 }
3236 curwin->w_cursor = pos; /* put cursor back at end */
3237 }
3238
3239#ifdef FEAT_VISUAL
3240 if (VIsual_active)
3241 {
3242 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3243 inc_cursor();
3244 if (VIsual_mode == 'V')
3245 {
3246 VIsual_mode = 'v';
3247 redraw_cmdline = TRUE; /* show mode later */
3248 }
3249 }
3250 else
3251#endif
3252 oap->inclusive = inclusive;
3253
3254 return OK;
3255}
3256
3257/*
3258 * Find sentence(s) under the cursor, cursor at end.
3259 * When Visual active, extend it by one or more sentences.
3260 */
3261 int
3262current_sent(oap, count, include)
3263 oparg_T *oap;
3264 long count;
3265 int include;
3266{
3267 pos_T start_pos;
3268 pos_T pos;
3269 int start_blank;
3270 int c;
3271 int at_start_sent;
3272 long ncount;
3273
3274 start_pos = curwin->w_cursor;
3275 pos = start_pos;
3276 findsent(FORWARD, 1L); /* Find start of next sentence. */
3277
3278#ifdef FEAT_VISUAL
3279 /*
3280 * When visual area is bigger than one character: Extend it.
3281 */
3282 if (VIsual_active && !equalpos(start_pos, VIsual))
3283 {
3284extend:
3285 if (lt(start_pos, VIsual))
3286 {
3287 /*
3288 * Cursor at start of Visual area.
3289 * Find out where we are:
3290 * - in the white space before a sentence
3291 * - in a sentence or just after it
3292 * - at the start of a sentence
3293 */
3294 at_start_sent = TRUE;
3295 decl(&pos);
3296 while (lt(pos, curwin->w_cursor))
3297 {
3298 c = gchar_pos(&pos);
3299 if (!vim_iswhite(c))
3300 {
3301 at_start_sent = FALSE;
3302 break;
3303 }
3304 incl(&pos);
3305 }
3306 if (!at_start_sent)
3307 {
3308 findsent(BACKWARD, 1L);
3309 if (equalpos(curwin->w_cursor, start_pos))
3310 at_start_sent = TRUE; /* exactly at start of sentence */
3311 else
3312 /* inside a sentence, go to its end (start of next) */
3313 findsent(FORWARD, 1L);
3314 }
3315 if (include) /* "as" gets twice as much as "is" */
3316 count *= 2;
3317 while (count--)
3318 {
3319 if (at_start_sent)
3320 find_first_blank(&curwin->w_cursor);
3321 c = gchar_cursor();
3322 if (!at_start_sent || (!include && !vim_iswhite(c)))
3323 findsent(BACKWARD, 1L);
3324 at_start_sent = !at_start_sent;
3325 }
3326 }
3327 else
3328 {
3329 /*
3330 * Cursor at end of Visual area.
3331 * Find out where we are:
3332 * - just before a sentence
3333 * - just before or in the white space before a sentence
3334 * - in a sentence
3335 */
3336 incl(&pos);
3337 at_start_sent = TRUE;
3338 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3339 {
3340 at_start_sent = FALSE;
3341 while (lt(pos, curwin->w_cursor))
3342 {
3343 c = gchar_pos(&pos);
3344 if (!vim_iswhite(c))
3345 {
3346 at_start_sent = TRUE;
3347 break;
3348 }
3349 incl(&pos);
3350 }
3351 if (at_start_sent) /* in the sentence */
3352 findsent(BACKWARD, 1L);
3353 else /* in/before white before a sentence */
3354 curwin->w_cursor = start_pos;
3355 }
3356
3357 if (include) /* "as" gets twice as much as "is" */
3358 count *= 2;
3359 findsent_forward(count, at_start_sent);
3360 if (*p_sel == 'e')
3361 ++curwin->w_cursor.col;
3362 }
3363 return OK;
3364 }
3365#endif
3366
3367 /*
3368 * If cursor started on blank, check if it is just before the start of the
3369 * next sentence.
3370 */
3371 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3372 incl(&pos);
3373 if (equalpos(pos, curwin->w_cursor))
3374 {
3375 start_blank = TRUE;
3376 find_first_blank(&start_pos); /* go back to first blank */
3377 }
3378 else
3379 {
3380 start_blank = FALSE;
3381 findsent(BACKWARD, 1L);
3382 start_pos = curwin->w_cursor;
3383 }
3384 if (include)
3385 ncount = count * 2;
3386 else
3387 {
3388 ncount = count;
3389 if (start_blank)
3390 --ncount;
3391 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003392 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003393 findsent_forward(ncount, TRUE);
3394 else
3395 decl(&curwin->w_cursor);
3396
3397 if (include)
3398 {
3399 /*
3400 * If the blank in front of the sentence is included, exclude the
3401 * blanks at the end of the sentence, go back to the first blank.
3402 * If there are no trailing blanks, try to include leading blanks.
3403 */
3404 if (start_blank)
3405 {
3406 find_first_blank(&curwin->w_cursor);
3407 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3408 if (vim_iswhite(c))
3409 decl(&curwin->w_cursor);
3410 }
3411 else if (c = gchar_cursor(), !vim_iswhite(c))
3412 find_first_blank(&start_pos);
3413 }
3414
3415#ifdef FEAT_VISUAL
3416 if (VIsual_active)
3417 {
3418 /* avoid getting stuck with "is" on a single space before a sent. */
3419 if (equalpos(start_pos, curwin->w_cursor))
3420 goto extend;
3421 if (*p_sel == 'e')
3422 ++curwin->w_cursor.col;
3423 VIsual = start_pos;
3424 VIsual_mode = 'v';
3425 redraw_curbuf_later(INVERTED); /* update the inversion */
3426 }
3427 else
3428#endif
3429 {
3430 /* include a newline after the sentence, if there is one */
3431 if (incl(&curwin->w_cursor) == -1)
3432 oap->inclusive = TRUE;
3433 else
3434 oap->inclusive = FALSE;
3435 oap->start = start_pos;
3436 oap->motion_type = MCHAR;
3437 }
3438 return OK;
3439}
3440
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003441/*
3442 * Find block under the cursor, cursor at end.
3443 * "what" and "other" are two matching parenthesis/paren/etc.
3444 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003445 int
3446current_block(oap, count, include, what, other)
3447 oparg_T *oap;
3448 long count;
3449 int include; /* TRUE == include white space */
3450 int what; /* '(', '{', etc. */
3451 int other; /* ')', '}', etc. */
3452{
3453 pos_T old_pos;
3454 pos_T *pos = NULL;
3455 pos_T start_pos;
3456 pos_T *end_pos;
3457 pos_T old_start, old_end;
3458 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003459 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003460
3461 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003462 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003463 old_start = old_end;
3464
3465 /*
3466 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3467 */
3468#ifdef FEAT_VISUAL
3469 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3470#endif
3471 {
3472 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003473 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 while (inindent(1))
3475 if (inc_cursor() != 0)
3476 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003477 if (gchar_cursor() == what)
3478 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003479 ++curwin->w_cursor.col;
3480 }
3481#ifdef FEAT_VISUAL
3482 else if (lt(VIsual, curwin->w_cursor))
3483 {
3484 old_start = VIsual;
3485 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3486 }
3487 else
3488 old_end = VIsual;
3489#endif
3490
3491 /*
3492 * Search backwards for unclosed '(', '{', etc..
3493 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003494 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003495 */
3496 save_cpo = p_cpo;
3497 p_cpo = (char_u *)"%";
3498 while (count-- > 0)
3499 {
3500 if ((pos = findmatch(NULL, what)) == NULL)
3501 break;
3502 curwin->w_cursor = *pos;
3503 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3504 }
3505 p_cpo = save_cpo;
3506
3507 /*
3508 * Search for matching ')', '}', etc.
3509 * Put this position in curwin->w_cursor.
3510 */
3511 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3512 {
3513 curwin->w_cursor = old_pos;
3514 return FAIL;
3515 }
3516 curwin->w_cursor = *end_pos;
3517
3518 /*
3519 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3520 * If the ending '}' is only preceded by indent, skip that indent.
3521 * But only if the resulting area is not smaller than what we started with.
3522 */
3523 while (!include)
3524 {
3525 incl(&start_pos);
3526 sol = (curwin->w_cursor.col == 0);
3527 decl(&curwin->w_cursor);
3528 if (what == '{')
3529 while (inindent(1))
3530 {
3531 sol = TRUE;
3532 if (decl(&curwin->w_cursor) != 0)
3533 break;
3534 }
3535#ifdef FEAT_VISUAL
3536 /*
3537 * In Visual mode, when the resulting area is not bigger than what we
3538 * started with, extend it to the next block, and then exclude again.
3539 */
3540 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3541 && VIsual_active)
3542 {
3543 curwin->w_cursor = old_start;
3544 decl(&curwin->w_cursor);
3545 if ((pos = findmatch(NULL, what)) == NULL)
3546 {
3547 curwin->w_cursor = old_pos;
3548 return FAIL;
3549 }
3550 start_pos = *pos;
3551 curwin->w_cursor = *pos;
3552 if ((end_pos = findmatch(NULL, other)) == NULL)
3553 {
3554 curwin->w_cursor = old_pos;
3555 return FAIL;
3556 }
3557 curwin->w_cursor = *end_pos;
3558 }
3559 else
3560#endif
3561 break;
3562 }
3563
3564#ifdef FEAT_VISUAL
3565 if (VIsual_active)
3566 {
3567 if (*p_sel == 'e')
3568 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003569 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003570 inc(&curwin->w_cursor); /* include the line break */
3571 VIsual = start_pos;
3572 VIsual_mode = 'v';
3573 redraw_curbuf_later(INVERTED); /* update the inversion */
3574 showmode();
3575 }
3576 else
3577#endif
3578 {
3579 oap->start = start_pos;
3580 oap->motion_type = MCHAR;
3581 if (sol)
3582 {
3583 incl(&curwin->w_cursor);
3584 oap->inclusive = FALSE;
3585 }
3586 else
3587 oap->inclusive = TRUE;
3588 }
3589
3590 return OK;
3591}
3592
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003593static int in_html_tag __ARGS((int));
3594
3595/*
3596 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3597 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3598 */
3599 static int
3600in_html_tag(end_tag)
3601 int end_tag;
3602{
3603 char_u *line = ml_get_curline();
3604 char_u *p;
3605 int c;
3606 int lc = NUL;
3607 pos_T pos;
3608
3609#ifdef FEAT_MBYTE
3610 if (enc_dbcs)
3611 {
3612 char_u *lp = NULL;
3613
3614 /* We search forward until the cursor, because searching backwards is
3615 * very slow for DBCS encodings. */
3616 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3617 if (*p == '>' || *p == '<')
3618 {
3619 lc = *p;
3620 lp = p;
3621 }
3622 if (*p != '<') /* check for '<' under cursor */
3623 {
3624 if (lc != '<')
3625 return FALSE;
3626 p = lp;
3627 }
3628 }
3629 else
3630#endif
3631 {
3632 for (p = line + curwin->w_cursor.col; p > line; )
3633 {
3634 if (*p == '<') /* find '<' under/before cursor */
3635 break;
3636 mb_ptr_back(line, p);
3637 if (*p == '>') /* find '>' before cursor */
3638 break;
3639 }
3640 if (*p != '<')
3641 return FALSE;
3642 }
3643
3644 pos.lnum = curwin->w_cursor.lnum;
3645 pos.col = p - line;
3646
3647 mb_ptr_adv(p);
3648 if (end_tag)
3649 /* check that there is a '/' after the '<' */
3650 return *p == '/';
3651
3652 /* check that there is no '/' after the '<' */
3653 if (*p == '/')
3654 return FALSE;
3655
3656 /* check that the matching '>' is not preceded by '/' */
3657 for (;;)
3658 {
3659 if (inc(&pos) < 0)
3660 return FALSE;
3661 c = *ml_get_pos(&pos);
3662 if (c == '>')
3663 break;
3664 lc = c;
3665 }
3666 return lc != '/';
3667}
3668
3669/*
3670 * Find tag block under the cursor, cursor at end.
3671 */
3672 int
3673current_tagblock(oap, count_arg, include)
3674 oparg_T *oap;
3675 long count_arg;
3676 int include; /* TRUE == include white space */
3677{
3678 long count = count_arg;
3679 long n;
3680 pos_T old_pos;
3681 pos_T start_pos;
3682 pos_T end_pos;
3683 pos_T old_start, old_end;
3684 char_u *spat, *epat;
3685 char_u *p;
3686 char_u *cp;
3687 int len;
3688 int r;
3689 int do_include = include;
3690 int save_p_ws = p_ws;
3691 int retval = FAIL;
3692
3693 p_ws = FALSE;
3694
3695 old_pos = curwin->w_cursor;
3696 old_end = curwin->w_cursor; /* remember where we started */
3697 old_start = old_end;
3698
3699 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003700 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003701 */
3702#ifdef FEAT_VISUAL
3703 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3704#endif
3705 {
3706 setpcmark();
3707
3708 /* ignore indent */
3709 while (inindent(1))
3710 if (inc_cursor() != 0)
3711 break;
3712
3713 if (in_html_tag(FALSE))
3714 {
3715 /* cursor on start tag, move to just after it */
3716 while (*ml_get_cursor() != '>')
3717 if (inc_cursor() < 0)
3718 break;
3719 }
3720 else if (in_html_tag(TRUE))
3721 {
3722 /* cursor on end tag, move to just before it */
3723 while (*ml_get_cursor() != '<')
3724 if (dec_cursor() < 0)
3725 break;
3726 dec_cursor();
3727 old_end = curwin->w_cursor;
3728 }
3729 }
3730#ifdef FEAT_VISUAL
3731 else if (lt(VIsual, curwin->w_cursor))
3732 {
3733 old_start = VIsual;
3734 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3735 }
3736 else
3737 old_end = VIsual;
3738#endif
3739
3740again:
3741 /*
3742 * Search backwards for unclosed "<aaa>".
3743 * Put this position in start_pos.
3744 */
3745 for (n = 0; n < count; ++n)
3746 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003747 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003748 (char_u *)"",
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003749 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0, NULL) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003750 {
3751 curwin->w_cursor = old_pos;
3752 goto theend;
3753 }
3754 }
3755 start_pos = curwin->w_cursor;
3756
3757 /*
3758 * Search for matching "</aaa>". First isolate the "aaa".
3759 */
3760 inc_cursor();
3761 p = ml_get_cursor();
3762 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3763 ;
3764 len = cp - p;
3765 if (len == 0)
3766 {
3767 curwin->w_cursor = old_pos;
3768 goto theend;
3769 }
3770 spat = alloc(len + 29);
3771 epat = alloc(len + 9);
3772 if (spat == NULL || epat == NULL)
3773 {
3774 vim_free(spat);
3775 vim_free(epat);
3776 curwin->w_cursor = old_pos;
3777 goto theend;
3778 }
3779 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3780 sprintf((char *)epat, "</%.*s>\\c", len, p);
3781
Bram Moolenaar1d2ba7f2006-02-14 22:29:30 +00003782 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"", 0, NULL);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003783
3784 vim_free(spat);
3785 vim_free(epat);
3786
3787 if (r < 1 || lt(curwin->w_cursor, old_end))
3788 {
3789 /* Can't find other end or it's before the previous end. Could be a
3790 * HTML tag that doesn't have a matching end. Search backwards for
3791 * another starting tag. */
3792 count = 1;
3793 curwin->w_cursor = start_pos;
3794 goto again;
3795 }
3796
3797 if (do_include || r < 1)
3798 {
3799 /* Include up to the '>'. */
3800 while (*ml_get_cursor() != '>')
3801 if (inc_cursor() < 0)
3802 break;
3803 }
3804 else
3805 {
3806 /* Exclude the '<' of the end tag. */
3807 if (*ml_get_cursor() == '<')
3808 dec_cursor();
3809 }
3810 end_pos = curwin->w_cursor;
3811
3812 if (!do_include)
3813 {
3814 /* Exclude the start tag. */
3815 curwin->w_cursor = start_pos;
3816 while (inc_cursor() >= 0)
3817 if (*ml_get_cursor() == '>' && lt(curwin->w_cursor, end_pos))
3818 {
3819 inc_cursor();
3820 start_pos = curwin->w_cursor;
3821 break;
3822 }
3823 curwin->w_cursor = end_pos;
3824
Bram Moolenaar45360022005-07-21 21:08:21 +00003825 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003826 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003827 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003828 {
3829 do_include = TRUE;
3830 curwin->w_cursor = old_start;
3831 count = count_arg;
3832 goto again;
3833 }
3834 }
3835
3836#ifdef FEAT_VISUAL
3837 if (VIsual_active)
3838 {
3839 if (*p_sel == 'e')
3840 ++curwin->w_cursor.col;
3841 VIsual = start_pos;
3842 VIsual_mode = 'v';
3843 redraw_curbuf_later(INVERTED); /* update the inversion */
3844 showmode();
3845 }
3846 else
3847#endif
3848 {
3849 oap->start = start_pos;
3850 oap->motion_type = MCHAR;
3851 oap->inclusive = TRUE;
3852 }
3853 retval = OK;
3854
3855theend:
3856 p_ws = save_p_ws;
3857 return retval;
3858}
3859
Bram Moolenaar071d4272004-06-13 20:20:40 +00003860 int
3861current_par(oap, count, include, type)
3862 oparg_T *oap;
3863 long count;
3864 int include; /* TRUE == include white space */
3865 int type; /* 'p' for paragraph, 'S' for section */
3866{
3867 linenr_T start_lnum;
3868 linenr_T end_lnum;
3869 int white_in_front;
3870 int dir;
3871 int start_is_white;
3872 int prev_start_is_white;
3873 int retval = OK;
3874 int do_white = FALSE;
3875 int t;
3876 int i;
3877
3878 if (type == 'S') /* not implemented yet */
3879 return FAIL;
3880
3881 start_lnum = curwin->w_cursor.lnum;
3882
3883#ifdef FEAT_VISUAL
3884 /*
3885 * When visual area is more than one line: extend it.
3886 */
3887 if (VIsual_active && start_lnum != VIsual.lnum)
3888 {
3889extend:
3890 if (start_lnum < VIsual.lnum)
3891 dir = BACKWARD;
3892 else
3893 dir = FORWARD;
3894 for (i = count; --i >= 0; )
3895 {
3896 if (start_lnum ==
3897 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3898 {
3899 retval = FAIL;
3900 break;
3901 }
3902
3903 prev_start_is_white = -1;
3904 for (t = 0; t < 2; ++t)
3905 {
3906 start_lnum += dir;
3907 start_is_white = linewhite(start_lnum);
3908 if (prev_start_is_white == start_is_white)
3909 {
3910 start_lnum -= dir;
3911 break;
3912 }
3913 for (;;)
3914 {
3915 if (start_lnum == (dir == BACKWARD
3916 ? 1 : curbuf->b_ml.ml_line_count))
3917 break;
3918 if (start_is_white != linewhite(start_lnum + dir)
3919 || (!start_is_white
3920 && startPS(start_lnum + (dir > 0
3921 ? 1 : 0), 0, 0)))
3922 break;
3923 start_lnum += dir;
3924 }
3925 if (!include)
3926 break;
3927 if (start_lnum == (dir == BACKWARD
3928 ? 1 : curbuf->b_ml.ml_line_count))
3929 break;
3930 prev_start_is_white = start_is_white;
3931 }
3932 }
3933 curwin->w_cursor.lnum = start_lnum;
3934 curwin->w_cursor.col = 0;
3935 return retval;
3936 }
3937#endif
3938
3939 /*
3940 * First move back to the start_lnum of the paragraph or white lines
3941 */
3942 white_in_front = linewhite(start_lnum);
3943 while (start_lnum > 1)
3944 {
3945 if (white_in_front) /* stop at first white line */
3946 {
3947 if (!linewhite(start_lnum - 1))
3948 break;
3949 }
3950 else /* stop at first non-white line of start of paragraph */
3951 {
3952 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
3953 break;
3954 }
3955 --start_lnum;
3956 }
3957
3958 /*
3959 * Move past the end of any white lines.
3960 */
3961 end_lnum = start_lnum;
3962 while (linewhite(end_lnum) && end_lnum < curbuf->b_ml.ml_line_count)
3963 ++end_lnum;
3964
3965 --end_lnum;
3966 i = count;
3967 if (!include && white_in_front)
3968 --i;
3969 while (i--)
3970 {
3971 if (end_lnum == curbuf->b_ml.ml_line_count)
3972 return FAIL;
3973
3974 if (!include)
3975 do_white = linewhite(end_lnum + 1);
3976
3977 if (include || !do_white)
3978 {
3979 ++end_lnum;
3980 /*
3981 * skip to end of paragraph
3982 */
3983 while (end_lnum < curbuf->b_ml.ml_line_count
3984 && !linewhite(end_lnum + 1)
3985 && !startPS(end_lnum + 1, 0, 0))
3986 ++end_lnum;
3987 }
3988
3989 if (i == 0 && white_in_front && include)
3990 break;
3991
3992 /*
3993 * skip to end of white lines after paragraph
3994 */
3995 if (include || do_white)
3996 while (end_lnum < curbuf->b_ml.ml_line_count
3997 && linewhite(end_lnum + 1))
3998 ++end_lnum;
3999 }
4000
4001 /*
4002 * If there are no empty lines at the end, try to find some empty lines at
4003 * the start (unless that has been done already).
4004 */
4005 if (!white_in_front && !linewhite(end_lnum) && include)
4006 while (start_lnum > 1 && linewhite(start_lnum - 1))
4007 --start_lnum;
4008
4009#ifdef FEAT_VISUAL
4010 if (VIsual_active)
4011 {
4012 /* Problem: when doing "Vipipip" nothing happens in a single white
4013 * line, we get stuck there. Trap this here. */
4014 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4015 goto extend;
4016 VIsual.lnum = start_lnum;
4017 VIsual_mode = 'V';
4018 redraw_curbuf_later(INVERTED); /* update the inversion */
4019 showmode();
4020 }
4021 else
4022#endif
4023 {
4024 oap->start.lnum = start_lnum;
4025 oap->start.col = 0;
4026 oap->motion_type = MLINE;
4027 }
4028 curwin->w_cursor.lnum = end_lnum;
4029 curwin->w_cursor.col = 0;
4030
4031 return OK;
4032}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004033
4034static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4035static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4036
4037/*
4038 * Search quote char from string line[col].
4039 * Quote character escaped by one of the characters in "escape" is not counted
4040 * as a quote.
4041 * Returns column number of "quotechar" or -1 when not found.
4042 */
4043 static int
4044find_next_quote(line, col, quotechar, escape)
4045 char_u *line;
4046 int col;
4047 int quotechar;
4048 char_u *escape; /* escape characters, can be NULL */
4049{
4050 int c;
4051
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004052 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004053 {
4054 c = line[col];
4055 if (c == NUL)
4056 return -1;
4057 else if (escape != NULL && vim_strchr(escape, c))
4058 ++col;
4059 else if (c == quotechar)
4060 break;
4061#ifdef FEAT_MBYTE
4062 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004063 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004064 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004065#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004066 ++col;
4067 }
4068 return col;
4069}
4070
4071/*
4072 * Search backwards in "line" from column "col_start" to find "quotechar".
4073 * Quote character escaped by one of the characters in "escape" is not counted
4074 * as a quote.
4075 * Return the found column or zero.
4076 */
4077 static int
4078find_prev_quote(line, col_start, quotechar, escape)
4079 char_u *line;
4080 int col_start;
4081 int quotechar;
4082 char_u *escape; /* escape characters, can be NULL */
4083{
4084 int n;
4085
4086 while (col_start > 0)
4087 {
4088 --col_start;
4089#ifdef FEAT_MBYTE
4090 col_start -= (*mb_head_off)(line, line + col_start);
4091#endif
4092 n = 0;
4093 if (escape != NULL)
4094 while (col_start - n > 0 && vim_strchr(escape,
4095 line[col_start - n - 1]) != NULL)
4096 ++n;
4097 if (n & 1)
4098 col_start -= n; /* uneven number of escape chars, skip it */
4099 else if (line[col_start] == quotechar)
4100 break;
4101 }
4102 return col_start;
4103}
4104
4105/*
4106 * Find quote under the cursor, cursor at end.
4107 * Returns TRUE if found, else FALSE.
4108 */
4109 int
4110current_quote(oap, count, include, quotechar)
4111 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004112 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004113 int include; /* TRUE == include quote char */
4114 int quotechar; /* Quote character */
4115{
4116 char_u *line = ml_get_curline();
4117 int col_end;
4118 int col_start = curwin->w_cursor.col;
4119 int inclusive = FALSE;
4120#ifdef FEAT_VISUAL
4121 int vis_empty = TRUE; /* Visual selection <= 1 char */
4122 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004123 int inside_quotes = FALSE; /* Looks like "i'" done before */
4124 int selected_quote = FALSE; /* Has quote inside selection */
4125 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004126
4127 /* Correct cursor when 'selection' is exclusive */
4128 if (VIsual_active)
4129 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004130 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004131 if (*p_sel == 'e' && vis_bef_curs)
4132 dec_cursor();
4133 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004134 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004135
4136 if (!vis_empty)
4137 {
4138 /* Check if the existing selection exactly spans the text inside
4139 * quotes. */
4140 if (vis_bef_curs)
4141 {
4142 inside_quotes = VIsual.col > 0
4143 && line[VIsual.col - 1] == quotechar
4144 && line[curwin->w_cursor.col] != NUL
4145 && line[curwin->w_cursor.col + 1] == quotechar;
4146 i = VIsual.col;
4147 col_end = curwin->w_cursor.col;
4148 }
4149 else
4150 {
4151 inside_quotes = curwin->w_cursor.col > 0
4152 && line[curwin->w_cursor.col - 1] == quotechar
4153 && line[VIsual.col] != NUL
4154 && line[VIsual.col + 1] == quotechar;
4155 i = curwin->w_cursor.col;
4156 col_end = VIsual.col;
4157 }
4158
4159 /* Find out if we have a quote in the selection. */
4160 while (i <= col_end)
4161 if (line[i++] == quotechar)
4162 {
4163 selected_quote = TRUE;
4164 break;
4165 }
4166 }
4167
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004168 if (!vis_empty && line[col_start] == quotechar)
4169 {
4170 /* Already selecting something and on a quote character. Find the
4171 * next quoted string. */
4172 if (vis_bef_curs)
4173 {
4174 /* Assume we are on a closing quote: move to after the next
4175 * opening quote. */
4176 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4177 if (col_start < 0)
4178 return FALSE;
4179 col_end = find_next_quote(line, col_start + 1, quotechar,
4180 curbuf->b_p_qe);
4181 if (col_end < 0)
4182 {
4183 /* We were on a starting quote perhaps? */
4184 col_end = col_start;
4185 col_start = curwin->w_cursor.col;
4186 }
4187 }
4188 else
4189 {
4190 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4191 if (line[col_end] != quotechar)
4192 return FALSE;
4193 col_start = find_prev_quote(line, col_end, quotechar,
4194 curbuf->b_p_qe);
4195 if (line[col_start] != quotechar)
4196 {
4197 /* We were on an ending quote perhaps? */
4198 col_start = col_end;
4199 col_end = curwin->w_cursor.col;
4200 }
4201 }
4202 }
4203 else
4204#endif
4205
4206 if (line[col_start] == quotechar
4207#ifdef FEAT_VISUAL
4208 || !vis_empty
4209#endif
4210 )
4211 {
4212 int first_col = col_start;
4213
4214#ifdef FEAT_VISUAL
4215 if (!vis_empty)
4216 {
4217 if (vis_bef_curs)
4218 first_col = find_next_quote(line, col_start, quotechar, NULL);
4219 else
4220 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4221 }
4222#endif
4223 /* The cursor is on a quote, we don't know if it's the opening or
4224 * closing quote. Search from the start of the line to find out.
4225 * Also do this when there is a Visual area, a' may leave the cursor
4226 * in between two strings. */
4227 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004228 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004229 {
4230 /* Find open quote character. */
4231 col_start = find_next_quote(line, col_start, quotechar, NULL);
4232 if (col_start < 0 || col_start > first_col)
4233 return FALSE;
4234 /* Find close quote character. */
4235 col_end = find_next_quote(line, col_start + 1, quotechar,
4236 curbuf->b_p_qe);
4237 if (col_end < 0)
4238 return FALSE;
4239 /* If is cursor between start and end quote character, it is
4240 * target text object. */
4241 if (col_start <= first_col && first_col <= col_end)
4242 break;
4243 col_start = col_end + 1;
4244 }
4245 }
4246 else
4247 {
4248 /* Search backward for a starting quote. */
4249 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4250 if (line[col_start] != quotechar)
4251 {
4252 /* No quote before the cursor, look after the cursor. */
4253 col_start = find_next_quote(line, col_start, quotechar, NULL);
4254 if (col_start < 0)
4255 return FALSE;
4256 }
4257
4258 /* Find close quote character. */
4259 col_end = find_next_quote(line, col_start + 1, quotechar,
4260 curbuf->b_p_qe);
4261 if (col_end < 0)
4262 return FALSE;
4263 }
4264
4265 /* When "include" is TRUE, include spaces after closing quote or before
4266 * the starting quote. */
4267 if (include)
4268 {
4269 if (vim_iswhite(line[col_end + 1]))
4270 while (vim_iswhite(line[col_end + 1]))
4271 ++col_end;
4272 else
4273 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4274 --col_start;
4275 }
4276
Bram Moolenaarab194812005-09-14 21:40:12 +00004277 /* Set start position. After vi" another i" must include the ".
4278 * For v2i" include the quotes. */
4279 if (!include && count < 2
4280#ifdef FEAT_VISUAL
4281 && (vis_empty || !inside_quotes)
4282#endif
4283 )
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004284 ++col_start;
4285 curwin->w_cursor.col = col_start;
4286#ifdef FEAT_VISUAL
4287 if (VIsual_active)
4288 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004289 /* Set the start of the Visual area when the Visual area was empty, we
4290 * were just inside quotes or the Visual area didn't start at a quote
4291 * and didn't include a quote.
4292 */
4293 if (vis_empty
4294 || (vis_bef_curs
4295 && !selected_quote
4296 && (inside_quotes
4297 || (line[VIsual.col] != quotechar
4298 && (VIsual.col == 0
4299 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004300 {
4301 VIsual = curwin->w_cursor;
4302 redraw_curbuf_later(INVERTED);
4303 }
4304 }
4305 else
4306#endif
4307 {
4308 oap->start = curwin->w_cursor;
4309 oap->motion_type = MCHAR;
4310 }
4311
4312 /* Set end position. */
4313 curwin->w_cursor.col = col_end;
Bram Moolenaarab194812005-09-14 21:40:12 +00004314 if ((include || count > 1
4315#ifdef FEAT_VISUAL
4316 /* After vi" another i" must include the ". */
4317 || (!vis_empty && inside_quotes)
4318#endif
4319 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004320 inclusive = TRUE;
4321#ifdef FEAT_VISUAL
4322 if (VIsual_active)
4323 {
4324 if (vis_empty || vis_bef_curs)
4325 {
4326 /* decrement cursor when 'selection' is not exclusive */
4327 if (*p_sel != 'e')
4328 dec_cursor();
4329 }
4330 else
4331 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004332 /* Cursor is at start of Visual area. Set the end of the Visual
4333 * area when it was just inside quotes or it didn't end at a
4334 * quote. */
4335 if (inside_quotes
4336 || (!selected_quote
4337 && line[VIsual.col] != quotechar
4338 && (line[VIsual.col] == NUL
4339 || line[VIsual.col + 1] != quotechar)))
4340 {
4341 dec_cursor();
4342 VIsual = curwin->w_cursor;
4343 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004344 curwin->w_cursor.col = col_start;
4345 }
4346 if (VIsual_mode == 'V')
4347 {
4348 VIsual_mode = 'v';
4349 redraw_cmdline = TRUE; /* show mode later */
4350 }
4351 }
4352 else
4353#endif
4354 {
4355 /* Set inclusive and other oap's flags. */
4356 oap->inclusive = inclusive;
4357 }
4358
4359 return OK;
4360}
4361
4362#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363
4364#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4365 || defined(PROTO)
4366/*
4367 * return TRUE if line 'lnum' is empty or has white chars only.
4368 */
4369 int
4370linewhite(lnum)
4371 linenr_T lnum;
4372{
4373 char_u *p;
4374
4375 p = skipwhite(ml_get(lnum));
4376 return (*p == NUL);
4377}
4378#endif
4379
4380#if defined(FEAT_FIND_ID) || defined(PROTO)
4381/*
4382 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004383 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004384 */
4385/*ARGSUSED*/
4386 void
4387find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4388 type, count, action, start_lnum, end_lnum)
4389 char_u *ptr; /* pointer to search pattern */
4390 int dir; /* direction of expansion */
4391 int len; /* length of search pattern */
4392 int whole; /* match whole words only */
4393 int skip_comments; /* don't match inside comments */
4394 int type; /* Type of search; are we looking for a type?
4395 a macro? */
4396 long count;
4397 int action; /* What to do when we find it */
4398 linenr_T start_lnum; /* first line to start searching */
4399 linenr_T end_lnum; /* last line for searching */
4400{
4401 SearchedFile *files; /* Stack of included files */
4402 SearchedFile *bigger; /* When we need more space */
4403 int max_path_depth = 50;
4404 long match_count = 1;
4405
4406 char_u *pat;
4407 char_u *new_fname;
4408 char_u *curr_fname = curbuf->b_fname;
4409 char_u *prev_fname = NULL;
4410 linenr_T lnum;
4411 int depth;
4412 int depth_displayed; /* For type==CHECK_PATH */
4413 int old_files;
4414 int already_searched;
4415 char_u *file_line;
4416 char_u *line;
4417 char_u *p;
4418 char_u save_char;
4419 int define_matched;
4420 regmatch_T regmatch;
4421 regmatch_T incl_regmatch;
4422 regmatch_T def_regmatch;
4423 int matched = FALSE;
4424 int did_show = FALSE;
4425 int found = FALSE;
4426 int i;
4427 char_u *already = NULL;
4428 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004429 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004430#ifdef RISCOS
4431 int previous_munging = __riscosify_control;
4432#endif
4433#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4434 win_T *curwin_save = NULL;
4435#endif
4436
4437 regmatch.regprog = NULL;
4438 incl_regmatch.regprog = NULL;
4439 def_regmatch.regprog = NULL;
4440
4441 file_line = alloc(LSIZE);
4442 if (file_line == NULL)
4443 return;
4444
4445#ifdef RISCOS
4446 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004447 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004448#endif
4449
4450 if (type != CHECK_PATH && type != FIND_DEFINE
4451#ifdef FEAT_INS_EXPAND
4452 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4453 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004454 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004455#endif
4456 )
4457 {
4458 pat = alloc(len + 5);
4459 if (pat == NULL)
4460 goto fpip_end;
4461 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4462 /* ignore case according to p_ic, p_scs and pat */
4463 regmatch.rm_ic = ignorecase(pat);
4464 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4465 vim_free(pat);
4466 if (regmatch.regprog == NULL)
4467 goto fpip_end;
4468 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004469 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4470 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004471 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004472 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004473 if (incl_regmatch.regprog == NULL)
4474 goto fpip_end;
4475 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4476 }
4477 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4478 {
4479 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4480 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4481 if (def_regmatch.regprog == NULL)
4482 goto fpip_end;
4483 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4484 }
4485 files = (SearchedFile *)lalloc_clear((long_u)
4486 (max_path_depth * sizeof(SearchedFile)), TRUE);
4487 if (files == NULL)
4488 goto fpip_end;
4489 old_files = max_path_depth;
4490 depth = depth_displayed = -1;
4491
4492 lnum = start_lnum;
4493 if (end_lnum > curbuf->b_ml.ml_line_count)
4494 end_lnum = curbuf->b_ml.ml_line_count;
4495 if (lnum > end_lnum) /* do at least one line */
4496 lnum = end_lnum;
4497 line = ml_get(lnum);
4498
4499 for (;;)
4500 {
4501 if (incl_regmatch.regprog != NULL
4502 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4503 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004504 char_u *p_fname = (curr_fname == curbuf->b_fname)
4505 ? curbuf->b_ffname : curr_fname;
4506
4507 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4508 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4509 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
4510 incl_regmatch.endp[0] - incl_regmatch.startp[0],
4511 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4512 else
4513 /* Use text after match with 'include'. */
4514 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004515 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004516 already_searched = FALSE;
4517 if (new_fname != NULL)
4518 {
4519 /* Check whether we have already searched in this file */
4520 for (i = 0;; i++)
4521 {
4522 if (i == depth + 1)
4523 i = old_files;
4524 if (i == max_path_depth)
4525 break;
4526 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4527 {
4528 if (type != CHECK_PATH &&
4529 action == ACTION_SHOW_ALL && files[i].matched)
4530 {
4531 msg_putchar('\n'); /* cursor below last one */
4532 if (!got_int) /* don't display if 'q'
4533 typed at "--more--"
4534 mesage */
4535 {
4536 msg_home_replace_hl(new_fname);
4537 MSG_PUTS(_(" (includes previously listed match)"));
4538 prev_fname = NULL;
4539 }
4540 }
4541 vim_free(new_fname);
4542 new_fname = NULL;
4543 already_searched = TRUE;
4544 break;
4545 }
4546 }
4547 }
4548
4549 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4550 || (new_fname == NULL && !already_searched)))
4551 {
4552 if (did_show)
4553 msg_putchar('\n'); /* cursor below last one */
4554 else
4555 {
4556 gotocmdline(TRUE); /* cursor at status line */
4557 MSG_PUTS_TITLE(_("--- Included files "));
4558 if (action != ACTION_SHOW_ALL)
4559 MSG_PUTS_TITLE(_("not found "));
4560 MSG_PUTS_TITLE(_("in path ---\n"));
4561 }
4562 did_show = TRUE;
4563 while (depth_displayed < depth && !got_int)
4564 {
4565 ++depth_displayed;
4566 for (i = 0; i < depth_displayed; i++)
4567 MSG_PUTS(" ");
4568 msg_home_replace(files[depth_displayed].name);
4569 MSG_PUTS(" -->\n");
4570 }
4571 if (!got_int) /* don't display if 'q' typed
4572 for "--more--" message */
4573 {
4574 for (i = 0; i <= depth_displayed; i++)
4575 MSG_PUTS(" ");
4576 if (new_fname != NULL)
4577 {
4578 /* using "new_fname" is more reliable, e.g., when
4579 * 'includeexpr' is set. */
4580 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4581 }
4582 else
4583 {
4584 /*
4585 * Isolate the file name.
4586 * Include the surrounding "" or <> if present.
4587 */
4588 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4589 ;
4590 for (i = 0; vim_isfilec(p[i]); i++)
4591 ;
4592 if (i == 0)
4593 {
4594 /* Nothing found, use the rest of the line. */
4595 p = incl_regmatch.endp[0];
4596 i = STRLEN(p);
4597 }
4598 else
4599 {
4600 if (p[-1] == '"' || p[-1] == '<')
4601 {
4602 --p;
4603 ++i;
4604 }
4605 if (p[i] == '"' || p[i] == '>')
4606 ++i;
4607 }
4608 save_char = p[i];
4609 p[i] = NUL;
4610 msg_outtrans_attr(p, hl_attr(HLF_D));
4611 p[i] = save_char;
4612 }
4613
4614 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4615 {
4616 if (already_searched)
4617 MSG_PUTS(_(" (Already listed)"));
4618 else
4619 MSG_PUTS(_(" NOT FOUND"));
4620 }
4621 }
4622 out_flush(); /* output each line directly */
4623 }
4624
4625 if (new_fname != NULL)
4626 {
4627 /* Push the new file onto the file stack */
4628 if (depth + 1 == old_files)
4629 {
4630 bigger = (SearchedFile *)lalloc((long_u)(
4631 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4632 if (bigger != NULL)
4633 {
4634 for (i = 0; i <= depth; i++)
4635 bigger[i] = files[i];
4636 for (i = depth + 1; i < old_files + max_path_depth; i++)
4637 {
4638 bigger[i].fp = NULL;
4639 bigger[i].name = NULL;
4640 bigger[i].lnum = 0;
4641 bigger[i].matched = FALSE;
4642 }
4643 for (i = old_files; i < max_path_depth; i++)
4644 bigger[i + max_path_depth] = files[i];
4645 old_files += max_path_depth;
4646 max_path_depth *= 2;
4647 vim_free(files);
4648 files = bigger;
4649 }
4650 }
4651 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4652 == NULL)
4653 vim_free(new_fname);
4654 else
4655 {
4656 if (++depth == old_files)
4657 {
4658 /*
4659 * lalloc() for 'bigger' must have failed above. We
4660 * will forget one of our already visited files now.
4661 */
4662 vim_free(files[old_files].name);
4663 ++old_files;
4664 }
4665 files[depth].name = curr_fname = new_fname;
4666 files[depth].lnum = 0;
4667 files[depth].matched = FALSE;
4668#ifdef FEAT_INS_EXPAND
4669 if (action == ACTION_EXPAND)
4670 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004671 vim_snprintf((char*)IObuff, IOSIZE,
4672 _("Scanning included file: %s"),
4673 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004674 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4675 }
4676#endif
4677 }
4678 }
4679 }
4680 else
4681 {
4682 /*
4683 * Check if the line is a define (type == FIND_DEFINE)
4684 */
4685 p = line;
4686search_line:
4687 define_matched = FALSE;
4688 if (def_regmatch.regprog != NULL
4689 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4690 {
4691 /*
4692 * Pattern must be first identifier after 'define', so skip
4693 * to that position before checking for match of pattern. Also
4694 * don't let it match beyond the end of this identifier.
4695 */
4696 p = def_regmatch.endp[0];
4697 while (*p && !vim_iswordc(*p))
4698 p++;
4699 define_matched = TRUE;
4700 }
4701
4702 /*
4703 * Look for a match. Don't do this if we are looking for a
4704 * define and this line didn't match define_prog above.
4705 */
4706 if (def_regmatch.regprog == NULL || define_matched)
4707 {
4708 if (define_matched
4709#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004710 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004711#endif
4712 )
4713 {
4714 /* compare the first "len" chars from "ptr" */
4715 startp = skipwhite(p);
4716 if (p_ic)
4717 matched = !MB_STRNICMP(startp, ptr, len);
4718 else
4719 matched = !STRNCMP(startp, ptr, len);
4720 if (matched && define_matched && whole
4721 && vim_iswordc(startp[len]))
4722 matched = FALSE;
4723 }
4724 else if (regmatch.regprog != NULL
4725 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4726 {
4727 matched = TRUE;
4728 startp = regmatch.startp[0];
4729 /*
4730 * Check if the line is not a comment line (unless we are
4731 * looking for a define). A line starting with "# define"
4732 * is not considered to be a comment line.
4733 */
4734 if (!define_matched && skip_comments)
4735 {
4736#ifdef FEAT_COMMENTS
4737 if ((*line != '#' ||
4738 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4739 && get_leader_len(line, NULL, FALSE))
4740 matched = FALSE;
4741
4742 /*
4743 * Also check for a "/ *" or "/ /" before the match.
4744 * Skips lines like "int backwards; / * normal index
4745 * * /" when looking for "normal".
4746 * Note: Doesn't skip "/ *" in comments.
4747 */
4748 p = skipwhite(line);
4749 if (matched
4750 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4751#endif
4752 for (p = line; *p && p < startp; ++p)
4753 {
4754 if (matched
4755 && p[0] == '/'
4756 && (p[1] == '*' || p[1] == '/'))
4757 {
4758 matched = FALSE;
4759 /* After "//" all text is comment */
4760 if (p[1] == '/')
4761 break;
4762 ++p;
4763 }
4764 else if (!matched && p[0] == '*' && p[1] == '/')
4765 {
4766 /* Can find match after "* /". */
4767 matched = TRUE;
4768 ++p;
4769 }
4770 }
4771 }
4772 }
4773 }
4774 }
4775 if (matched)
4776 {
4777#ifdef FEAT_INS_EXPAND
4778 if (action == ACTION_EXPAND)
4779 {
4780 int reuse = 0;
4781 int add_r;
4782 char_u *aux;
4783
4784 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4785 break;
4786 found = TRUE;
4787 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004788 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004789 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004790 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004791 if (vim_iswordp(p))
4792 goto exit_matched;
4793 p = find_word_start(p);
4794 }
4795 p = find_word_end(p);
4796 i = (int)(p - aux);
4797
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
4800 /* get the next line */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004801 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004802 STRNCPY(IObuff, aux, i);
4803 if (!( depth < 0
4804 && lnum < end_lnum
4805 && (line = ml_get(++lnum)) != NULL)
4806 && !( depth >= 0
4807 && !vim_fgets(line = file_line,
4808 LSIZE, files[depth].fp)))
4809 goto exit_matched;
4810
4811 /* we read a line, set "already" to check this "line" later
4812 * if depth >= 0 we'll increase files[depth].lnum far
4813 * bellow -- Acevedo */
4814 already = aux = p = skipwhite(line);
4815 p = find_word_start(p);
4816 p = find_word_end(p);
4817 if (p > aux)
4818 {
4819 if (*aux != ')' && IObuff[i-1] != TAB)
4820 {
4821 if (IObuff[i-1] != ' ')
4822 IObuff[i++] = ' ';
4823 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4824 if (p_js
4825 && (IObuff[i-2] == '.'
4826 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4827 && (IObuff[i-2] == '?'
4828 || IObuff[i-2] == '!'))))
4829 IObuff[i++] = ' ';
4830 }
4831 /* copy as much as posible of the new word */
4832 if (p - aux >= IOSIZE - i)
4833 p = aux + IOSIZE - i - 1;
4834 STRNCPY(IObuff + i, aux, p - aux);
4835 i += (int)(p - aux);
4836 reuse |= CONT_S_IPOS;
4837 }
4838 IObuff[i] = NUL;
4839 aux = IObuff;
4840
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004841 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004842 goto exit_matched;
4843 }
4844
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004845 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004846 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4847 dir, reuse);
4848 if (add_r == OK)
4849 /* if dir was BACKWARD then honor it just once */
4850 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004851 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 break;
4853 }
4854 else
4855#endif
4856 if (action == ACTION_SHOW_ALL)
4857 {
4858 found = TRUE;
4859 if (!did_show)
4860 gotocmdline(TRUE); /* cursor at status line */
4861 if (curr_fname != prev_fname)
4862 {
4863 if (did_show)
4864 msg_putchar('\n'); /* cursor below last one */
4865 if (!got_int) /* don't display if 'q' typed
4866 at "--more--" mesage */
4867 msg_home_replace_hl(curr_fname);
4868 prev_fname = curr_fname;
4869 }
4870 did_show = TRUE;
4871 if (!got_int)
4872 show_pat_in_path(line, type, TRUE, action,
4873 (depth == -1) ? NULL : files[depth].fp,
4874 (depth == -1) ? &lnum : &files[depth].lnum,
4875 match_count++);
4876
4877 /* Set matched flag for this file and all the ones that
4878 * include it */
4879 for (i = 0; i <= depth; ++i)
4880 files[i].matched = TRUE;
4881 }
4882 else if (--count <= 0)
4883 {
4884 found = TRUE;
4885 if (depth == -1 && lnum == curwin->w_cursor.lnum
4886#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4887 && g_do_tagpreview == 0
4888#endif
4889 )
4890 EMSG(_("E387: Match is on current line"));
4891 else if (action == ACTION_SHOW)
4892 {
4893 show_pat_in_path(line, type, did_show, action,
4894 (depth == -1) ? NULL : files[depth].fp,
4895 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4896 did_show = TRUE;
4897 }
4898 else
4899 {
4900#ifdef FEAT_GUI
4901 need_mouse_correct = TRUE;
4902#endif
4903#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4904 /* ":psearch" uses the preview window */
4905 if (g_do_tagpreview != 0)
4906 {
4907 curwin_save = curwin;
4908 prepare_tagpreview();
4909 }
4910#endif
4911 if (action == ACTION_SPLIT)
4912 {
4913#ifdef FEAT_WINDOWS
4914 if (win_split(0, 0) == FAIL)
4915#endif
4916 break;
4917#ifdef FEAT_SCROLLBIND
4918 curwin->w_p_scb = FALSE;
4919#endif
4920 }
4921 if (depth == -1)
4922 {
4923 /* match in current file */
4924#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4925 if (g_do_tagpreview != 0)
4926 {
4927 if (getfile(0, curwin_save->w_buffer->b_fname,
4928 NULL, TRUE, lnum, FALSE) > 0)
4929 break; /* failed to jump to file */
4930 }
4931 else
4932#endif
4933 setpcmark();
4934 curwin->w_cursor.lnum = lnum;
4935 }
4936 else
4937 {
4938 if (getfile(0, files[depth].name, NULL, TRUE,
4939 files[depth].lnum, FALSE) > 0)
4940 break; /* failed to jump to file */
4941 /* autocommands may have changed the lnum, we don't
4942 * want that here */
4943 curwin->w_cursor.lnum = files[depth].lnum;
4944 }
4945 }
4946 if (action != ACTION_SHOW)
4947 {
4948 curwin->w_cursor.col = (colnr_T) (startp - line);
4949 curwin->w_set_curswant = TRUE;
4950 }
4951
4952#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4953 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004954 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004955 {
4956 /* Return cursor to where we were */
4957 validate_cursor();
4958 redraw_later(VALID);
4959 win_enter(curwin_save, TRUE);
4960 }
4961#endif
4962 break;
4963 }
4964#ifdef FEAT_INS_EXPAND
4965exit_matched:
4966#endif
4967 matched = FALSE;
4968 /* look for other matches in the rest of the line if we
4969 * are not at the end of it already */
4970 if (def_regmatch.regprog == NULL
4971#ifdef FEAT_INS_EXPAND
4972 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004973 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004974#endif
4975 && *(p = startp + 1))
4976 goto search_line;
4977 }
4978 line_breakcheck();
4979#ifdef FEAT_INS_EXPAND
4980 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00004981 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004982 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004983#else
4984 if (got_int)
4985#endif
4986 break;
4987
4988 /*
4989 * Read the next line. When reading an included file and encountering
4990 * end-of-file, close the file and continue in the file that included
4991 * it.
4992 */
4993 while (depth >= 0 && !already
4994 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4995 {
4996 fclose(files[depth].fp);
4997 --old_files;
4998 files[old_files].name = files[depth].name;
4999 files[old_files].matched = files[depth].matched;
5000 --depth;
5001 curr_fname = (depth == -1) ? curbuf->b_fname
5002 : files[depth].name;
5003 if (depth < depth_displayed)
5004 depth_displayed = depth;
5005 }
5006 if (depth >= 0) /* we could read the line */
5007 files[depth].lnum++;
5008 else if (!already)
5009 {
5010 if (++lnum > end_lnum)
5011 break;
5012 line = ml_get(lnum);
5013 }
5014 already = NULL;
5015 }
5016 /* End of big for (;;) loop. */
5017
5018 /* Close any files that are still open. */
5019 for (i = 0; i <= depth; i++)
5020 {
5021 fclose(files[i].fp);
5022 vim_free(files[i].name);
5023 }
5024 for (i = old_files; i < max_path_depth; i++)
5025 vim_free(files[i].name);
5026 vim_free(files);
5027
5028 if (type == CHECK_PATH)
5029 {
5030 if (!did_show)
5031 {
5032 if (action != ACTION_SHOW_ALL)
5033 MSG(_("All included files were found"));
5034 else
5035 MSG(_("No included files"));
5036 }
5037 }
5038 else if (!found
5039#ifdef FEAT_INS_EXPAND
5040 && action != ACTION_EXPAND
5041#endif
5042 )
5043 {
5044#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005045 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005046#else
5047 if (got_int)
5048#endif
5049 EMSG(_(e_interr));
5050 else if (type == FIND_DEFINE)
5051 EMSG(_("E388: Couldn't find definition"));
5052 else
5053 EMSG(_("E389: Couldn't find pattern"));
5054 }
5055 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5056 msg_end();
5057
5058fpip_end:
5059 vim_free(file_line);
5060 vim_free(regmatch.regprog);
5061 vim_free(incl_regmatch.regprog);
5062 vim_free(def_regmatch.regprog);
5063
5064#ifdef RISCOS
5065 /* Restore previous file munging state. */
5066 __riscosify_control = previous_munging;
5067#endif
5068}
5069
5070 static void
5071show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5072 char_u *line;
5073 int type;
5074 int did_show;
5075 int action;
5076 FILE *fp;
5077 linenr_T *lnum;
5078 long count;
5079{
5080 char_u *p;
5081
5082 if (did_show)
5083 msg_putchar('\n'); /* cursor below last one */
5084 else
5085 gotocmdline(TRUE); /* cursor at status line */
5086 if (got_int) /* 'q' typed at "--more--" message */
5087 return;
5088 for (;;)
5089 {
5090 p = line + STRLEN(line) - 1;
5091 if (fp != NULL)
5092 {
5093 /* We used fgets(), so get rid of newline at end */
5094 if (p >= line && *p == '\n')
5095 --p;
5096 if (p >= line && *p == '\r')
5097 --p;
5098 *(p + 1) = NUL;
5099 }
5100 if (action == ACTION_SHOW_ALL)
5101 {
5102 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5103 msg_puts(IObuff);
5104 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5105 /* Highlight line numbers */
5106 msg_puts_attr(IObuff, hl_attr(HLF_N));
5107 MSG_PUTS(" ");
5108 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005109 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005110 out_flush(); /* show one line at a time */
5111
5112 /* Definition continues until line that doesn't end with '\' */
5113 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5114 break;
5115
5116 if (fp != NULL)
5117 {
5118 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5119 break;
5120 ++*lnum;
5121 }
5122 else
5123 {
5124 if (++*lnum > curbuf->b_ml.ml_line_count)
5125 break;
5126 line = ml_get(*lnum);
5127 }
5128 msg_putchar('\n');
5129 }
5130}
5131#endif
5132
5133#ifdef FEAT_VIMINFO
5134 int
5135read_viminfo_search_pattern(virp, force)
5136 vir_T *virp;
5137 int force;
5138{
5139 char_u *lp;
5140 int idx = -1;
5141 int magic = FALSE;
5142 int no_scs = FALSE;
5143 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005144 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005145 long off = 0;
5146 int setlast = FALSE;
5147#ifdef FEAT_SEARCH_EXTRA
5148 static int hlsearch_on = FALSE;
5149#endif
5150 char_u *val;
5151
5152 /*
5153 * Old line types:
5154 * "/pat", "&pat": search/subst. pat
5155 * "~/pat", "~&pat": last used search/subst. pat
5156 * New line types:
5157 * "~h", "~H": hlsearch highlighting off/on
5158 * "~<magic><smartcase><line><end><off><last><which>pat"
5159 * <magic>: 'm' off, 'M' on
5160 * <smartcase>: 's' off, 'S' on
5161 * <line>: 'L' line offset, 'l' char offset
5162 * <end>: 'E' from end, 'e' from start
5163 * <off>: decimal, offset
5164 * <last>: '~' last used pattern
5165 * <which>: '/' search pat, '&' subst. pat
5166 */
5167 lp = virp->vir_line;
5168 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5169 {
5170 if (lp[1] == 'M') /* magic on */
5171 magic = TRUE;
5172 if (lp[2] == 's')
5173 no_scs = TRUE;
5174 if (lp[3] == 'L')
5175 off_line = TRUE;
5176 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005177 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005178 lp += 5;
5179 off = getdigits(&lp);
5180 }
5181 if (lp[0] == '~') /* use this pattern for last-used pattern */
5182 {
5183 setlast = TRUE;
5184 lp++;
5185 }
5186 if (lp[0] == '/')
5187 idx = RE_SEARCH;
5188 else if (lp[0] == '&')
5189 idx = RE_SUBST;
5190#ifdef FEAT_SEARCH_EXTRA
5191 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5192 hlsearch_on = FALSE;
5193 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5194 hlsearch_on = TRUE;
5195#endif
5196 if (idx >= 0)
5197 {
5198 if (force || spats[idx].pat == NULL)
5199 {
5200 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5201 TRUE);
5202 if (val != NULL)
5203 {
5204 set_last_search_pat(val, idx, magic, setlast);
5205 vim_free(val);
5206 spats[idx].no_scs = no_scs;
5207 spats[idx].off.line = off_line;
5208 spats[idx].off.end = off_end;
5209 spats[idx].off.off = off;
5210#ifdef FEAT_SEARCH_EXTRA
5211 if (setlast)
5212 no_hlsearch = !hlsearch_on;
5213#endif
5214 }
5215 }
5216 }
5217 return viminfo_readline(virp);
5218}
5219
5220 void
5221write_viminfo_search_pattern(fp)
5222 FILE *fp;
5223{
5224 if (get_viminfo_parameter('/') != 0)
5225 {
5226#ifdef FEAT_SEARCH_EXTRA
5227 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5228 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5229#endif
5230 wvsp_one(fp, RE_SEARCH, "", '/');
5231 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5232 }
5233}
5234
5235 static void
5236wvsp_one(fp, idx, s, sc)
5237 FILE *fp; /* file to write to */
5238 int idx; /* spats[] index */
5239 char *s; /* search pat */
5240 int sc; /* dir char */
5241{
5242 if (spats[idx].pat != NULL)
5243 {
5244 fprintf(fp, "\n# Last %sSearch Pattern:\n~", s);
5245 /* off.dir is not stored, it's reset to forward */
5246 fprintf(fp, "%c%c%c%c%ld%s%c",
5247 spats[idx].magic ? 'M' : 'm', /* magic */
5248 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5249 spats[idx].off.line ? 'L' : 'l', /* line offset */
5250 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5251 spats[idx].off.off, /* offset */
5252 last_idx == idx ? "~" : "", /* last used pat */
5253 sc);
5254 viminfo_writestring(fp, spats[idx].pat);
5255 }
5256}
5257#endif /* FEAT_VIMINFO */