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