blob: 456b694268a83dc2c5b063e1eb3e6c94f0e12b0f [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
1443 p = ml_get_curline();
1444 col = curwin->w_cursor.col;
1445 len = (int)STRLEN(p);
1446
1447 while (count--)
1448 {
1449#ifdef FEAT_MBYTE
1450 if (has_mbyte)
1451 {
1452 for (;;)
1453 {
1454 if (dir > 0)
1455 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001456 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001457 if (col >= len)
1458 return FAIL;
1459 }
1460 else
1461 {
1462 if (col == 0)
1463 return FAIL;
1464 col -= (*mb_head_off)(p, p + col - 1) + 1;
1465 }
1466 if (bytelen == 1)
1467 {
1468 if (p[col] == c)
1469 break;
1470 }
1471 else
1472 {
1473 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1474 break;
1475 }
1476 }
1477 }
1478 else
1479#endif
1480 {
1481 for (;;)
1482 {
1483 if ((col += dir) < 0 || col >= len)
1484 return FAIL;
1485 if (p[col] == c)
1486 break;
1487 }
1488 }
1489 }
1490
1491 if (t_cmd)
1492 {
1493 /* backup to before the character (possibly double-byte) */
1494 col -= dir;
1495#ifdef FEAT_MBYTE
1496 if (has_mbyte)
1497 {
1498 if (dir < 0)
1499 /* Landed on the search char which is bytelen long */
1500 col += bytelen - 1;
1501 else
1502 /* To previous char, which may be multi-byte. */
1503 col -= (*mb_head_off)(p, p + col);
1504 }
1505#endif
1506 }
1507 curwin->w_cursor.col = col;
1508
1509 return OK;
1510}
1511
1512/*
1513 * "Other" Searches
1514 */
1515
1516/*
1517 * findmatch - find the matching paren or brace
1518 *
1519 * Improvement over vi: Braces inside quotes are ignored.
1520 */
1521 pos_T *
1522findmatch(oap, initc)
1523 oparg_T *oap;
1524 int initc;
1525{
1526 return findmatchlimit(oap, initc, 0, 0);
1527}
1528
1529/*
1530 * Return TRUE if the character before "linep[col]" equals "ch".
1531 * Return FALSE if "col" is zero.
1532 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1533 * is NULL.
1534 * Handles multibyte string correctly.
1535 */
1536 static int
1537check_prevcol(linep, col, ch, prevcol)
1538 char_u *linep;
1539 int col;
1540 int ch;
1541 int *prevcol;
1542{
1543 --col;
1544#ifdef FEAT_MBYTE
1545 if (col > 0 && has_mbyte)
1546 col -= (*mb_head_off)(linep, linep + col);
1547#endif
1548 if (prevcol)
1549 *prevcol = col;
1550 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1551}
1552
1553/*
1554 * findmatchlimit -- find the matching paren or brace, if it exists within
1555 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1556 * the edge of the file.
1557 *
1558 * "initc" is the character to find a match for. NUL means to find the
1559 * character at or after the cursor.
1560 *
1561 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1562 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1563 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1564 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001565 *
1566 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1567 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001568 */
1569
1570 pos_T *
1571findmatchlimit(oap, initc, flags, maxtravel)
1572 oparg_T *oap;
1573 int initc;
1574 int flags;
1575 int maxtravel;
1576{
1577 static pos_T pos; /* current search position */
1578 int findc = 0; /* matching brace */
1579 int c;
1580 int count = 0; /* cumulative number of braces */
1581 int backwards = FALSE; /* init for gcc */
1582 int inquote = FALSE; /* TRUE when inside quotes */
1583 char_u *linep; /* pointer to current line */
1584 char_u *ptr;
1585 int do_quotes; /* check for quotes in current line */
1586 int at_start; /* do_quotes value at start position */
1587 int hash_dir = 0; /* Direction searched for # things */
1588 int comment_dir = 0; /* Direction searched for comments */
1589 pos_T match_pos; /* Where last slash-star was found */
1590 int start_in_quotes; /* start position is in quotes */
1591 int traveled = 0; /* how far we've searched so far */
1592 int ignore_cend = FALSE; /* ignore comment end */
1593 int cpo_match; /* vi compatible matching */
1594 int cpo_bsl; /* don't recognize backslashes */
1595 int match_escaped = 0; /* search for escaped match */
1596 int dir; /* Direction to search */
1597 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001598#ifdef FEAT_LISP
1599 int lispcomm = FALSE; /* inside of Lisp-style comment */
1600 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1601#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001602
1603 pos = curwin->w_cursor;
1604 linep = ml_get(pos.lnum);
1605
1606 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1607 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1608
1609 /* Direction to search when initc is '/', '*' or '#' */
1610 if (flags & FM_BACKWARD)
1611 dir = BACKWARD;
1612 else if (flags & FM_FORWARD)
1613 dir = FORWARD;
1614 else
1615 dir = 0;
1616
1617 /*
1618 * if initc given, look in the table for the matching character
1619 * '/' and '*' are special cases: look for start or end of comment.
1620 * When '/' is used, we ignore running backwards into an star-slash, for
1621 * "[*" command, we just want to find any comment.
1622 */
1623 if (initc == '/' || initc == '*')
1624 {
1625 comment_dir = dir;
1626 if (initc == '/')
1627 ignore_cend = TRUE;
1628 backwards = (dir == FORWARD) ? FALSE : TRUE;
1629 initc = NUL;
1630 }
1631 else if (initc != '#' && initc != NUL)
1632 {
1633 /* 'matchpairs' is "x:y,x:y" */
1634 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1635 {
1636 if (*ptr == initc)
1637 {
1638 findc = initc;
1639 initc = ptr[2];
1640 backwards = TRUE;
1641 break;
1642 }
1643 ptr += 2;
1644 if (*ptr == initc)
1645 {
1646 findc = initc;
1647 initc = ptr[-2];
1648 backwards = FALSE;
1649 break;
1650 }
1651 if (ptr[1] != ',')
1652 break;
1653 }
1654 if (!findc) /* invalid initc! */
1655 return NULL;
1656 }
1657 /*
1658 * Either initc is '#', or no initc was given and we need to look under the
1659 * cursor.
1660 */
1661 else
1662 {
1663 if (initc == '#')
1664 {
1665 hash_dir = dir;
1666 }
1667 else
1668 {
1669 /*
1670 * initc was not given, must look for something to match under
1671 * or near the cursor.
1672 * Only check for special things when 'cpo' doesn't have '%'.
1673 */
1674 if (!cpo_match)
1675 {
1676 /* Are we before or at #if, #else etc.? */
1677 ptr = skipwhite(linep);
1678 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1679 {
1680 ptr = skipwhite(ptr + 1);
1681 if ( STRNCMP(ptr, "if", 2) == 0
1682 || STRNCMP(ptr, "endif", 5) == 0
1683 || STRNCMP(ptr, "el", 2) == 0)
1684 hash_dir = 1;
1685 }
1686
1687 /* Are we on a comment? */
1688 else if (linep[pos.col] == '/')
1689 {
1690 if (linep[pos.col + 1] == '*')
1691 {
1692 comment_dir = FORWARD;
1693 backwards = FALSE;
1694 pos.col++;
1695 }
1696 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1697 {
1698 comment_dir = BACKWARD;
1699 backwards = TRUE;
1700 pos.col--;
1701 }
1702 }
1703 else if (linep[pos.col] == '*')
1704 {
1705 if (linep[pos.col + 1] == '/')
1706 {
1707 comment_dir = BACKWARD;
1708 backwards = TRUE;
1709 }
1710 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1711 {
1712 comment_dir = FORWARD;
1713 backwards = FALSE;
1714 }
1715 }
1716 }
1717
1718 /*
1719 * If we are not on a comment or the # at the start of a line, then
1720 * look for brace anywhere on this line after the cursor.
1721 */
1722 if (!hash_dir && !comment_dir)
1723 {
1724 /*
1725 * Find the brace under or after the cursor.
1726 * If beyond the end of the line, use the last character in
1727 * the line.
1728 */
1729 if (linep[pos.col] == NUL && pos.col)
1730 --pos.col;
1731 for (;;)
1732 {
1733 initc = linep[pos.col];
1734 if (initc == NUL)
1735 break;
1736
1737 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1738 {
1739 if (*ptr == initc)
1740 {
1741 findc = ptr[2];
1742 backwards = FALSE;
1743 break;
1744 }
1745 ptr += 2;
1746 if (*ptr == initc)
1747 {
1748 findc = ptr[-2];
1749 backwards = TRUE;
1750 break;
1751 }
1752 if (!*++ptr)
1753 break;
1754 }
1755 if (findc)
1756 break;
1757#ifdef FEAT_MBYTE
1758 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001759 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001760 else
1761#endif
1762 ++pos.col;
1763 }
1764 if (!findc)
1765 {
1766 /* no brace in the line, maybe use " #if" then */
1767 if (!cpo_match && *skipwhite(linep) == '#')
1768 hash_dir = 1;
1769 else
1770 return NULL;
1771 }
1772 else if (!cpo_bsl)
1773 {
1774 int col, bslcnt = 0;
1775
1776 /* Set "match_escaped" if there are an odd number of
1777 * backslashes. */
1778 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1779 bslcnt++;
1780 match_escaped = (bslcnt & 1);
1781 }
1782 }
1783 }
1784 if (hash_dir)
1785 {
1786 /*
1787 * Look for matching #if, #else, #elif, or #endif
1788 */
1789 if (oap != NULL)
1790 oap->motion_type = MLINE; /* Linewise for this case only */
1791 if (initc != '#')
1792 {
1793 ptr = skipwhite(skipwhite(linep) + 1);
1794 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1795 hash_dir = 1;
1796 else if (STRNCMP(ptr, "endif", 5) == 0)
1797 hash_dir = -1;
1798 else
1799 return NULL;
1800 }
1801 pos.col = 0;
1802 while (!got_int)
1803 {
1804 if (hash_dir > 0)
1805 {
1806 if (pos.lnum == curbuf->b_ml.ml_line_count)
1807 break;
1808 }
1809 else if (pos.lnum == 1)
1810 break;
1811 pos.lnum += hash_dir;
1812 linep = ml_get(pos.lnum);
1813 line_breakcheck(); /* check for CTRL-C typed */
1814 ptr = skipwhite(linep);
1815 if (*ptr != '#')
1816 continue;
1817 pos.col = (colnr_T) (ptr - linep);
1818 ptr = skipwhite(ptr + 1);
1819 if (hash_dir > 0)
1820 {
1821 if (STRNCMP(ptr, "if", 2) == 0)
1822 count++;
1823 else if (STRNCMP(ptr, "el", 2) == 0)
1824 {
1825 if (count == 0)
1826 return &pos;
1827 }
1828 else if (STRNCMP(ptr, "endif", 5) == 0)
1829 {
1830 if (count == 0)
1831 return &pos;
1832 count--;
1833 }
1834 }
1835 else
1836 {
1837 if (STRNCMP(ptr, "if", 2) == 0)
1838 {
1839 if (count == 0)
1840 return &pos;
1841 count--;
1842 }
1843 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1844 {
1845 if (count == 0)
1846 return &pos;
1847 }
1848 else if (STRNCMP(ptr, "endif", 5) == 0)
1849 count++;
1850 }
1851 }
1852 return NULL;
1853 }
1854 }
1855
1856#ifdef FEAT_RIGHTLEFT
1857 /* This is just guessing: when 'rightleft' is set, search for a maching
1858 * paren/brace in the other direction. */
1859 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1860 backwards = !backwards;
1861#endif
1862
1863 do_quotes = -1;
1864 start_in_quotes = MAYBE;
1865 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001866 if ((backwards && comment_dir)
1867#ifdef FEAT_LISP
1868 || lisp
1869#endif
1870 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001871 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001872#ifdef FEAT_LISP
1873 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1874 lispcomm = TRUE; /* find match inside this comment */
1875#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001876 while (!got_int)
1877 {
1878 /*
1879 * Go to the next position, forward or backward. We could use
1880 * inc() and dec() here, but that is much slower
1881 */
1882 if (backwards)
1883 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001884#ifdef FEAT_LISP
1885 /* char to match is inside of comment, don't search outside */
1886 if (lispcomm && pos.col < (colnr_T)comment_col)
1887 break;
1888#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001889 if (pos.col == 0) /* at start of line, go to prev. one */
1890 {
1891 if (pos.lnum == 1) /* start of file */
1892 break;
1893 --pos.lnum;
1894
1895 if (maxtravel && traveled++ > maxtravel)
1896 break;
1897
1898 linep = ml_get(pos.lnum);
1899 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1900 do_quotes = -1;
1901 line_breakcheck();
1902
1903 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001904 if (comment_dir
1905#ifdef FEAT_LISP
1906 || lisp
1907#endif
1908 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001909 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001910#ifdef FEAT_LISP
1911 /* skip comment */
1912 if (lisp && comment_col != MAXCOL)
1913 pos.col = comment_col;
1914#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001915 }
1916 else
1917 {
1918 --pos.col;
1919#ifdef FEAT_MBYTE
1920 if (has_mbyte)
1921 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1922#endif
1923 }
1924 }
1925 else /* forward search */
1926 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001927 if (linep[pos.col] == NUL
1928 /* at end of line, go to next one */
1929#ifdef FEAT_LISP
1930 /* don't search for match in comment */
1931 || (lisp && comment_col != MAXCOL
1932 && pos.col == (colnr_T)comment_col)
1933#endif
1934 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001935 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001936 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
1937#ifdef FEAT_LISP
1938 /* line is exhausted and comment with it,
1939 * don't search for match in code */
1940 || lispcomm
1941#endif
1942 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001943 break;
1944 ++pos.lnum;
1945
1946 if (maxtravel && traveled++ > maxtravel)
1947 break;
1948
1949 linep = ml_get(pos.lnum);
1950 pos.col = 0;
1951 do_quotes = -1;
1952 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001953#ifdef FEAT_LISP
1954 if (lisp) /* find comment pos in new line */
1955 comment_col = check_linecomment(linep);
1956#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001957 }
1958 else
1959 {
1960#ifdef FEAT_MBYTE
1961 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001962 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001963 else
1964#endif
1965 ++pos.col;
1966 }
1967 }
1968
1969 /*
1970 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
1971 */
1972 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
1973 (linep[0] == '{' || linep[0] == '}'))
1974 {
1975 if (linep[0] == findc && count == 0) /* match! */
1976 return &pos;
1977 break; /* out of scope */
1978 }
1979
1980 if (comment_dir)
1981 {
1982 /* Note: comments do not nest, and we ignore quotes in them */
1983 /* TODO: ignore comment brackets inside strings */
1984 if (comment_dir == FORWARD)
1985 {
1986 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
1987 {
1988 pos.col++;
1989 return &pos;
1990 }
1991 }
1992 else /* Searching backwards */
1993 {
1994 /*
1995 * A comment may contain / * or / /, it may also start or end
1996 * with / * /. Ignore a / * after / /.
1997 */
1998 if (pos.col == 0)
1999 continue;
2000 else if ( linep[pos.col - 1] == '/'
2001 && linep[pos.col] == '*'
2002 && (int)pos.col < comment_col)
2003 {
2004 count++;
2005 match_pos = pos;
2006 match_pos.col--;
2007 }
2008 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2009 {
2010 if (count > 0)
2011 pos = match_pos;
2012 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2013 && (int)pos.col <= comment_col)
2014 pos.col -= 2;
2015 else if (ignore_cend)
2016 continue;
2017 else
2018 return NULL;
2019 return &pos;
2020 }
2021 }
2022 continue;
2023 }
2024
2025 /*
2026 * If smart matching ('cpoptions' does not contain '%'), braces inside
2027 * of quotes are ignored, but only if there is an even number of
2028 * quotes in the line.
2029 */
2030 if (cpo_match)
2031 do_quotes = 0;
2032 else if (do_quotes == -1)
2033 {
2034 /*
2035 * Count the number of quotes in the line, skipping \" and '"'.
2036 * Watch out for "\\".
2037 */
2038 at_start = do_quotes;
2039 for (ptr = linep; *ptr; ++ptr)
2040 {
2041 if (ptr == linep + pos.col + backwards)
2042 at_start = (do_quotes & 1);
2043 if (*ptr == '"'
2044 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2045 ++do_quotes;
2046 if (*ptr == '\\' && ptr[1] != NUL)
2047 ++ptr;
2048 }
2049 do_quotes &= 1; /* result is 1 with even number of quotes */
2050
2051 /*
2052 * If we find an uneven count, check current line and previous
2053 * one for a '\' at the end.
2054 */
2055 if (!do_quotes)
2056 {
2057 inquote = FALSE;
2058 if (ptr[-1] == '\\')
2059 {
2060 do_quotes = 1;
2061 if (start_in_quotes == MAYBE)
2062 {
2063 /* Do we need to use at_start here? */
2064 inquote = TRUE;
2065 start_in_quotes = TRUE;
2066 }
2067 else if (backwards)
2068 inquote = TRUE;
2069 }
2070 if (pos.lnum > 1)
2071 {
2072 ptr = ml_get(pos.lnum - 1);
2073 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2074 {
2075 do_quotes = 1;
2076 if (start_in_quotes == MAYBE)
2077 {
2078 inquote = at_start;
2079 if (inquote)
2080 start_in_quotes = TRUE;
2081 }
2082 else if (!backwards)
2083 inquote = TRUE;
2084 }
2085 }
2086 }
2087 }
2088 if (start_in_quotes == MAYBE)
2089 start_in_quotes = FALSE;
2090
2091 /*
2092 * If 'smartmatch' is set:
2093 * Things inside quotes are ignored by setting 'inquote'. If we
2094 * find a quote without a preceding '\' invert 'inquote'. At the
2095 * end of a line not ending in '\' we reset 'inquote'.
2096 *
2097 * In lines with an uneven number of quotes (without preceding '\')
2098 * we do not know which part to ignore. Therefore we only set
2099 * inquote if the number of quotes in a line is even, unless this
2100 * line or the previous one ends in a '\'. Complicated, isn't it?
2101 */
2102 switch (c = linep[pos.col])
2103 {
2104 case NUL:
2105 /* at end of line without trailing backslash, reset inquote */
2106 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2107 {
2108 inquote = FALSE;
2109 start_in_quotes = FALSE;
2110 }
2111 break;
2112
2113 case '"':
2114 /* a quote that is preceded with an odd number of backslashes is
2115 * ignored */
2116 if (do_quotes)
2117 {
2118 int col;
2119
2120 for (col = pos.col - 1; col >= 0; --col)
2121 if (linep[col] != '\\')
2122 break;
2123 if ((((int)pos.col - 1 - col) & 1) == 0)
2124 {
2125 inquote = !inquote;
2126 start_in_quotes = FALSE;
2127 }
2128 }
2129 break;
2130
2131 /*
2132 * If smart matching ('cpoptions' does not contain '%'):
2133 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2134 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2135 * skipped, there is never a brace in them.
2136 * Ignore this when finding matches for `'.
2137 */
2138 case '\'':
2139 if (!cpo_match && initc != '\'' && findc != '\'')
2140 {
2141 if (backwards)
2142 {
2143 if (pos.col > 1)
2144 {
2145 if (linep[pos.col - 2] == '\'')
2146 {
2147 pos.col -= 2;
2148 break;
2149 }
2150 else if (linep[pos.col - 2] == '\\' &&
2151 pos.col > 2 && linep[pos.col - 3] == '\'')
2152 {
2153 pos.col -= 3;
2154 break;
2155 }
2156 }
2157 }
2158 else if (linep[pos.col + 1]) /* forward search */
2159 {
2160 if (linep[pos.col + 1] == '\\' &&
2161 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2162 {
2163 pos.col += 3;
2164 break;
2165 }
2166 else if (linep[pos.col + 2] == '\'')
2167 {
2168 pos.col += 2;
2169 break;
2170 }
2171 }
2172 }
2173 /* FALLTHROUGH */
2174
2175 default:
2176#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002177 /*
2178 * For Lisp skip over backslashed (), {} and [].
2179 * (actually, we skip #\( et al)
2180 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002181 if (curbuf->b_p_lisp
2182 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002183 && pos.col > 1
2184 && check_prevcol(linep, pos.col, '\\', NULL)
2185 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002186 break;
2187#endif
2188
2189 /* Check for match outside of quotes, and inside of
2190 * quotes when the start is also inside of quotes. */
2191 if ((!inquote || start_in_quotes == TRUE)
2192 && (c == initc || c == findc))
2193 {
2194 int col, bslcnt = 0;
2195
2196 if (!cpo_bsl)
2197 {
2198 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2199 bslcnt++;
2200 }
2201 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2202 * what we expect. */
2203 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2204 {
2205 if (c == initc)
2206 count++;
2207 else
2208 {
2209 if (count == 0)
2210 return &pos;
2211 count--;
2212 }
2213 }
2214 }
2215 }
2216 }
2217
2218 if (comment_dir == BACKWARD && count > 0)
2219 {
2220 pos = match_pos;
2221 return &pos;
2222 }
2223 return (pos_T *)NULL; /* never found it */
2224}
2225
2226/*
2227 * Check if line[] contains a / / comment.
2228 * Return MAXCOL if not, otherwise return the column.
2229 * TODO: skip strings.
2230 */
2231 static int
2232check_linecomment(line)
2233 char_u *line;
2234{
2235 char_u *p;
2236
2237 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002238#ifdef FEAT_LISP
2239 /* skip Lispish one-line comments */
2240 if (curbuf->b_p_lisp)
2241 {
2242 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2243 {
2244 int instr = FALSE; /* inside of string */
2245
2246 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002247 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002248 {
2249 if (*p == '"')
2250 {
2251 if (instr)
2252 {
2253 if (*(p - 1) != '\\') /* skip escaped quote */
2254 instr = FALSE;
2255 }
2256 else if (p == line || ((p - line) >= 2
2257 /* skip #\" form */
2258 && *(p - 1) != '\\' && *(p - 2) != '#'))
2259 instr = TRUE;
2260 }
2261 else if (!instr && ((p - line) < 2
2262 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2263 break; /* found! */
2264 ++p;
2265 }
2266 }
2267 else
2268 p = NULL;
2269 }
2270 else
2271#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002272 while ((p = vim_strchr(p, '/')) != NULL)
2273 {
2274 if (p[1] == '/')
2275 break;
2276 ++p;
2277 }
2278
2279 if (p == NULL)
2280 return MAXCOL;
2281 return (int)(p - line);
2282}
2283
2284/*
2285 * Move cursor briefly to character matching the one under the cursor.
2286 * Used for Insert mode and "r" command.
2287 * Show the match only if it is visible on the screen.
2288 * If there isn't a match, then beep.
2289 */
2290 void
2291showmatch(c)
2292 int c; /* char to show match for */
2293{
2294 pos_T *lpos, save_cursor;
2295 pos_T mpos;
2296 colnr_T vcol;
2297 long save_so;
2298 long save_siso;
2299#ifdef CURSOR_SHAPE
2300 int save_state;
2301#endif
2302 colnr_T save_dollar_vcol;
2303 char_u *p;
2304
2305 /*
2306 * Only show match for chars in the 'matchpairs' option.
2307 */
2308 /* 'matchpairs' is "x:y,x:y" */
2309 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2310 {
2311#ifdef FEAT_RIGHTLEFT
2312 if (*p == c && (curwin->w_p_rl ^ p_ri))
2313 break;
2314#endif
2315 p += 2;
2316 if (*p == c
2317#ifdef FEAT_RIGHTLEFT
2318 && !(curwin->w_p_rl ^ p_ri)
2319#endif
2320 )
2321 break;
2322 if (p[1] != ',')
2323 return;
2324 }
2325
2326 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2327 vim_beep();
2328 else if (lpos->lnum >= curwin->w_topline)
2329 {
2330 if (!curwin->w_p_wrap)
2331 getvcol(curwin, lpos, NULL, &vcol, NULL);
2332 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2333 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2334 {
2335 mpos = *lpos; /* save the pos, update_screen() may change it */
2336 save_cursor = curwin->w_cursor;
2337 save_so = p_so;
2338 save_siso = p_siso;
2339 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2340 * stop displaying the "$". */
2341 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2342 dollar_vcol = 0;
2343 ++curwin->w_virtcol; /* do display ')' just before "$" */
2344 update_screen(VALID); /* show the new char first */
2345
2346 save_dollar_vcol = dollar_vcol;
2347#ifdef CURSOR_SHAPE
2348 save_state = State;
2349 State = SHOWMATCH;
2350 ui_cursor_shape(); /* may show different cursor shape */
2351#endif
2352 curwin->w_cursor = mpos; /* move to matching char */
2353 p_so = 0; /* don't use 'scrolloff' here */
2354 p_siso = 0; /* don't use 'sidescrolloff' here */
2355 showruler(FALSE);
2356 setcursor();
2357 cursor_on(); /* make sure that the cursor is shown */
2358 out_flush();
2359#ifdef FEAT_GUI
2360 if (gui.in_use)
2361 {
2362 gui_update_cursor(TRUE, FALSE);
2363 gui_mch_flush();
2364 }
2365#endif
2366 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2367 * which resets it if the matching position is in a previous line
2368 * and has a higher column number. */
2369 dollar_vcol = save_dollar_vcol;
2370
2371 /*
2372 * brief pause, unless 'm' is present in 'cpo' and a character is
2373 * available.
2374 */
2375 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2376 ui_delay(p_mat * 100L, TRUE);
2377 else if (!char_avail())
2378 ui_delay(p_mat * 100L, FALSE);
2379 curwin->w_cursor = save_cursor; /* restore cursor position */
2380 p_so = save_so;
2381 p_siso = save_siso;
2382#ifdef CURSOR_SHAPE
2383 State = save_state;
2384 ui_cursor_shape(); /* may show different cursor shape */
2385#endif
2386 }
2387 }
2388}
2389
2390/*
2391 * findsent(dir, count) - Find the start of the next sentence in direction
2392 * 'dir' Sentences are supposed to end in ".", "!" or "?" followed by white
2393 * space or a line break. Also stop at an empty line.
2394 * Return OK if the next sentence was found.
2395 */
2396 int
2397findsent(dir, count)
2398 int dir;
2399 long count;
2400{
2401 pos_T pos, tpos;
2402 int c;
2403 int (*func) __ARGS((pos_T *));
2404 int startlnum;
2405 int noskip = FALSE; /* do not skip blanks */
2406 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002407 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002408
2409 pos = curwin->w_cursor;
2410 if (dir == FORWARD)
2411 func = incl;
2412 else
2413 func = decl;
2414
2415 while (count--)
2416 {
2417 /*
2418 * if on an empty line, skip upto a non-empty line
2419 */
2420 if (gchar_pos(&pos) == NUL)
2421 {
2422 do
2423 if ((*func)(&pos) == -1)
2424 break;
2425 while (gchar_pos(&pos) == NUL);
2426 if (dir == FORWARD)
2427 goto found;
2428 }
2429 /*
2430 * if on the start of a paragraph or a section and searching forward,
2431 * go to the next line
2432 */
2433 else if (dir == FORWARD && pos.col == 0 &&
2434 startPS(pos.lnum, NUL, FALSE))
2435 {
2436 if (pos.lnum == curbuf->b_ml.ml_line_count)
2437 return FAIL;
2438 ++pos.lnum;
2439 goto found;
2440 }
2441 else if (dir == BACKWARD)
2442 decl(&pos);
2443
2444 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002445 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002446 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2447 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2448 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002449 if (vim_strchr((char_u *)".!?", c) != NULL)
2450 {
2451 /* Only skip over a '.', '!' and '?' once. */
2452 if (found_dot)
2453 break;
2454 found_dot = TRUE;
2455 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002456 if (decl(&pos) == -1)
2457 break;
2458 /* when going forward: Stop in front of empty line */
2459 if (lineempty(pos.lnum) && dir == FORWARD)
2460 {
2461 incl(&pos);
2462 goto found;
2463 }
2464 }
2465
2466 /* remember the line where the search started */
2467 startlnum = pos.lnum;
2468 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2469
2470 for (;;) /* find end of sentence */
2471 {
2472 c = gchar_pos(&pos);
2473 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2474 {
2475 if (dir == BACKWARD && pos.lnum != startlnum)
2476 ++pos.lnum;
2477 break;
2478 }
2479 if (c == '.' || c == '!' || c == '?')
2480 {
2481 tpos = pos;
2482 do
2483 if ((c = inc(&tpos)) == -1)
2484 break;
2485 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2486 != NULL);
2487 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2488 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2489 && gchar_pos(&tpos) == ' ')))
2490 {
2491 pos = tpos;
2492 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2493 inc(&pos);
2494 break;
2495 }
2496 }
2497 if ((*func)(&pos) == -1)
2498 {
2499 if (count)
2500 return FAIL;
2501 noskip = TRUE;
2502 break;
2503 }
2504 }
2505found:
2506 /* skip white space */
2507 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2508 if (incl(&pos) == -1)
2509 break;
2510 }
2511
2512 setpcmark();
2513 curwin->w_cursor = pos;
2514 return OK;
2515}
2516
2517/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002518 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002519 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002520 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002521 * If 'what' is '{' or '}' we go to the next section.
2522 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002523 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002524 */
2525 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002526findpar(pincl, dir, count, what, both)
2527 int *pincl; /* Return: TRUE if last char is to be included */
2528 int dir;
2529 long count;
2530 int what;
2531 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002532{
2533 linenr_T curr;
2534 int did_skip; /* TRUE after separating lines have been skipped */
2535 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002536 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002537#ifdef FEAT_FOLDING
2538 linenr_T fold_first; /* first line of a closed fold */
2539 linenr_T fold_last; /* last line of a closed fold */
2540 int fold_skipped; /* TRUE if a closed fold was skipped this
2541 iteration */
2542#endif
2543
2544 curr = curwin->w_cursor.lnum;
2545
2546 while (count--)
2547 {
2548 did_skip = FALSE;
2549 for (first = TRUE; ; first = FALSE)
2550 {
2551 if (*ml_get(curr) != NUL)
2552 did_skip = TRUE;
2553
2554#ifdef FEAT_FOLDING
2555 /* skip folded lines */
2556 fold_skipped = FALSE;
2557 if (first && hasFolding(curr, &fold_first, &fold_last))
2558 {
2559 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2560 fold_skipped = TRUE;
2561 }
2562#endif
2563
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002564 /* POSIX has it's own ideas of what a paragraph boundary is and it
2565 * doesn't match historical Vi: It also stops at a "{" in the
2566 * first column and at an empty line. */
2567 if (!first && did_skip && (startPS(curr, what, both)
2568 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002569 break;
2570
2571#ifdef FEAT_FOLDING
2572 if (fold_skipped)
2573 curr -= dir;
2574#endif
2575 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2576 {
2577 if (count)
2578 return FALSE;
2579 curr -= dir;
2580 break;
2581 }
2582 }
2583 }
2584 setpcmark();
2585 if (both && *ml_get(curr) == '}') /* include line with '}' */
2586 ++curr;
2587 curwin->w_cursor.lnum = curr;
2588 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2589 {
2590 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2591 {
2592 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002593 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594 }
2595 }
2596 else
2597 curwin->w_cursor.col = 0;
2598 return TRUE;
2599}
2600
2601/*
2602 * check if the string 's' is a nroff macro that is in option 'opt'
2603 */
2604 static int
2605inmacro(opt, s)
2606 char_u *opt;
2607 char_u *s;
2608{
2609 char_u *macro;
2610
2611 for (macro = opt; macro[0]; ++macro)
2612 {
2613 /* Accept two characters in the option being equal to two characters
2614 * in the line. A space in the option matches with a space in the
2615 * line or the line having ended. */
2616 if ( (macro[0] == s[0]
2617 || (macro[0] == ' '
2618 && (s[0] == NUL || s[0] == ' ')))
2619 && (macro[1] == s[1]
2620 || ((macro[1] == NUL || macro[1] == ' ')
2621 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2622 break;
2623 ++macro;
2624 if (macro[0] == NUL)
2625 break;
2626 }
2627 return (macro[0] != NUL);
2628}
2629
2630/*
2631 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2632 * If 'para' is '{' or '}' only check for sections.
2633 * If 'both' is TRUE also stop at '}'
2634 */
2635 int
2636startPS(lnum, para, both)
2637 linenr_T lnum;
2638 int para;
2639 int both;
2640{
2641 char_u *s;
2642
2643 s = ml_get(lnum);
2644 if (*s == para || *s == '\f' || (both && *s == '}'))
2645 return TRUE;
2646 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2647 (!para && inmacro(p_para, s + 1))))
2648 return TRUE;
2649 return FALSE;
2650}
2651
2652/*
2653 * The following routines do the word searches performed by the 'w', 'W',
2654 * 'b', 'B', 'e', and 'E' commands.
2655 */
2656
2657/*
2658 * To perform these searches, characters are placed into one of three
2659 * classes, and transitions between classes determine word boundaries.
2660 *
2661 * The classes are:
2662 *
2663 * 0 - white space
2664 * 1 - punctuation
2665 * 2 or higher - keyword characters (letters, digits and underscore)
2666 */
2667
2668static int cls_bigword; /* TRUE for "W", "B" or "E" */
2669
2670/*
2671 * cls() - returns the class of character at curwin->w_cursor
2672 *
2673 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2674 * from class 2 and higher are reported as class 1 since only white space
2675 * boundaries are of interest.
2676 */
2677 static int
2678cls()
2679{
2680 int c;
2681
2682 c = gchar_cursor();
2683#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2684 if (p_altkeymap && c == F_BLANK)
2685 return 0;
2686#endif
2687 if (c == ' ' || c == '\t' || c == NUL)
2688 return 0;
2689#ifdef FEAT_MBYTE
2690 if (enc_dbcs != 0 && c > 0xFF)
2691 {
2692 /* If cls_bigword, report multi-byte chars as class 1. */
2693 if (enc_dbcs == DBCS_KOR && cls_bigword)
2694 return 1;
2695
2696 /* process code leading/trailing bytes */
2697 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2698 }
2699 if (enc_utf8)
2700 {
2701 c = utf_class(c);
2702 if (c != 0 && cls_bigword)
2703 return 1;
2704 return c;
2705 }
2706#endif
2707
2708 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2709 if (cls_bigword)
2710 return 1;
2711
2712 if (vim_iswordc(c))
2713 return 2;
2714 return 1;
2715}
2716
2717
2718/*
2719 * fwd_word(count, type, eol) - move forward one word
2720 *
2721 * Returns FAIL if the cursor was already at the end of the file.
2722 * If eol is TRUE, last word stops at end of line (for operators).
2723 */
2724 int
2725fwd_word(count, bigword, eol)
2726 long count;
2727 int bigword; /* "W", "E" or "B" */
2728 int eol;
2729{
2730 int sclass; /* starting class */
2731 int i;
2732 int last_line;
2733
2734#ifdef FEAT_VIRTUALEDIT
2735 curwin->w_cursor.coladd = 0;
2736#endif
2737 cls_bigword = bigword;
2738 while (--count >= 0)
2739 {
2740#ifdef FEAT_FOLDING
2741 /* When inside a range of folded lines, move to the last char of the
2742 * last line. */
2743 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2744 coladvance((colnr_T)MAXCOL);
2745#endif
2746 sclass = cls();
2747
2748 /*
2749 * We always move at least one character, unless on the last
2750 * character in the buffer.
2751 */
2752 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2753 i = inc_cursor();
2754 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2755 return FAIL;
2756 if (i == 1 && eol && count == 0) /* started at last char in line */
2757 return OK;
2758
2759 /*
2760 * Go one char past end of current word (if any)
2761 */
2762 if (sclass != 0)
2763 while (cls() == sclass)
2764 {
2765 i = inc_cursor();
2766 if (i == -1 || (i >= 1 && eol && count == 0))
2767 return OK;
2768 }
2769
2770 /*
2771 * go to next non-white
2772 */
2773 while (cls() == 0)
2774 {
2775 /*
2776 * We'll stop if we land on a blank line
2777 */
2778 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2779 break;
2780
2781 i = inc_cursor();
2782 if (i == -1 || (i >= 1 && eol && count == 0))
2783 return OK;
2784 }
2785 }
2786 return OK;
2787}
2788
2789/*
2790 * bck_word() - move backward 'count' words
2791 *
2792 * If stop is TRUE and we are already on the start of a word, move one less.
2793 *
2794 * Returns FAIL if top of the file was reached.
2795 */
2796 int
2797bck_word(count, bigword, stop)
2798 long count;
2799 int bigword;
2800 int stop;
2801{
2802 int sclass; /* starting class */
2803
2804#ifdef FEAT_VIRTUALEDIT
2805 curwin->w_cursor.coladd = 0;
2806#endif
2807 cls_bigword = bigword;
2808 while (--count >= 0)
2809 {
2810#ifdef FEAT_FOLDING
2811 /* When inside a range of folded lines, move to the first char of the
2812 * first line. */
2813 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2814 curwin->w_cursor.col = 0;
2815#endif
2816 sclass = cls();
2817 if (dec_cursor() == -1) /* started at start of file */
2818 return FAIL;
2819
2820 if (!stop || sclass == cls() || sclass == 0)
2821 {
2822 /*
2823 * Skip white space before the word.
2824 * Stop on an empty line.
2825 */
2826 while (cls() == 0)
2827 {
2828 if (curwin->w_cursor.col == 0
2829 && lineempty(curwin->w_cursor.lnum))
2830 goto finished;
2831 if (dec_cursor() == -1) /* hit start of file, stop here */
2832 return OK;
2833 }
2834
2835 /*
2836 * Move backward to start of this word.
2837 */
2838 if (skip_chars(cls(), BACKWARD))
2839 return OK;
2840 }
2841
2842 inc_cursor(); /* overshot - forward one */
2843finished:
2844 stop = FALSE;
2845 }
2846 return OK;
2847}
2848
2849/*
2850 * end_word() - move to the end of the word
2851 *
2852 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2853 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2854 * motion crosses blank lines. When the real vi crosses a blank line in an
2855 * 'e' motion, the cursor is placed on the FIRST character of the next
2856 * non-blank line. The 'E' command, however, works correctly. Since this
2857 * appears to be a bug, I have not duplicated it here.
2858 *
2859 * Returns FAIL if end of the file was reached.
2860 *
2861 * If stop is TRUE and we are already on the end of a word, move one less.
2862 * If empty is TRUE stop on an empty line.
2863 */
2864 int
2865end_word(count, bigword, stop, empty)
2866 long count;
2867 int bigword;
2868 int stop;
2869 int empty;
2870{
2871 int sclass; /* starting class */
2872
2873#ifdef FEAT_VIRTUALEDIT
2874 curwin->w_cursor.coladd = 0;
2875#endif
2876 cls_bigword = bigword;
2877 while (--count >= 0)
2878 {
2879#ifdef FEAT_FOLDING
2880 /* When inside a range of folded lines, move to the last char of the
2881 * last line. */
2882 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2883 coladvance((colnr_T)MAXCOL);
2884#endif
2885 sclass = cls();
2886 if (inc_cursor() == -1)
2887 return FAIL;
2888
2889 /*
2890 * If we're in the middle of a word, we just have to move to the end
2891 * of it.
2892 */
2893 if (cls() == sclass && sclass != 0)
2894 {
2895 /*
2896 * Move forward to end of the current word
2897 */
2898 if (skip_chars(sclass, FORWARD))
2899 return FAIL;
2900 }
2901 else if (!stop || sclass == 0)
2902 {
2903 /*
2904 * We were at the end of a word. Go to the end of the next word.
2905 * First skip white space, if 'empty' is TRUE, stop at empty line.
2906 */
2907 while (cls() == 0)
2908 {
2909 if (empty && curwin->w_cursor.col == 0
2910 && lineempty(curwin->w_cursor.lnum))
2911 goto finished;
2912 if (inc_cursor() == -1) /* hit end of file, stop here */
2913 return FAIL;
2914 }
2915
2916 /*
2917 * Move forward to the end of this word.
2918 */
2919 if (skip_chars(cls(), FORWARD))
2920 return FAIL;
2921 }
2922 dec_cursor(); /* overshot - one char backward */
2923finished:
2924 stop = FALSE; /* we move only one word less */
2925 }
2926 return OK;
2927}
2928
2929/*
2930 * Move back to the end of the word.
2931 *
2932 * Returns FAIL if start of the file was reached.
2933 */
2934 int
2935bckend_word(count, bigword, eol)
2936 long count;
2937 int bigword; /* TRUE for "B" */
2938 int eol; /* TRUE: stop at end of line. */
2939{
2940 int sclass; /* starting class */
2941 int i;
2942
2943#ifdef FEAT_VIRTUALEDIT
2944 curwin->w_cursor.coladd = 0;
2945#endif
2946 cls_bigword = bigword;
2947 while (--count >= 0)
2948 {
2949 sclass = cls();
2950 if ((i = dec_cursor()) == -1)
2951 return FAIL;
2952 if (eol && i == 1)
2953 return OK;
2954
2955 /*
2956 * Move backward to before the start of this word.
2957 */
2958 if (sclass != 0)
2959 {
2960 while (cls() == sclass)
2961 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2962 return OK;
2963 }
2964
2965 /*
2966 * Move backward to end of the previous word
2967 */
2968 while (cls() == 0)
2969 {
2970 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
2971 break;
2972 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2973 return OK;
2974 }
2975 }
2976 return OK;
2977}
2978
2979/*
2980 * Skip a row of characters of the same class.
2981 * Return TRUE when end-of-file reached, FALSE otherwise.
2982 */
2983 static int
2984skip_chars(cclass, dir)
2985 int cclass;
2986 int dir;
2987{
2988 while (cls() == cclass)
2989 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
2990 return TRUE;
2991 return FALSE;
2992}
2993
2994#ifdef FEAT_TEXTOBJ
2995/*
2996 * Go back to the start of the word or the start of white space
2997 */
2998 static void
2999back_in_line()
3000{
3001 int sclass; /* starting class */
3002
3003 sclass = cls();
3004 for (;;)
3005 {
3006 if (curwin->w_cursor.col == 0) /* stop at start of line */
3007 break;
3008 dec_cursor();
3009 if (cls() != sclass) /* stop at start of word */
3010 {
3011 inc_cursor();
3012 break;
3013 }
3014 }
3015}
3016
3017 static void
3018find_first_blank(posp)
3019 pos_T *posp;
3020{
3021 int c;
3022
3023 while (decl(posp) != -1)
3024 {
3025 c = gchar_pos(posp);
3026 if (!vim_iswhite(c))
3027 {
3028 incl(posp);
3029 break;
3030 }
3031 }
3032}
3033
3034/*
3035 * Skip count/2 sentences and count/2 separating white spaces.
3036 */
3037 static void
3038findsent_forward(count, at_start_sent)
3039 long count;
3040 int at_start_sent; /* cursor is at start of sentence */
3041{
3042 while (count--)
3043 {
3044 findsent(FORWARD, 1L);
3045 if (at_start_sent)
3046 find_first_blank(&curwin->w_cursor);
3047 if (count == 0 || at_start_sent)
3048 decl(&curwin->w_cursor);
3049 at_start_sent = !at_start_sent;
3050 }
3051}
3052
3053/*
3054 * Find word under cursor, cursor at end.
3055 * Used while an operator is pending, and in Visual mode.
3056 */
3057 int
3058current_word(oap, count, include, bigword)
3059 oparg_T *oap;
3060 long count;
3061 int include; /* TRUE: include word and white space */
3062 int bigword; /* FALSE == word, TRUE == WORD */
3063{
3064 pos_T start_pos;
3065 pos_T pos;
3066 int inclusive = TRUE;
3067 int include_white = FALSE;
3068
3069 cls_bigword = bigword;
3070
3071#ifdef FEAT_VISUAL
3072 /* Correct cursor when 'selection' is exclusive */
3073 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3074 dec_cursor();
3075
3076 /*
3077 * When Visual mode is not active, or when the VIsual area is only one
3078 * character, select the word and/or white space under the cursor.
3079 */
3080 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3081#endif
3082 {
3083 /*
3084 * Go to start of current word or white space.
3085 */
3086 back_in_line();
3087 start_pos = curwin->w_cursor;
3088
3089 /*
3090 * If the start is on white space, and white space should be included
3091 * (" word"), or start is not on white space, and white space should
3092 * not be included ("word"), find end of word.
3093 */
3094 if ((cls() == 0) == include)
3095 {
3096 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3097 return FAIL;
3098 }
3099 else
3100 {
3101 /*
3102 * If the start is not on white space, and white space should be
3103 * included ("word "), or start is on white space and white
3104 * space should not be included (" "), find start of word.
3105 * If we end up in the first column of the next line (single char
3106 * word) back up to end of the line.
3107 */
3108 fwd_word(1L, bigword, TRUE);
3109 if (curwin->w_cursor.col == 0)
3110 decl(&curwin->w_cursor);
3111 else
3112 oneleft();
3113
3114 if (include)
3115 include_white = TRUE;
3116 }
3117
3118#ifdef FEAT_VISUAL
3119 if (VIsual_active)
3120 {
3121 /* should do something when inclusive == FALSE ! */
3122 VIsual = start_pos;
3123 redraw_curbuf_later(INVERTED); /* update the inversion */
3124 }
3125 else
3126#endif
3127 {
3128 oap->start = start_pos;
3129 oap->motion_type = MCHAR;
3130 }
3131 --count;
3132 }
3133
3134 /*
3135 * When count is still > 0, extend with more objects.
3136 */
3137 while (count > 0)
3138 {
3139 inclusive = TRUE;
3140#ifdef FEAT_VISUAL
3141 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3142 {
3143 /*
3144 * In Visual mode, with cursor at start: move cursor back.
3145 */
3146 if (decl(&curwin->w_cursor) == -1)
3147 return FAIL;
3148 if (include != (cls() != 0))
3149 {
3150 if (bck_word(1L, bigword, TRUE) == FAIL)
3151 return FAIL;
3152 }
3153 else
3154 {
3155 if (bckend_word(1L, bigword, TRUE) == FAIL)
3156 return FAIL;
3157 (void)incl(&curwin->w_cursor);
3158 }
3159 }
3160 else
3161#endif
3162 {
3163 /*
3164 * Move cursor forward one word and/or white area.
3165 */
3166 if (incl(&curwin->w_cursor) == -1)
3167 return FAIL;
3168 if (include != (cls() == 0))
3169 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003170 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003171 return FAIL;
3172 /*
3173 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003174 * the first character on the line.
3175 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003176 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003177 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003178 inclusive = FALSE;
3179 }
3180 else
3181 {
3182 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3183 return FAIL;
3184 }
3185 }
3186 --count;
3187 }
3188
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003189 if (include_white && (cls() != 0
3190 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003191 {
3192 /*
3193 * If we don't include white space at the end, move the start
3194 * to include some white space there. This makes "daw" work
3195 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003196 * word). Also when "2daw" deletes "word." at the end of the line
3197 * (cursor is at start of next line).
3198 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003199 */
3200 pos = curwin->w_cursor; /* save cursor position */
3201 curwin->w_cursor = start_pos;
3202 if (oneleft() == OK)
3203 {
3204 back_in_line();
3205 if (cls() == 0 && curwin->w_cursor.col > 0)
3206 {
3207#ifdef FEAT_VISUAL
3208 if (VIsual_active)
3209 VIsual = curwin->w_cursor;
3210 else
3211#endif
3212 oap->start = curwin->w_cursor;
3213 }
3214 }
3215 curwin->w_cursor = pos; /* put cursor back at end */
3216 }
3217
3218#ifdef FEAT_VISUAL
3219 if (VIsual_active)
3220 {
3221 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3222 inc_cursor();
3223 if (VIsual_mode == 'V')
3224 {
3225 VIsual_mode = 'v';
3226 redraw_cmdline = TRUE; /* show mode later */
3227 }
3228 }
3229 else
3230#endif
3231 oap->inclusive = inclusive;
3232
3233 return OK;
3234}
3235
3236/*
3237 * Find sentence(s) under the cursor, cursor at end.
3238 * When Visual active, extend it by one or more sentences.
3239 */
3240 int
3241current_sent(oap, count, include)
3242 oparg_T *oap;
3243 long count;
3244 int include;
3245{
3246 pos_T start_pos;
3247 pos_T pos;
3248 int start_blank;
3249 int c;
3250 int at_start_sent;
3251 long ncount;
3252
3253 start_pos = curwin->w_cursor;
3254 pos = start_pos;
3255 findsent(FORWARD, 1L); /* Find start of next sentence. */
3256
3257#ifdef FEAT_VISUAL
3258 /*
3259 * When visual area is bigger than one character: Extend it.
3260 */
3261 if (VIsual_active && !equalpos(start_pos, VIsual))
3262 {
3263extend:
3264 if (lt(start_pos, VIsual))
3265 {
3266 /*
3267 * Cursor at start of Visual area.
3268 * Find out where we are:
3269 * - in the white space before a sentence
3270 * - in a sentence or just after it
3271 * - at the start of a sentence
3272 */
3273 at_start_sent = TRUE;
3274 decl(&pos);
3275 while (lt(pos, curwin->w_cursor))
3276 {
3277 c = gchar_pos(&pos);
3278 if (!vim_iswhite(c))
3279 {
3280 at_start_sent = FALSE;
3281 break;
3282 }
3283 incl(&pos);
3284 }
3285 if (!at_start_sent)
3286 {
3287 findsent(BACKWARD, 1L);
3288 if (equalpos(curwin->w_cursor, start_pos))
3289 at_start_sent = TRUE; /* exactly at start of sentence */
3290 else
3291 /* inside a sentence, go to its end (start of next) */
3292 findsent(FORWARD, 1L);
3293 }
3294 if (include) /* "as" gets twice as much as "is" */
3295 count *= 2;
3296 while (count--)
3297 {
3298 if (at_start_sent)
3299 find_first_blank(&curwin->w_cursor);
3300 c = gchar_cursor();
3301 if (!at_start_sent || (!include && !vim_iswhite(c)))
3302 findsent(BACKWARD, 1L);
3303 at_start_sent = !at_start_sent;
3304 }
3305 }
3306 else
3307 {
3308 /*
3309 * Cursor at end of Visual area.
3310 * Find out where we are:
3311 * - just before a sentence
3312 * - just before or in the white space before a sentence
3313 * - in a sentence
3314 */
3315 incl(&pos);
3316 at_start_sent = TRUE;
3317 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3318 {
3319 at_start_sent = FALSE;
3320 while (lt(pos, curwin->w_cursor))
3321 {
3322 c = gchar_pos(&pos);
3323 if (!vim_iswhite(c))
3324 {
3325 at_start_sent = TRUE;
3326 break;
3327 }
3328 incl(&pos);
3329 }
3330 if (at_start_sent) /* in the sentence */
3331 findsent(BACKWARD, 1L);
3332 else /* in/before white before a sentence */
3333 curwin->w_cursor = start_pos;
3334 }
3335
3336 if (include) /* "as" gets twice as much as "is" */
3337 count *= 2;
3338 findsent_forward(count, at_start_sent);
3339 if (*p_sel == 'e')
3340 ++curwin->w_cursor.col;
3341 }
3342 return OK;
3343 }
3344#endif
3345
3346 /*
3347 * If cursor started on blank, check if it is just before the start of the
3348 * next sentence.
3349 */
3350 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3351 incl(&pos);
3352 if (equalpos(pos, curwin->w_cursor))
3353 {
3354 start_blank = TRUE;
3355 find_first_blank(&start_pos); /* go back to first blank */
3356 }
3357 else
3358 {
3359 start_blank = FALSE;
3360 findsent(BACKWARD, 1L);
3361 start_pos = curwin->w_cursor;
3362 }
3363 if (include)
3364 ncount = count * 2;
3365 else
3366 {
3367 ncount = count;
3368 if (start_blank)
3369 --ncount;
3370 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003371 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003372 findsent_forward(ncount, TRUE);
3373 else
3374 decl(&curwin->w_cursor);
3375
3376 if (include)
3377 {
3378 /*
3379 * If the blank in front of the sentence is included, exclude the
3380 * blanks at the end of the sentence, go back to the first blank.
3381 * If there are no trailing blanks, try to include leading blanks.
3382 */
3383 if (start_blank)
3384 {
3385 find_first_blank(&curwin->w_cursor);
3386 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3387 if (vim_iswhite(c))
3388 decl(&curwin->w_cursor);
3389 }
3390 else if (c = gchar_cursor(), !vim_iswhite(c))
3391 find_first_blank(&start_pos);
3392 }
3393
3394#ifdef FEAT_VISUAL
3395 if (VIsual_active)
3396 {
3397 /* avoid getting stuck with "is" on a single space before a sent. */
3398 if (equalpos(start_pos, curwin->w_cursor))
3399 goto extend;
3400 if (*p_sel == 'e')
3401 ++curwin->w_cursor.col;
3402 VIsual = start_pos;
3403 VIsual_mode = 'v';
3404 redraw_curbuf_later(INVERTED); /* update the inversion */
3405 }
3406 else
3407#endif
3408 {
3409 /* include a newline after the sentence, if there is one */
3410 if (incl(&curwin->w_cursor) == -1)
3411 oap->inclusive = TRUE;
3412 else
3413 oap->inclusive = FALSE;
3414 oap->start = start_pos;
3415 oap->motion_type = MCHAR;
3416 }
3417 return OK;
3418}
3419
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003420/*
3421 * Find block under the cursor, cursor at end.
3422 * "what" and "other" are two matching parenthesis/paren/etc.
3423 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003424 int
3425current_block(oap, count, include, what, other)
3426 oparg_T *oap;
3427 long count;
3428 int include; /* TRUE == include white space */
3429 int what; /* '(', '{', etc. */
3430 int other; /* ')', '}', etc. */
3431{
3432 pos_T old_pos;
3433 pos_T *pos = NULL;
3434 pos_T start_pos;
3435 pos_T *end_pos;
3436 pos_T old_start, old_end;
3437 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003438 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003439
3440 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003441 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003442 old_start = old_end;
3443
3444 /*
3445 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3446 */
3447#ifdef FEAT_VISUAL
3448 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3449#endif
3450 {
3451 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003452 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 while (inindent(1))
3454 if (inc_cursor() != 0)
3455 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003456 if (gchar_cursor() == what)
3457 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003458 ++curwin->w_cursor.col;
3459 }
3460#ifdef FEAT_VISUAL
3461 else if (lt(VIsual, curwin->w_cursor))
3462 {
3463 old_start = VIsual;
3464 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3465 }
3466 else
3467 old_end = VIsual;
3468#endif
3469
3470 /*
3471 * Search backwards for unclosed '(', '{', etc..
3472 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003473 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003474 */
3475 save_cpo = p_cpo;
3476 p_cpo = (char_u *)"%";
3477 while (count-- > 0)
3478 {
3479 if ((pos = findmatch(NULL, what)) == NULL)
3480 break;
3481 curwin->w_cursor = *pos;
3482 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3483 }
3484 p_cpo = save_cpo;
3485
3486 /*
3487 * Search for matching ')', '}', etc.
3488 * Put this position in curwin->w_cursor.
3489 */
3490 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3491 {
3492 curwin->w_cursor = old_pos;
3493 return FAIL;
3494 }
3495 curwin->w_cursor = *end_pos;
3496
3497 /*
3498 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3499 * If the ending '}' is only preceded by indent, skip that indent.
3500 * But only if the resulting area is not smaller than what we started with.
3501 */
3502 while (!include)
3503 {
3504 incl(&start_pos);
3505 sol = (curwin->w_cursor.col == 0);
3506 decl(&curwin->w_cursor);
3507 if (what == '{')
3508 while (inindent(1))
3509 {
3510 sol = TRUE;
3511 if (decl(&curwin->w_cursor) != 0)
3512 break;
3513 }
3514#ifdef FEAT_VISUAL
3515 /*
3516 * In Visual mode, when the resulting area is not bigger than what we
3517 * started with, extend it to the next block, and then exclude again.
3518 */
3519 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3520 && VIsual_active)
3521 {
3522 curwin->w_cursor = old_start;
3523 decl(&curwin->w_cursor);
3524 if ((pos = findmatch(NULL, what)) == NULL)
3525 {
3526 curwin->w_cursor = old_pos;
3527 return FAIL;
3528 }
3529 start_pos = *pos;
3530 curwin->w_cursor = *pos;
3531 if ((end_pos = findmatch(NULL, other)) == NULL)
3532 {
3533 curwin->w_cursor = old_pos;
3534 return FAIL;
3535 }
3536 curwin->w_cursor = *end_pos;
3537 }
3538 else
3539#endif
3540 break;
3541 }
3542
3543#ifdef FEAT_VISUAL
3544 if (VIsual_active)
3545 {
3546 if (*p_sel == 'e')
3547 ++curwin->w_cursor.col;
3548 if (sol)
3549 inc(&curwin->w_cursor); /* include the line break */
3550 VIsual = start_pos;
3551 VIsual_mode = 'v';
3552 redraw_curbuf_later(INVERTED); /* update the inversion */
3553 showmode();
3554 }
3555 else
3556#endif
3557 {
3558 oap->start = start_pos;
3559 oap->motion_type = MCHAR;
3560 if (sol)
3561 {
3562 incl(&curwin->w_cursor);
3563 oap->inclusive = FALSE;
3564 }
3565 else
3566 oap->inclusive = TRUE;
3567 }
3568
3569 return OK;
3570}
3571
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003572static int in_html_tag __ARGS((int));
3573
3574/*
3575 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3576 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3577 */
3578 static int
3579in_html_tag(end_tag)
3580 int end_tag;
3581{
3582 char_u *line = ml_get_curline();
3583 char_u *p;
3584 int c;
3585 int lc = NUL;
3586 pos_T pos;
3587
3588#ifdef FEAT_MBYTE
3589 if (enc_dbcs)
3590 {
3591 char_u *lp = NULL;
3592
3593 /* We search forward until the cursor, because searching backwards is
3594 * very slow for DBCS encodings. */
3595 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3596 if (*p == '>' || *p == '<')
3597 {
3598 lc = *p;
3599 lp = p;
3600 }
3601 if (*p != '<') /* check for '<' under cursor */
3602 {
3603 if (lc != '<')
3604 return FALSE;
3605 p = lp;
3606 }
3607 }
3608 else
3609#endif
3610 {
3611 for (p = line + curwin->w_cursor.col; p > line; )
3612 {
3613 if (*p == '<') /* find '<' under/before cursor */
3614 break;
3615 mb_ptr_back(line, p);
3616 if (*p == '>') /* find '>' before cursor */
3617 break;
3618 }
3619 if (*p != '<')
3620 return FALSE;
3621 }
3622
3623 pos.lnum = curwin->w_cursor.lnum;
3624 pos.col = p - line;
3625
3626 mb_ptr_adv(p);
3627 if (end_tag)
3628 /* check that there is a '/' after the '<' */
3629 return *p == '/';
3630
3631 /* check that there is no '/' after the '<' */
3632 if (*p == '/')
3633 return FALSE;
3634
3635 /* check that the matching '>' is not preceded by '/' */
3636 for (;;)
3637 {
3638 if (inc(&pos) < 0)
3639 return FALSE;
3640 c = *ml_get_pos(&pos);
3641 if (c == '>')
3642 break;
3643 lc = c;
3644 }
3645 return lc != '/';
3646}
3647
3648/*
3649 * Find tag block under the cursor, cursor at end.
3650 */
3651 int
3652current_tagblock(oap, count_arg, include)
3653 oparg_T *oap;
3654 long count_arg;
3655 int include; /* TRUE == include white space */
3656{
3657 long count = count_arg;
3658 long n;
3659 pos_T old_pos;
3660 pos_T start_pos;
3661 pos_T end_pos;
3662 pos_T old_start, old_end;
3663 char_u *spat, *epat;
3664 char_u *p;
3665 char_u *cp;
3666 int len;
3667 int r;
3668 int do_include = include;
3669 int save_p_ws = p_ws;
3670 int retval = FAIL;
3671
3672 p_ws = FALSE;
3673
3674 old_pos = curwin->w_cursor;
3675 old_end = curwin->w_cursor; /* remember where we started */
3676 old_start = old_end;
3677
3678 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003679 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003680 */
3681#ifdef FEAT_VISUAL
3682 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3683#endif
3684 {
3685 setpcmark();
3686
3687 /* ignore indent */
3688 while (inindent(1))
3689 if (inc_cursor() != 0)
3690 break;
3691
3692 if (in_html_tag(FALSE))
3693 {
3694 /* cursor on start tag, move to just after it */
3695 while (*ml_get_cursor() != '>')
3696 if (inc_cursor() < 0)
3697 break;
3698 }
3699 else if (in_html_tag(TRUE))
3700 {
3701 /* cursor on end tag, move to just before it */
3702 while (*ml_get_cursor() != '<')
3703 if (dec_cursor() < 0)
3704 break;
3705 dec_cursor();
3706 old_end = curwin->w_cursor;
3707 }
3708 }
3709#ifdef FEAT_VISUAL
3710 else if (lt(VIsual, curwin->w_cursor))
3711 {
3712 old_start = VIsual;
3713 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3714 }
3715 else
3716 old_end = VIsual;
3717#endif
3718
3719again:
3720 /*
3721 * Search backwards for unclosed "<aaa>".
3722 * Put this position in start_pos.
3723 */
3724 for (n = 0; n < count; ++n)
3725 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003726 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003727 (char_u *)"",
3728 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0) <= 0)
3729 {
3730 curwin->w_cursor = old_pos;
3731 goto theend;
3732 }
3733 }
3734 start_pos = curwin->w_cursor;
3735
3736 /*
3737 * Search for matching "</aaa>". First isolate the "aaa".
3738 */
3739 inc_cursor();
3740 p = ml_get_cursor();
3741 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3742 ;
3743 len = cp - p;
3744 if (len == 0)
3745 {
3746 curwin->w_cursor = old_pos;
3747 goto theend;
3748 }
3749 spat = alloc(len + 29);
3750 epat = alloc(len + 9);
3751 if (spat == NULL || epat == NULL)
3752 {
3753 vim_free(spat);
3754 vim_free(epat);
3755 curwin->w_cursor = old_pos;
3756 goto theend;
3757 }
3758 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3759 sprintf((char *)epat, "</%.*s>\\c", len, p);
3760
3761 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"", 0);
3762
3763 vim_free(spat);
3764 vim_free(epat);
3765
3766 if (r < 1 || lt(curwin->w_cursor, old_end))
3767 {
3768 /* Can't find other end or it's before the previous end. Could be a
3769 * HTML tag that doesn't have a matching end. Search backwards for
3770 * another starting tag. */
3771 count = 1;
3772 curwin->w_cursor = start_pos;
3773 goto again;
3774 }
3775
3776 if (do_include || r < 1)
3777 {
3778 /* Include up to the '>'. */
3779 while (*ml_get_cursor() != '>')
3780 if (inc_cursor() < 0)
3781 break;
3782 }
3783 else
3784 {
3785 /* Exclude the '<' of the end tag. */
3786 if (*ml_get_cursor() == '<')
3787 dec_cursor();
3788 }
3789 end_pos = curwin->w_cursor;
3790
3791 if (!do_include)
3792 {
3793 /* Exclude the start tag. */
3794 curwin->w_cursor = start_pos;
3795 while (inc_cursor() >= 0)
3796 if (*ml_get_cursor() == '>' && lt(curwin->w_cursor, end_pos))
3797 {
3798 inc_cursor();
3799 start_pos = curwin->w_cursor;
3800 break;
3801 }
3802 curwin->w_cursor = end_pos;
3803
Bram Moolenaar45360022005-07-21 21:08:21 +00003804 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003805 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003806 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003807 {
3808 do_include = TRUE;
3809 curwin->w_cursor = old_start;
3810 count = count_arg;
3811 goto again;
3812 }
3813 }
3814
3815#ifdef FEAT_VISUAL
3816 if (VIsual_active)
3817 {
3818 if (*p_sel == 'e')
3819 ++curwin->w_cursor.col;
3820 VIsual = start_pos;
3821 VIsual_mode = 'v';
3822 redraw_curbuf_later(INVERTED); /* update the inversion */
3823 showmode();
3824 }
3825 else
3826#endif
3827 {
3828 oap->start = start_pos;
3829 oap->motion_type = MCHAR;
3830 oap->inclusive = TRUE;
3831 }
3832 retval = OK;
3833
3834theend:
3835 p_ws = save_p_ws;
3836 return retval;
3837}
3838
Bram Moolenaar071d4272004-06-13 20:20:40 +00003839 int
3840current_par(oap, count, include, type)
3841 oparg_T *oap;
3842 long count;
3843 int include; /* TRUE == include white space */
3844 int type; /* 'p' for paragraph, 'S' for section */
3845{
3846 linenr_T start_lnum;
3847 linenr_T end_lnum;
3848 int white_in_front;
3849 int dir;
3850 int start_is_white;
3851 int prev_start_is_white;
3852 int retval = OK;
3853 int do_white = FALSE;
3854 int t;
3855 int i;
3856
3857 if (type == 'S') /* not implemented yet */
3858 return FAIL;
3859
3860 start_lnum = curwin->w_cursor.lnum;
3861
3862#ifdef FEAT_VISUAL
3863 /*
3864 * When visual area is more than one line: extend it.
3865 */
3866 if (VIsual_active && start_lnum != VIsual.lnum)
3867 {
3868extend:
3869 if (start_lnum < VIsual.lnum)
3870 dir = BACKWARD;
3871 else
3872 dir = FORWARD;
3873 for (i = count; --i >= 0; )
3874 {
3875 if (start_lnum ==
3876 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3877 {
3878 retval = FAIL;
3879 break;
3880 }
3881
3882 prev_start_is_white = -1;
3883 for (t = 0; t < 2; ++t)
3884 {
3885 start_lnum += dir;
3886 start_is_white = linewhite(start_lnum);
3887 if (prev_start_is_white == start_is_white)
3888 {
3889 start_lnum -= dir;
3890 break;
3891 }
3892 for (;;)
3893 {
3894 if (start_lnum == (dir == BACKWARD
3895 ? 1 : curbuf->b_ml.ml_line_count))
3896 break;
3897 if (start_is_white != linewhite(start_lnum + dir)
3898 || (!start_is_white
3899 && startPS(start_lnum + (dir > 0
3900 ? 1 : 0), 0, 0)))
3901 break;
3902 start_lnum += dir;
3903 }
3904 if (!include)
3905 break;
3906 if (start_lnum == (dir == BACKWARD
3907 ? 1 : curbuf->b_ml.ml_line_count))
3908 break;
3909 prev_start_is_white = start_is_white;
3910 }
3911 }
3912 curwin->w_cursor.lnum = start_lnum;
3913 curwin->w_cursor.col = 0;
3914 return retval;
3915 }
3916#endif
3917
3918 /*
3919 * First move back to the start_lnum of the paragraph or white lines
3920 */
3921 white_in_front = linewhite(start_lnum);
3922 while (start_lnum > 1)
3923 {
3924 if (white_in_front) /* stop at first white line */
3925 {
3926 if (!linewhite(start_lnum - 1))
3927 break;
3928 }
3929 else /* stop at first non-white line of start of paragraph */
3930 {
3931 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
3932 break;
3933 }
3934 --start_lnum;
3935 }
3936
3937 /*
3938 * Move past the end of any white lines.
3939 */
3940 end_lnum = start_lnum;
3941 while (linewhite(end_lnum) && end_lnum < curbuf->b_ml.ml_line_count)
3942 ++end_lnum;
3943
3944 --end_lnum;
3945 i = count;
3946 if (!include && white_in_front)
3947 --i;
3948 while (i--)
3949 {
3950 if (end_lnum == curbuf->b_ml.ml_line_count)
3951 return FAIL;
3952
3953 if (!include)
3954 do_white = linewhite(end_lnum + 1);
3955
3956 if (include || !do_white)
3957 {
3958 ++end_lnum;
3959 /*
3960 * skip to end of paragraph
3961 */
3962 while (end_lnum < curbuf->b_ml.ml_line_count
3963 && !linewhite(end_lnum + 1)
3964 && !startPS(end_lnum + 1, 0, 0))
3965 ++end_lnum;
3966 }
3967
3968 if (i == 0 && white_in_front && include)
3969 break;
3970
3971 /*
3972 * skip to end of white lines after paragraph
3973 */
3974 if (include || do_white)
3975 while (end_lnum < curbuf->b_ml.ml_line_count
3976 && linewhite(end_lnum + 1))
3977 ++end_lnum;
3978 }
3979
3980 /*
3981 * If there are no empty lines at the end, try to find some empty lines at
3982 * the start (unless that has been done already).
3983 */
3984 if (!white_in_front && !linewhite(end_lnum) && include)
3985 while (start_lnum > 1 && linewhite(start_lnum - 1))
3986 --start_lnum;
3987
3988#ifdef FEAT_VISUAL
3989 if (VIsual_active)
3990 {
3991 /* Problem: when doing "Vipipip" nothing happens in a single white
3992 * line, we get stuck there. Trap this here. */
3993 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
3994 goto extend;
3995 VIsual.lnum = start_lnum;
3996 VIsual_mode = 'V';
3997 redraw_curbuf_later(INVERTED); /* update the inversion */
3998 showmode();
3999 }
4000 else
4001#endif
4002 {
4003 oap->start.lnum = start_lnum;
4004 oap->start.col = 0;
4005 oap->motion_type = MLINE;
4006 }
4007 curwin->w_cursor.lnum = end_lnum;
4008 curwin->w_cursor.col = 0;
4009
4010 return OK;
4011}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004012
4013static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4014static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4015
4016/*
4017 * Search quote char from string line[col].
4018 * Quote character escaped by one of the characters in "escape" is not counted
4019 * as a quote.
4020 * Returns column number of "quotechar" or -1 when not found.
4021 */
4022 static int
4023find_next_quote(line, col, quotechar, escape)
4024 char_u *line;
4025 int col;
4026 int quotechar;
4027 char_u *escape; /* escape characters, can be NULL */
4028{
4029 int c;
4030
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004031 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004032 {
4033 c = line[col];
4034 if (c == NUL)
4035 return -1;
4036 else if (escape != NULL && vim_strchr(escape, c))
4037 ++col;
4038 else if (c == quotechar)
4039 break;
4040#ifdef FEAT_MBYTE
4041 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004042 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004043 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004044#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004045 ++col;
4046 }
4047 return col;
4048}
4049
4050/*
4051 * Search backwards in "line" from column "col_start" to find "quotechar".
4052 * Quote character escaped by one of the characters in "escape" is not counted
4053 * as a quote.
4054 * Return the found column or zero.
4055 */
4056 static int
4057find_prev_quote(line, col_start, quotechar, escape)
4058 char_u *line;
4059 int col_start;
4060 int quotechar;
4061 char_u *escape; /* escape characters, can be NULL */
4062{
4063 int n;
4064
4065 while (col_start > 0)
4066 {
4067 --col_start;
4068#ifdef FEAT_MBYTE
4069 col_start -= (*mb_head_off)(line, line + col_start);
4070#endif
4071 n = 0;
4072 if (escape != NULL)
4073 while (col_start - n > 0 && vim_strchr(escape,
4074 line[col_start - n - 1]) != NULL)
4075 ++n;
4076 if (n & 1)
4077 col_start -= n; /* uneven number of escape chars, skip it */
4078 else if (line[col_start] == quotechar)
4079 break;
4080 }
4081 return col_start;
4082}
4083
4084/*
4085 * Find quote under the cursor, cursor at end.
4086 * Returns TRUE if found, else FALSE.
4087 */
4088 int
4089current_quote(oap, count, include, quotechar)
4090 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004091 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004092 int include; /* TRUE == include quote char */
4093 int quotechar; /* Quote character */
4094{
4095 char_u *line = ml_get_curline();
4096 int col_end;
4097 int col_start = curwin->w_cursor.col;
4098 int inclusive = FALSE;
4099#ifdef FEAT_VISUAL
4100 int vis_empty = TRUE; /* Visual selection <= 1 char */
4101 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004102 int inside_quotes = FALSE; /* Looks like "i'" done before */
4103 int selected_quote = FALSE; /* Has quote inside selection */
4104 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004105
4106 /* Correct cursor when 'selection' is exclusive */
4107 if (VIsual_active)
4108 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004109 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004110 if (*p_sel == 'e' && vis_bef_curs)
4111 dec_cursor();
4112 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004113 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004114
4115 if (!vis_empty)
4116 {
4117 /* Check if the existing selection exactly spans the text inside
4118 * quotes. */
4119 if (vis_bef_curs)
4120 {
4121 inside_quotes = VIsual.col > 0
4122 && line[VIsual.col - 1] == quotechar
4123 && line[curwin->w_cursor.col] != NUL
4124 && line[curwin->w_cursor.col + 1] == quotechar;
4125 i = VIsual.col;
4126 col_end = curwin->w_cursor.col;
4127 }
4128 else
4129 {
4130 inside_quotes = curwin->w_cursor.col > 0
4131 && line[curwin->w_cursor.col - 1] == quotechar
4132 && line[VIsual.col] != NUL
4133 && line[VIsual.col + 1] == quotechar;
4134 i = curwin->w_cursor.col;
4135 col_end = VIsual.col;
4136 }
4137
4138 /* Find out if we have a quote in the selection. */
4139 while (i <= col_end)
4140 if (line[i++] == quotechar)
4141 {
4142 selected_quote = TRUE;
4143 break;
4144 }
4145 }
4146
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004147 if (!vis_empty && line[col_start] == quotechar)
4148 {
4149 /* Already selecting something and on a quote character. Find the
4150 * next quoted string. */
4151 if (vis_bef_curs)
4152 {
4153 /* Assume we are on a closing quote: move to after the next
4154 * opening quote. */
4155 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4156 if (col_start < 0)
4157 return FALSE;
4158 col_end = find_next_quote(line, col_start + 1, quotechar,
4159 curbuf->b_p_qe);
4160 if (col_end < 0)
4161 {
4162 /* We were on a starting quote perhaps? */
4163 col_end = col_start;
4164 col_start = curwin->w_cursor.col;
4165 }
4166 }
4167 else
4168 {
4169 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4170 if (line[col_end] != quotechar)
4171 return FALSE;
4172 col_start = find_prev_quote(line, col_end, quotechar,
4173 curbuf->b_p_qe);
4174 if (line[col_start] != quotechar)
4175 {
4176 /* We were on an ending quote perhaps? */
4177 col_start = col_end;
4178 col_end = curwin->w_cursor.col;
4179 }
4180 }
4181 }
4182 else
4183#endif
4184
4185 if (line[col_start] == quotechar
4186#ifdef FEAT_VISUAL
4187 || !vis_empty
4188#endif
4189 )
4190 {
4191 int first_col = col_start;
4192
4193#ifdef FEAT_VISUAL
4194 if (!vis_empty)
4195 {
4196 if (vis_bef_curs)
4197 first_col = find_next_quote(line, col_start, quotechar, NULL);
4198 else
4199 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4200 }
4201#endif
4202 /* The cursor is on a quote, we don't know if it's the opening or
4203 * closing quote. Search from the start of the line to find out.
4204 * Also do this when there is a Visual area, a' may leave the cursor
4205 * in between two strings. */
4206 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004207 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004208 {
4209 /* Find open quote character. */
4210 col_start = find_next_quote(line, col_start, quotechar, NULL);
4211 if (col_start < 0 || col_start > first_col)
4212 return FALSE;
4213 /* Find close quote character. */
4214 col_end = find_next_quote(line, col_start + 1, quotechar,
4215 curbuf->b_p_qe);
4216 if (col_end < 0)
4217 return FALSE;
4218 /* If is cursor between start and end quote character, it is
4219 * target text object. */
4220 if (col_start <= first_col && first_col <= col_end)
4221 break;
4222 col_start = col_end + 1;
4223 }
4224 }
4225 else
4226 {
4227 /* Search backward for a starting quote. */
4228 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4229 if (line[col_start] != quotechar)
4230 {
4231 /* No quote before the cursor, look after the cursor. */
4232 col_start = find_next_quote(line, col_start, quotechar, NULL);
4233 if (col_start < 0)
4234 return FALSE;
4235 }
4236
4237 /* Find close quote character. */
4238 col_end = find_next_quote(line, col_start + 1, quotechar,
4239 curbuf->b_p_qe);
4240 if (col_end < 0)
4241 return FALSE;
4242 }
4243
4244 /* When "include" is TRUE, include spaces after closing quote or before
4245 * the starting quote. */
4246 if (include)
4247 {
4248 if (vim_iswhite(line[col_end + 1]))
4249 while (vim_iswhite(line[col_end + 1]))
4250 ++col_end;
4251 else
4252 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4253 --col_start;
4254 }
4255
Bram Moolenaarab194812005-09-14 21:40:12 +00004256 /* Set start position. After vi" another i" must include the ".
4257 * For v2i" include the quotes. */
4258 if (!include && count < 2
4259#ifdef FEAT_VISUAL
4260 && (vis_empty || !inside_quotes)
4261#endif
4262 )
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004263 ++col_start;
4264 curwin->w_cursor.col = col_start;
4265#ifdef FEAT_VISUAL
4266 if (VIsual_active)
4267 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004268 /* Set the start of the Visual area when the Visual area was empty, we
4269 * were just inside quotes or the Visual area didn't start at a quote
4270 * and didn't include a quote.
4271 */
4272 if (vis_empty
4273 || (vis_bef_curs
4274 && !selected_quote
4275 && (inside_quotes
4276 || (line[VIsual.col] != quotechar
4277 && (VIsual.col == 0
4278 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004279 {
4280 VIsual = curwin->w_cursor;
4281 redraw_curbuf_later(INVERTED);
4282 }
4283 }
4284 else
4285#endif
4286 {
4287 oap->start = curwin->w_cursor;
4288 oap->motion_type = MCHAR;
4289 }
4290
4291 /* Set end position. */
4292 curwin->w_cursor.col = col_end;
Bram Moolenaarab194812005-09-14 21:40:12 +00004293 if ((include || count > 1
4294#ifdef FEAT_VISUAL
4295 /* After vi" another i" must include the ". */
4296 || (!vis_empty && inside_quotes)
4297#endif
4298 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004299 inclusive = TRUE;
4300#ifdef FEAT_VISUAL
4301 if (VIsual_active)
4302 {
4303 if (vis_empty || vis_bef_curs)
4304 {
4305 /* decrement cursor when 'selection' is not exclusive */
4306 if (*p_sel != 'e')
4307 dec_cursor();
4308 }
4309 else
4310 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004311 /* Cursor is at start of Visual area. Set the end of the Visual
4312 * area when it was just inside quotes or it didn't end at a
4313 * quote. */
4314 if (inside_quotes
4315 || (!selected_quote
4316 && line[VIsual.col] != quotechar
4317 && (line[VIsual.col] == NUL
4318 || line[VIsual.col + 1] != quotechar)))
4319 {
4320 dec_cursor();
4321 VIsual = curwin->w_cursor;
4322 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004323 curwin->w_cursor.col = col_start;
4324 }
4325 if (VIsual_mode == 'V')
4326 {
4327 VIsual_mode = 'v';
4328 redraw_cmdline = TRUE; /* show mode later */
4329 }
4330 }
4331 else
4332#endif
4333 {
4334 /* Set inclusive and other oap's flags. */
4335 oap->inclusive = inclusive;
4336 }
4337
4338 return OK;
4339}
4340
4341#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004342
4343#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4344 || defined(PROTO)
4345/*
4346 * return TRUE if line 'lnum' is empty or has white chars only.
4347 */
4348 int
4349linewhite(lnum)
4350 linenr_T lnum;
4351{
4352 char_u *p;
4353
4354 p = skipwhite(ml_get(lnum));
4355 return (*p == NUL);
4356}
4357#endif
4358
4359#if defined(FEAT_FIND_ID) || defined(PROTO)
4360/*
4361 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004362 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004363 */
4364/*ARGSUSED*/
4365 void
4366find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4367 type, count, action, start_lnum, end_lnum)
4368 char_u *ptr; /* pointer to search pattern */
4369 int dir; /* direction of expansion */
4370 int len; /* length of search pattern */
4371 int whole; /* match whole words only */
4372 int skip_comments; /* don't match inside comments */
4373 int type; /* Type of search; are we looking for a type?
4374 a macro? */
4375 long count;
4376 int action; /* What to do when we find it */
4377 linenr_T start_lnum; /* first line to start searching */
4378 linenr_T end_lnum; /* last line for searching */
4379{
4380 SearchedFile *files; /* Stack of included files */
4381 SearchedFile *bigger; /* When we need more space */
4382 int max_path_depth = 50;
4383 long match_count = 1;
4384
4385 char_u *pat;
4386 char_u *new_fname;
4387 char_u *curr_fname = curbuf->b_fname;
4388 char_u *prev_fname = NULL;
4389 linenr_T lnum;
4390 int depth;
4391 int depth_displayed; /* For type==CHECK_PATH */
4392 int old_files;
4393 int already_searched;
4394 char_u *file_line;
4395 char_u *line;
4396 char_u *p;
4397 char_u save_char;
4398 int define_matched;
4399 regmatch_T regmatch;
4400 regmatch_T incl_regmatch;
4401 regmatch_T def_regmatch;
4402 int matched = FALSE;
4403 int did_show = FALSE;
4404 int found = FALSE;
4405 int i;
4406 char_u *already = NULL;
4407 char_u *startp = NULL;
4408#ifdef RISCOS
4409 int previous_munging = __riscosify_control;
4410#endif
4411#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4412 win_T *curwin_save = NULL;
4413#endif
4414
4415 regmatch.regprog = NULL;
4416 incl_regmatch.regprog = NULL;
4417 def_regmatch.regprog = NULL;
4418
4419 file_line = alloc(LSIZE);
4420 if (file_line == NULL)
4421 return;
4422
4423#ifdef RISCOS
4424 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004425 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004426#endif
4427
4428 if (type != CHECK_PATH && type != FIND_DEFINE
4429#ifdef FEAT_INS_EXPAND
4430 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4431 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004432 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004433#endif
4434 )
4435 {
4436 pat = alloc(len + 5);
4437 if (pat == NULL)
4438 goto fpip_end;
4439 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4440 /* ignore case according to p_ic, p_scs and pat */
4441 regmatch.rm_ic = ignorecase(pat);
4442 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4443 vim_free(pat);
4444 if (regmatch.regprog == NULL)
4445 goto fpip_end;
4446 }
4447 if (*curbuf->b_p_inc != NUL || *p_inc != NUL)
4448 {
4449 incl_regmatch.regprog = vim_regcomp(*curbuf->b_p_inc == NUL
4450 ? p_inc : curbuf->b_p_inc, p_magic ? RE_MAGIC : 0);
4451 if (incl_regmatch.regprog == NULL)
4452 goto fpip_end;
4453 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4454 }
4455 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4456 {
4457 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4458 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4459 if (def_regmatch.regprog == NULL)
4460 goto fpip_end;
4461 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4462 }
4463 files = (SearchedFile *)lalloc_clear((long_u)
4464 (max_path_depth * sizeof(SearchedFile)), TRUE);
4465 if (files == NULL)
4466 goto fpip_end;
4467 old_files = max_path_depth;
4468 depth = depth_displayed = -1;
4469
4470 lnum = start_lnum;
4471 if (end_lnum > curbuf->b_ml.ml_line_count)
4472 end_lnum = curbuf->b_ml.ml_line_count;
4473 if (lnum > end_lnum) /* do at least one line */
4474 lnum = end_lnum;
4475 line = ml_get(lnum);
4476
4477 for (;;)
4478 {
4479 if (incl_regmatch.regprog != NULL
4480 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4481 {
4482 new_fname = file_name_in_line(incl_regmatch.endp[0],
4483 0, FNAME_EXP|FNAME_INCL|FNAME_REL, 1L,
4484 curr_fname == curbuf->b_fname
4485 ? curbuf->b_ffname : curr_fname);
4486 already_searched = FALSE;
4487 if (new_fname != NULL)
4488 {
4489 /* Check whether we have already searched in this file */
4490 for (i = 0;; i++)
4491 {
4492 if (i == depth + 1)
4493 i = old_files;
4494 if (i == max_path_depth)
4495 break;
4496 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4497 {
4498 if (type != CHECK_PATH &&
4499 action == ACTION_SHOW_ALL && files[i].matched)
4500 {
4501 msg_putchar('\n'); /* cursor below last one */
4502 if (!got_int) /* don't display if 'q'
4503 typed at "--more--"
4504 mesage */
4505 {
4506 msg_home_replace_hl(new_fname);
4507 MSG_PUTS(_(" (includes previously listed match)"));
4508 prev_fname = NULL;
4509 }
4510 }
4511 vim_free(new_fname);
4512 new_fname = NULL;
4513 already_searched = TRUE;
4514 break;
4515 }
4516 }
4517 }
4518
4519 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4520 || (new_fname == NULL && !already_searched)))
4521 {
4522 if (did_show)
4523 msg_putchar('\n'); /* cursor below last one */
4524 else
4525 {
4526 gotocmdline(TRUE); /* cursor at status line */
4527 MSG_PUTS_TITLE(_("--- Included files "));
4528 if (action != ACTION_SHOW_ALL)
4529 MSG_PUTS_TITLE(_("not found "));
4530 MSG_PUTS_TITLE(_("in path ---\n"));
4531 }
4532 did_show = TRUE;
4533 while (depth_displayed < depth && !got_int)
4534 {
4535 ++depth_displayed;
4536 for (i = 0; i < depth_displayed; i++)
4537 MSG_PUTS(" ");
4538 msg_home_replace(files[depth_displayed].name);
4539 MSG_PUTS(" -->\n");
4540 }
4541 if (!got_int) /* don't display if 'q' typed
4542 for "--more--" message */
4543 {
4544 for (i = 0; i <= depth_displayed; i++)
4545 MSG_PUTS(" ");
4546 if (new_fname != NULL)
4547 {
4548 /* using "new_fname" is more reliable, e.g., when
4549 * 'includeexpr' is set. */
4550 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4551 }
4552 else
4553 {
4554 /*
4555 * Isolate the file name.
4556 * Include the surrounding "" or <> if present.
4557 */
4558 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4559 ;
4560 for (i = 0; vim_isfilec(p[i]); i++)
4561 ;
4562 if (i == 0)
4563 {
4564 /* Nothing found, use the rest of the line. */
4565 p = incl_regmatch.endp[0];
4566 i = STRLEN(p);
4567 }
4568 else
4569 {
4570 if (p[-1] == '"' || p[-1] == '<')
4571 {
4572 --p;
4573 ++i;
4574 }
4575 if (p[i] == '"' || p[i] == '>')
4576 ++i;
4577 }
4578 save_char = p[i];
4579 p[i] = NUL;
4580 msg_outtrans_attr(p, hl_attr(HLF_D));
4581 p[i] = save_char;
4582 }
4583
4584 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4585 {
4586 if (already_searched)
4587 MSG_PUTS(_(" (Already listed)"));
4588 else
4589 MSG_PUTS(_(" NOT FOUND"));
4590 }
4591 }
4592 out_flush(); /* output each line directly */
4593 }
4594
4595 if (new_fname != NULL)
4596 {
4597 /* Push the new file onto the file stack */
4598 if (depth + 1 == old_files)
4599 {
4600 bigger = (SearchedFile *)lalloc((long_u)(
4601 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4602 if (bigger != NULL)
4603 {
4604 for (i = 0; i <= depth; i++)
4605 bigger[i] = files[i];
4606 for (i = depth + 1; i < old_files + max_path_depth; i++)
4607 {
4608 bigger[i].fp = NULL;
4609 bigger[i].name = NULL;
4610 bigger[i].lnum = 0;
4611 bigger[i].matched = FALSE;
4612 }
4613 for (i = old_files; i < max_path_depth; i++)
4614 bigger[i + max_path_depth] = files[i];
4615 old_files += max_path_depth;
4616 max_path_depth *= 2;
4617 vim_free(files);
4618 files = bigger;
4619 }
4620 }
4621 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4622 == NULL)
4623 vim_free(new_fname);
4624 else
4625 {
4626 if (++depth == old_files)
4627 {
4628 /*
4629 * lalloc() for 'bigger' must have failed above. We
4630 * will forget one of our already visited files now.
4631 */
4632 vim_free(files[old_files].name);
4633 ++old_files;
4634 }
4635 files[depth].name = curr_fname = new_fname;
4636 files[depth].lnum = 0;
4637 files[depth].matched = FALSE;
4638#ifdef FEAT_INS_EXPAND
4639 if (action == ACTION_EXPAND)
4640 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004641 vim_snprintf((char*)IObuff, IOSIZE,
4642 _("Scanning included file: %s"),
4643 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004644 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4645 }
4646#endif
4647 }
4648 }
4649 }
4650 else
4651 {
4652 /*
4653 * Check if the line is a define (type == FIND_DEFINE)
4654 */
4655 p = line;
4656search_line:
4657 define_matched = FALSE;
4658 if (def_regmatch.regprog != NULL
4659 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4660 {
4661 /*
4662 * Pattern must be first identifier after 'define', so skip
4663 * to that position before checking for match of pattern. Also
4664 * don't let it match beyond the end of this identifier.
4665 */
4666 p = def_regmatch.endp[0];
4667 while (*p && !vim_iswordc(*p))
4668 p++;
4669 define_matched = TRUE;
4670 }
4671
4672 /*
4673 * Look for a match. Don't do this if we are looking for a
4674 * define and this line didn't match define_prog above.
4675 */
4676 if (def_regmatch.regprog == NULL || define_matched)
4677 {
4678 if (define_matched
4679#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004680 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004681#endif
4682 )
4683 {
4684 /* compare the first "len" chars from "ptr" */
4685 startp = skipwhite(p);
4686 if (p_ic)
4687 matched = !MB_STRNICMP(startp, ptr, len);
4688 else
4689 matched = !STRNCMP(startp, ptr, len);
4690 if (matched && define_matched && whole
4691 && vim_iswordc(startp[len]))
4692 matched = FALSE;
4693 }
4694 else if (regmatch.regprog != NULL
4695 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4696 {
4697 matched = TRUE;
4698 startp = regmatch.startp[0];
4699 /*
4700 * Check if the line is not a comment line (unless we are
4701 * looking for a define). A line starting with "# define"
4702 * is not considered to be a comment line.
4703 */
4704 if (!define_matched && skip_comments)
4705 {
4706#ifdef FEAT_COMMENTS
4707 if ((*line != '#' ||
4708 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4709 && get_leader_len(line, NULL, FALSE))
4710 matched = FALSE;
4711
4712 /*
4713 * Also check for a "/ *" or "/ /" before the match.
4714 * Skips lines like "int backwards; / * normal index
4715 * * /" when looking for "normal".
4716 * Note: Doesn't skip "/ *" in comments.
4717 */
4718 p = skipwhite(line);
4719 if (matched
4720 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4721#endif
4722 for (p = line; *p && p < startp; ++p)
4723 {
4724 if (matched
4725 && p[0] == '/'
4726 && (p[1] == '*' || p[1] == '/'))
4727 {
4728 matched = FALSE;
4729 /* After "//" all text is comment */
4730 if (p[1] == '/')
4731 break;
4732 ++p;
4733 }
4734 else if (!matched && p[0] == '*' && p[1] == '/')
4735 {
4736 /* Can find match after "* /". */
4737 matched = TRUE;
4738 ++p;
4739 }
4740 }
4741 }
4742 }
4743 }
4744 }
4745 if (matched)
4746 {
4747#ifdef FEAT_INS_EXPAND
4748 if (action == ACTION_EXPAND)
4749 {
4750 int reuse = 0;
4751 int add_r;
4752 char_u *aux;
4753
4754 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4755 break;
4756 found = TRUE;
4757 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004758 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004759 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004760 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004761 if (vim_iswordp(p))
4762 goto exit_matched;
4763 p = find_word_start(p);
4764 }
4765 p = find_word_end(p);
4766 i = (int)(p - aux);
4767
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004768 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004769 {
4770 /* get the next line */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004771 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004772 STRNCPY(IObuff, aux, i);
4773 if (!( depth < 0
4774 && lnum < end_lnum
4775 && (line = ml_get(++lnum)) != NULL)
4776 && !( depth >= 0
4777 && !vim_fgets(line = file_line,
4778 LSIZE, files[depth].fp)))
4779 goto exit_matched;
4780
4781 /* we read a line, set "already" to check this "line" later
4782 * if depth >= 0 we'll increase files[depth].lnum far
4783 * bellow -- Acevedo */
4784 already = aux = p = skipwhite(line);
4785 p = find_word_start(p);
4786 p = find_word_end(p);
4787 if (p > aux)
4788 {
4789 if (*aux != ')' && IObuff[i-1] != TAB)
4790 {
4791 if (IObuff[i-1] != ' ')
4792 IObuff[i++] = ' ';
4793 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4794 if (p_js
4795 && (IObuff[i-2] == '.'
4796 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4797 && (IObuff[i-2] == '?'
4798 || IObuff[i-2] == '!'))))
4799 IObuff[i++] = ' ';
4800 }
4801 /* copy as much as posible of the new word */
4802 if (p - aux >= IOSIZE - i)
4803 p = aux + IOSIZE - i - 1;
4804 STRNCPY(IObuff + i, aux, p - aux);
4805 i += (int)(p - aux);
4806 reuse |= CONT_S_IPOS;
4807 }
4808 IObuff[i] = NUL;
4809 aux = IObuff;
4810
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 goto exit_matched;
4813 }
4814
4815 add_r = ins_compl_add_infercase(aux, i,
4816 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4817 dir, reuse);
4818 if (add_r == OK)
4819 /* if dir was BACKWARD then honor it just once */
4820 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004821 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004822 break;
4823 }
4824 else
4825#endif
4826 if (action == ACTION_SHOW_ALL)
4827 {
4828 found = TRUE;
4829 if (!did_show)
4830 gotocmdline(TRUE); /* cursor at status line */
4831 if (curr_fname != prev_fname)
4832 {
4833 if (did_show)
4834 msg_putchar('\n'); /* cursor below last one */
4835 if (!got_int) /* don't display if 'q' typed
4836 at "--more--" mesage */
4837 msg_home_replace_hl(curr_fname);
4838 prev_fname = curr_fname;
4839 }
4840 did_show = TRUE;
4841 if (!got_int)
4842 show_pat_in_path(line, type, TRUE, action,
4843 (depth == -1) ? NULL : files[depth].fp,
4844 (depth == -1) ? &lnum : &files[depth].lnum,
4845 match_count++);
4846
4847 /* Set matched flag for this file and all the ones that
4848 * include it */
4849 for (i = 0; i <= depth; ++i)
4850 files[i].matched = TRUE;
4851 }
4852 else if (--count <= 0)
4853 {
4854 found = TRUE;
4855 if (depth == -1 && lnum == curwin->w_cursor.lnum
4856#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4857 && g_do_tagpreview == 0
4858#endif
4859 )
4860 EMSG(_("E387: Match is on current line"));
4861 else if (action == ACTION_SHOW)
4862 {
4863 show_pat_in_path(line, type, did_show, action,
4864 (depth == -1) ? NULL : files[depth].fp,
4865 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4866 did_show = TRUE;
4867 }
4868 else
4869 {
4870#ifdef FEAT_GUI
4871 need_mouse_correct = TRUE;
4872#endif
4873#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4874 /* ":psearch" uses the preview window */
4875 if (g_do_tagpreview != 0)
4876 {
4877 curwin_save = curwin;
4878 prepare_tagpreview();
4879 }
4880#endif
4881 if (action == ACTION_SPLIT)
4882 {
4883#ifdef FEAT_WINDOWS
4884 if (win_split(0, 0) == FAIL)
4885#endif
4886 break;
4887#ifdef FEAT_SCROLLBIND
4888 curwin->w_p_scb = FALSE;
4889#endif
4890 }
4891 if (depth == -1)
4892 {
4893 /* match in current file */
4894#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4895 if (g_do_tagpreview != 0)
4896 {
4897 if (getfile(0, curwin_save->w_buffer->b_fname,
4898 NULL, TRUE, lnum, FALSE) > 0)
4899 break; /* failed to jump to file */
4900 }
4901 else
4902#endif
4903 setpcmark();
4904 curwin->w_cursor.lnum = lnum;
4905 }
4906 else
4907 {
4908 if (getfile(0, files[depth].name, NULL, TRUE,
4909 files[depth].lnum, FALSE) > 0)
4910 break; /* failed to jump to file */
4911 /* autocommands may have changed the lnum, we don't
4912 * want that here */
4913 curwin->w_cursor.lnum = files[depth].lnum;
4914 }
4915 }
4916 if (action != ACTION_SHOW)
4917 {
4918 curwin->w_cursor.col = (colnr_T) (startp - line);
4919 curwin->w_set_curswant = TRUE;
4920 }
4921
4922#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4923 if (g_do_tagpreview != 0
4924 && curwin != curwin_save && win_valid(curwin_save))
4925 {
4926 /* Return cursor to where we were */
4927 validate_cursor();
4928 redraw_later(VALID);
4929 win_enter(curwin_save, TRUE);
4930 }
4931#endif
4932 break;
4933 }
4934#ifdef FEAT_INS_EXPAND
4935exit_matched:
4936#endif
4937 matched = FALSE;
4938 /* look for other matches in the rest of the line if we
4939 * are not at the end of it already */
4940 if (def_regmatch.regprog == NULL
4941#ifdef FEAT_INS_EXPAND
4942 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004943 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004944#endif
4945 && *(p = startp + 1))
4946 goto search_line;
4947 }
4948 line_breakcheck();
4949#ifdef FEAT_INS_EXPAND
4950 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00004951 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004952 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004953#else
4954 if (got_int)
4955#endif
4956 break;
4957
4958 /*
4959 * Read the next line. When reading an included file and encountering
4960 * end-of-file, close the file and continue in the file that included
4961 * it.
4962 */
4963 while (depth >= 0 && !already
4964 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
4965 {
4966 fclose(files[depth].fp);
4967 --old_files;
4968 files[old_files].name = files[depth].name;
4969 files[old_files].matched = files[depth].matched;
4970 --depth;
4971 curr_fname = (depth == -1) ? curbuf->b_fname
4972 : files[depth].name;
4973 if (depth < depth_displayed)
4974 depth_displayed = depth;
4975 }
4976 if (depth >= 0) /* we could read the line */
4977 files[depth].lnum++;
4978 else if (!already)
4979 {
4980 if (++lnum > end_lnum)
4981 break;
4982 line = ml_get(lnum);
4983 }
4984 already = NULL;
4985 }
4986 /* End of big for (;;) loop. */
4987
4988 /* Close any files that are still open. */
4989 for (i = 0; i <= depth; i++)
4990 {
4991 fclose(files[i].fp);
4992 vim_free(files[i].name);
4993 }
4994 for (i = old_files; i < max_path_depth; i++)
4995 vim_free(files[i].name);
4996 vim_free(files);
4997
4998 if (type == CHECK_PATH)
4999 {
5000 if (!did_show)
5001 {
5002 if (action != ACTION_SHOW_ALL)
5003 MSG(_("All included files were found"));
5004 else
5005 MSG(_("No included files"));
5006 }
5007 }
5008 else if (!found
5009#ifdef FEAT_INS_EXPAND
5010 && action != ACTION_EXPAND
5011#endif
5012 )
5013 {
5014#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005015 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005016#else
5017 if (got_int)
5018#endif
5019 EMSG(_(e_interr));
5020 else if (type == FIND_DEFINE)
5021 EMSG(_("E388: Couldn't find definition"));
5022 else
5023 EMSG(_("E389: Couldn't find pattern"));
5024 }
5025 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5026 msg_end();
5027
5028fpip_end:
5029 vim_free(file_line);
5030 vim_free(regmatch.regprog);
5031 vim_free(incl_regmatch.regprog);
5032 vim_free(def_regmatch.regprog);
5033
5034#ifdef RISCOS
5035 /* Restore previous file munging state. */
5036 __riscosify_control = previous_munging;
5037#endif
5038}
5039
5040 static void
5041show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5042 char_u *line;
5043 int type;
5044 int did_show;
5045 int action;
5046 FILE *fp;
5047 linenr_T *lnum;
5048 long count;
5049{
5050 char_u *p;
5051
5052 if (did_show)
5053 msg_putchar('\n'); /* cursor below last one */
5054 else
5055 gotocmdline(TRUE); /* cursor at status line */
5056 if (got_int) /* 'q' typed at "--more--" message */
5057 return;
5058 for (;;)
5059 {
5060 p = line + STRLEN(line) - 1;
5061 if (fp != NULL)
5062 {
5063 /* We used fgets(), so get rid of newline at end */
5064 if (p >= line && *p == '\n')
5065 --p;
5066 if (p >= line && *p == '\r')
5067 --p;
5068 *(p + 1) = NUL;
5069 }
5070 if (action == ACTION_SHOW_ALL)
5071 {
5072 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5073 msg_puts(IObuff);
5074 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5075 /* Highlight line numbers */
5076 msg_puts_attr(IObuff, hl_attr(HLF_N));
5077 MSG_PUTS(" ");
5078 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005079 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005080 out_flush(); /* show one line at a time */
5081
5082 /* Definition continues until line that doesn't end with '\' */
5083 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5084 break;
5085
5086 if (fp != NULL)
5087 {
5088 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5089 break;
5090 ++*lnum;
5091 }
5092 else
5093 {
5094 if (++*lnum > curbuf->b_ml.ml_line_count)
5095 break;
5096 line = ml_get(*lnum);
5097 }
5098 msg_putchar('\n');
5099 }
5100}
5101#endif
5102
5103#ifdef FEAT_VIMINFO
5104 int
5105read_viminfo_search_pattern(virp, force)
5106 vir_T *virp;
5107 int force;
5108{
5109 char_u *lp;
5110 int idx = -1;
5111 int magic = FALSE;
5112 int no_scs = FALSE;
5113 int off_line = FALSE;
5114 int off_end = FALSE;
5115 long off = 0;
5116 int setlast = FALSE;
5117#ifdef FEAT_SEARCH_EXTRA
5118 static int hlsearch_on = FALSE;
5119#endif
5120 char_u *val;
5121
5122 /*
5123 * Old line types:
5124 * "/pat", "&pat": search/subst. pat
5125 * "~/pat", "~&pat": last used search/subst. pat
5126 * New line types:
5127 * "~h", "~H": hlsearch highlighting off/on
5128 * "~<magic><smartcase><line><end><off><last><which>pat"
5129 * <magic>: 'm' off, 'M' on
5130 * <smartcase>: 's' off, 'S' on
5131 * <line>: 'L' line offset, 'l' char offset
5132 * <end>: 'E' from end, 'e' from start
5133 * <off>: decimal, offset
5134 * <last>: '~' last used pattern
5135 * <which>: '/' search pat, '&' subst. pat
5136 */
5137 lp = virp->vir_line;
5138 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5139 {
5140 if (lp[1] == 'M') /* magic on */
5141 magic = TRUE;
5142 if (lp[2] == 's')
5143 no_scs = TRUE;
5144 if (lp[3] == 'L')
5145 off_line = TRUE;
5146 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005147 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005148 lp += 5;
5149 off = getdigits(&lp);
5150 }
5151 if (lp[0] == '~') /* use this pattern for last-used pattern */
5152 {
5153 setlast = TRUE;
5154 lp++;
5155 }
5156 if (lp[0] == '/')
5157 idx = RE_SEARCH;
5158 else if (lp[0] == '&')
5159 idx = RE_SUBST;
5160#ifdef FEAT_SEARCH_EXTRA
5161 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5162 hlsearch_on = FALSE;
5163 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5164 hlsearch_on = TRUE;
5165#endif
5166 if (idx >= 0)
5167 {
5168 if (force || spats[idx].pat == NULL)
5169 {
5170 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5171 TRUE);
5172 if (val != NULL)
5173 {
5174 set_last_search_pat(val, idx, magic, setlast);
5175 vim_free(val);
5176 spats[idx].no_scs = no_scs;
5177 spats[idx].off.line = off_line;
5178 spats[idx].off.end = off_end;
5179 spats[idx].off.off = off;
5180#ifdef FEAT_SEARCH_EXTRA
5181 if (setlast)
5182 no_hlsearch = !hlsearch_on;
5183#endif
5184 }
5185 }
5186 }
5187 return viminfo_readline(virp);
5188}
5189
5190 void
5191write_viminfo_search_pattern(fp)
5192 FILE *fp;
5193{
5194 if (get_viminfo_parameter('/') != 0)
5195 {
5196#ifdef FEAT_SEARCH_EXTRA
5197 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5198 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5199#endif
5200 wvsp_one(fp, RE_SEARCH, "", '/');
5201 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5202 }
5203}
5204
5205 static void
5206wvsp_one(fp, idx, s, sc)
5207 FILE *fp; /* file to write to */
5208 int idx; /* spats[] index */
5209 char *s; /* search pat */
5210 int sc; /* dir char */
5211{
5212 if (spats[idx].pat != NULL)
5213 {
5214 fprintf(fp, "\n# Last %sSearch Pattern:\n~", s);
5215 /* off.dir is not stored, it's reset to forward */
5216 fprintf(fp, "%c%c%c%c%ld%s%c",
5217 spats[idx].magic ? 'M' : 'm', /* magic */
5218 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5219 spats[idx].off.line ? 'L' : 'l', /* line offset */
5220 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5221 spats[idx].off.off, /* offset */
5222 last_idx == idx ? "~" : "", /* last used pat */
5223 sc);
5224 viminfo_writestring(fp, spats[idx].pat);
5225 }
5226}
5227#endif /* FEAT_VIMINFO */