blob: 035733b4a1b18004fdcc93095d23a3c351b99eb3 [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 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000104#endif
105
106#ifdef FEAT_FIND_ID
107/*
108 * Type used by find_pattern_in_path() to remember which included files have
109 * been searched already.
110 */
111typedef struct SearchedFile
112{
113 FILE *fp; /* File pointer */
114 char_u *name; /* Full name of file */
115 linenr_T lnum; /* Line we were up to in file */
116 int matched; /* Found a match in this file */
117} SearchedFile;
118#endif
119
120/*
121 * translate search pattern for vim_regcomp()
122 *
123 * pat_save == RE_SEARCH: save pat in spats[RE_SEARCH].pat (normal search cmd)
124 * pat_save == RE_SUBST: save pat in spats[RE_SUBST].pat (:substitute command)
125 * pat_save == RE_BOTH: save pat in both patterns (:global command)
126 * pat_use == RE_SEARCH: use previous search pattern if "pat" is NULL
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000127 * pat_use == RE_SUBST: use previous substitute pattern if "pat" is NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +0000128 * pat_use == RE_LAST: use last used pattern if "pat" is NULL
129 * options & SEARCH_HIS: put search string in history
130 * options & SEARCH_KEEP: keep previous search pattern
131 *
132 * returns FAIL if failed, OK otherwise.
133 */
134 int
135search_regcomp(pat, pat_save, pat_use, options, regmatch)
136 char_u *pat;
137 int pat_save;
138 int pat_use;
139 int options;
140 regmmatch_T *regmatch; /* return: pattern and ignore-case flag */
141{
142 int magic;
143 int i;
144
145 rc_did_emsg = FALSE;
146 magic = p_magic;
147
148 /*
149 * If no pattern given, use a previously defined pattern.
150 */
151 if (pat == NULL || *pat == NUL)
152 {
153 if (pat_use == RE_LAST)
154 i = last_idx;
155 else
156 i = pat_use;
157 if (spats[i].pat == NULL) /* pattern was never defined */
158 {
159 if (pat_use == RE_SUBST)
160 EMSG(_(e_nopresub));
161 else
162 EMSG(_(e_noprevre));
163 rc_did_emsg = TRUE;
164 return FAIL;
165 }
166 pat = spats[i].pat;
167 magic = spats[i].magic;
168 no_smartcase = spats[i].no_scs;
169 }
170#ifdef FEAT_CMDHIST
171 else if (options & SEARCH_HIS) /* put new pattern in history */
172 add_to_history(HIST_SEARCH, pat, TRUE, NUL);
173#endif
174
175#ifdef FEAT_RIGHTLEFT
176 if (mr_pattern_alloced)
177 {
178 vim_free(mr_pattern);
179 mr_pattern_alloced = FALSE;
180 }
181
182 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
183 {
184 char_u *rev_pattern;
185
186 rev_pattern = reverse_text(pat);
187 if (rev_pattern == NULL)
188 mr_pattern = pat; /* out of memory, keep normal pattern. */
189 else
190 {
191 mr_pattern = rev_pattern;
192 mr_pattern_alloced = TRUE;
193 }
194 }
195 else
196#endif
197 mr_pattern = pat;
198
199 /*
200 * Save the currently used pattern in the appropriate place,
201 * unless the pattern should not be remembered.
202 */
203 if (!(options & SEARCH_KEEP))
204 {
205 /* search or global command */
206 if (pat_save == RE_SEARCH || pat_save == RE_BOTH)
207 save_re_pat(RE_SEARCH, pat, magic);
208 /* substitute or global command */
209 if (pat_save == RE_SUBST || pat_save == RE_BOTH)
210 save_re_pat(RE_SUBST, pat, magic);
211 }
212
213 regmatch->rmm_ic = ignorecase(pat);
Bram Moolenaar3b56eb32005-07-11 22:40:32 +0000214 regmatch->rmm_maxcol = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000215 regmatch->regprog = vim_regcomp(pat, magic ? RE_MAGIC : 0);
216 if (regmatch->regprog == NULL)
217 return FAIL;
218 return OK;
219}
220
221/*
222 * Get search pattern used by search_regcomp().
223 */
224 char_u *
225get_search_pat()
226{
227 return mr_pattern;
228}
229
Bram Moolenaarabc97732007-08-08 20:49:37 +0000230#if defined(FEAT_RIGHTLEFT) || defined(PROTO)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000231/*
232 * Reverse text into allocated memory.
233 * Returns the allocated string, NULL when out of memory.
234 */
Bram Moolenaarabc97732007-08-08 20:49:37 +0000235 char_u *
Bram Moolenaar071d4272004-06-13 20:20:40 +0000236reverse_text(s)
237 char_u *s;
238{
239 unsigned len;
240 unsigned s_i, rev_i;
241 char_u *rev;
242
243 /*
244 * Reverse the pattern.
245 */
246 len = (unsigned)STRLEN(s);
247 rev = alloc(len + 1);
248 if (rev != NULL)
249 {
250 rev_i = len;
251 for (s_i = 0; s_i < len; ++s_i)
252 {
253# ifdef FEAT_MBYTE
254 if (has_mbyte)
255 {
256 int mb_len;
257
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000258 mb_len = (*mb_ptr2len)(s + s_i);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000259 rev_i -= mb_len;
260 mch_memmove(rev + rev_i, s + s_i, mb_len);
261 s_i += mb_len - 1;
262 }
263 else
264# endif
265 rev[--rev_i] = s[s_i];
266
267 }
268 rev[len] = NUL;
269 }
270 return rev;
271}
272#endif
273
274 static void
275save_re_pat(idx, pat, magic)
276 int idx;
277 char_u *pat;
278 int magic;
279{
280 if (spats[idx].pat != pat)
281 {
282 vim_free(spats[idx].pat);
283 spats[idx].pat = vim_strsave(pat);
284 spats[idx].magic = magic;
285 spats[idx].no_scs = no_smartcase;
286 last_idx = idx;
287#ifdef FEAT_SEARCH_EXTRA
288 /* If 'hlsearch' set and search pat changed: need redraw. */
289 if (p_hls)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000290 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000291 no_hlsearch = FALSE;
292#endif
293 }
294}
295
296#if defined(FEAT_AUTOCMD) || defined(FEAT_EVAL) || defined(PROTO)
297/*
298 * Save the search patterns, so they can be restored later.
299 * Used before/after executing autocommands and user functions.
300 */
301static int save_level = 0;
302
303 void
304save_search_patterns()
305{
306 if (save_level++ == 0)
307 {
308 saved_spats[0] = spats[0];
309 if (spats[0].pat != NULL)
310 saved_spats[0].pat = vim_strsave(spats[0].pat);
311 saved_spats[1] = spats[1];
312 if (spats[1].pat != NULL)
313 saved_spats[1].pat = vim_strsave(spats[1].pat);
314 saved_last_idx = last_idx;
315# ifdef FEAT_SEARCH_EXTRA
316 saved_no_hlsearch = no_hlsearch;
317# endif
318 }
319}
320
321 void
322restore_search_patterns()
323{
324 if (--save_level == 0)
325 {
326 vim_free(spats[0].pat);
327 spats[0] = saved_spats[0];
328 vim_free(spats[1].pat);
329 spats[1] = saved_spats[1];
330 last_idx = saved_last_idx;
331# ifdef FEAT_SEARCH_EXTRA
332 no_hlsearch = saved_no_hlsearch;
333# endif
334 }
335}
336#endif
337
Bram Moolenaarf461c8e2005-06-25 23:04:51 +0000338#if defined(EXITFREE) || defined(PROTO)
339 void
340free_search_patterns()
341{
342 vim_free(spats[0].pat);
343 vim_free(spats[1].pat);
344}
345#endif
346
Bram Moolenaar071d4272004-06-13 20:20:40 +0000347/*
348 * Return TRUE when case should be ignored for search pattern "pat".
349 * Uses the 'ignorecase' and 'smartcase' options.
350 */
351 int
352ignorecase(pat)
353 char_u *pat;
354{
355 char_u *p;
356 int ic;
357
358 ic = p_ic;
359 if (ic && !no_smartcase && p_scs
360#ifdef FEAT_INS_EXPAND
361 && !(ctrl_x_mode && curbuf->b_p_inf)
362#endif
363 )
364 {
365 /* don't ignore case if pattern has uppercase */
366 for (p = pat; *p; )
367 {
368#ifdef FEAT_MBYTE
369 int l;
370
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000371 if (has_mbyte && (l = (*mb_ptr2len)(p)) > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000372 {
373 if (enc_utf8 && utf_isupper(utf_ptr2char(p)))
374 {
375 ic = FALSE;
376 break;
377 }
378 p += l;
379 }
380 else
381#endif
382 if (*p == '\\' && p[1] != NUL) /* skip "\S" et al. */
383 p += 2;
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000384 else if (isupper(*p))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000385 {
386 ic = FALSE;
387 break;
388 }
Bram Moolenaar75c50c42005-06-04 22:06:24 +0000389 else
390 ++p;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000391 }
392 }
393 no_smartcase = FALSE;
394
395 return ic;
396}
397
398 char_u *
399last_search_pat()
400{
401 return spats[last_idx].pat;
402}
403
404/*
405 * Reset search direction to forward. For "gd" and "gD" commands.
406 */
407 void
408reset_search_dir()
409{
410 spats[0].off.dir = '/';
411}
412
413#if defined(FEAT_EVAL) || defined(FEAT_VIMINFO)
414/*
415 * Set the last search pattern. For ":let @/ =" and viminfo.
416 * Also set the saved search pattern, so that this works in an autocommand.
417 */
418 void
419set_last_search_pat(s, idx, magic, setlast)
420 char_u *s;
421 int idx;
422 int magic;
423 int setlast;
424{
425 vim_free(spats[idx].pat);
426 /* An empty string means that nothing should be matched. */
427 if (*s == NUL)
428 spats[idx].pat = NULL;
429 else
430 spats[idx].pat = vim_strsave(s);
431 spats[idx].magic = magic;
432 spats[idx].no_scs = FALSE;
433 spats[idx].off.dir = '/';
434 spats[idx].off.line = FALSE;
435 spats[idx].off.end = FALSE;
436 spats[idx].off.off = 0;
437 if (setlast)
438 last_idx = idx;
439 if (save_level)
440 {
441 vim_free(saved_spats[idx].pat);
442 saved_spats[idx] = spats[0];
443 if (spats[idx].pat == NULL)
444 saved_spats[idx].pat = NULL;
445 else
446 saved_spats[idx].pat = vim_strsave(spats[idx].pat);
447 saved_last_idx = last_idx;
448 }
449# ifdef FEAT_SEARCH_EXTRA
450 /* If 'hlsearch' set and search pat changed: need redraw. */
451 if (p_hls && idx == last_idx && !no_hlsearch)
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +0000452 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000453# endif
454}
455#endif
456
457#ifdef FEAT_SEARCH_EXTRA
458/*
459 * Get a regexp program for the last used search pattern.
460 * This is used for highlighting all matches in a window.
461 * Values returned in regmatch->regprog and regmatch->rmm_ic.
462 */
463 void
464last_pat_prog(regmatch)
465 regmmatch_T *regmatch;
466{
467 if (spats[last_idx].pat == NULL)
468 {
469 regmatch->regprog = NULL;
470 return;
471 }
472 ++emsg_off; /* So it doesn't beep if bad expr */
473 (void)search_regcomp((char_u *)"", 0, last_idx, SEARCH_KEEP, regmatch);
474 --emsg_off;
475}
476#endif
477
478/*
479 * lowest level search function.
480 * Search for 'count'th occurrence of pattern 'pat' in direction 'dir'.
481 * Start at position 'pos' and return the found position in 'pos'.
482 *
483 * if (options & SEARCH_MSG) == 0 don't give any messages
484 * if (options & SEARCH_MSG) == SEARCH_NFMSG don't give 'notfound' messages
485 * if (options & SEARCH_MSG) == SEARCH_MSG give all messages
486 * if (options & SEARCH_HIS) put search pattern in history
487 * if (options & SEARCH_END) return position at end of match
488 * if (options & SEARCH_START) accept match at pos itself
489 * if (options & SEARCH_KEEP) keep previous search pattern
490 * if (options & SEARCH_FOLD) match only once in a closed fold
491 * if (options & SEARCH_PEEK) check for typed char, cancel search
492 *
493 * Return FAIL (zero) for failure, non-zero for success.
494 * When FEAT_EVAL is defined, returns the index of the first matching
495 * subpattern plus one; one if there was none.
496 */
Bram Moolenaar76929292008-01-06 19:07:36 +0000497/*ARGSUSED*/
Bram Moolenaar071d4272004-06-13 20:20:40 +0000498 int
Bram Moolenaar76929292008-01-06 19:07:36 +0000499searchit(win, buf, pos, dir, pat, count, options, pat_use, stop_lnum, tm)
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 Moolenaar76929292008-01-06 19:07:36 +0000510 proftime_T *tm; /* timeout limit or NULL */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000511{
512 int found;
513 linenr_T lnum; /* no init to shut up Apollo cc */
514 regmmatch_T regmatch;
515 char_u *ptr;
516 colnr_T matchcol;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000517 lpos_T endpos;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000518 lpos_T matchpos;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000519 int loop;
520 pos_T start_pos;
521 int at_first_line;
522 int extra_col;
523 int match_ok;
524 long nmatched;
525 int submatch = 0;
Bram Moolenaar280f1262006-01-30 00:14:18 +0000526 int save_called_emsg = called_emsg;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000527#ifdef FEAT_SEARCH_EXTRA
528 int break_loop = FALSE;
529#else
530# define break_loop FALSE
531#endif
532
533 if (search_regcomp(pat, RE_SEARCH, pat_use,
534 (options & (SEARCH_HIS + SEARCH_KEEP)), &regmatch) == FAIL)
535 {
536 if ((options & SEARCH_MSG) && !rc_did_emsg)
537 EMSG2(_("E383: Invalid search string: %s"), mr_pattern);
538 return FAIL;
539 }
540
541 if (options & SEARCH_START)
542 extra_col = 0;
543#ifdef FEAT_MBYTE
544 /* Watch out for the "col" being MAXCOL - 2, used in a closed fold. */
545 else if (has_mbyte && pos->lnum >= 1 && pos->lnum <= buf->b_ml.ml_line_count
546 && pos->col < MAXCOL - 2)
Bram Moolenaarae5bce12005-08-15 21:41:48 +0000547 {
548 ptr = ml_get_buf(buf, pos->lnum, FALSE) + pos->col;
549 if (*ptr == NUL)
550 extra_col = 1;
551 else
552 extra_col = (*mb_ptr2len)(ptr);
553 }
Bram Moolenaar071d4272004-06-13 20:20:40 +0000554#endif
555 else
556 extra_col = 1;
557
Bram Moolenaar280f1262006-01-30 00:14:18 +0000558 /*
559 * find the string
560 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000561 called_emsg = FALSE;
562 do /* loop for count */
563 {
564 start_pos = *pos; /* remember start pos for detecting no match */
565 found = 0; /* default: not found */
566 at_first_line = TRUE; /* default: start in first line */
567 if (pos->lnum == 0) /* correct lnum for when starting in line 0 */
568 {
569 pos->lnum = 1;
570 pos->col = 0;
571 at_first_line = FALSE; /* not in first line now */
572 }
573
574 /*
575 * Start searching in current line, unless searching backwards and
576 * we're in column 0.
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000577 * If we are searching backwards, in column 0, and not including the
578 * current position, gain some efficiency by skipping back a line.
579 * Otherwise begin the search in the current line.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000580 */
Bram Moolenaar7a42fa32007-07-10 11:28:55 +0000581 if (dir == BACKWARD && start_pos.col == 0
582 && (options & SEARCH_START) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000583 {
584 lnum = pos->lnum - 1;
585 at_first_line = FALSE;
586 }
587 else
588 lnum = pos->lnum;
589
590 for (loop = 0; loop <= 1; ++loop) /* loop twice if 'wrapscan' set */
591 {
592 for ( ; lnum > 0 && lnum <= buf->b_ml.ml_line_count;
593 lnum += dir, at_first_line = FALSE)
594 {
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000595 /* Stop after checking "stop_lnum", if it's set. */
596 if (stop_lnum != 0 && (dir == FORWARD
597 ? lnum > stop_lnum : lnum < stop_lnum))
598 break;
Bram Moolenaar76929292008-01-06 19:07:36 +0000599#ifdef FEAT_RELTIME
600 /* Stop after passing the "tm" time limit. */
601 if (tm != NULL && profile_passed_limit(tm))
602 break;
603#endif
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000604
Bram Moolenaar071d4272004-06-13 20:20:40 +0000605 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000606 * Look for a match somewhere in line "lnum".
Bram Moolenaar071d4272004-06-13 20:20:40 +0000607 */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000608 nmatched = vim_regexec_multi(&regmatch, win, buf,
609 lnum, (colnr_T)0);
610 /* Abort searching on an error (e.g., out of stack). */
611 if (called_emsg)
612 break;
613 if (nmatched > 0)
614 {
615 /* match may actually be in another line when using \zs */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000616 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000617 endpos = regmatch.endpos[0];
618# ifdef FEAT_EVAL
619 submatch = first_submatch(&regmatch);
620# endif
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000621 /* Line me be past end of buffer for "\n\zs". */
622 if (lnum + matchpos.lnum > buf->b_ml.ml_line_count)
623 ptr = (char_u *)"";
624 else
625 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000626
627 /*
628 * Forward search in the first line: match should be after
629 * the start position. If not, continue at the end of the
630 * match (this is vi compatible) or on the next char.
631 */
632 if (dir == FORWARD && at_first_line)
633 {
634 match_ok = TRUE;
635 /*
Bram Moolenaar677ee682005-01-27 14:41:15 +0000636 * When the match starts in a next line it's certainly
637 * past the start position.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000638 * When match lands on a NUL the cursor will be put
639 * one back afterwards, compare with that position,
640 * otherwise "/$" will get stuck on end of line.
641 */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000642 while (matchpos.lnum == 0
643 && ((options & SEARCH_END)
644 ? (nmatched == 1
645 && (int)endpos.col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000646 < (int)start_pos.col + extra_col)
Bram Moolenaar677ee682005-01-27 14:41:15 +0000647 : ((int)matchpos.col
648 - (ptr[matchpos.col] == NUL)
649 < (int)start_pos.col + extra_col)))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000650 {
651 /*
652 * If vi-compatible searching, continue at the end
653 * of the match, otherwise continue one position
654 * forward.
655 */
656 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
657 {
658 if (nmatched > 1)
659 {
660 /* end is in next line, thus no match in
661 * this line */
662 match_ok = FALSE;
663 break;
664 }
665 matchcol = endpos.col;
666 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000667 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000668 && ptr[matchcol] != NUL)
669 {
670#ifdef FEAT_MBYTE
671 if (has_mbyte)
672 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000673 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000674 else
675#endif
676 ++matchcol;
677 }
678 }
679 else
680 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000681 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000682 if (ptr[matchcol] != NUL)
683 {
684#ifdef FEAT_MBYTE
685 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000686 matchcol += (*mb_ptr2len)(ptr
Bram Moolenaar071d4272004-06-13 20:20:40 +0000687 + matchcol);
688 else
689#endif
690 ++matchcol;
691 }
692 }
693 if (ptr[matchcol] == NUL
694 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000695 win, buf, lnum + matchpos.lnum,
696 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000697 {
698 match_ok = FALSE;
699 break;
700 }
Bram Moolenaar677ee682005-01-27 14:41:15 +0000701 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000702 endpos = regmatch.endpos[0];
703# ifdef FEAT_EVAL
704 submatch = first_submatch(&regmatch);
705# endif
706
707 /* Need to get the line pointer again, a
708 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000709 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000710 }
711 if (!match_ok)
712 continue;
713 }
714 if (dir == BACKWARD)
715 {
716 /*
717 * Now, if there are multiple matches on this line,
718 * we have to get the last one. Or the last one before
719 * the cursor, if we're on that line.
720 * When putting the new cursor at the end, compare
721 * relative to the end of the match.
722 */
723 match_ok = FALSE;
724 for (;;)
725 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000726 /* Remember a position that is before the start
727 * position, we use it if it's the last match in
728 * the line. Always accept a position after
729 * wrapping around. */
730 if (loop
731 || ((options & SEARCH_END)
732 ? (lnum + regmatch.endpos[0].lnum
733 < start_pos.lnum
734 || (lnum + regmatch.endpos[0].lnum
735 == start_pos.lnum
736 && (int)regmatch.endpos[0].col - 1
Bram Moolenaar071d4272004-06-13 20:20:40 +0000737 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000738 <= (int)start_pos.col))
739 : (lnum + regmatch.startpos[0].lnum
740 < start_pos.lnum
741 || (lnum + regmatch.startpos[0].lnum
742 == start_pos.lnum
743 && (int)regmatch.startpos[0].col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000744 + extra_col
Bram Moolenaar677ee682005-01-27 14:41:15 +0000745 <= (int)start_pos.col))))
Bram Moolenaar071d4272004-06-13 20:20:40 +0000746 {
Bram Moolenaar071d4272004-06-13 20:20:40 +0000747 match_ok = TRUE;
Bram Moolenaar677ee682005-01-27 14:41:15 +0000748 matchpos = regmatch.startpos[0];
Bram Moolenaar071d4272004-06-13 20:20:40 +0000749 endpos = regmatch.endpos[0];
750# ifdef FEAT_EVAL
751 submatch = first_submatch(&regmatch);
752# endif
753 }
754 else
755 break;
756
757 /*
758 * We found a valid match, now check if there is
759 * another one after it.
760 * If vi-compatible searching, continue at the end
761 * of the match, otherwise continue one position
762 * forward.
763 */
764 if (vim_strchr(p_cpo, CPO_SEARCH) != NULL)
765 {
766 if (nmatched > 1)
767 break;
768 matchcol = endpos.col;
769 /* for empty match: advance one char */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000770 if (matchcol == matchpos.col
Bram Moolenaar071d4272004-06-13 20:20:40 +0000771 && ptr[matchcol] != NUL)
772 {
773#ifdef FEAT_MBYTE
774 if (has_mbyte)
775 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000776 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000777 else
778#endif
779 ++matchcol;
780 }
781 }
782 else
783 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000784 /* Stop when the match is in a next line. */
785 if (matchpos.lnum > 0)
786 break;
787 matchcol = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000788 if (ptr[matchcol] != NUL)
789 {
790#ifdef FEAT_MBYTE
791 if (has_mbyte)
792 matchcol +=
Bram Moolenaar0fa313a2005-08-10 21:07:57 +0000793 (*mb_ptr2len)(ptr + matchcol);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000794 else
795#endif
796 ++matchcol;
797 }
798 }
799 if (ptr[matchcol] == NUL
800 || (nmatched = vim_regexec_multi(&regmatch,
Bram Moolenaar677ee682005-01-27 14:41:15 +0000801 win, buf, lnum + matchpos.lnum,
802 matchcol)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000803 break;
804
805 /* Need to get the line pointer again, a
806 * multi-line search may have made it invalid. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000807 ptr = ml_get_buf(buf, lnum + matchpos.lnum, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000808 }
809
810 /*
811 * If there is only a match after the cursor, skip
812 * this match.
813 */
814 if (!match_ok)
815 continue;
816 }
817
818 if (options & SEARCH_END && !(options & SEARCH_NOOF))
819 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000820 pos->lnum = lnum + endpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000821 pos->col = endpos.col - 1;
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000822#ifdef FEAT_MBYTE
823 if (has_mbyte)
824 {
Bram Moolenaarfb7c90c2007-01-16 15:01:41 +0000825 /* 'e' offset may put us just below the last line */
826 if (pos->lnum > buf->b_ml.ml_line_count)
Bram Moolenaar8c471fa2007-01-16 21:14:45 +0000827 ptr = (char_u *)"";
Bram Moolenaarfb7c90c2007-01-16 15:01:41 +0000828 else
829 ptr = ml_get_buf(buf, pos->lnum, FALSE);
Bram Moolenaar910f66f2006-04-05 20:41:53 +0000830 pos->col -= (*mb_head_off)(ptr, ptr + pos->col);
831 }
832#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +0000833 }
834 else
835 {
Bram Moolenaar677ee682005-01-27 14:41:15 +0000836 pos->lnum = lnum + matchpos.lnum;
837 pos->col = matchpos.col;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000838 }
839#ifdef FEAT_VIRTUALEDIT
840 pos->coladd = 0;
841#endif
842 found = 1;
843
844 /* Set variables used for 'incsearch' highlighting. */
Bram Moolenaar677ee682005-01-27 14:41:15 +0000845 search_match_lines = endpos.lnum - matchpos.lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000846 search_match_endcol = endpos.col;
847 break;
848 }
849 line_breakcheck(); /* stop if ctrl-C typed */
850 if (got_int)
851 break;
852
853#ifdef FEAT_SEARCH_EXTRA
854 /* Cancel searching if a character was typed. Used for
855 * 'incsearch'. Don't check too often, that would slowdown
856 * searching too much. */
857 if ((options & SEARCH_PEEK)
858 && ((lnum - pos->lnum) & 0x3f) == 0
859 && char_avail())
860 {
861 break_loop = TRUE;
862 break;
863 }
864#endif
865
866 if (loop && lnum == start_pos.lnum)
867 break; /* if second loop, stop where started */
868 }
869 at_first_line = FALSE;
870
871 /*
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000872 * Stop the search if wrapscan isn't set, "stop_lnum" is
873 * specified, after an interrupt, after a match and after looping
874 * twice.
Bram Moolenaar071d4272004-06-13 20:20:40 +0000875 */
Bram Moolenaara23ccb82006-02-27 00:08:02 +0000876 if (!p_ws || stop_lnum != 0 || got_int || called_emsg
877 || break_loop || found || loop)
Bram Moolenaar071d4272004-06-13 20:20:40 +0000878 break;
879
880 /*
881 * If 'wrapscan' is set we continue at the other end of the file.
882 * If 'shortmess' does not contain 's', we give a message.
883 * This message is also remembered in keep_msg for when the screen
884 * is redrawn. The keep_msg is cleared whenever another message is
885 * written.
886 */
887 if (dir == BACKWARD) /* start second loop at the other end */
Bram Moolenaar071d4272004-06-13 20:20:40 +0000888 lnum = buf->b_ml.ml_line_count;
Bram Moolenaar071d4272004-06-13 20:20:40 +0000889 else
Bram Moolenaar071d4272004-06-13 20:20:40 +0000890 lnum = 1;
Bram Moolenaar92d640f2005-09-05 22:11:52 +0000891 if (!shortmess(SHM_SEARCH) && (options & SEARCH_MSG))
892 give_warning((char_u *)_(dir == BACKWARD
893 ? top_bot_msg : bot_top_msg), TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +0000894 }
895 if (got_int || called_emsg || break_loop)
896 break;
897 }
898 while (--count > 0 && found); /* stop after count matches or no match */
899
900 vim_free(regmatch.regprog);
901
Bram Moolenaar280f1262006-01-30 00:14:18 +0000902 called_emsg |= save_called_emsg;
903
Bram Moolenaar071d4272004-06-13 20:20:40 +0000904 if (!found) /* did not find it */
905 {
906 if (got_int)
907 EMSG(_(e_interr));
908 else if ((options & SEARCH_MSG) == SEARCH_MSG)
909 {
910 if (p_ws)
911 EMSG2(_(e_patnotf2), mr_pattern);
912 else if (lnum == 0)
913 EMSG2(_("E384: search hit TOP without match for: %s"),
914 mr_pattern);
915 else
916 EMSG2(_("E385: search hit BOTTOM without match for: %s"),
917 mr_pattern);
918 }
919 return FAIL;
920 }
921
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000922 /* A pattern like "\n\zs" may go past the last line. */
923 if (pos->lnum > buf->b_ml.ml_line_count)
924 {
925 pos->lnum = buf->b_ml.ml_line_count;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +0000926 pos->col = (int)STRLEN(ml_get_buf(buf, pos->lnum, FALSE));
Bram Moolenaar32466aa2006-02-24 23:53:04 +0000927 if (pos->col > 0)
928 --pos->col;
929 }
930
Bram Moolenaar071d4272004-06-13 20:20:40 +0000931 return submatch + 1;
932}
933
934#ifdef FEAT_EVAL
935/*
936 * Return the number of the first subpat that matched.
937 */
938 static int
939first_submatch(rp)
940 regmmatch_T *rp;
941{
942 int submatch;
943
944 for (submatch = 1; ; ++submatch)
945 {
946 if (rp->startpos[submatch].lnum >= 0)
947 break;
948 if (submatch == 9)
949 {
950 submatch = 0;
951 break;
952 }
953 }
954 return submatch;
955}
956#endif
957
958/*
959 * Highest level string search function.
Bram Moolenaarb8017e72007-05-10 18:59:07 +0000960 * Search for the 'count'th occurrence of pattern 'pat' in direction 'dirc'
Bram Moolenaar071d4272004-06-13 20:20:40 +0000961 * If 'dirc' is 0: use previous dir.
962 * If 'pat' is NULL or empty : use previous string.
963 * If 'options & SEARCH_REV' : go in reverse of previous dir.
964 * If 'options & SEARCH_ECHO': echo the search command and handle options
965 * If 'options & SEARCH_MSG' : may give error message
966 * If 'options & SEARCH_OPT' : interpret optional flags
967 * If 'options & SEARCH_HIS' : put search pattern in history
968 * If 'options & SEARCH_NOOF': don't add offset to position
969 * If 'options & SEARCH_MARK': set previous context mark
970 * If 'options & SEARCH_KEEP': keep previous search pattern
971 * If 'options & SEARCH_START': accept match at curpos itself
972 * If 'options & SEARCH_PEEK': check for typed char, cancel search
973 *
974 * Careful: If spats[0].off.line == TRUE and spats[0].off.off == 0 this
975 * makes the movement linewise without moving the match position.
976 *
977 * return 0 for failure, 1 for found, 2 for found and line offset added
978 */
979 int
980do_search(oap, dirc, pat, count, options)
981 oparg_T *oap; /* can be NULL */
982 int dirc; /* '/' or '?' */
983 char_u *pat;
984 long count;
985 int options;
986{
987 pos_T pos; /* position of the last match */
988 char_u *searchstr;
989 struct soffset old_off;
990 int retval; /* Return value */
991 char_u *p;
992 long c;
993 char_u *dircp;
994 char_u *strcopy = NULL;
995 char_u *ps;
996
997 /*
998 * A line offset is not remembered, this is vi compatible.
999 */
1000 if (spats[0].off.line && vim_strchr(p_cpo, CPO_LINEOFF) != NULL)
1001 {
1002 spats[0].off.line = FALSE;
1003 spats[0].off.off = 0;
1004 }
1005
1006 /*
1007 * Save the values for when (options & SEARCH_KEEP) is used.
1008 * (there is no "if ()" around this because gcc wants them initialized)
1009 */
1010 old_off = spats[0].off;
1011
1012 pos = curwin->w_cursor; /* start searching at the cursor position */
1013
1014 /*
1015 * Find out the direction of the search.
1016 */
1017 if (dirc == 0)
1018 dirc = spats[0].off.dir;
1019 else
1020 spats[0].off.dir = dirc;
1021 if (options & SEARCH_REV)
1022 {
1023#ifdef WIN32
1024 /* There is a bug in the Visual C++ 2.2 compiler which means that
1025 * dirc always ends up being '/' */
1026 dirc = (dirc == '/') ? '?' : '/';
1027#else
1028 if (dirc == '/')
1029 dirc = '?';
1030 else
1031 dirc = '/';
1032#endif
1033 }
1034
1035#ifdef FEAT_FOLDING
1036 /* If the cursor is in a closed fold, don't find another match in the same
1037 * fold. */
1038 if (dirc == '/')
1039 {
1040 if (hasFolding(pos.lnum, NULL, &pos.lnum))
1041 pos.col = MAXCOL - 2; /* avoid overflow when adding 1 */
1042 }
1043 else
1044 {
1045 if (hasFolding(pos.lnum, &pos.lnum, NULL))
1046 pos.col = 0;
1047 }
1048#endif
1049
1050#ifdef FEAT_SEARCH_EXTRA
1051 /*
1052 * Turn 'hlsearch' highlighting back on.
1053 */
1054 if (no_hlsearch && !(options & SEARCH_KEEP))
1055 {
Bram Moolenaar1c8f93f2006-03-12 22:10:07 +00001056 redraw_all_later(SOME_VALID);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001057 no_hlsearch = FALSE;
1058 }
1059#endif
1060
1061 /*
1062 * Repeat the search when pattern followed by ';', e.g. "/foo/;?bar".
1063 */
1064 for (;;)
1065 {
1066 searchstr = pat;
1067 dircp = NULL;
1068 /* use previous pattern */
1069 if (pat == NULL || *pat == NUL || *pat == dirc)
1070 {
1071 if (spats[RE_SEARCH].pat == NULL) /* no previous pattern */
1072 {
1073 EMSG(_(e_noprevre));
1074 retval = 0;
1075 goto end_do_search;
1076 }
1077 /* make search_regcomp() use spats[RE_SEARCH].pat */
1078 searchstr = (char_u *)"";
1079 }
1080
1081 if (pat != NULL && *pat != NUL) /* look for (new) offset */
1082 {
1083 /*
1084 * Find end of regular expression.
1085 * If there is a matching '/' or '?', toss it.
1086 */
1087 ps = strcopy;
1088 p = skip_regexp(pat, dirc, (int)p_magic, &strcopy);
1089 if (strcopy != ps)
1090 {
1091 /* made a copy of "pat" to change "\?" to "?" */
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00001092 searchcmdlen += (int)(STRLEN(pat) - STRLEN(strcopy));
Bram Moolenaar071d4272004-06-13 20:20:40 +00001093 pat = strcopy;
1094 searchstr = strcopy;
1095 }
1096 if (*p == dirc)
1097 {
1098 dircp = p; /* remember where we put the NUL */
1099 *p++ = NUL;
1100 }
1101 spats[0].off.line = FALSE;
1102 spats[0].off.end = FALSE;
1103 spats[0].off.off = 0;
1104 /*
1105 * Check for a line offset or a character offset.
1106 * For get_address (echo off) we don't check for a character
1107 * offset, because it is meaningless and the 's' could be a
1108 * substitute command.
1109 */
1110 if (*p == '+' || *p == '-' || VIM_ISDIGIT(*p))
1111 spats[0].off.line = TRUE;
1112 else if ((options & SEARCH_OPT) &&
1113 (*p == 'e' || *p == 's' || *p == 'b'))
1114 {
1115 if (*p == 'e') /* end */
1116 spats[0].off.end = SEARCH_END;
1117 ++p;
1118 }
1119 if (VIM_ISDIGIT(*p) || *p == '+' || *p == '-') /* got an offset */
1120 {
1121 /* 'nr' or '+nr' or '-nr' */
1122 if (VIM_ISDIGIT(*p) || VIM_ISDIGIT(*(p + 1)))
1123 spats[0].off.off = atol((char *)p);
1124 else if (*p == '-') /* single '-' */
1125 spats[0].off.off = -1;
1126 else /* single '+' */
1127 spats[0].off.off = 1;
1128 ++p;
1129 while (VIM_ISDIGIT(*p)) /* skip number */
1130 ++p;
1131 }
1132
1133 /* compute length of search command for get_address() */
1134 searchcmdlen += (int)(p - pat);
1135
1136 pat = p; /* put pat after search command */
1137 }
1138
1139 if ((options & SEARCH_ECHO) && messaging()
1140 && !cmd_silent && msg_silent == 0)
1141 {
1142 char_u *msgbuf;
1143 char_u *trunc;
1144
1145 if (*searchstr == NUL)
1146 p = spats[last_idx].pat;
1147 else
1148 p = searchstr;
1149 msgbuf = alloc((unsigned)(STRLEN(p) + 40));
1150 if (msgbuf != NULL)
1151 {
1152 msgbuf[0] = dirc;
Bram Moolenaarcafda4f2005-09-06 19:25:11 +00001153#ifdef FEAT_MBYTE
1154 if (enc_utf8 && utf_iscomposing(utf_ptr2char(p)))
1155 {
1156 /* Use a space to draw the composing char on. */
1157 msgbuf[1] = ' ';
1158 STRCPY(msgbuf + 2, p);
1159 }
1160 else
1161#endif
1162 STRCPY(msgbuf + 1, p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001163 if (spats[0].off.line || spats[0].off.end || spats[0].off.off)
1164 {
1165 p = msgbuf + STRLEN(msgbuf);
1166 *p++ = dirc;
1167 if (spats[0].off.end)
1168 *p++ = 'e';
1169 else if (!spats[0].off.line)
1170 *p++ = 's';
1171 if (spats[0].off.off > 0 || spats[0].off.line)
1172 *p++ = '+';
1173 if (spats[0].off.off != 0 || spats[0].off.line)
1174 sprintf((char *)p, "%ld", spats[0].off.off);
1175 else
1176 *p = NUL;
1177 }
1178
1179 msg_start();
Bram Moolenaara4a08382005-09-09 19:52:02 +00001180 trunc = msg_strtrunc(msgbuf, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001181
1182#ifdef FEAT_RIGHTLEFT
1183 /* The search pattern could be shown on the right in rightleft
1184 * mode, but the 'ruler' and 'showcmd' area use it too, thus
1185 * it would be blanked out again very soon. Show it on the
1186 * left, but do reverse the text. */
1187 if (curwin->w_p_rl && *curwin->w_p_rlc == 's')
1188 {
1189 char_u *r;
1190
1191 r = reverse_text(trunc != NULL ? trunc : msgbuf);
1192 if (r != NULL)
1193 {
1194 vim_free(trunc);
1195 trunc = r;
1196 }
1197 }
1198#endif
1199 if (trunc != NULL)
1200 {
1201 msg_outtrans(trunc);
1202 vim_free(trunc);
1203 }
1204 else
1205 msg_outtrans(msgbuf);
1206 msg_clr_eos();
1207 msg_check();
1208 vim_free(msgbuf);
1209
1210 gotocmdline(FALSE);
1211 out_flush();
1212 msg_nowait = TRUE; /* don't wait for this message */
1213 }
1214 }
1215
1216 /*
1217 * If there is a character offset, subtract it from the current
1218 * position, so we don't get stuck at "?pat?e+2" or "/pat/s-2".
Bram Moolenaared203462004-06-16 11:19:22 +00001219 * Skip this if pos.col is near MAXCOL (closed fold).
Bram Moolenaar071d4272004-06-13 20:20:40 +00001220 * This is not done for a line offset, because then we would not be vi
1221 * compatible.
1222 */
Bram Moolenaared203462004-06-16 11:19:22 +00001223 if (!spats[0].off.line && spats[0].off.off && pos.col < MAXCOL - 2)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001224 {
1225 if (spats[0].off.off > 0)
1226 {
1227 for (c = spats[0].off.off; c; --c)
1228 if (decl(&pos) == -1)
1229 break;
1230 if (c) /* at start of buffer */
1231 {
1232 pos.lnum = 0; /* allow lnum == 0 here */
1233 pos.col = MAXCOL;
1234 }
1235 }
1236 else
1237 {
1238 for (c = spats[0].off.off; c; ++c)
1239 if (incl(&pos) == -1)
1240 break;
1241 if (c) /* at end of buffer */
1242 {
1243 pos.lnum = curbuf->b_ml.ml_line_count + 1;
1244 pos.col = 0;
1245 }
1246 }
1247 }
1248
1249#ifdef FEAT_FKMAP /* when in Farsi mode, reverse the character flow */
1250 if (p_altkeymap && curwin->w_p_rl)
1251 lrFswap(searchstr,0);
1252#endif
1253
1254 c = searchit(curwin, curbuf, &pos, dirc == '/' ? FORWARD : BACKWARD,
1255 searchstr, count, spats[0].off.end + (options &
1256 (SEARCH_KEEP + SEARCH_PEEK + SEARCH_HIS
1257 + SEARCH_MSG + SEARCH_START
1258 + ((pat != NULL && *pat == ';') ? 0 : SEARCH_NOOF))),
Bram Moolenaar76929292008-01-06 19:07:36 +00001259 RE_LAST, (linenr_T)0, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001260
1261 if (dircp != NULL)
1262 *dircp = dirc; /* restore second '/' or '?' for normal_cmd() */
1263 if (c == FAIL)
1264 {
1265 retval = 0;
1266 goto end_do_search;
1267 }
1268 if (spats[0].off.end && oap != NULL)
1269 oap->inclusive = TRUE; /* 'e' includes last character */
1270
1271 retval = 1; /* pattern found */
1272
1273 /*
1274 * Add character and/or line offset
1275 */
Bram Moolenaar9160f302006-08-29 15:58:12 +00001276 if (!(options & SEARCH_NOOF) || (pat != NULL && *pat == ';'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001277 {
1278 if (spats[0].off.line) /* Add the offset to the line number. */
1279 {
1280 c = pos.lnum + spats[0].off.off;
1281 if (c < 1)
1282 pos.lnum = 1;
1283 else if (c > curbuf->b_ml.ml_line_count)
1284 pos.lnum = curbuf->b_ml.ml_line_count;
1285 else
1286 pos.lnum = c;
1287 pos.col = 0;
1288
1289 retval = 2; /* pattern found, line offset added */
1290 }
Bram Moolenaared203462004-06-16 11:19:22 +00001291 else if (pos.col < MAXCOL - 2) /* just in case */
Bram Moolenaar071d4272004-06-13 20:20:40 +00001292 {
1293 /* to the right, check for end of file */
1294 if (spats[0].off.off > 0)
1295 {
1296 for (c = spats[0].off.off; c; --c)
1297 if (incl(&pos) == -1)
1298 break;
1299 }
1300 /* to the left, check for start of file */
1301 else
1302 {
1303 if ((c = pos.col + spats[0].off.off) >= 0)
1304 pos.col = c;
1305 else
1306 for (c = spats[0].off.off; c; ++c)
1307 if (decl(&pos) == -1)
1308 break;
1309 }
1310 }
1311 }
1312
1313 /*
1314 * The search command can be followed by a ';' to do another search.
1315 * For example: "/pat/;/foo/+3;?bar"
1316 * This is like doing another search command, except:
1317 * - The remembered direction '/' or '?' is from the first search.
1318 * - When an error happens the cursor isn't moved at all.
1319 * Don't do this when called by get_address() (it handles ';' itself).
1320 */
1321 if (!(options & SEARCH_OPT) || pat == NULL || *pat != ';')
1322 break;
1323
1324 dirc = *++pat;
1325 if (dirc != '?' && dirc != '/')
1326 {
1327 retval = 0;
1328 EMSG(_("E386: Expected '?' or '/' after ';'"));
1329 goto end_do_search;
1330 }
1331 ++pat;
1332 }
1333
1334 if (options & SEARCH_MARK)
1335 setpcmark();
1336 curwin->w_cursor = pos;
1337 curwin->w_set_curswant = TRUE;
1338
1339end_do_search:
1340 if (options & SEARCH_KEEP)
1341 spats[0].off = old_off;
1342 vim_free(strcopy);
1343
1344 return retval;
1345}
1346
1347#if defined(FEAT_INS_EXPAND) || defined(PROTO)
1348/*
1349 * search_for_exact_line(buf, pos, dir, pat)
1350 *
1351 * Search for a line starting with the given pattern (ignoring leading
1352 * white-space), starting from pos and going in direction dir. pos will
1353 * contain the position of the match found. Blank lines match only if
1354 * ADDING is set. if p_ic is set then the pattern must be in lowercase.
1355 * Return OK for success, or FAIL if no line found.
1356 */
1357 int
1358search_for_exact_line(buf, pos, dir, pat)
1359 buf_T *buf;
1360 pos_T *pos;
1361 int dir;
1362 char_u *pat;
1363{
1364 linenr_T start = 0;
1365 char_u *ptr;
1366 char_u *p;
1367
1368 if (buf->b_ml.ml_line_count == 0)
1369 return FAIL;
1370 for (;;)
1371 {
1372 pos->lnum += dir;
1373 if (pos->lnum < 1)
1374 {
1375 if (p_ws)
1376 {
1377 pos->lnum = buf->b_ml.ml_line_count;
1378 if (!shortmess(SHM_SEARCH))
1379 give_warning((char_u *)_(top_bot_msg), TRUE);
1380 }
1381 else
1382 {
1383 pos->lnum = 1;
1384 break;
1385 }
1386 }
1387 else if (pos->lnum > buf->b_ml.ml_line_count)
1388 {
1389 if (p_ws)
1390 {
1391 pos->lnum = 1;
1392 if (!shortmess(SHM_SEARCH))
1393 give_warning((char_u *)_(bot_top_msg), TRUE);
1394 }
1395 else
1396 {
1397 pos->lnum = 1;
1398 break;
1399 }
1400 }
1401 if (pos->lnum == start)
1402 break;
1403 if (start == 0)
1404 start = pos->lnum;
1405 ptr = ml_get_buf(buf, pos->lnum, FALSE);
1406 p = skipwhite(ptr);
1407 pos->col = (colnr_T) (p - ptr);
1408
1409 /* when adding lines the matching line may be empty but it is not
1410 * ignored because we are interested in the next line -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001411 if ((compl_cont_status & CONT_ADDING)
1412 && !(compl_cont_status & CONT_SOL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00001413 {
1414 if ((p_ic ? MB_STRICMP(p, pat) : STRCMP(p, pat)) == 0)
1415 return OK;
1416 }
1417 else if (*p != NUL) /* ignore empty lines */
1418 { /* expanding lines or words */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00001419 if ((p_ic ? MB_STRNICMP(p, pat, compl_length)
1420 : STRNCMP(p, pat, compl_length)) == 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001421 return OK;
1422 }
1423 }
1424 return FAIL;
1425}
1426#endif /* FEAT_INS_EXPAND */
1427
1428/*
1429 * Character Searches
1430 */
1431
1432/*
1433 * Search for a character in a line. If "t_cmd" is FALSE, move to the
1434 * position of the character, otherwise move to just before the char.
1435 * Do this "cap->count1" times.
1436 * Return FAIL or OK.
1437 */
1438 int
1439searchc(cap, t_cmd)
1440 cmdarg_T *cap;
1441 int t_cmd;
1442{
1443 int c = cap->nchar; /* char to search for */
1444 int dir = cap->arg; /* TRUE for searching forward */
1445 long count = cap->count1; /* repeat count */
1446 static int lastc = NUL; /* last character searched for */
1447 static int lastcdir; /* last direction of character search */
1448 static int last_t_cmd; /* last search t_cmd */
1449 int col;
1450 char_u *p;
1451 int len;
1452#ifdef FEAT_MBYTE
1453 static char_u bytes[MB_MAXBYTES];
1454 static int bytelen = 1; /* >1 for multi-byte char */
1455#endif
1456
1457 if (c != NUL) /* normal search: remember args for repeat */
1458 {
1459 if (!KeyStuffed) /* don't remember when redoing */
1460 {
1461 lastc = c;
1462 lastcdir = dir;
1463 last_t_cmd = t_cmd;
1464#ifdef FEAT_MBYTE
1465 bytelen = (*mb_char2bytes)(c, bytes);
1466 if (cap->ncharC1 != 0)
1467 {
1468 bytelen += (*mb_char2bytes)(cap->ncharC1, bytes + bytelen);
1469 if (cap->ncharC2 != 0)
1470 bytelen += (*mb_char2bytes)(cap->ncharC2, bytes + bytelen);
1471 }
1472#endif
1473 }
1474 }
1475 else /* repeat previous search */
1476 {
1477 if (lastc == NUL)
1478 return FAIL;
1479 if (dir) /* repeat in opposite direction */
1480 dir = -lastcdir;
1481 else
1482 dir = lastcdir;
1483 t_cmd = last_t_cmd;
1484 c = lastc;
1485 /* For multi-byte re-use last bytes[] and bytelen. */
1486 }
1487
Bram Moolenaar60a795a2005-09-16 21:55:43 +00001488 if (dir == BACKWARD)
1489 cap->oap->inclusive = FALSE;
1490 else
1491 cap->oap->inclusive = TRUE;
1492
Bram Moolenaar071d4272004-06-13 20:20:40 +00001493 p = ml_get_curline();
1494 col = curwin->w_cursor.col;
1495 len = (int)STRLEN(p);
1496
1497 while (count--)
1498 {
1499#ifdef FEAT_MBYTE
1500 if (has_mbyte)
1501 {
1502 for (;;)
1503 {
1504 if (dir > 0)
1505 {
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001506 col += (*mb_ptr2len)(p + col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001507 if (col >= len)
1508 return FAIL;
1509 }
1510 else
1511 {
1512 if (col == 0)
1513 return FAIL;
1514 col -= (*mb_head_off)(p, p + col - 1) + 1;
1515 }
1516 if (bytelen == 1)
1517 {
1518 if (p[col] == c)
1519 break;
1520 }
1521 else
1522 {
1523 if (vim_memcmp(p + col, bytes, bytelen) == 0)
1524 break;
1525 }
1526 }
1527 }
1528 else
1529#endif
1530 {
1531 for (;;)
1532 {
1533 if ((col += dir) < 0 || col >= len)
1534 return FAIL;
1535 if (p[col] == c)
1536 break;
1537 }
1538 }
1539 }
1540
1541 if (t_cmd)
1542 {
1543 /* backup to before the character (possibly double-byte) */
1544 col -= dir;
1545#ifdef FEAT_MBYTE
1546 if (has_mbyte)
1547 {
1548 if (dir < 0)
1549 /* Landed on the search char which is bytelen long */
1550 col += bytelen - 1;
1551 else
1552 /* To previous char, which may be multi-byte. */
1553 col -= (*mb_head_off)(p, p + col);
1554 }
1555#endif
1556 }
1557 curwin->w_cursor.col = col;
1558
1559 return OK;
1560}
1561
1562/*
1563 * "Other" Searches
1564 */
1565
1566/*
1567 * findmatch - find the matching paren or brace
1568 *
1569 * Improvement over vi: Braces inside quotes are ignored.
1570 */
1571 pos_T *
1572findmatch(oap, initc)
1573 oparg_T *oap;
1574 int initc;
1575{
1576 return findmatchlimit(oap, initc, 0, 0);
1577}
1578
1579/*
1580 * Return TRUE if the character before "linep[col]" equals "ch".
1581 * Return FALSE if "col" is zero.
1582 * Update "*prevcol" to the column of the previous character, unless "prevcol"
1583 * is NULL.
1584 * Handles multibyte string correctly.
1585 */
1586 static int
1587check_prevcol(linep, col, ch, prevcol)
1588 char_u *linep;
1589 int col;
1590 int ch;
1591 int *prevcol;
1592{
1593 --col;
1594#ifdef FEAT_MBYTE
1595 if (col > 0 && has_mbyte)
1596 col -= (*mb_head_off)(linep, linep + col);
1597#endif
1598 if (prevcol)
1599 *prevcol = col;
1600 return (col >= 0 && linep[col] == ch) ? TRUE : FALSE;
1601}
1602
1603/*
1604 * findmatchlimit -- find the matching paren or brace, if it exists within
1605 * maxtravel lines of here. A maxtravel of 0 means search until falling off
1606 * the edge of the file.
1607 *
1608 * "initc" is the character to find a match for. NUL means to find the
1609 * character at or after the cursor.
1610 *
1611 * flags: FM_BACKWARD search backwards (when initc is '/', '*' or '#')
1612 * FM_FORWARD search forwards (when initc is '/', '*' or '#')
1613 * FM_BLOCKSTOP stop at start/end of block ({ or } in column 0)
1614 * FM_SKIPCOMM skip comments (not implemented yet!)
Bram Moolenaarf75a9632005-09-13 21:20:47 +00001615 *
1616 * "oap" is only used to set oap->motion_type for a linewise motion, it be
1617 * NULL
Bram Moolenaar071d4272004-06-13 20:20:40 +00001618 */
1619
1620 pos_T *
1621findmatchlimit(oap, initc, flags, maxtravel)
1622 oparg_T *oap;
1623 int initc;
1624 int flags;
1625 int maxtravel;
1626{
1627 static pos_T pos; /* current search position */
1628 int findc = 0; /* matching brace */
1629 int c;
1630 int count = 0; /* cumulative number of braces */
1631 int backwards = FALSE; /* init for gcc */
1632 int inquote = FALSE; /* TRUE when inside quotes */
1633 char_u *linep; /* pointer to current line */
1634 char_u *ptr;
1635 int do_quotes; /* check for quotes in current line */
1636 int at_start; /* do_quotes value at start position */
1637 int hash_dir = 0; /* Direction searched for # things */
1638 int comment_dir = 0; /* Direction searched for comments */
1639 pos_T match_pos; /* Where last slash-star was found */
1640 int start_in_quotes; /* start position is in quotes */
1641 int traveled = 0; /* how far we've searched so far */
1642 int ignore_cend = FALSE; /* ignore comment end */
1643 int cpo_match; /* vi compatible matching */
1644 int cpo_bsl; /* don't recognize backslashes */
1645 int match_escaped = 0; /* search for escaped match */
1646 int dir; /* Direction to search */
1647 int comment_col = MAXCOL; /* start of / / comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001648#ifdef FEAT_LISP
1649 int lispcomm = FALSE; /* inside of Lisp-style comment */
1650 int lisp = curbuf->b_p_lisp; /* engage Lisp-specific hacks ;) */
1651#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001652
1653 pos = curwin->w_cursor;
1654 linep = ml_get(pos.lnum);
1655
1656 cpo_match = (vim_strchr(p_cpo, CPO_MATCH) != NULL);
1657 cpo_bsl = (vim_strchr(p_cpo, CPO_MATCHBSL) != NULL);
1658
1659 /* Direction to search when initc is '/', '*' or '#' */
1660 if (flags & FM_BACKWARD)
1661 dir = BACKWARD;
1662 else if (flags & FM_FORWARD)
1663 dir = FORWARD;
1664 else
1665 dir = 0;
1666
1667 /*
1668 * if initc given, look in the table for the matching character
1669 * '/' and '*' are special cases: look for start or end of comment.
1670 * When '/' is used, we ignore running backwards into an star-slash, for
1671 * "[*" command, we just want to find any comment.
1672 */
1673 if (initc == '/' || initc == '*')
1674 {
1675 comment_dir = dir;
1676 if (initc == '/')
1677 ignore_cend = TRUE;
1678 backwards = (dir == FORWARD) ? FALSE : TRUE;
1679 initc = NUL;
1680 }
1681 else if (initc != '#' && initc != NUL)
1682 {
1683 /* 'matchpairs' is "x:y,x:y" */
1684 for (ptr = curbuf->b_p_mps; *ptr; ptr += 2)
1685 {
1686 if (*ptr == initc)
1687 {
1688 findc = initc;
1689 initc = ptr[2];
1690 backwards = TRUE;
1691 break;
1692 }
1693 ptr += 2;
1694 if (*ptr == initc)
1695 {
1696 findc = initc;
1697 initc = ptr[-2];
1698 backwards = FALSE;
1699 break;
1700 }
1701 if (ptr[1] != ',')
1702 break;
1703 }
1704 if (!findc) /* invalid initc! */
1705 return NULL;
1706 }
1707 /*
1708 * Either initc is '#', or no initc was given and we need to look under the
1709 * cursor.
1710 */
1711 else
1712 {
1713 if (initc == '#')
1714 {
1715 hash_dir = dir;
1716 }
1717 else
1718 {
1719 /*
1720 * initc was not given, must look for something to match under
1721 * or near the cursor.
1722 * Only check for special things when 'cpo' doesn't have '%'.
1723 */
1724 if (!cpo_match)
1725 {
1726 /* Are we before or at #if, #else etc.? */
1727 ptr = skipwhite(linep);
1728 if (*ptr == '#' && pos.col <= (colnr_T)(ptr - linep))
1729 {
1730 ptr = skipwhite(ptr + 1);
1731 if ( STRNCMP(ptr, "if", 2) == 0
1732 || STRNCMP(ptr, "endif", 5) == 0
1733 || STRNCMP(ptr, "el", 2) == 0)
1734 hash_dir = 1;
1735 }
1736
1737 /* Are we on a comment? */
1738 else if (linep[pos.col] == '/')
1739 {
1740 if (linep[pos.col + 1] == '*')
1741 {
1742 comment_dir = FORWARD;
1743 backwards = FALSE;
1744 pos.col++;
1745 }
1746 else if (pos.col > 0 && linep[pos.col - 1] == '*')
1747 {
1748 comment_dir = BACKWARD;
1749 backwards = TRUE;
1750 pos.col--;
1751 }
1752 }
1753 else if (linep[pos.col] == '*')
1754 {
1755 if (linep[pos.col + 1] == '/')
1756 {
1757 comment_dir = BACKWARD;
1758 backwards = TRUE;
1759 }
1760 else if (pos.col > 0 && linep[pos.col - 1] == '/')
1761 {
1762 comment_dir = FORWARD;
1763 backwards = FALSE;
1764 }
1765 }
1766 }
1767
1768 /*
1769 * If we are not on a comment or the # at the start of a line, then
1770 * look for brace anywhere on this line after the cursor.
1771 */
1772 if (!hash_dir && !comment_dir)
1773 {
1774 /*
1775 * Find the brace under or after the cursor.
1776 * If beyond the end of the line, use the last character in
1777 * the line.
1778 */
1779 if (linep[pos.col] == NUL && pos.col)
1780 --pos.col;
1781 for (;;)
1782 {
1783 initc = linep[pos.col];
1784 if (initc == NUL)
1785 break;
1786
1787 for (ptr = curbuf->b_p_mps; *ptr; ++ptr)
1788 {
1789 if (*ptr == initc)
1790 {
1791 findc = ptr[2];
1792 backwards = FALSE;
1793 break;
1794 }
1795 ptr += 2;
1796 if (*ptr == initc)
1797 {
1798 findc = ptr[-2];
1799 backwards = TRUE;
1800 break;
1801 }
1802 if (!*++ptr)
1803 break;
1804 }
1805 if (findc)
1806 break;
1807#ifdef FEAT_MBYTE
1808 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001809 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001810 else
1811#endif
1812 ++pos.col;
1813 }
1814 if (!findc)
1815 {
1816 /* no brace in the line, maybe use " #if" then */
1817 if (!cpo_match && *skipwhite(linep) == '#')
1818 hash_dir = 1;
1819 else
1820 return NULL;
1821 }
1822 else if (!cpo_bsl)
1823 {
1824 int col, bslcnt = 0;
1825
1826 /* Set "match_escaped" if there are an odd number of
1827 * backslashes. */
1828 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
1829 bslcnt++;
1830 match_escaped = (bslcnt & 1);
1831 }
1832 }
1833 }
1834 if (hash_dir)
1835 {
1836 /*
1837 * Look for matching #if, #else, #elif, or #endif
1838 */
1839 if (oap != NULL)
1840 oap->motion_type = MLINE; /* Linewise for this case only */
1841 if (initc != '#')
1842 {
1843 ptr = skipwhite(skipwhite(linep) + 1);
1844 if (STRNCMP(ptr, "if", 2) == 0 || STRNCMP(ptr, "el", 2) == 0)
1845 hash_dir = 1;
1846 else if (STRNCMP(ptr, "endif", 5) == 0)
1847 hash_dir = -1;
1848 else
1849 return NULL;
1850 }
1851 pos.col = 0;
1852 while (!got_int)
1853 {
1854 if (hash_dir > 0)
1855 {
1856 if (pos.lnum == curbuf->b_ml.ml_line_count)
1857 break;
1858 }
1859 else if (pos.lnum == 1)
1860 break;
1861 pos.lnum += hash_dir;
1862 linep = ml_get(pos.lnum);
1863 line_breakcheck(); /* check for CTRL-C typed */
1864 ptr = skipwhite(linep);
1865 if (*ptr != '#')
1866 continue;
1867 pos.col = (colnr_T) (ptr - linep);
1868 ptr = skipwhite(ptr + 1);
1869 if (hash_dir > 0)
1870 {
1871 if (STRNCMP(ptr, "if", 2) == 0)
1872 count++;
1873 else if (STRNCMP(ptr, "el", 2) == 0)
1874 {
1875 if (count == 0)
1876 return &pos;
1877 }
1878 else if (STRNCMP(ptr, "endif", 5) == 0)
1879 {
1880 if (count == 0)
1881 return &pos;
1882 count--;
1883 }
1884 }
1885 else
1886 {
1887 if (STRNCMP(ptr, "if", 2) == 0)
1888 {
1889 if (count == 0)
1890 return &pos;
1891 count--;
1892 }
1893 else if (initc == '#' && STRNCMP(ptr, "el", 2) == 0)
1894 {
1895 if (count == 0)
1896 return &pos;
1897 }
1898 else if (STRNCMP(ptr, "endif", 5) == 0)
1899 count++;
1900 }
1901 }
1902 return NULL;
1903 }
1904 }
1905
1906#ifdef FEAT_RIGHTLEFT
Bram Moolenaarabc97732007-08-08 20:49:37 +00001907 /* This is just guessing: when 'rightleft' is set, search for a matching
Bram Moolenaar071d4272004-06-13 20:20:40 +00001908 * paren/brace in the other direction. */
1909 if (curwin->w_p_rl && vim_strchr((char_u *)"()[]{}<>", initc) != NULL)
1910 backwards = !backwards;
1911#endif
1912
1913 do_quotes = -1;
1914 start_in_quotes = MAYBE;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00001915 clearpos(&match_pos);
1916
Bram Moolenaar071d4272004-06-13 20:20:40 +00001917 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001918 if ((backwards && comment_dir)
1919#ifdef FEAT_LISP
1920 || lisp
1921#endif
1922 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001923 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001924#ifdef FEAT_LISP
1925 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1926 lispcomm = TRUE; /* find match inside this comment */
1927#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001928 while (!got_int)
1929 {
1930 /*
1931 * Go to the next position, forward or backward. We could use
1932 * inc() and dec() here, but that is much slower
1933 */
1934 if (backwards)
1935 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001936#ifdef FEAT_LISP
1937 /* char to match is inside of comment, don't search outside */
1938 if (lispcomm && pos.col < (colnr_T)comment_col)
1939 break;
1940#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001941 if (pos.col == 0) /* at start of line, go to prev. one */
1942 {
1943 if (pos.lnum == 1) /* start of file */
1944 break;
1945 --pos.lnum;
1946
Bram Moolenaar9e54a0e2006-04-14 20:42:25 +00001947 if (maxtravel > 0 && ++traveled > maxtravel)
Bram Moolenaar071d4272004-06-13 20:20:40 +00001948 break;
1949
1950 linep = ml_get(pos.lnum);
1951 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1952 do_quotes = -1;
1953 line_breakcheck();
1954
1955 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001956 if (comment_dir
1957#ifdef FEAT_LISP
1958 || lisp
1959#endif
1960 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001961 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001962#ifdef FEAT_LISP
1963 /* skip comment */
1964 if (lisp && comment_col != MAXCOL)
1965 pos.col = comment_col;
1966#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001967 }
1968 else
1969 {
1970 --pos.col;
1971#ifdef FEAT_MBYTE
1972 if (has_mbyte)
1973 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1974#endif
1975 }
1976 }
1977 else /* forward search */
1978 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001979 if (linep[pos.col] == NUL
1980 /* at end of line, go to next one */
1981#ifdef FEAT_LISP
1982 /* don't search for match in comment */
1983 || (lisp && comment_col != MAXCOL
1984 && pos.col == (colnr_T)comment_col)
1985#endif
1986 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001987 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001988 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
1989#ifdef FEAT_LISP
1990 /* line is exhausted and comment with it,
1991 * don't search for match in code */
1992 || lispcomm
1993#endif
1994 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001995 break;
1996 ++pos.lnum;
1997
1998 if (maxtravel && traveled++ > maxtravel)
1999 break;
2000
2001 linep = ml_get(pos.lnum);
2002 pos.col = 0;
2003 do_quotes = -1;
2004 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002005#ifdef FEAT_LISP
2006 if (lisp) /* find comment pos in new line */
2007 comment_col = check_linecomment(linep);
2008#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002009 }
2010 else
2011 {
2012#ifdef FEAT_MBYTE
2013 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00002014 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002015 else
2016#endif
2017 ++pos.col;
2018 }
2019 }
2020
2021 /*
2022 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2023 */
2024 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2025 (linep[0] == '{' || linep[0] == '}'))
2026 {
2027 if (linep[0] == findc && count == 0) /* match! */
2028 return &pos;
2029 break; /* out of scope */
2030 }
2031
2032 if (comment_dir)
2033 {
2034 /* Note: comments do not nest, and we ignore quotes in them */
2035 /* TODO: ignore comment brackets inside strings */
2036 if (comment_dir == FORWARD)
2037 {
2038 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2039 {
2040 pos.col++;
2041 return &pos;
2042 }
2043 }
2044 else /* Searching backwards */
2045 {
2046 /*
2047 * A comment may contain / * or / /, it may also start or end
2048 * with / * /. Ignore a / * after / /.
2049 */
2050 if (pos.col == 0)
2051 continue;
2052 else if ( linep[pos.col - 1] == '/'
2053 && linep[pos.col] == '*'
2054 && (int)pos.col < comment_col)
2055 {
2056 count++;
2057 match_pos = pos;
2058 match_pos.col--;
2059 }
2060 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2061 {
2062 if (count > 0)
2063 pos = match_pos;
2064 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2065 && (int)pos.col <= comment_col)
2066 pos.col -= 2;
2067 else if (ignore_cend)
2068 continue;
2069 else
2070 return NULL;
2071 return &pos;
2072 }
2073 }
2074 continue;
2075 }
2076
2077 /*
2078 * If smart matching ('cpoptions' does not contain '%'), braces inside
2079 * of quotes are ignored, but only if there is an even number of
2080 * quotes in the line.
2081 */
2082 if (cpo_match)
2083 do_quotes = 0;
2084 else if (do_quotes == -1)
2085 {
2086 /*
2087 * Count the number of quotes in the line, skipping \" and '"'.
2088 * Watch out for "\\".
2089 */
2090 at_start = do_quotes;
2091 for (ptr = linep; *ptr; ++ptr)
2092 {
2093 if (ptr == linep + pos.col + backwards)
2094 at_start = (do_quotes & 1);
2095 if (*ptr == '"'
2096 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2097 ++do_quotes;
2098 if (*ptr == '\\' && ptr[1] != NUL)
2099 ++ptr;
2100 }
2101 do_quotes &= 1; /* result is 1 with even number of quotes */
2102
2103 /*
2104 * If we find an uneven count, check current line and previous
2105 * one for a '\' at the end.
2106 */
2107 if (!do_quotes)
2108 {
2109 inquote = FALSE;
2110 if (ptr[-1] == '\\')
2111 {
2112 do_quotes = 1;
2113 if (start_in_quotes == MAYBE)
2114 {
2115 /* Do we need to use at_start here? */
2116 inquote = TRUE;
2117 start_in_quotes = TRUE;
2118 }
2119 else if (backwards)
2120 inquote = TRUE;
2121 }
2122 if (pos.lnum > 1)
2123 {
2124 ptr = ml_get(pos.lnum - 1);
2125 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2126 {
2127 do_quotes = 1;
2128 if (start_in_quotes == MAYBE)
2129 {
2130 inquote = at_start;
2131 if (inquote)
2132 start_in_quotes = TRUE;
2133 }
2134 else if (!backwards)
2135 inquote = TRUE;
2136 }
Bram Moolenaaraec11792007-07-10 11:09:36 +00002137
2138 /* ml_get() only keeps one line, need to get linep again */
2139 linep = ml_get(pos.lnum);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002140 }
2141 }
2142 }
2143 if (start_in_quotes == MAYBE)
2144 start_in_quotes = FALSE;
2145
2146 /*
2147 * If 'smartmatch' is set:
2148 * Things inside quotes are ignored by setting 'inquote'. If we
2149 * find a quote without a preceding '\' invert 'inquote'. At the
2150 * end of a line not ending in '\' we reset 'inquote'.
2151 *
2152 * In lines with an uneven number of quotes (without preceding '\')
2153 * we do not know which part to ignore. Therefore we only set
2154 * inquote if the number of quotes in a line is even, unless this
2155 * line or the previous one ends in a '\'. Complicated, isn't it?
2156 */
2157 switch (c = linep[pos.col])
2158 {
2159 case NUL:
2160 /* at end of line without trailing backslash, reset inquote */
2161 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2162 {
2163 inquote = FALSE;
2164 start_in_quotes = FALSE;
2165 }
2166 break;
2167
2168 case '"':
2169 /* a quote that is preceded with an odd number of backslashes is
2170 * ignored */
2171 if (do_quotes)
2172 {
2173 int col;
2174
2175 for (col = pos.col - 1; col >= 0; --col)
2176 if (linep[col] != '\\')
2177 break;
2178 if ((((int)pos.col - 1 - col) & 1) == 0)
2179 {
2180 inquote = !inquote;
2181 start_in_quotes = FALSE;
2182 }
2183 }
2184 break;
2185
2186 /*
2187 * If smart matching ('cpoptions' does not contain '%'):
2188 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2189 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2190 * skipped, there is never a brace in them.
2191 * Ignore this when finding matches for `'.
2192 */
2193 case '\'':
2194 if (!cpo_match && initc != '\'' && findc != '\'')
2195 {
2196 if (backwards)
2197 {
2198 if (pos.col > 1)
2199 {
2200 if (linep[pos.col - 2] == '\'')
2201 {
2202 pos.col -= 2;
2203 break;
2204 }
2205 else if (linep[pos.col - 2] == '\\' &&
2206 pos.col > 2 && linep[pos.col - 3] == '\'')
2207 {
2208 pos.col -= 3;
2209 break;
2210 }
2211 }
2212 }
2213 else if (linep[pos.col + 1]) /* forward search */
2214 {
2215 if (linep[pos.col + 1] == '\\' &&
2216 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2217 {
2218 pos.col += 3;
2219 break;
2220 }
2221 else if (linep[pos.col + 2] == '\'')
2222 {
2223 pos.col += 2;
2224 break;
2225 }
2226 }
2227 }
2228 /* FALLTHROUGH */
2229
2230 default:
2231#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002232 /*
2233 * For Lisp skip over backslashed (), {} and [].
2234 * (actually, we skip #\( et al)
2235 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002236 if (curbuf->b_p_lisp
2237 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002238 && pos.col > 1
2239 && check_prevcol(linep, pos.col, '\\', NULL)
2240 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002241 break;
2242#endif
2243
2244 /* Check for match outside of quotes, and inside of
2245 * quotes when the start is also inside of quotes. */
2246 if ((!inquote || start_in_quotes == TRUE)
2247 && (c == initc || c == findc))
2248 {
2249 int col, bslcnt = 0;
2250
2251 if (!cpo_bsl)
2252 {
2253 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2254 bslcnt++;
2255 }
2256 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2257 * what we expect. */
2258 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2259 {
2260 if (c == initc)
2261 count++;
2262 else
2263 {
2264 if (count == 0)
2265 return &pos;
2266 count--;
2267 }
2268 }
2269 }
2270 }
2271 }
2272
2273 if (comment_dir == BACKWARD && count > 0)
2274 {
2275 pos = match_pos;
2276 return &pos;
2277 }
2278 return (pos_T *)NULL; /* never found it */
2279}
2280
2281/*
2282 * Check if line[] contains a / / comment.
2283 * Return MAXCOL if not, otherwise return the column.
2284 * TODO: skip strings.
2285 */
2286 static int
2287check_linecomment(line)
2288 char_u *line;
2289{
2290 char_u *p;
2291
2292 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002293#ifdef FEAT_LISP
2294 /* skip Lispish one-line comments */
2295 if (curbuf->b_p_lisp)
2296 {
2297 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2298 {
2299 int instr = FALSE; /* inside of string */
2300
2301 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002302 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002303 {
2304 if (*p == '"')
2305 {
2306 if (instr)
2307 {
2308 if (*(p - 1) != '\\') /* skip escaped quote */
2309 instr = FALSE;
2310 }
2311 else if (p == line || ((p - line) >= 2
2312 /* skip #\" form */
2313 && *(p - 1) != '\\' && *(p - 2) != '#'))
2314 instr = TRUE;
2315 }
2316 else if (!instr && ((p - line) < 2
2317 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2318 break; /* found! */
2319 ++p;
2320 }
2321 }
2322 else
2323 p = NULL;
2324 }
2325 else
2326#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002327 while ((p = vim_strchr(p, '/')) != NULL)
2328 {
Bram Moolenaar78d4aba2008-01-01 14:43:35 +00002329 /* accept a double /, unless it's preceded with * and followed by *,
2330 * because * / / * is an end and start of a C comment */
2331 if (p[1] == '/' && (p == line || p[-1] != '*' || p[2] != '*'))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002332 break;
2333 ++p;
2334 }
2335
2336 if (p == NULL)
2337 return MAXCOL;
2338 return (int)(p - line);
2339}
2340
2341/*
2342 * Move cursor briefly to character matching the one under the cursor.
2343 * Used for Insert mode and "r" command.
2344 * Show the match only if it is visible on the screen.
2345 * If there isn't a match, then beep.
2346 */
2347 void
2348showmatch(c)
2349 int c; /* char to show match for */
2350{
2351 pos_T *lpos, save_cursor;
2352 pos_T mpos;
2353 colnr_T vcol;
2354 long save_so;
2355 long save_siso;
2356#ifdef CURSOR_SHAPE
2357 int save_state;
2358#endif
2359 colnr_T save_dollar_vcol;
2360 char_u *p;
2361
2362 /*
2363 * Only show match for chars in the 'matchpairs' option.
2364 */
2365 /* 'matchpairs' is "x:y,x:y" */
2366 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2367 {
2368#ifdef FEAT_RIGHTLEFT
2369 if (*p == c && (curwin->w_p_rl ^ p_ri))
2370 break;
2371#endif
2372 p += 2;
2373 if (*p == c
2374#ifdef FEAT_RIGHTLEFT
2375 && !(curwin->w_p_rl ^ p_ri)
2376#endif
2377 )
2378 break;
2379 if (p[1] != ',')
2380 return;
2381 }
2382
2383 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2384 vim_beep();
2385 else if (lpos->lnum >= curwin->w_topline)
2386 {
2387 if (!curwin->w_p_wrap)
2388 getvcol(curwin, lpos, NULL, &vcol, NULL);
2389 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2390 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2391 {
2392 mpos = *lpos; /* save the pos, update_screen() may change it */
2393 save_cursor = curwin->w_cursor;
2394 save_so = p_so;
2395 save_siso = p_siso;
2396 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2397 * stop displaying the "$". */
2398 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2399 dollar_vcol = 0;
2400 ++curwin->w_virtcol; /* do display ')' just before "$" */
2401 update_screen(VALID); /* show the new char first */
2402
2403 save_dollar_vcol = dollar_vcol;
2404#ifdef CURSOR_SHAPE
2405 save_state = State;
2406 State = SHOWMATCH;
2407 ui_cursor_shape(); /* may show different cursor shape */
2408#endif
2409 curwin->w_cursor = mpos; /* move to matching char */
2410 p_so = 0; /* don't use 'scrolloff' here */
2411 p_siso = 0; /* don't use 'sidescrolloff' here */
2412 showruler(FALSE);
2413 setcursor();
2414 cursor_on(); /* make sure that the cursor is shown */
2415 out_flush();
2416#ifdef FEAT_GUI
2417 if (gui.in_use)
2418 {
2419 gui_update_cursor(TRUE, FALSE);
2420 gui_mch_flush();
2421 }
2422#endif
2423 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2424 * which resets it if the matching position is in a previous line
2425 * and has a higher column number. */
2426 dollar_vcol = save_dollar_vcol;
2427
2428 /*
2429 * brief pause, unless 'm' is present in 'cpo' and a character is
2430 * available.
2431 */
2432 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2433 ui_delay(p_mat * 100L, TRUE);
2434 else if (!char_avail())
2435 ui_delay(p_mat * 100L, FALSE);
2436 curwin->w_cursor = save_cursor; /* restore cursor position */
2437 p_so = save_so;
2438 p_siso = save_siso;
2439#ifdef CURSOR_SHAPE
2440 State = save_state;
2441 ui_cursor_shape(); /* may show different cursor shape */
2442#endif
2443 }
2444 }
2445}
2446
2447/*
2448 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002449 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002450 * space or a line break. Also stop at an empty line.
2451 * Return OK if the next sentence was found.
2452 */
2453 int
2454findsent(dir, count)
2455 int dir;
2456 long count;
2457{
2458 pos_T pos, tpos;
2459 int c;
2460 int (*func) __ARGS((pos_T *));
2461 int startlnum;
2462 int noskip = FALSE; /* do not skip blanks */
2463 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002464 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002465
2466 pos = curwin->w_cursor;
2467 if (dir == FORWARD)
2468 func = incl;
2469 else
2470 func = decl;
2471
2472 while (count--)
2473 {
2474 /*
2475 * if on an empty line, skip upto a non-empty line
2476 */
2477 if (gchar_pos(&pos) == NUL)
2478 {
2479 do
2480 if ((*func)(&pos) == -1)
2481 break;
2482 while (gchar_pos(&pos) == NUL);
2483 if (dir == FORWARD)
2484 goto found;
2485 }
2486 /*
2487 * if on the start of a paragraph or a section and searching forward,
2488 * go to the next line
2489 */
2490 else if (dir == FORWARD && pos.col == 0 &&
2491 startPS(pos.lnum, NUL, FALSE))
2492 {
2493 if (pos.lnum == curbuf->b_ml.ml_line_count)
2494 return FAIL;
2495 ++pos.lnum;
2496 goto found;
2497 }
2498 else if (dir == BACKWARD)
2499 decl(&pos);
2500
2501 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002502 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002503 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2504 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2505 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002506 if (vim_strchr((char_u *)".!?", c) != NULL)
2507 {
2508 /* Only skip over a '.', '!' and '?' once. */
2509 if (found_dot)
2510 break;
2511 found_dot = TRUE;
2512 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002513 if (decl(&pos) == -1)
2514 break;
2515 /* when going forward: Stop in front of empty line */
2516 if (lineempty(pos.lnum) && dir == FORWARD)
2517 {
2518 incl(&pos);
2519 goto found;
2520 }
2521 }
2522
2523 /* remember the line where the search started */
2524 startlnum = pos.lnum;
2525 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2526
2527 for (;;) /* find end of sentence */
2528 {
2529 c = gchar_pos(&pos);
2530 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2531 {
2532 if (dir == BACKWARD && pos.lnum != startlnum)
2533 ++pos.lnum;
2534 break;
2535 }
2536 if (c == '.' || c == '!' || c == '?')
2537 {
2538 tpos = pos;
2539 do
2540 if ((c = inc(&tpos)) == -1)
2541 break;
2542 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2543 != NULL);
2544 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2545 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2546 && gchar_pos(&tpos) == ' ')))
2547 {
2548 pos = tpos;
2549 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2550 inc(&pos);
2551 break;
2552 }
2553 }
2554 if ((*func)(&pos) == -1)
2555 {
2556 if (count)
2557 return FAIL;
2558 noskip = TRUE;
2559 break;
2560 }
2561 }
2562found:
2563 /* skip white space */
2564 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2565 if (incl(&pos) == -1)
2566 break;
2567 }
2568
2569 setpcmark();
2570 curwin->w_cursor = pos;
2571 return OK;
2572}
2573
2574/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002575 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002576 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002577 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002578 * If 'what' is '{' or '}' we go to the next section.
2579 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002580 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002581 */
2582 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002583findpar(pincl, dir, count, what, both)
2584 int *pincl; /* Return: TRUE if last char is to be included */
2585 int dir;
2586 long count;
2587 int what;
2588 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002589{
2590 linenr_T curr;
2591 int did_skip; /* TRUE after separating lines have been skipped */
2592 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002593 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002594#ifdef FEAT_FOLDING
2595 linenr_T fold_first; /* first line of a closed fold */
2596 linenr_T fold_last; /* last line of a closed fold */
2597 int fold_skipped; /* TRUE if a closed fold was skipped this
2598 iteration */
2599#endif
2600
2601 curr = curwin->w_cursor.lnum;
2602
2603 while (count--)
2604 {
2605 did_skip = FALSE;
2606 for (first = TRUE; ; first = FALSE)
2607 {
2608 if (*ml_get(curr) != NUL)
2609 did_skip = TRUE;
2610
2611#ifdef FEAT_FOLDING
2612 /* skip folded lines */
2613 fold_skipped = FALSE;
2614 if (first && hasFolding(curr, &fold_first, &fold_last))
2615 {
2616 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2617 fold_skipped = TRUE;
2618 }
2619#endif
2620
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002621 /* POSIX has it's own ideas of what a paragraph boundary is and it
2622 * doesn't match historical Vi: It also stops at a "{" in the
2623 * first column and at an empty line. */
2624 if (!first && did_skip && (startPS(curr, what, both)
2625 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002626 break;
2627
2628#ifdef FEAT_FOLDING
2629 if (fold_skipped)
2630 curr -= dir;
2631#endif
2632 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2633 {
2634 if (count)
2635 return FALSE;
2636 curr -= dir;
2637 break;
2638 }
2639 }
2640 }
2641 setpcmark();
2642 if (both && *ml_get(curr) == '}') /* include line with '}' */
2643 ++curr;
2644 curwin->w_cursor.lnum = curr;
2645 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2646 {
2647 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2648 {
2649 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002650 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002651 }
2652 }
2653 else
2654 curwin->w_cursor.col = 0;
2655 return TRUE;
2656}
2657
2658/*
2659 * check if the string 's' is a nroff macro that is in option 'opt'
2660 */
2661 static int
2662inmacro(opt, s)
2663 char_u *opt;
2664 char_u *s;
2665{
2666 char_u *macro;
2667
2668 for (macro = opt; macro[0]; ++macro)
2669 {
2670 /* Accept two characters in the option being equal to two characters
2671 * in the line. A space in the option matches with a space in the
2672 * line or the line having ended. */
2673 if ( (macro[0] == s[0]
2674 || (macro[0] == ' '
2675 && (s[0] == NUL || s[0] == ' ')))
2676 && (macro[1] == s[1]
2677 || ((macro[1] == NUL || macro[1] == ' ')
2678 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2679 break;
2680 ++macro;
2681 if (macro[0] == NUL)
2682 break;
2683 }
2684 return (macro[0] != NUL);
2685}
2686
2687/*
2688 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2689 * If 'para' is '{' or '}' only check for sections.
2690 * If 'both' is TRUE also stop at '}'
2691 */
2692 int
2693startPS(lnum, para, both)
2694 linenr_T lnum;
2695 int para;
2696 int both;
2697{
2698 char_u *s;
2699
2700 s = ml_get(lnum);
2701 if (*s == para || *s == '\f' || (both && *s == '}'))
2702 return TRUE;
2703 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2704 (!para && inmacro(p_para, s + 1))))
2705 return TRUE;
2706 return FALSE;
2707}
2708
2709/*
2710 * The following routines do the word searches performed by the 'w', 'W',
2711 * 'b', 'B', 'e', and 'E' commands.
2712 */
2713
2714/*
2715 * To perform these searches, characters are placed into one of three
2716 * classes, and transitions between classes determine word boundaries.
2717 *
2718 * The classes are:
2719 *
2720 * 0 - white space
2721 * 1 - punctuation
2722 * 2 or higher - keyword characters (letters, digits and underscore)
2723 */
2724
2725static int cls_bigword; /* TRUE for "W", "B" or "E" */
2726
2727/*
2728 * cls() - returns the class of character at curwin->w_cursor
2729 *
2730 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2731 * from class 2 and higher are reported as class 1 since only white space
2732 * boundaries are of interest.
2733 */
2734 static int
2735cls()
2736{
2737 int c;
2738
2739 c = gchar_cursor();
2740#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2741 if (p_altkeymap && c == F_BLANK)
2742 return 0;
2743#endif
2744 if (c == ' ' || c == '\t' || c == NUL)
2745 return 0;
2746#ifdef FEAT_MBYTE
2747 if (enc_dbcs != 0 && c > 0xFF)
2748 {
2749 /* If cls_bigword, report multi-byte chars as class 1. */
2750 if (enc_dbcs == DBCS_KOR && cls_bigword)
2751 return 1;
2752
2753 /* process code leading/trailing bytes */
2754 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2755 }
2756 if (enc_utf8)
2757 {
2758 c = utf_class(c);
2759 if (c != 0 && cls_bigword)
2760 return 1;
2761 return c;
2762 }
2763#endif
2764
2765 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2766 if (cls_bigword)
2767 return 1;
2768
2769 if (vim_iswordc(c))
2770 return 2;
2771 return 1;
2772}
2773
2774
2775/*
2776 * fwd_word(count, type, eol) - move forward one word
2777 *
2778 * Returns FAIL if the cursor was already at the end of the file.
2779 * If eol is TRUE, last word stops at end of line (for operators).
2780 */
2781 int
2782fwd_word(count, bigword, eol)
2783 long count;
2784 int bigword; /* "W", "E" or "B" */
2785 int eol;
2786{
2787 int sclass; /* starting class */
2788 int i;
2789 int last_line;
2790
2791#ifdef FEAT_VIRTUALEDIT
2792 curwin->w_cursor.coladd = 0;
2793#endif
2794 cls_bigword = bigword;
2795 while (--count >= 0)
2796 {
2797#ifdef FEAT_FOLDING
2798 /* When inside a range of folded lines, move to the last char of the
2799 * last line. */
2800 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2801 coladvance((colnr_T)MAXCOL);
2802#endif
2803 sclass = cls();
2804
2805 /*
2806 * We always move at least one character, unless on the last
2807 * character in the buffer.
2808 */
2809 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2810 i = inc_cursor();
2811 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2812 return FAIL;
Bram Moolenaar9a149792007-07-10 10:38:02 +00002813 if (i >= 1 && eol && count == 0) /* started at last char in line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002814 return OK;
2815
2816 /*
2817 * Go one char past end of current word (if any)
2818 */
2819 if (sclass != 0)
2820 while (cls() == sclass)
2821 {
2822 i = inc_cursor();
2823 if (i == -1 || (i >= 1 && eol && count == 0))
2824 return OK;
2825 }
2826
2827 /*
2828 * go to next non-white
2829 */
2830 while (cls() == 0)
2831 {
2832 /*
2833 * We'll stop if we land on a blank line
2834 */
2835 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2836 break;
2837
2838 i = inc_cursor();
2839 if (i == -1 || (i >= 1 && eol && count == 0))
2840 return OK;
2841 }
2842 }
2843 return OK;
2844}
2845
2846/*
2847 * bck_word() - move backward 'count' words
2848 *
2849 * If stop is TRUE and we are already on the start of a word, move one less.
2850 *
2851 * Returns FAIL if top of the file was reached.
2852 */
2853 int
2854bck_word(count, bigword, stop)
2855 long count;
2856 int bigword;
2857 int stop;
2858{
2859 int sclass; /* starting class */
2860
2861#ifdef FEAT_VIRTUALEDIT
2862 curwin->w_cursor.coladd = 0;
2863#endif
2864 cls_bigword = bigword;
2865 while (--count >= 0)
2866 {
2867#ifdef FEAT_FOLDING
2868 /* When inside a range of folded lines, move to the first char of the
2869 * first line. */
2870 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2871 curwin->w_cursor.col = 0;
2872#endif
2873 sclass = cls();
2874 if (dec_cursor() == -1) /* started at start of file */
2875 return FAIL;
2876
2877 if (!stop || sclass == cls() || sclass == 0)
2878 {
2879 /*
2880 * Skip white space before the word.
2881 * Stop on an empty line.
2882 */
2883 while (cls() == 0)
2884 {
2885 if (curwin->w_cursor.col == 0
2886 && lineempty(curwin->w_cursor.lnum))
2887 goto finished;
2888 if (dec_cursor() == -1) /* hit start of file, stop here */
2889 return OK;
2890 }
2891
2892 /*
2893 * Move backward to start of this word.
2894 */
2895 if (skip_chars(cls(), BACKWARD))
2896 return OK;
2897 }
2898
2899 inc_cursor(); /* overshot - forward one */
2900finished:
2901 stop = FALSE;
2902 }
2903 return OK;
2904}
2905
2906/*
2907 * end_word() - move to the end of the word
2908 *
2909 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2910 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2911 * motion crosses blank lines. When the real vi crosses a blank line in an
2912 * 'e' motion, the cursor is placed on the FIRST character of the next
2913 * non-blank line. The 'E' command, however, works correctly. Since this
2914 * appears to be a bug, I have not duplicated it here.
2915 *
2916 * Returns FAIL if end of the file was reached.
2917 *
2918 * If stop is TRUE and we are already on the end of a word, move one less.
2919 * If empty is TRUE stop on an empty line.
2920 */
2921 int
2922end_word(count, bigword, stop, empty)
2923 long count;
2924 int bigword;
2925 int stop;
2926 int empty;
2927{
2928 int sclass; /* starting class */
2929
2930#ifdef FEAT_VIRTUALEDIT
2931 curwin->w_cursor.coladd = 0;
2932#endif
2933 cls_bigword = bigword;
2934 while (--count >= 0)
2935 {
2936#ifdef FEAT_FOLDING
2937 /* When inside a range of folded lines, move to the last char of the
2938 * last line. */
2939 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2940 coladvance((colnr_T)MAXCOL);
2941#endif
2942 sclass = cls();
2943 if (inc_cursor() == -1)
2944 return FAIL;
2945
2946 /*
2947 * If we're in the middle of a word, we just have to move to the end
2948 * of it.
2949 */
2950 if (cls() == sclass && sclass != 0)
2951 {
2952 /*
2953 * Move forward to end of the current word
2954 */
2955 if (skip_chars(sclass, FORWARD))
2956 return FAIL;
2957 }
2958 else if (!stop || sclass == 0)
2959 {
2960 /*
2961 * We were at the end of a word. Go to the end of the next word.
2962 * First skip white space, if 'empty' is TRUE, stop at empty line.
2963 */
2964 while (cls() == 0)
2965 {
2966 if (empty && curwin->w_cursor.col == 0
2967 && lineempty(curwin->w_cursor.lnum))
2968 goto finished;
2969 if (inc_cursor() == -1) /* hit end of file, stop here */
2970 return FAIL;
2971 }
2972
2973 /*
2974 * Move forward to the end of this word.
2975 */
2976 if (skip_chars(cls(), FORWARD))
2977 return FAIL;
2978 }
2979 dec_cursor(); /* overshot - one char backward */
2980finished:
2981 stop = FALSE; /* we move only one word less */
2982 }
2983 return OK;
2984}
2985
2986/*
2987 * Move back to the end of the word.
2988 *
2989 * Returns FAIL if start of the file was reached.
2990 */
2991 int
2992bckend_word(count, bigword, eol)
2993 long count;
2994 int bigword; /* TRUE for "B" */
2995 int eol; /* TRUE: stop at end of line. */
2996{
2997 int sclass; /* starting class */
2998 int i;
2999
3000#ifdef FEAT_VIRTUALEDIT
3001 curwin->w_cursor.coladd = 0;
3002#endif
3003 cls_bigword = bigword;
3004 while (--count >= 0)
3005 {
3006 sclass = cls();
3007 if ((i = dec_cursor()) == -1)
3008 return FAIL;
3009 if (eol && i == 1)
3010 return OK;
3011
3012 /*
3013 * Move backward to before the start of this word.
3014 */
3015 if (sclass != 0)
3016 {
3017 while (cls() == sclass)
3018 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3019 return OK;
3020 }
3021
3022 /*
3023 * Move backward to end of the previous word
3024 */
3025 while (cls() == 0)
3026 {
3027 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3028 break;
3029 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3030 return OK;
3031 }
3032 }
3033 return OK;
3034}
3035
3036/*
3037 * Skip a row of characters of the same class.
3038 * Return TRUE when end-of-file reached, FALSE otherwise.
3039 */
3040 static int
3041skip_chars(cclass, dir)
3042 int cclass;
3043 int dir;
3044{
3045 while (cls() == cclass)
3046 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3047 return TRUE;
3048 return FALSE;
3049}
3050
3051#ifdef FEAT_TEXTOBJ
3052/*
3053 * Go back to the start of the word or the start of white space
3054 */
3055 static void
3056back_in_line()
3057{
3058 int sclass; /* starting class */
3059
3060 sclass = cls();
3061 for (;;)
3062 {
3063 if (curwin->w_cursor.col == 0) /* stop at start of line */
3064 break;
3065 dec_cursor();
3066 if (cls() != sclass) /* stop at start of word */
3067 {
3068 inc_cursor();
3069 break;
3070 }
3071 }
3072}
3073
3074 static void
3075find_first_blank(posp)
3076 pos_T *posp;
3077{
3078 int c;
3079
3080 while (decl(posp) != -1)
3081 {
3082 c = gchar_pos(posp);
3083 if (!vim_iswhite(c))
3084 {
3085 incl(posp);
3086 break;
3087 }
3088 }
3089}
3090
3091/*
3092 * Skip count/2 sentences and count/2 separating white spaces.
3093 */
3094 static void
3095findsent_forward(count, at_start_sent)
3096 long count;
3097 int at_start_sent; /* cursor is at start of sentence */
3098{
3099 while (count--)
3100 {
3101 findsent(FORWARD, 1L);
3102 if (at_start_sent)
3103 find_first_blank(&curwin->w_cursor);
3104 if (count == 0 || at_start_sent)
3105 decl(&curwin->w_cursor);
3106 at_start_sent = !at_start_sent;
3107 }
3108}
3109
3110/*
3111 * Find word under cursor, cursor at end.
3112 * Used while an operator is pending, and in Visual mode.
3113 */
3114 int
3115current_word(oap, count, include, bigword)
3116 oparg_T *oap;
3117 long count;
3118 int include; /* TRUE: include word and white space */
3119 int bigword; /* FALSE == word, TRUE == WORD */
3120{
3121 pos_T start_pos;
3122 pos_T pos;
3123 int inclusive = TRUE;
3124 int include_white = FALSE;
3125
3126 cls_bigword = bigword;
Bram Moolenaarfd2ac762006-03-01 22:09:21 +00003127 clearpos(&start_pos);
Bram Moolenaar071d4272004-06-13 20:20:40 +00003128
3129#ifdef FEAT_VISUAL
3130 /* Correct cursor when 'selection' is exclusive */
3131 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3132 dec_cursor();
3133
3134 /*
3135 * When Visual mode is not active, or when the VIsual area is only one
3136 * character, select the word and/or white space under the cursor.
3137 */
3138 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3139#endif
3140 {
3141 /*
3142 * Go to start of current word or white space.
3143 */
3144 back_in_line();
3145 start_pos = curwin->w_cursor;
3146
3147 /*
3148 * If the start is on white space, and white space should be included
3149 * (" word"), or start is not on white space, and white space should
3150 * not be included ("word"), find end of word.
3151 */
3152 if ((cls() == 0) == include)
3153 {
3154 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3155 return FAIL;
3156 }
3157 else
3158 {
3159 /*
3160 * If the start is not on white space, and white space should be
3161 * included ("word "), or start is on white space and white
3162 * space should not be included (" "), find start of word.
3163 * If we end up in the first column of the next line (single char
3164 * word) back up to end of the line.
3165 */
3166 fwd_word(1L, bigword, TRUE);
3167 if (curwin->w_cursor.col == 0)
3168 decl(&curwin->w_cursor);
3169 else
3170 oneleft();
3171
3172 if (include)
3173 include_white = TRUE;
3174 }
3175
3176#ifdef FEAT_VISUAL
3177 if (VIsual_active)
3178 {
3179 /* should do something when inclusive == FALSE ! */
3180 VIsual = start_pos;
3181 redraw_curbuf_later(INVERTED); /* update the inversion */
3182 }
3183 else
3184#endif
3185 {
3186 oap->start = start_pos;
3187 oap->motion_type = MCHAR;
3188 }
3189 --count;
3190 }
3191
3192 /*
3193 * When count is still > 0, extend with more objects.
3194 */
3195 while (count > 0)
3196 {
3197 inclusive = TRUE;
3198#ifdef FEAT_VISUAL
3199 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3200 {
3201 /*
3202 * In Visual mode, with cursor at start: move cursor back.
3203 */
3204 if (decl(&curwin->w_cursor) == -1)
3205 return FAIL;
3206 if (include != (cls() != 0))
3207 {
3208 if (bck_word(1L, bigword, TRUE) == FAIL)
3209 return FAIL;
3210 }
3211 else
3212 {
3213 if (bckend_word(1L, bigword, TRUE) == FAIL)
3214 return FAIL;
3215 (void)incl(&curwin->w_cursor);
3216 }
3217 }
3218 else
3219#endif
3220 {
3221 /*
3222 * Move cursor forward one word and/or white area.
3223 */
3224 if (incl(&curwin->w_cursor) == -1)
3225 return FAIL;
3226 if (include != (cls() == 0))
3227 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003228 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003229 return FAIL;
3230 /*
3231 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003232 * the first character on the line.
3233 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003234 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003235 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003236 inclusive = FALSE;
3237 }
3238 else
3239 {
3240 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3241 return FAIL;
3242 }
3243 }
3244 --count;
3245 }
3246
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003247 if (include_white && (cls() != 0
3248 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003249 {
3250 /*
3251 * If we don't include white space at the end, move the start
3252 * to include some white space there. This makes "daw" work
3253 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003254 * word). Also when "2daw" deletes "word." at the end of the line
3255 * (cursor is at start of next line).
3256 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003257 */
3258 pos = curwin->w_cursor; /* save cursor position */
3259 curwin->w_cursor = start_pos;
3260 if (oneleft() == OK)
3261 {
3262 back_in_line();
3263 if (cls() == 0 && curwin->w_cursor.col > 0)
3264 {
3265#ifdef FEAT_VISUAL
3266 if (VIsual_active)
3267 VIsual = curwin->w_cursor;
3268 else
3269#endif
3270 oap->start = curwin->w_cursor;
3271 }
3272 }
3273 curwin->w_cursor = pos; /* put cursor back at end */
3274 }
3275
3276#ifdef FEAT_VISUAL
3277 if (VIsual_active)
3278 {
3279 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3280 inc_cursor();
3281 if (VIsual_mode == 'V')
3282 {
3283 VIsual_mode = 'v';
3284 redraw_cmdline = TRUE; /* show mode later */
3285 }
3286 }
3287 else
3288#endif
3289 oap->inclusive = inclusive;
3290
3291 return OK;
3292}
3293
3294/*
3295 * Find sentence(s) under the cursor, cursor at end.
3296 * When Visual active, extend it by one or more sentences.
3297 */
3298 int
3299current_sent(oap, count, include)
3300 oparg_T *oap;
3301 long count;
3302 int include;
3303{
3304 pos_T start_pos;
3305 pos_T pos;
3306 int start_blank;
3307 int c;
3308 int at_start_sent;
3309 long ncount;
3310
3311 start_pos = curwin->w_cursor;
3312 pos = start_pos;
3313 findsent(FORWARD, 1L); /* Find start of next sentence. */
3314
3315#ifdef FEAT_VISUAL
3316 /*
3317 * When visual area is bigger than one character: Extend it.
3318 */
3319 if (VIsual_active && !equalpos(start_pos, VIsual))
3320 {
3321extend:
3322 if (lt(start_pos, VIsual))
3323 {
3324 /*
3325 * Cursor at start of Visual area.
3326 * Find out where we are:
3327 * - in the white space before a sentence
3328 * - in a sentence or just after it
3329 * - at the start of a sentence
3330 */
3331 at_start_sent = TRUE;
3332 decl(&pos);
3333 while (lt(pos, curwin->w_cursor))
3334 {
3335 c = gchar_pos(&pos);
3336 if (!vim_iswhite(c))
3337 {
3338 at_start_sent = FALSE;
3339 break;
3340 }
3341 incl(&pos);
3342 }
3343 if (!at_start_sent)
3344 {
3345 findsent(BACKWARD, 1L);
3346 if (equalpos(curwin->w_cursor, start_pos))
3347 at_start_sent = TRUE; /* exactly at start of sentence */
3348 else
3349 /* inside a sentence, go to its end (start of next) */
3350 findsent(FORWARD, 1L);
3351 }
3352 if (include) /* "as" gets twice as much as "is" */
3353 count *= 2;
3354 while (count--)
3355 {
3356 if (at_start_sent)
3357 find_first_blank(&curwin->w_cursor);
3358 c = gchar_cursor();
3359 if (!at_start_sent || (!include && !vim_iswhite(c)))
3360 findsent(BACKWARD, 1L);
3361 at_start_sent = !at_start_sent;
3362 }
3363 }
3364 else
3365 {
3366 /*
3367 * Cursor at end of Visual area.
3368 * Find out where we are:
3369 * - just before a sentence
3370 * - just before or in the white space before a sentence
3371 * - in a sentence
3372 */
3373 incl(&pos);
3374 at_start_sent = TRUE;
3375 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3376 {
3377 at_start_sent = FALSE;
3378 while (lt(pos, curwin->w_cursor))
3379 {
3380 c = gchar_pos(&pos);
3381 if (!vim_iswhite(c))
3382 {
3383 at_start_sent = TRUE;
3384 break;
3385 }
3386 incl(&pos);
3387 }
3388 if (at_start_sent) /* in the sentence */
3389 findsent(BACKWARD, 1L);
3390 else /* in/before white before a sentence */
3391 curwin->w_cursor = start_pos;
3392 }
3393
3394 if (include) /* "as" gets twice as much as "is" */
3395 count *= 2;
3396 findsent_forward(count, at_start_sent);
3397 if (*p_sel == 'e')
3398 ++curwin->w_cursor.col;
3399 }
3400 return OK;
3401 }
3402#endif
3403
3404 /*
3405 * If cursor started on blank, check if it is just before the start of the
3406 * next sentence.
3407 */
3408 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3409 incl(&pos);
3410 if (equalpos(pos, curwin->w_cursor))
3411 {
3412 start_blank = TRUE;
3413 find_first_blank(&start_pos); /* go back to first blank */
3414 }
3415 else
3416 {
3417 start_blank = FALSE;
3418 findsent(BACKWARD, 1L);
3419 start_pos = curwin->w_cursor;
3420 }
3421 if (include)
3422 ncount = count * 2;
3423 else
3424 {
3425 ncount = count;
3426 if (start_blank)
3427 --ncount;
3428 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003429 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003430 findsent_forward(ncount, TRUE);
3431 else
3432 decl(&curwin->w_cursor);
3433
3434 if (include)
3435 {
3436 /*
3437 * If the blank in front of the sentence is included, exclude the
3438 * blanks at the end of the sentence, go back to the first blank.
3439 * If there are no trailing blanks, try to include leading blanks.
3440 */
3441 if (start_blank)
3442 {
3443 find_first_blank(&curwin->w_cursor);
3444 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3445 if (vim_iswhite(c))
3446 decl(&curwin->w_cursor);
3447 }
3448 else if (c = gchar_cursor(), !vim_iswhite(c))
3449 find_first_blank(&start_pos);
3450 }
3451
3452#ifdef FEAT_VISUAL
3453 if (VIsual_active)
3454 {
3455 /* avoid getting stuck with "is" on a single space before a sent. */
3456 if (equalpos(start_pos, curwin->w_cursor))
3457 goto extend;
3458 if (*p_sel == 'e')
3459 ++curwin->w_cursor.col;
3460 VIsual = start_pos;
3461 VIsual_mode = 'v';
3462 redraw_curbuf_later(INVERTED); /* update the inversion */
3463 }
3464 else
3465#endif
3466 {
3467 /* include a newline after the sentence, if there is one */
3468 if (incl(&curwin->w_cursor) == -1)
3469 oap->inclusive = TRUE;
3470 else
3471 oap->inclusive = FALSE;
3472 oap->start = start_pos;
3473 oap->motion_type = MCHAR;
3474 }
3475 return OK;
3476}
3477
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003478/*
3479 * Find block under the cursor, cursor at end.
3480 * "what" and "other" are two matching parenthesis/paren/etc.
3481 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 int
3483current_block(oap, count, include, what, other)
3484 oparg_T *oap;
3485 long count;
3486 int include; /* TRUE == include white space */
3487 int what; /* '(', '{', etc. */
3488 int other; /* ')', '}', etc. */
3489{
3490 pos_T old_pos;
3491 pos_T *pos = NULL;
3492 pos_T start_pos;
3493 pos_T *end_pos;
3494 pos_T old_start, old_end;
3495 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003496 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003497
3498 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003499 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003500 old_start = old_end;
3501
3502 /*
3503 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3504 */
3505#ifdef FEAT_VISUAL
3506 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3507#endif
3508 {
3509 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003510 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003511 while (inindent(1))
3512 if (inc_cursor() != 0)
3513 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003514 if (gchar_cursor() == what)
3515 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003516 ++curwin->w_cursor.col;
3517 }
3518#ifdef FEAT_VISUAL
3519 else if (lt(VIsual, curwin->w_cursor))
3520 {
3521 old_start = VIsual;
3522 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3523 }
3524 else
3525 old_end = VIsual;
3526#endif
3527
3528 /*
3529 * Search backwards for unclosed '(', '{', etc..
3530 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003531 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003532 */
3533 save_cpo = p_cpo;
3534 p_cpo = (char_u *)"%";
3535 while (count-- > 0)
3536 {
3537 if ((pos = findmatch(NULL, what)) == NULL)
3538 break;
3539 curwin->w_cursor = *pos;
3540 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3541 }
3542 p_cpo = save_cpo;
3543
3544 /*
3545 * Search for matching ')', '}', etc.
3546 * Put this position in curwin->w_cursor.
3547 */
3548 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3549 {
3550 curwin->w_cursor = old_pos;
3551 return FAIL;
3552 }
3553 curwin->w_cursor = *end_pos;
3554
3555 /*
3556 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3557 * If the ending '}' is only preceded by indent, skip that indent.
3558 * But only if the resulting area is not smaller than what we started with.
3559 */
3560 while (!include)
3561 {
3562 incl(&start_pos);
3563 sol = (curwin->w_cursor.col == 0);
3564 decl(&curwin->w_cursor);
3565 if (what == '{')
3566 while (inindent(1))
3567 {
3568 sol = TRUE;
3569 if (decl(&curwin->w_cursor) != 0)
3570 break;
3571 }
3572#ifdef FEAT_VISUAL
3573 /*
3574 * In Visual mode, when the resulting area is not bigger than what we
3575 * started with, extend it to the next block, and then exclude again.
3576 */
3577 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3578 && VIsual_active)
3579 {
3580 curwin->w_cursor = old_start;
3581 decl(&curwin->w_cursor);
3582 if ((pos = findmatch(NULL, what)) == NULL)
3583 {
3584 curwin->w_cursor = old_pos;
3585 return FAIL;
3586 }
3587 start_pos = *pos;
3588 curwin->w_cursor = *pos;
3589 if ((end_pos = findmatch(NULL, other)) == NULL)
3590 {
3591 curwin->w_cursor = old_pos;
3592 return FAIL;
3593 }
3594 curwin->w_cursor = *end_pos;
3595 }
3596 else
3597#endif
3598 break;
3599 }
3600
3601#ifdef FEAT_VISUAL
3602 if (VIsual_active)
3603 {
3604 if (*p_sel == 'e')
3605 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003606 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003607 inc(&curwin->w_cursor); /* include the line break */
3608 VIsual = start_pos;
3609 VIsual_mode = 'v';
3610 redraw_curbuf_later(INVERTED); /* update the inversion */
3611 showmode();
3612 }
3613 else
3614#endif
3615 {
3616 oap->start = start_pos;
3617 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003618 oap->inclusive = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003619 if (sol)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003620 incl(&curwin->w_cursor);
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003621 else if (lt(start_pos, curwin->w_cursor))
3622 /* Include the character under the cursor. */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003623 oap->inclusive = TRUE;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003624 else
3625 /* End is before the start (no text in between <>, [], etc.): don't
3626 * operate on any text. */
3627 curwin->w_cursor = start_pos;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003628 }
3629
3630 return OK;
3631}
3632
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003633static int in_html_tag __ARGS((int));
3634
3635/*
3636 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3637 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3638 */
3639 static int
3640in_html_tag(end_tag)
3641 int end_tag;
3642{
3643 char_u *line = ml_get_curline();
3644 char_u *p;
3645 int c;
3646 int lc = NUL;
3647 pos_T pos;
3648
3649#ifdef FEAT_MBYTE
3650 if (enc_dbcs)
3651 {
3652 char_u *lp = NULL;
3653
3654 /* We search forward until the cursor, because searching backwards is
3655 * very slow for DBCS encodings. */
3656 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3657 if (*p == '>' || *p == '<')
3658 {
3659 lc = *p;
3660 lp = p;
3661 }
3662 if (*p != '<') /* check for '<' under cursor */
3663 {
3664 if (lc != '<')
3665 return FALSE;
3666 p = lp;
3667 }
3668 }
3669 else
3670#endif
3671 {
3672 for (p = line + curwin->w_cursor.col; p > line; )
3673 {
3674 if (*p == '<') /* find '<' under/before cursor */
3675 break;
3676 mb_ptr_back(line, p);
3677 if (*p == '>') /* find '>' before cursor */
3678 break;
3679 }
3680 if (*p != '<')
3681 return FALSE;
3682 }
3683
3684 pos.lnum = curwin->w_cursor.lnum;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003685 pos.col = (colnr_T)(p - line);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003686
3687 mb_ptr_adv(p);
3688 if (end_tag)
3689 /* check that there is a '/' after the '<' */
3690 return *p == '/';
3691
3692 /* check that there is no '/' after the '<' */
3693 if (*p == '/')
3694 return FALSE;
3695
3696 /* check that the matching '>' is not preceded by '/' */
3697 for (;;)
3698 {
3699 if (inc(&pos) < 0)
3700 return FALSE;
3701 c = *ml_get_pos(&pos);
3702 if (c == '>')
3703 break;
3704 lc = c;
3705 }
3706 return lc != '/';
3707}
3708
3709/*
3710 * Find tag block under the cursor, cursor at end.
3711 */
3712 int
3713current_tagblock(oap, count_arg, include)
3714 oparg_T *oap;
3715 long count_arg;
3716 int include; /* TRUE == include white space */
3717{
3718 long count = count_arg;
3719 long n;
3720 pos_T old_pos;
3721 pos_T start_pos;
3722 pos_T end_pos;
3723 pos_T old_start, old_end;
3724 char_u *spat, *epat;
3725 char_u *p;
3726 char_u *cp;
3727 int len;
3728 int r;
3729 int do_include = include;
3730 int save_p_ws = p_ws;
3731 int retval = FAIL;
3732
3733 p_ws = FALSE;
3734
3735 old_pos = curwin->w_cursor;
3736 old_end = curwin->w_cursor; /* remember where we started */
3737 old_start = old_end;
3738
3739 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003740 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003741 */
3742#ifdef FEAT_VISUAL
3743 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3744#endif
3745 {
3746 setpcmark();
3747
3748 /* ignore indent */
3749 while (inindent(1))
3750 if (inc_cursor() != 0)
3751 break;
3752
3753 if (in_html_tag(FALSE))
3754 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003755 /* cursor on start tag, move to its '>' */
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003756 while (*ml_get_cursor() != '>')
3757 if (inc_cursor() < 0)
3758 break;
3759 }
3760 else if (in_html_tag(TRUE))
3761 {
3762 /* cursor on end tag, move to just before it */
3763 while (*ml_get_cursor() != '<')
3764 if (dec_cursor() < 0)
3765 break;
3766 dec_cursor();
3767 old_end = curwin->w_cursor;
3768 }
3769 }
3770#ifdef FEAT_VISUAL
3771 else if (lt(VIsual, curwin->w_cursor))
3772 {
3773 old_start = VIsual;
3774 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3775 }
3776 else
3777 old_end = VIsual;
3778#endif
3779
3780again:
3781 /*
3782 * Search backwards for unclosed "<aaa>".
3783 * Put this position in start_pos.
3784 */
3785 for (n = 0; n < count; ++n)
3786 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003787 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003788 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003789 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
Bram Moolenaar76929292008-01-06 19:07:36 +00003790 NULL, (linenr_T)0, 0L) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003791 {
3792 curwin->w_cursor = old_pos;
3793 goto theend;
3794 }
3795 }
3796 start_pos = curwin->w_cursor;
3797
3798 /*
3799 * Search for matching "</aaa>". First isolate the "aaa".
3800 */
3801 inc_cursor();
3802 p = ml_get_cursor();
3803 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3804 ;
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00003805 len = (int)(cp - p);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003806 if (len == 0)
3807 {
3808 curwin->w_cursor = old_pos;
3809 goto theend;
3810 }
3811 spat = alloc(len + 29);
3812 epat = alloc(len + 9);
3813 if (spat == NULL || epat == NULL)
3814 {
3815 vim_free(spat);
3816 vim_free(epat);
3817 curwin->w_cursor = old_pos;
3818 goto theend;
3819 }
3820 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3821 sprintf((char *)epat, "</%.*s>\\c", len, p);
3822
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003823 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
Bram Moolenaar76929292008-01-06 19:07:36 +00003824 0, NULL, (linenr_T)0, 0L);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003825
3826 vim_free(spat);
3827 vim_free(epat);
3828
3829 if (r < 1 || lt(curwin->w_cursor, old_end))
3830 {
3831 /* Can't find other end or it's before the previous end. Could be a
3832 * HTML tag that doesn't have a matching end. Search backwards for
3833 * another starting tag. */
3834 count = 1;
3835 curwin->w_cursor = start_pos;
3836 goto again;
3837 }
3838
3839 if (do_include || r < 1)
3840 {
3841 /* Include up to the '>'. */
3842 while (*ml_get_cursor() != '>')
3843 if (inc_cursor() < 0)
3844 break;
3845 }
3846 else
3847 {
3848 /* Exclude the '<' of the end tag. */
3849 if (*ml_get_cursor() == '<')
3850 dec_cursor();
3851 }
3852 end_pos = curwin->w_cursor;
3853
3854 if (!do_include)
3855 {
3856 /* Exclude the start tag. */
3857 curwin->w_cursor = start_pos;
3858 while (inc_cursor() >= 0)
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003859 if (*ml_get_cursor() == '>')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003860 {
3861 inc_cursor();
3862 start_pos = curwin->w_cursor;
3863 break;
3864 }
3865 curwin->w_cursor = end_pos;
3866
Bram Moolenaar45360022005-07-21 21:08:21 +00003867 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003868 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003869 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003870 {
3871 do_include = TRUE;
3872 curwin->w_cursor = old_start;
3873 count = count_arg;
3874 goto again;
3875 }
3876 }
3877
3878#ifdef FEAT_VISUAL
3879 if (VIsual_active)
3880 {
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003881 /* If the end is before the start there is no text between tags, select
3882 * the char under the cursor. */
3883 if (lt(end_pos, start_pos))
3884 curwin->w_cursor = start_pos;
3885 else if (*p_sel == 'e')
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003886 ++curwin->w_cursor.col;
3887 VIsual = start_pos;
3888 VIsual_mode = 'v';
3889 redraw_curbuf_later(INVERTED); /* update the inversion */
3890 showmode();
3891 }
3892 else
3893#endif
3894 {
3895 oap->start = start_pos;
3896 oap->motion_type = MCHAR;
Bram Moolenaar1864a4e2007-06-19 10:56:05 +00003897 if (lt(end_pos, start_pos))
3898 {
3899 /* End is before the start: there is no text between tags; operate
3900 * on an empty area. */
3901 curwin->w_cursor = start_pos;
3902 oap->inclusive = FALSE;
3903 }
3904 else
3905 oap->inclusive = TRUE;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003906 }
3907 retval = OK;
3908
3909theend:
3910 p_ws = save_p_ws;
3911 return retval;
3912}
3913
Bram Moolenaar071d4272004-06-13 20:20:40 +00003914 int
3915current_par(oap, count, include, type)
3916 oparg_T *oap;
3917 long count;
3918 int include; /* TRUE == include white space */
3919 int type; /* 'p' for paragraph, 'S' for section */
3920{
3921 linenr_T start_lnum;
3922 linenr_T end_lnum;
3923 int white_in_front;
3924 int dir;
3925 int start_is_white;
3926 int prev_start_is_white;
3927 int retval = OK;
3928 int do_white = FALSE;
3929 int t;
3930 int i;
3931
3932 if (type == 'S') /* not implemented yet */
3933 return FAIL;
3934
3935 start_lnum = curwin->w_cursor.lnum;
3936
3937#ifdef FEAT_VISUAL
3938 /*
3939 * When visual area is more than one line: extend it.
3940 */
3941 if (VIsual_active && start_lnum != VIsual.lnum)
3942 {
3943extend:
3944 if (start_lnum < VIsual.lnum)
3945 dir = BACKWARD;
3946 else
3947 dir = FORWARD;
3948 for (i = count; --i >= 0; )
3949 {
3950 if (start_lnum ==
3951 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3952 {
3953 retval = FAIL;
3954 break;
3955 }
3956
3957 prev_start_is_white = -1;
3958 for (t = 0; t < 2; ++t)
3959 {
3960 start_lnum += dir;
3961 start_is_white = linewhite(start_lnum);
3962 if (prev_start_is_white == start_is_white)
3963 {
3964 start_lnum -= dir;
3965 break;
3966 }
3967 for (;;)
3968 {
3969 if (start_lnum == (dir == BACKWARD
3970 ? 1 : curbuf->b_ml.ml_line_count))
3971 break;
3972 if (start_is_white != linewhite(start_lnum + dir)
3973 || (!start_is_white
3974 && startPS(start_lnum + (dir > 0
3975 ? 1 : 0), 0, 0)))
3976 break;
3977 start_lnum += dir;
3978 }
3979 if (!include)
3980 break;
3981 if (start_lnum == (dir == BACKWARD
3982 ? 1 : curbuf->b_ml.ml_line_count))
3983 break;
3984 prev_start_is_white = start_is_white;
3985 }
3986 }
3987 curwin->w_cursor.lnum = start_lnum;
3988 curwin->w_cursor.col = 0;
3989 return retval;
3990 }
3991#endif
3992
3993 /*
3994 * First move back to the start_lnum of the paragraph or white lines
3995 */
3996 white_in_front = linewhite(start_lnum);
3997 while (start_lnum > 1)
3998 {
3999 if (white_in_front) /* stop at first white line */
4000 {
4001 if (!linewhite(start_lnum - 1))
4002 break;
4003 }
4004 else /* stop at first non-white line of start of paragraph */
4005 {
4006 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
4007 break;
4008 }
4009 --start_lnum;
4010 }
4011
4012 /*
4013 * Move past the end of any white lines.
4014 */
4015 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00004016 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
4017 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004018
4019 --end_lnum;
4020 i = count;
4021 if (!include && white_in_front)
4022 --i;
4023 while (i--)
4024 {
4025 if (end_lnum == curbuf->b_ml.ml_line_count)
4026 return FAIL;
4027
4028 if (!include)
4029 do_white = linewhite(end_lnum + 1);
4030
4031 if (include || !do_white)
4032 {
4033 ++end_lnum;
4034 /*
4035 * skip to end of paragraph
4036 */
4037 while (end_lnum < curbuf->b_ml.ml_line_count
4038 && !linewhite(end_lnum + 1)
4039 && !startPS(end_lnum + 1, 0, 0))
4040 ++end_lnum;
4041 }
4042
4043 if (i == 0 && white_in_front && include)
4044 break;
4045
4046 /*
4047 * skip to end of white lines after paragraph
4048 */
4049 if (include || do_white)
4050 while (end_lnum < curbuf->b_ml.ml_line_count
4051 && linewhite(end_lnum + 1))
4052 ++end_lnum;
4053 }
4054
4055 /*
4056 * If there are no empty lines at the end, try to find some empty lines at
4057 * the start (unless that has been done already).
4058 */
4059 if (!white_in_front && !linewhite(end_lnum) && include)
4060 while (start_lnum > 1 && linewhite(start_lnum - 1))
4061 --start_lnum;
4062
4063#ifdef FEAT_VISUAL
4064 if (VIsual_active)
4065 {
4066 /* Problem: when doing "Vipipip" nothing happens in a single white
4067 * line, we get stuck there. Trap this here. */
4068 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4069 goto extend;
4070 VIsual.lnum = start_lnum;
4071 VIsual_mode = 'V';
4072 redraw_curbuf_later(INVERTED); /* update the inversion */
4073 showmode();
4074 }
4075 else
4076#endif
4077 {
4078 oap->start.lnum = start_lnum;
4079 oap->start.col = 0;
4080 oap->motion_type = MLINE;
4081 }
4082 curwin->w_cursor.lnum = end_lnum;
4083 curwin->w_cursor.col = 0;
4084
4085 return OK;
4086}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004087
4088static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4089static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4090
4091/*
4092 * Search quote char from string line[col].
4093 * Quote character escaped by one of the characters in "escape" is not counted
4094 * as a quote.
4095 * Returns column number of "quotechar" or -1 when not found.
4096 */
4097 static int
4098find_next_quote(line, col, quotechar, escape)
4099 char_u *line;
4100 int col;
4101 int quotechar;
4102 char_u *escape; /* escape characters, can be NULL */
4103{
4104 int c;
4105
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004106 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004107 {
4108 c = line[col];
4109 if (c == NUL)
4110 return -1;
4111 else if (escape != NULL && vim_strchr(escape, c))
4112 ++col;
4113 else if (c == quotechar)
4114 break;
4115#ifdef FEAT_MBYTE
4116 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004117 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004118 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004119#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004120 ++col;
4121 }
4122 return col;
4123}
4124
4125/*
4126 * Search backwards in "line" from column "col_start" to find "quotechar".
4127 * Quote character escaped by one of the characters in "escape" is not counted
4128 * as a quote.
4129 * Return the found column or zero.
4130 */
4131 static int
4132find_prev_quote(line, col_start, quotechar, escape)
4133 char_u *line;
4134 int col_start;
4135 int quotechar;
4136 char_u *escape; /* escape characters, can be NULL */
4137{
4138 int n;
4139
4140 while (col_start > 0)
4141 {
4142 --col_start;
4143#ifdef FEAT_MBYTE
4144 col_start -= (*mb_head_off)(line, line + col_start);
4145#endif
4146 n = 0;
4147 if (escape != NULL)
4148 while (col_start - n > 0 && vim_strchr(escape,
4149 line[col_start - n - 1]) != NULL)
4150 ++n;
4151 if (n & 1)
4152 col_start -= n; /* uneven number of escape chars, skip it */
4153 else if (line[col_start] == quotechar)
4154 break;
4155 }
4156 return col_start;
4157}
4158
4159/*
4160 * Find quote under the cursor, cursor at end.
4161 * Returns TRUE if found, else FALSE.
4162 */
4163 int
4164current_quote(oap, count, include, quotechar)
4165 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004166 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004167 int include; /* TRUE == include quote char */
4168 int quotechar; /* Quote character */
4169{
4170 char_u *line = ml_get_curline();
4171 int col_end;
4172 int col_start = curwin->w_cursor.col;
4173 int inclusive = FALSE;
4174#ifdef FEAT_VISUAL
4175 int vis_empty = TRUE; /* Visual selection <= 1 char */
4176 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004177 int inside_quotes = FALSE; /* Looks like "i'" done before */
4178 int selected_quote = FALSE; /* Has quote inside selection */
4179 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004180
4181 /* Correct cursor when 'selection' is exclusive */
4182 if (VIsual_active)
4183 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004184 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004185 if (*p_sel == 'e' && vis_bef_curs)
4186 dec_cursor();
4187 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004188 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004189
4190 if (!vis_empty)
4191 {
4192 /* Check if the existing selection exactly spans the text inside
4193 * quotes. */
4194 if (vis_bef_curs)
4195 {
4196 inside_quotes = VIsual.col > 0
4197 && line[VIsual.col - 1] == quotechar
4198 && line[curwin->w_cursor.col] != NUL
4199 && line[curwin->w_cursor.col + 1] == quotechar;
4200 i = VIsual.col;
4201 col_end = curwin->w_cursor.col;
4202 }
4203 else
4204 {
4205 inside_quotes = curwin->w_cursor.col > 0
4206 && line[curwin->w_cursor.col - 1] == quotechar
4207 && line[VIsual.col] != NUL
4208 && line[VIsual.col + 1] == quotechar;
4209 i = curwin->w_cursor.col;
4210 col_end = VIsual.col;
4211 }
4212
4213 /* Find out if we have a quote in the selection. */
4214 while (i <= col_end)
4215 if (line[i++] == quotechar)
4216 {
4217 selected_quote = TRUE;
4218 break;
4219 }
4220 }
4221
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004222 if (!vis_empty && line[col_start] == quotechar)
4223 {
4224 /* Already selecting something and on a quote character. Find the
4225 * next quoted string. */
4226 if (vis_bef_curs)
4227 {
4228 /* Assume we are on a closing quote: move to after the next
4229 * opening quote. */
4230 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4231 if (col_start < 0)
4232 return FALSE;
4233 col_end = find_next_quote(line, col_start + 1, quotechar,
4234 curbuf->b_p_qe);
4235 if (col_end < 0)
4236 {
4237 /* We were on a starting quote perhaps? */
4238 col_end = col_start;
4239 col_start = curwin->w_cursor.col;
4240 }
4241 }
4242 else
4243 {
4244 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4245 if (line[col_end] != quotechar)
4246 return FALSE;
4247 col_start = find_prev_quote(line, col_end, quotechar,
4248 curbuf->b_p_qe);
4249 if (line[col_start] != quotechar)
4250 {
4251 /* We were on an ending quote perhaps? */
4252 col_start = col_end;
4253 col_end = curwin->w_cursor.col;
4254 }
4255 }
4256 }
4257 else
4258#endif
4259
4260 if (line[col_start] == quotechar
4261#ifdef FEAT_VISUAL
4262 || !vis_empty
4263#endif
4264 )
4265 {
4266 int first_col = col_start;
4267
4268#ifdef FEAT_VISUAL
4269 if (!vis_empty)
4270 {
4271 if (vis_bef_curs)
4272 first_col = find_next_quote(line, col_start, quotechar, NULL);
4273 else
4274 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4275 }
4276#endif
4277 /* The cursor is on a quote, we don't know if it's the opening or
4278 * closing quote. Search from the start of the line to find out.
4279 * Also do this when there is a Visual area, a' may leave the cursor
4280 * in between two strings. */
4281 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004282 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004283 {
4284 /* Find open quote character. */
4285 col_start = find_next_quote(line, col_start, quotechar, NULL);
4286 if (col_start < 0 || col_start > first_col)
4287 return FALSE;
4288 /* Find close quote character. */
4289 col_end = find_next_quote(line, col_start + 1, quotechar,
4290 curbuf->b_p_qe);
4291 if (col_end < 0)
4292 return FALSE;
4293 /* If is cursor between start and end quote character, it is
4294 * target text object. */
4295 if (col_start <= first_col && first_col <= col_end)
4296 break;
4297 col_start = col_end + 1;
4298 }
4299 }
4300 else
4301 {
4302 /* Search backward for a starting quote. */
4303 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4304 if (line[col_start] != quotechar)
4305 {
4306 /* No quote before the cursor, look after the cursor. */
4307 col_start = find_next_quote(line, col_start, quotechar, NULL);
4308 if (col_start < 0)
4309 return FALSE;
4310 }
4311
4312 /* Find close quote character. */
4313 col_end = find_next_quote(line, col_start + 1, quotechar,
4314 curbuf->b_p_qe);
4315 if (col_end < 0)
4316 return FALSE;
4317 }
4318
4319 /* When "include" is TRUE, include spaces after closing quote or before
4320 * the starting quote. */
4321 if (include)
4322 {
4323 if (vim_iswhite(line[col_end + 1]))
4324 while (vim_iswhite(line[col_end + 1]))
4325 ++col_end;
4326 else
4327 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4328 --col_start;
4329 }
4330
Bram Moolenaarab194812005-09-14 21:40:12 +00004331 /* Set start position. After vi" another i" must include the ".
4332 * For v2i" include the quotes. */
4333 if (!include && count < 2
4334#ifdef FEAT_VISUAL
4335 && (vis_empty || !inside_quotes)
4336#endif
4337 )
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004338 ++col_start;
4339 curwin->w_cursor.col = col_start;
4340#ifdef FEAT_VISUAL
4341 if (VIsual_active)
4342 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004343 /* Set the start of the Visual area when the Visual area was empty, we
4344 * were just inside quotes or the Visual area didn't start at a quote
4345 * and didn't include a quote.
4346 */
4347 if (vis_empty
4348 || (vis_bef_curs
4349 && !selected_quote
4350 && (inside_quotes
4351 || (line[VIsual.col] != quotechar
4352 && (VIsual.col == 0
4353 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004354 {
4355 VIsual = curwin->w_cursor;
4356 redraw_curbuf_later(INVERTED);
4357 }
4358 }
4359 else
4360#endif
4361 {
4362 oap->start = curwin->w_cursor;
4363 oap->motion_type = MCHAR;
4364 }
4365
4366 /* Set end position. */
4367 curwin->w_cursor.col = col_end;
Bram Moolenaarab194812005-09-14 21:40:12 +00004368 if ((include || count > 1
4369#ifdef FEAT_VISUAL
4370 /* After vi" another i" must include the ". */
4371 || (!vis_empty && inside_quotes)
4372#endif
4373 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004374 inclusive = TRUE;
4375#ifdef FEAT_VISUAL
4376 if (VIsual_active)
4377 {
4378 if (vis_empty || vis_bef_curs)
4379 {
4380 /* decrement cursor when 'selection' is not exclusive */
4381 if (*p_sel != 'e')
4382 dec_cursor();
4383 }
4384 else
4385 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004386 /* Cursor is at start of Visual area. Set the end of the Visual
4387 * area when it was just inside quotes or it didn't end at a
4388 * quote. */
4389 if (inside_quotes
4390 || (!selected_quote
4391 && line[VIsual.col] != quotechar
4392 && (line[VIsual.col] == NUL
4393 || line[VIsual.col + 1] != quotechar)))
4394 {
4395 dec_cursor();
4396 VIsual = curwin->w_cursor;
4397 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004398 curwin->w_cursor.col = col_start;
4399 }
4400 if (VIsual_mode == 'V')
4401 {
4402 VIsual_mode = 'v';
4403 redraw_cmdline = TRUE; /* show mode later */
4404 }
4405 }
4406 else
4407#endif
4408 {
4409 /* Set inclusive and other oap's flags. */
4410 oap->inclusive = inclusive;
4411 }
4412
4413 return OK;
4414}
4415
4416#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004417
4418#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4419 || defined(PROTO)
4420/*
4421 * return TRUE if line 'lnum' is empty or has white chars only.
4422 */
4423 int
4424linewhite(lnum)
4425 linenr_T lnum;
4426{
4427 char_u *p;
4428
4429 p = skipwhite(ml_get(lnum));
4430 return (*p == NUL);
4431}
4432#endif
4433
4434#if defined(FEAT_FIND_ID) || defined(PROTO)
4435/*
4436 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004437 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004438 */
4439/*ARGSUSED*/
4440 void
4441find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4442 type, count, action, start_lnum, end_lnum)
4443 char_u *ptr; /* pointer to search pattern */
4444 int dir; /* direction of expansion */
4445 int len; /* length of search pattern */
4446 int whole; /* match whole words only */
4447 int skip_comments; /* don't match inside comments */
4448 int type; /* Type of search; are we looking for a type?
4449 a macro? */
4450 long count;
4451 int action; /* What to do when we find it */
4452 linenr_T start_lnum; /* first line to start searching */
4453 linenr_T end_lnum; /* last line for searching */
4454{
4455 SearchedFile *files; /* Stack of included files */
4456 SearchedFile *bigger; /* When we need more space */
4457 int max_path_depth = 50;
4458 long match_count = 1;
4459
4460 char_u *pat;
4461 char_u *new_fname;
4462 char_u *curr_fname = curbuf->b_fname;
4463 char_u *prev_fname = NULL;
4464 linenr_T lnum;
4465 int depth;
4466 int depth_displayed; /* For type==CHECK_PATH */
4467 int old_files;
4468 int already_searched;
4469 char_u *file_line;
4470 char_u *line;
4471 char_u *p;
4472 char_u save_char;
4473 int define_matched;
4474 regmatch_T regmatch;
4475 regmatch_T incl_regmatch;
4476 regmatch_T def_regmatch;
4477 int matched = FALSE;
4478 int did_show = FALSE;
4479 int found = FALSE;
4480 int i;
4481 char_u *already = NULL;
4482 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004483 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004484#ifdef RISCOS
4485 int previous_munging = __riscosify_control;
4486#endif
4487#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4488 win_T *curwin_save = NULL;
4489#endif
4490
4491 regmatch.regprog = NULL;
4492 incl_regmatch.regprog = NULL;
4493 def_regmatch.regprog = NULL;
4494
4495 file_line = alloc(LSIZE);
4496 if (file_line == NULL)
4497 return;
4498
4499#ifdef RISCOS
4500 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004501 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004502#endif
4503
4504 if (type != CHECK_PATH && type != FIND_DEFINE
4505#ifdef FEAT_INS_EXPAND
4506 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4507 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004508 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004509#endif
4510 )
4511 {
4512 pat = alloc(len + 5);
4513 if (pat == NULL)
4514 goto fpip_end;
4515 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4516 /* ignore case according to p_ic, p_scs and pat */
4517 regmatch.rm_ic = ignorecase(pat);
4518 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4519 vim_free(pat);
4520 if (regmatch.regprog == NULL)
4521 goto fpip_end;
4522 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004523 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4524 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004525 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004526 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004527 if (incl_regmatch.regprog == NULL)
4528 goto fpip_end;
4529 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4530 }
4531 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4532 {
4533 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4534 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4535 if (def_regmatch.regprog == NULL)
4536 goto fpip_end;
4537 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4538 }
4539 files = (SearchedFile *)lalloc_clear((long_u)
4540 (max_path_depth * sizeof(SearchedFile)), TRUE);
4541 if (files == NULL)
4542 goto fpip_end;
4543 old_files = max_path_depth;
4544 depth = depth_displayed = -1;
4545
4546 lnum = start_lnum;
4547 if (end_lnum > curbuf->b_ml.ml_line_count)
4548 end_lnum = curbuf->b_ml.ml_line_count;
4549 if (lnum > end_lnum) /* do at least one line */
4550 lnum = end_lnum;
4551 line = ml_get(lnum);
4552
4553 for (;;)
4554 {
4555 if (incl_regmatch.regprog != NULL
4556 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4557 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004558 char_u *p_fname = (curr_fname == curbuf->b_fname)
4559 ? curbuf->b_ffname : curr_fname;
4560
4561 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4562 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4563 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004564 (int)(incl_regmatch.endp[0] - incl_regmatch.startp[0]),
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004565 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4566 else
4567 /* Use text after match with 'include'. */
4568 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004569 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004570 already_searched = FALSE;
4571 if (new_fname != NULL)
4572 {
4573 /* Check whether we have already searched in this file */
4574 for (i = 0;; i++)
4575 {
4576 if (i == depth + 1)
4577 i = old_files;
4578 if (i == max_path_depth)
4579 break;
4580 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4581 {
4582 if (type != CHECK_PATH &&
4583 action == ACTION_SHOW_ALL && files[i].matched)
4584 {
4585 msg_putchar('\n'); /* cursor below last one */
4586 if (!got_int) /* don't display if 'q'
4587 typed at "--more--"
4588 mesage */
4589 {
4590 msg_home_replace_hl(new_fname);
4591 MSG_PUTS(_(" (includes previously listed match)"));
4592 prev_fname = NULL;
4593 }
4594 }
4595 vim_free(new_fname);
4596 new_fname = NULL;
4597 already_searched = TRUE;
4598 break;
4599 }
4600 }
4601 }
4602
4603 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4604 || (new_fname == NULL && !already_searched)))
4605 {
4606 if (did_show)
4607 msg_putchar('\n'); /* cursor below last one */
4608 else
4609 {
4610 gotocmdline(TRUE); /* cursor at status line */
4611 MSG_PUTS_TITLE(_("--- Included files "));
4612 if (action != ACTION_SHOW_ALL)
4613 MSG_PUTS_TITLE(_("not found "));
4614 MSG_PUTS_TITLE(_("in path ---\n"));
4615 }
4616 did_show = TRUE;
4617 while (depth_displayed < depth && !got_int)
4618 {
4619 ++depth_displayed;
4620 for (i = 0; i < depth_displayed; i++)
4621 MSG_PUTS(" ");
4622 msg_home_replace(files[depth_displayed].name);
4623 MSG_PUTS(" -->\n");
4624 }
4625 if (!got_int) /* don't display if 'q' typed
4626 for "--more--" message */
4627 {
4628 for (i = 0; i <= depth_displayed; i++)
4629 MSG_PUTS(" ");
4630 if (new_fname != NULL)
4631 {
4632 /* using "new_fname" is more reliable, e.g., when
4633 * 'includeexpr' is set. */
4634 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4635 }
4636 else
4637 {
4638 /*
4639 * Isolate the file name.
4640 * Include the surrounding "" or <> if present.
4641 */
4642 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4643 ;
4644 for (i = 0; vim_isfilec(p[i]); i++)
4645 ;
4646 if (i == 0)
4647 {
4648 /* Nothing found, use the rest of the line. */
4649 p = incl_regmatch.endp[0];
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00004650 i = (int)STRLEN(p);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004651 }
4652 else
4653 {
4654 if (p[-1] == '"' || p[-1] == '<')
4655 {
4656 --p;
4657 ++i;
4658 }
4659 if (p[i] == '"' || p[i] == '>')
4660 ++i;
4661 }
4662 save_char = p[i];
4663 p[i] = NUL;
4664 msg_outtrans_attr(p, hl_attr(HLF_D));
4665 p[i] = save_char;
4666 }
4667
4668 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4669 {
4670 if (already_searched)
4671 MSG_PUTS(_(" (Already listed)"));
4672 else
4673 MSG_PUTS(_(" NOT FOUND"));
4674 }
4675 }
4676 out_flush(); /* output each line directly */
4677 }
4678
4679 if (new_fname != NULL)
4680 {
4681 /* Push the new file onto the file stack */
4682 if (depth + 1 == old_files)
4683 {
4684 bigger = (SearchedFile *)lalloc((long_u)(
4685 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4686 if (bigger != NULL)
4687 {
4688 for (i = 0; i <= depth; i++)
4689 bigger[i] = files[i];
4690 for (i = depth + 1; i < old_files + max_path_depth; i++)
4691 {
4692 bigger[i].fp = NULL;
4693 bigger[i].name = NULL;
4694 bigger[i].lnum = 0;
4695 bigger[i].matched = FALSE;
4696 }
4697 for (i = old_files; i < max_path_depth; i++)
4698 bigger[i + max_path_depth] = files[i];
4699 old_files += max_path_depth;
4700 max_path_depth *= 2;
4701 vim_free(files);
4702 files = bigger;
4703 }
4704 }
4705 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4706 == NULL)
4707 vim_free(new_fname);
4708 else
4709 {
4710 if (++depth == old_files)
4711 {
4712 /*
4713 * lalloc() for 'bigger' must have failed above. We
4714 * will forget one of our already visited files now.
4715 */
4716 vim_free(files[old_files].name);
4717 ++old_files;
4718 }
4719 files[depth].name = curr_fname = new_fname;
4720 files[depth].lnum = 0;
4721 files[depth].matched = FALSE;
4722#ifdef FEAT_INS_EXPAND
4723 if (action == ACTION_EXPAND)
4724 {
Bram Moolenaardf40adf2006-10-14 12:32:39 +00004725 msg_hist_off = TRUE; /* reset in msg_trunc_attr() */
Bram Moolenaar555b2802005-05-19 21:08:39 +00004726 vim_snprintf((char*)IObuff, IOSIZE,
4727 _("Scanning included file: %s"),
4728 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004729 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4730 }
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004731 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004732#endif
Bram Moolenaar87b5ca52006-03-04 21:55:31 +00004733 if (p_verbose >= 5)
4734 {
4735 verbose_enter();
4736 smsg((char_u *)_("Searching included file %s"),
4737 (char *)new_fname);
4738 verbose_leave();
4739 }
4740
Bram Moolenaar071d4272004-06-13 20:20:40 +00004741 }
4742 }
4743 }
4744 else
4745 {
4746 /*
4747 * Check if the line is a define (type == FIND_DEFINE)
4748 */
4749 p = line;
4750search_line:
4751 define_matched = FALSE;
4752 if (def_regmatch.regprog != NULL
4753 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4754 {
4755 /*
4756 * Pattern must be first identifier after 'define', so skip
4757 * to that position before checking for match of pattern. Also
4758 * don't let it match beyond the end of this identifier.
4759 */
4760 p = def_regmatch.endp[0];
4761 while (*p && !vim_iswordc(*p))
4762 p++;
4763 define_matched = TRUE;
4764 }
4765
4766 /*
4767 * Look for a match. Don't do this if we are looking for a
4768 * define and this line didn't match define_prog above.
4769 */
4770 if (def_regmatch.regprog == NULL || define_matched)
4771 {
4772 if (define_matched
4773#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004774 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004775#endif
4776 )
4777 {
4778 /* compare the first "len" chars from "ptr" */
4779 startp = skipwhite(p);
4780 if (p_ic)
4781 matched = !MB_STRNICMP(startp, ptr, len);
4782 else
4783 matched = !STRNCMP(startp, ptr, len);
4784 if (matched && define_matched && whole
4785 && vim_iswordc(startp[len]))
4786 matched = FALSE;
4787 }
4788 else if (regmatch.regprog != NULL
4789 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4790 {
4791 matched = TRUE;
4792 startp = regmatch.startp[0];
4793 /*
4794 * Check if the line is not a comment line (unless we are
4795 * looking for a define). A line starting with "# define"
4796 * is not considered to be a comment line.
4797 */
4798 if (!define_matched && skip_comments)
4799 {
4800#ifdef FEAT_COMMENTS
4801 if ((*line != '#' ||
4802 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4803 && get_leader_len(line, NULL, FALSE))
4804 matched = FALSE;
4805
4806 /*
4807 * Also check for a "/ *" or "/ /" before the match.
4808 * Skips lines like "int backwards; / * normal index
4809 * * /" when looking for "normal".
4810 * Note: Doesn't skip "/ *" in comments.
4811 */
4812 p = skipwhite(line);
4813 if (matched
4814 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4815#endif
4816 for (p = line; *p && p < startp; ++p)
4817 {
4818 if (matched
4819 && p[0] == '/'
4820 && (p[1] == '*' || p[1] == '/'))
4821 {
4822 matched = FALSE;
4823 /* After "//" all text is comment */
4824 if (p[1] == '/')
4825 break;
4826 ++p;
4827 }
4828 else if (!matched && p[0] == '*' && p[1] == '/')
4829 {
4830 /* Can find match after "* /". */
4831 matched = TRUE;
4832 ++p;
4833 }
4834 }
4835 }
4836 }
4837 }
4838 }
4839 if (matched)
4840 {
4841#ifdef FEAT_INS_EXPAND
4842 if (action == ACTION_EXPAND)
4843 {
4844 int reuse = 0;
4845 int add_r;
4846 char_u *aux;
4847
4848 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4849 break;
4850 found = TRUE;
4851 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004852 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004853 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004854 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004855 if (vim_iswordp(p))
4856 goto exit_matched;
4857 p = find_word_start(p);
4858 }
4859 p = find_word_end(p);
4860 i = (int)(p - aux);
4861
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004862 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004863 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004864 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004865 STRNCPY(IObuff, aux, i);
Bram Moolenaar89d40322006-08-29 15:30:07 +00004866
4867 /* Get the next line: when "depth" < 0 from the current
4868 * buffer, otherwise from the included file. Jump to
4869 * exit_matched when past the last line. */
4870 if (depth < 0)
4871 {
4872 if (lnum >= end_lnum)
4873 goto exit_matched;
4874 line = ml_get(++lnum);
4875 }
4876 else if (vim_fgets(line = file_line,
4877 LSIZE, files[depth].fp))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004878 goto exit_matched;
4879
4880 /* we read a line, set "already" to check this "line" later
4881 * if depth >= 0 we'll increase files[depth].lnum far
4882 * bellow -- Acevedo */
4883 already = aux = p = skipwhite(line);
4884 p = find_word_start(p);
4885 p = find_word_end(p);
4886 if (p > aux)
4887 {
4888 if (*aux != ')' && IObuff[i-1] != TAB)
4889 {
4890 if (IObuff[i-1] != ' ')
4891 IObuff[i++] = ' ';
4892 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4893 if (p_js
4894 && (IObuff[i-2] == '.'
4895 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4896 && (IObuff[i-2] == '?'
4897 || IObuff[i-2] == '!'))))
4898 IObuff[i++] = ' ';
4899 }
4900 /* copy as much as posible of the new word */
4901 if (p - aux >= IOSIZE - i)
4902 p = aux + IOSIZE - i - 1;
4903 STRNCPY(IObuff + i, aux, p - aux);
4904 i += (int)(p - aux);
4905 reuse |= CONT_S_IPOS;
4906 }
4907 IObuff[i] = NUL;
4908 aux = IObuff;
4909
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004910 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004911 goto exit_matched;
4912 }
4913
Bram Moolenaare8c3a142006-08-29 14:30:35 +00004914 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004915 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4916 dir, reuse);
4917 if (add_r == OK)
4918 /* if dir was BACKWARD then honor it just once */
4919 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004920 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004921 break;
4922 }
4923 else
4924#endif
4925 if (action == ACTION_SHOW_ALL)
4926 {
4927 found = TRUE;
4928 if (!did_show)
4929 gotocmdline(TRUE); /* cursor at status line */
4930 if (curr_fname != prev_fname)
4931 {
4932 if (did_show)
4933 msg_putchar('\n'); /* cursor below last one */
4934 if (!got_int) /* don't display if 'q' typed
4935 at "--more--" mesage */
4936 msg_home_replace_hl(curr_fname);
4937 prev_fname = curr_fname;
4938 }
4939 did_show = TRUE;
4940 if (!got_int)
4941 show_pat_in_path(line, type, TRUE, action,
4942 (depth == -1) ? NULL : files[depth].fp,
4943 (depth == -1) ? &lnum : &files[depth].lnum,
4944 match_count++);
4945
4946 /* Set matched flag for this file and all the ones that
4947 * include it */
4948 for (i = 0; i <= depth; ++i)
4949 files[i].matched = TRUE;
4950 }
4951 else if (--count <= 0)
4952 {
4953 found = TRUE;
4954 if (depth == -1 && lnum == curwin->w_cursor.lnum
4955#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4956 && g_do_tagpreview == 0
4957#endif
4958 )
4959 EMSG(_("E387: Match is on current line"));
4960 else if (action == ACTION_SHOW)
4961 {
4962 show_pat_in_path(line, type, did_show, action,
4963 (depth == -1) ? NULL : files[depth].fp,
4964 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4965 did_show = TRUE;
4966 }
4967 else
4968 {
4969#ifdef FEAT_GUI
4970 need_mouse_correct = TRUE;
4971#endif
4972#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4973 /* ":psearch" uses the preview window */
4974 if (g_do_tagpreview != 0)
4975 {
4976 curwin_save = curwin;
Bram Moolenaard2cec5b2006-03-28 21:08:56 +00004977 prepare_tagpreview(TRUE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004978 }
4979#endif
4980 if (action == ACTION_SPLIT)
4981 {
4982#ifdef FEAT_WINDOWS
4983 if (win_split(0, 0) == FAIL)
4984#endif
4985 break;
4986#ifdef FEAT_SCROLLBIND
4987 curwin->w_p_scb = FALSE;
4988#endif
4989 }
4990 if (depth == -1)
4991 {
4992 /* match in current file */
4993#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4994 if (g_do_tagpreview != 0)
4995 {
4996 if (getfile(0, curwin_save->w_buffer->b_fname,
4997 NULL, TRUE, lnum, FALSE) > 0)
4998 break; /* failed to jump to file */
4999 }
5000 else
5001#endif
5002 setpcmark();
5003 curwin->w_cursor.lnum = lnum;
5004 }
5005 else
5006 {
5007 if (getfile(0, files[depth].name, NULL, TRUE,
5008 files[depth].lnum, FALSE) > 0)
5009 break; /* failed to jump to file */
5010 /* autocommands may have changed the lnum, we don't
5011 * want that here */
5012 curwin->w_cursor.lnum = files[depth].lnum;
5013 }
5014 }
5015 if (action != ACTION_SHOW)
5016 {
5017 curwin->w_cursor.col = (colnr_T) (startp - line);
5018 curwin->w_set_curswant = TRUE;
5019 }
5020
5021#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
5022 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00005023 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00005024 {
5025 /* Return cursor to where we were */
5026 validate_cursor();
5027 redraw_later(VALID);
5028 win_enter(curwin_save, TRUE);
5029 }
5030#endif
5031 break;
5032 }
5033#ifdef FEAT_INS_EXPAND
5034exit_matched:
5035#endif
5036 matched = FALSE;
5037 /* look for other matches in the rest of the line if we
5038 * are not at the end of it already */
5039 if (def_regmatch.regprog == NULL
5040#ifdef FEAT_INS_EXPAND
5041 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005042 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005043#endif
5044 && *(p = startp + 1))
5045 goto search_line;
5046 }
5047 line_breakcheck();
5048#ifdef FEAT_INS_EXPAND
5049 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00005050 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005051 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005052#else
5053 if (got_int)
5054#endif
5055 break;
5056
5057 /*
5058 * Read the next line. When reading an included file and encountering
5059 * end-of-file, close the file and continue in the file that included
5060 * it.
5061 */
5062 while (depth >= 0 && !already
5063 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5064 {
5065 fclose(files[depth].fp);
5066 --old_files;
5067 files[old_files].name = files[depth].name;
5068 files[old_files].matched = files[depth].matched;
5069 --depth;
5070 curr_fname = (depth == -1) ? curbuf->b_fname
5071 : files[depth].name;
5072 if (depth < depth_displayed)
5073 depth_displayed = depth;
5074 }
5075 if (depth >= 0) /* we could read the line */
5076 files[depth].lnum++;
5077 else if (!already)
5078 {
5079 if (++lnum > end_lnum)
5080 break;
5081 line = ml_get(lnum);
5082 }
5083 already = NULL;
5084 }
5085 /* End of big for (;;) loop. */
5086
5087 /* Close any files that are still open. */
5088 for (i = 0; i <= depth; i++)
5089 {
5090 fclose(files[i].fp);
5091 vim_free(files[i].name);
5092 }
5093 for (i = old_files; i < max_path_depth; i++)
5094 vim_free(files[i].name);
5095 vim_free(files);
5096
5097 if (type == CHECK_PATH)
5098 {
5099 if (!did_show)
5100 {
5101 if (action != ACTION_SHOW_ALL)
5102 MSG(_("All included files were found"));
5103 else
5104 MSG(_("No included files"));
5105 }
5106 }
5107 else if (!found
5108#ifdef FEAT_INS_EXPAND
5109 && action != ACTION_EXPAND
5110#endif
5111 )
5112 {
5113#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005114 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005115#else
5116 if (got_int)
5117#endif
5118 EMSG(_(e_interr));
5119 else if (type == FIND_DEFINE)
5120 EMSG(_("E388: Couldn't find definition"));
5121 else
5122 EMSG(_("E389: Couldn't find pattern"));
5123 }
5124 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5125 msg_end();
5126
5127fpip_end:
5128 vim_free(file_line);
5129 vim_free(regmatch.regprog);
5130 vim_free(incl_regmatch.regprog);
5131 vim_free(def_regmatch.regprog);
5132
5133#ifdef RISCOS
5134 /* Restore previous file munging state. */
5135 __riscosify_control = previous_munging;
5136#endif
5137}
5138
5139 static void
5140show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5141 char_u *line;
5142 int type;
5143 int did_show;
5144 int action;
5145 FILE *fp;
5146 linenr_T *lnum;
5147 long count;
5148{
5149 char_u *p;
5150
5151 if (did_show)
5152 msg_putchar('\n'); /* cursor below last one */
Bram Moolenaar91170f82006-05-05 21:15:17 +00005153 else if (!msg_silent)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005154 gotocmdline(TRUE); /* cursor at status line */
5155 if (got_int) /* 'q' typed at "--more--" message */
5156 return;
5157 for (;;)
5158 {
5159 p = line + STRLEN(line) - 1;
5160 if (fp != NULL)
5161 {
5162 /* We used fgets(), so get rid of newline at end */
5163 if (p >= line && *p == '\n')
5164 --p;
5165 if (p >= line && *p == '\r')
5166 --p;
5167 *(p + 1) = NUL;
5168 }
5169 if (action == ACTION_SHOW_ALL)
5170 {
5171 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5172 msg_puts(IObuff);
5173 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5174 /* Highlight line numbers */
5175 msg_puts_attr(IObuff, hl_attr(HLF_N));
5176 MSG_PUTS(" ");
5177 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005178 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005179 out_flush(); /* show one line at a time */
5180
5181 /* Definition continues until line that doesn't end with '\' */
5182 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5183 break;
5184
5185 if (fp != NULL)
5186 {
5187 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5188 break;
5189 ++*lnum;
5190 }
5191 else
5192 {
5193 if (++*lnum > curbuf->b_ml.ml_line_count)
5194 break;
5195 line = ml_get(*lnum);
5196 }
5197 msg_putchar('\n');
5198 }
5199}
5200#endif
5201
5202#ifdef FEAT_VIMINFO
5203 int
5204read_viminfo_search_pattern(virp, force)
5205 vir_T *virp;
5206 int force;
5207{
5208 char_u *lp;
5209 int idx = -1;
5210 int magic = FALSE;
5211 int no_scs = FALSE;
5212 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005213 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005214 long off = 0;
5215 int setlast = FALSE;
5216#ifdef FEAT_SEARCH_EXTRA
5217 static int hlsearch_on = FALSE;
5218#endif
5219 char_u *val;
5220
5221 /*
5222 * Old line types:
5223 * "/pat", "&pat": search/subst. pat
5224 * "~/pat", "~&pat": last used search/subst. pat
5225 * New line types:
5226 * "~h", "~H": hlsearch highlighting off/on
5227 * "~<magic><smartcase><line><end><off><last><which>pat"
5228 * <magic>: 'm' off, 'M' on
5229 * <smartcase>: 's' off, 'S' on
5230 * <line>: 'L' line offset, 'l' char offset
5231 * <end>: 'E' from end, 'e' from start
5232 * <off>: decimal, offset
5233 * <last>: '~' last used pattern
5234 * <which>: '/' search pat, '&' subst. pat
5235 */
5236 lp = virp->vir_line;
5237 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5238 {
5239 if (lp[1] == 'M') /* magic on */
5240 magic = TRUE;
5241 if (lp[2] == 's')
5242 no_scs = TRUE;
5243 if (lp[3] == 'L')
5244 off_line = TRUE;
5245 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005246 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005247 lp += 5;
5248 off = getdigits(&lp);
5249 }
5250 if (lp[0] == '~') /* use this pattern for last-used pattern */
5251 {
5252 setlast = TRUE;
5253 lp++;
5254 }
5255 if (lp[0] == '/')
5256 idx = RE_SEARCH;
5257 else if (lp[0] == '&')
5258 idx = RE_SUBST;
5259#ifdef FEAT_SEARCH_EXTRA
5260 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5261 hlsearch_on = FALSE;
5262 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5263 hlsearch_on = TRUE;
5264#endif
5265 if (idx >= 0)
5266 {
5267 if (force || spats[idx].pat == NULL)
5268 {
5269 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5270 TRUE);
5271 if (val != NULL)
5272 {
5273 set_last_search_pat(val, idx, magic, setlast);
5274 vim_free(val);
5275 spats[idx].no_scs = no_scs;
5276 spats[idx].off.line = off_line;
5277 spats[idx].off.end = off_end;
5278 spats[idx].off.off = off;
5279#ifdef FEAT_SEARCH_EXTRA
5280 if (setlast)
5281 no_hlsearch = !hlsearch_on;
5282#endif
5283 }
5284 }
5285 }
5286 return viminfo_readline(virp);
5287}
5288
5289 void
5290write_viminfo_search_pattern(fp)
5291 FILE *fp;
5292{
5293 if (get_viminfo_parameter('/') != 0)
5294 {
5295#ifdef FEAT_SEARCH_EXTRA
5296 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5297 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5298#endif
5299 wvsp_one(fp, RE_SEARCH, "", '/');
5300 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5301 }
5302}
5303
5304 static void
5305wvsp_one(fp, idx, s, sc)
5306 FILE *fp; /* file to write to */
5307 int idx; /* spats[] index */
5308 char *s; /* search pat */
5309 int sc; /* dir char */
5310{
5311 if (spats[idx].pat != NULL)
5312 {
Bram Moolenaara93fa7e2006-04-17 22:14:47 +00005313 fprintf(fp, _("\n# Last %sSearch Pattern:\n~"), s);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005314 /* off.dir is not stored, it's reset to forward */
5315 fprintf(fp, "%c%c%c%c%ld%s%c",
5316 spats[idx].magic ? 'M' : 'm', /* magic */
5317 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5318 spats[idx].off.line ? 'L' : 'l', /* line offset */
5319 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5320 spats[idx].off.off, /* offset */
5321 last_idx == idx ? "~" : "", /* last used pat */
5322 sc);
5323 viminfo_writestring(fp, spats[idx].pat);
5324 }
5325}
5326#endif /* FEAT_VIMINFO */