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