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