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