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