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