blob: 27b9ac13dceaa814d6bb18c6b4636b2185dcb350 [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;
1894 /* backward search: Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001895 if ((backwards && comment_dir)
1896#ifdef FEAT_LISP
1897 || lisp
1898#endif
1899 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001900 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001901#ifdef FEAT_LISP
1902 if (lisp && comment_col != MAXCOL && pos.col > (colnr_T)comment_col)
1903 lispcomm = TRUE; /* find match inside this comment */
1904#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001905 while (!got_int)
1906 {
1907 /*
1908 * Go to the next position, forward or backward. We could use
1909 * inc() and dec() here, but that is much slower
1910 */
1911 if (backwards)
1912 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001913#ifdef FEAT_LISP
1914 /* char to match is inside of comment, don't search outside */
1915 if (lispcomm && pos.col < (colnr_T)comment_col)
1916 break;
1917#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001918 if (pos.col == 0) /* at start of line, go to prev. one */
1919 {
1920 if (pos.lnum == 1) /* start of file */
1921 break;
1922 --pos.lnum;
1923
1924 if (maxtravel && traveled++ > maxtravel)
1925 break;
1926
1927 linep = ml_get(pos.lnum);
1928 pos.col = (colnr_T)STRLEN(linep); /* pos.col on trailing NUL */
1929 do_quotes = -1;
1930 line_breakcheck();
1931
1932 /* Check if this line contains a single-line comment */
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001933 if (comment_dir
1934#ifdef FEAT_LISP
1935 || lisp
1936#endif
1937 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001938 comment_col = check_linecomment(linep);
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001939#ifdef FEAT_LISP
1940 /* skip comment */
1941 if (lisp && comment_col != MAXCOL)
1942 pos.col = comment_col;
1943#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001944 }
1945 else
1946 {
1947 --pos.col;
1948#ifdef FEAT_MBYTE
1949 if (has_mbyte)
1950 pos.col -= (*mb_head_off)(linep, linep + pos.col);
1951#endif
1952 }
1953 }
1954 else /* forward search */
1955 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001956 if (linep[pos.col] == NUL
1957 /* at end of line, go to next one */
1958#ifdef FEAT_LISP
1959 /* don't search for match in comment */
1960 || (lisp && comment_col != MAXCOL
1961 && pos.col == (colnr_T)comment_col)
1962#endif
1963 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001964 {
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001965 if (pos.lnum == curbuf->b_ml.ml_line_count /* end of file */
1966#ifdef FEAT_LISP
1967 /* line is exhausted and comment with it,
1968 * don't search for match in code */
1969 || lispcomm
1970#endif
1971 )
Bram Moolenaar071d4272004-06-13 20:20:40 +00001972 break;
1973 ++pos.lnum;
1974
1975 if (maxtravel && traveled++ > maxtravel)
1976 break;
1977
1978 linep = ml_get(pos.lnum);
1979 pos.col = 0;
1980 do_quotes = -1;
1981 line_breakcheck();
Bram Moolenaar325b7a22004-07-05 15:58:32 +00001982#ifdef FEAT_LISP
1983 if (lisp) /* find comment pos in new line */
1984 comment_col = check_linecomment(linep);
1985#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00001986 }
1987 else
1988 {
1989#ifdef FEAT_MBYTE
1990 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00001991 pos.col += (*mb_ptr2len)(linep + pos.col);
Bram Moolenaar071d4272004-06-13 20:20:40 +00001992 else
1993#endif
1994 ++pos.col;
1995 }
1996 }
1997
1998 /*
1999 * If FM_BLOCKSTOP given, stop at a '{' or '}' in column 0.
2000 */
2001 if (pos.col == 0 && (flags & FM_BLOCKSTOP) &&
2002 (linep[0] == '{' || linep[0] == '}'))
2003 {
2004 if (linep[0] == findc && count == 0) /* match! */
2005 return &pos;
2006 break; /* out of scope */
2007 }
2008
2009 if (comment_dir)
2010 {
2011 /* Note: comments do not nest, and we ignore quotes in them */
2012 /* TODO: ignore comment brackets inside strings */
2013 if (comment_dir == FORWARD)
2014 {
2015 if (linep[pos.col] == '*' && linep[pos.col + 1] == '/')
2016 {
2017 pos.col++;
2018 return &pos;
2019 }
2020 }
2021 else /* Searching backwards */
2022 {
2023 /*
2024 * A comment may contain / * or / /, it may also start or end
2025 * with / * /. Ignore a / * after / /.
2026 */
2027 if (pos.col == 0)
2028 continue;
2029 else if ( linep[pos.col - 1] == '/'
2030 && linep[pos.col] == '*'
2031 && (int)pos.col < comment_col)
2032 {
2033 count++;
2034 match_pos = pos;
2035 match_pos.col--;
2036 }
2037 else if (linep[pos.col - 1] == '*' && linep[pos.col] == '/')
2038 {
2039 if (count > 0)
2040 pos = match_pos;
2041 else if (pos.col > 1 && linep[pos.col - 2] == '/'
2042 && (int)pos.col <= comment_col)
2043 pos.col -= 2;
2044 else if (ignore_cend)
2045 continue;
2046 else
2047 return NULL;
2048 return &pos;
2049 }
2050 }
2051 continue;
2052 }
2053
2054 /*
2055 * If smart matching ('cpoptions' does not contain '%'), braces inside
2056 * of quotes are ignored, but only if there is an even number of
2057 * quotes in the line.
2058 */
2059 if (cpo_match)
2060 do_quotes = 0;
2061 else if (do_quotes == -1)
2062 {
2063 /*
2064 * Count the number of quotes in the line, skipping \" and '"'.
2065 * Watch out for "\\".
2066 */
2067 at_start = do_quotes;
2068 for (ptr = linep; *ptr; ++ptr)
2069 {
2070 if (ptr == linep + pos.col + backwards)
2071 at_start = (do_quotes & 1);
2072 if (*ptr == '"'
2073 && (ptr == linep || ptr[-1] != '\'' || ptr[1] != '\''))
2074 ++do_quotes;
2075 if (*ptr == '\\' && ptr[1] != NUL)
2076 ++ptr;
2077 }
2078 do_quotes &= 1; /* result is 1 with even number of quotes */
2079
2080 /*
2081 * If we find an uneven count, check current line and previous
2082 * one for a '\' at the end.
2083 */
2084 if (!do_quotes)
2085 {
2086 inquote = FALSE;
2087 if (ptr[-1] == '\\')
2088 {
2089 do_quotes = 1;
2090 if (start_in_quotes == MAYBE)
2091 {
2092 /* Do we need to use at_start here? */
2093 inquote = TRUE;
2094 start_in_quotes = TRUE;
2095 }
2096 else if (backwards)
2097 inquote = TRUE;
2098 }
2099 if (pos.lnum > 1)
2100 {
2101 ptr = ml_get(pos.lnum - 1);
2102 if (*ptr && *(ptr + STRLEN(ptr) - 1) == '\\')
2103 {
2104 do_quotes = 1;
2105 if (start_in_quotes == MAYBE)
2106 {
2107 inquote = at_start;
2108 if (inquote)
2109 start_in_quotes = TRUE;
2110 }
2111 else if (!backwards)
2112 inquote = TRUE;
2113 }
2114 }
2115 }
2116 }
2117 if (start_in_quotes == MAYBE)
2118 start_in_quotes = FALSE;
2119
2120 /*
2121 * If 'smartmatch' is set:
2122 * Things inside quotes are ignored by setting 'inquote'. If we
2123 * find a quote without a preceding '\' invert 'inquote'. At the
2124 * end of a line not ending in '\' we reset 'inquote'.
2125 *
2126 * In lines with an uneven number of quotes (without preceding '\')
2127 * we do not know which part to ignore. Therefore we only set
2128 * inquote if the number of quotes in a line is even, unless this
2129 * line or the previous one ends in a '\'. Complicated, isn't it?
2130 */
2131 switch (c = linep[pos.col])
2132 {
2133 case NUL:
2134 /* at end of line without trailing backslash, reset inquote */
2135 if (pos.col == 0 || linep[pos.col - 1] != '\\')
2136 {
2137 inquote = FALSE;
2138 start_in_quotes = FALSE;
2139 }
2140 break;
2141
2142 case '"':
2143 /* a quote that is preceded with an odd number of backslashes is
2144 * ignored */
2145 if (do_quotes)
2146 {
2147 int col;
2148
2149 for (col = pos.col - 1; col >= 0; --col)
2150 if (linep[col] != '\\')
2151 break;
2152 if ((((int)pos.col - 1 - col) & 1) == 0)
2153 {
2154 inquote = !inquote;
2155 start_in_quotes = FALSE;
2156 }
2157 }
2158 break;
2159
2160 /*
2161 * If smart matching ('cpoptions' does not contain '%'):
2162 * Skip things in single quotes: 'x' or '\x'. Be careful for single
2163 * single quotes, eg jon's. Things like '\233' or '\x3f' are not
2164 * skipped, there is never a brace in them.
2165 * Ignore this when finding matches for `'.
2166 */
2167 case '\'':
2168 if (!cpo_match && initc != '\'' && findc != '\'')
2169 {
2170 if (backwards)
2171 {
2172 if (pos.col > 1)
2173 {
2174 if (linep[pos.col - 2] == '\'')
2175 {
2176 pos.col -= 2;
2177 break;
2178 }
2179 else if (linep[pos.col - 2] == '\\' &&
2180 pos.col > 2 && linep[pos.col - 3] == '\'')
2181 {
2182 pos.col -= 3;
2183 break;
2184 }
2185 }
2186 }
2187 else if (linep[pos.col + 1]) /* forward search */
2188 {
2189 if (linep[pos.col + 1] == '\\' &&
2190 linep[pos.col + 2] && linep[pos.col + 3] == '\'')
2191 {
2192 pos.col += 3;
2193 break;
2194 }
2195 else if (linep[pos.col + 2] == '\'')
2196 {
2197 pos.col += 2;
2198 break;
2199 }
2200 }
2201 }
2202 /* FALLTHROUGH */
2203
2204 default:
2205#ifdef FEAT_LISP
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002206 /*
2207 * For Lisp skip over backslashed (), {} and [].
2208 * (actually, we skip #\( et al)
2209 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00002210 if (curbuf->b_p_lisp
2211 && vim_strchr((char_u *)"(){}[]", c) != NULL
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002212 && pos.col > 1
2213 && check_prevcol(linep, pos.col, '\\', NULL)
2214 && check_prevcol(linep, pos.col - 1, '#', NULL))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002215 break;
2216#endif
2217
2218 /* Check for match outside of quotes, and inside of
2219 * quotes when the start is also inside of quotes. */
2220 if ((!inquote || start_in_quotes == TRUE)
2221 && (c == initc || c == findc))
2222 {
2223 int col, bslcnt = 0;
2224
2225 if (!cpo_bsl)
2226 {
2227 for (col = pos.col; check_prevcol(linep, col, '\\', &col);)
2228 bslcnt++;
2229 }
2230 /* Only accept a match when 'M' is in 'cpo' or when ecaping is
2231 * what we expect. */
2232 if (cpo_bsl || (bslcnt & 1) == match_escaped)
2233 {
2234 if (c == initc)
2235 count++;
2236 else
2237 {
2238 if (count == 0)
2239 return &pos;
2240 count--;
2241 }
2242 }
2243 }
2244 }
2245 }
2246
2247 if (comment_dir == BACKWARD && count > 0)
2248 {
2249 pos = match_pos;
2250 return &pos;
2251 }
2252 return (pos_T *)NULL; /* never found it */
2253}
2254
2255/*
2256 * Check if line[] contains a / / comment.
2257 * Return MAXCOL if not, otherwise return the column.
2258 * TODO: skip strings.
2259 */
2260 static int
2261check_linecomment(line)
2262 char_u *line;
2263{
2264 char_u *p;
2265
2266 p = line;
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002267#ifdef FEAT_LISP
2268 /* skip Lispish one-line comments */
2269 if (curbuf->b_p_lisp)
2270 {
2271 if (vim_strchr(p, ';') != NULL) /* there may be comments */
2272 {
2273 int instr = FALSE; /* inside of string */
2274
2275 p = line; /* scan from start */
Bram Moolenaar520470a2005-06-16 21:59:56 +00002276 while ((p = vim_strpbrk(p, (char_u *)"\";")) != NULL)
Bram Moolenaar325b7a22004-07-05 15:58:32 +00002277 {
2278 if (*p == '"')
2279 {
2280 if (instr)
2281 {
2282 if (*(p - 1) != '\\') /* skip escaped quote */
2283 instr = FALSE;
2284 }
2285 else if (p == line || ((p - line) >= 2
2286 /* skip #\" form */
2287 && *(p - 1) != '\\' && *(p - 2) != '#'))
2288 instr = TRUE;
2289 }
2290 else if (!instr && ((p - line) < 2
2291 || (*(p - 1) != '\\' && *(p - 2) != '#')))
2292 break; /* found! */
2293 ++p;
2294 }
2295 }
2296 else
2297 p = NULL;
2298 }
2299 else
2300#endif
Bram Moolenaar071d4272004-06-13 20:20:40 +00002301 while ((p = vim_strchr(p, '/')) != NULL)
2302 {
2303 if (p[1] == '/')
2304 break;
2305 ++p;
2306 }
2307
2308 if (p == NULL)
2309 return MAXCOL;
2310 return (int)(p - line);
2311}
2312
2313/*
2314 * Move cursor briefly to character matching the one under the cursor.
2315 * Used for Insert mode and "r" command.
2316 * Show the match only if it is visible on the screen.
2317 * If there isn't a match, then beep.
2318 */
2319 void
2320showmatch(c)
2321 int c; /* char to show match for */
2322{
2323 pos_T *lpos, save_cursor;
2324 pos_T mpos;
2325 colnr_T vcol;
2326 long save_so;
2327 long save_siso;
2328#ifdef CURSOR_SHAPE
2329 int save_state;
2330#endif
2331 colnr_T save_dollar_vcol;
2332 char_u *p;
2333
2334 /*
2335 * Only show match for chars in the 'matchpairs' option.
2336 */
2337 /* 'matchpairs' is "x:y,x:y" */
2338 for (p = curbuf->b_p_mps; *p != NUL; p += 2)
2339 {
2340#ifdef FEAT_RIGHTLEFT
2341 if (*p == c && (curwin->w_p_rl ^ p_ri))
2342 break;
2343#endif
2344 p += 2;
2345 if (*p == c
2346#ifdef FEAT_RIGHTLEFT
2347 && !(curwin->w_p_rl ^ p_ri)
2348#endif
2349 )
2350 break;
2351 if (p[1] != ',')
2352 return;
2353 }
2354
2355 if ((lpos = findmatch(NULL, NUL)) == NULL) /* no match, so beep */
2356 vim_beep();
2357 else if (lpos->lnum >= curwin->w_topline)
2358 {
2359 if (!curwin->w_p_wrap)
2360 getvcol(curwin, lpos, NULL, &vcol, NULL);
2361 if (curwin->w_p_wrap || (vcol >= curwin->w_leftcol
2362 && vcol < curwin->w_leftcol + W_WIDTH(curwin)))
2363 {
2364 mpos = *lpos; /* save the pos, update_screen() may change it */
2365 save_cursor = curwin->w_cursor;
2366 save_so = p_so;
2367 save_siso = p_siso;
2368 /* Handle "$" in 'cpo': If the ')' is typed on top of the "$",
2369 * stop displaying the "$". */
2370 if (dollar_vcol > 0 && dollar_vcol == curwin->w_virtcol)
2371 dollar_vcol = 0;
2372 ++curwin->w_virtcol; /* do display ')' just before "$" */
2373 update_screen(VALID); /* show the new char first */
2374
2375 save_dollar_vcol = dollar_vcol;
2376#ifdef CURSOR_SHAPE
2377 save_state = State;
2378 State = SHOWMATCH;
2379 ui_cursor_shape(); /* may show different cursor shape */
2380#endif
2381 curwin->w_cursor = mpos; /* move to matching char */
2382 p_so = 0; /* don't use 'scrolloff' here */
2383 p_siso = 0; /* don't use 'sidescrolloff' here */
2384 showruler(FALSE);
2385 setcursor();
2386 cursor_on(); /* make sure that the cursor is shown */
2387 out_flush();
2388#ifdef FEAT_GUI
2389 if (gui.in_use)
2390 {
2391 gui_update_cursor(TRUE, FALSE);
2392 gui_mch_flush();
2393 }
2394#endif
2395 /* Restore dollar_vcol(), because setcursor() may call curs_rows()
2396 * which resets it if the matching position is in a previous line
2397 * and has a higher column number. */
2398 dollar_vcol = save_dollar_vcol;
2399
2400 /*
2401 * brief pause, unless 'm' is present in 'cpo' and a character is
2402 * available.
2403 */
2404 if (vim_strchr(p_cpo, CPO_SHOWMATCH) != NULL)
2405 ui_delay(p_mat * 100L, TRUE);
2406 else if (!char_avail())
2407 ui_delay(p_mat * 100L, FALSE);
2408 curwin->w_cursor = save_cursor; /* restore cursor position */
2409 p_so = save_so;
2410 p_siso = save_siso;
2411#ifdef CURSOR_SHAPE
2412 State = save_state;
2413 ui_cursor_shape(); /* may show different cursor shape */
2414#endif
2415 }
2416 }
2417}
2418
2419/*
2420 * findsent(dir, count) - Find the start of the next sentence in direction
Bram Moolenaarebefac62005-12-28 22:39:57 +00002421 * "dir" Sentences are supposed to end in ".", "!" or "?" followed by white
Bram Moolenaar071d4272004-06-13 20:20:40 +00002422 * space or a line break. Also stop at an empty line.
2423 * Return OK if the next sentence was found.
2424 */
2425 int
2426findsent(dir, count)
2427 int dir;
2428 long count;
2429{
2430 pos_T pos, tpos;
2431 int c;
2432 int (*func) __ARGS((pos_T *));
2433 int startlnum;
2434 int noskip = FALSE; /* do not skip blanks */
2435 int cpo_J;
Bram Moolenaardef9e822004-12-31 20:58:58 +00002436 int found_dot;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002437
2438 pos = curwin->w_cursor;
2439 if (dir == FORWARD)
2440 func = incl;
2441 else
2442 func = decl;
2443
2444 while (count--)
2445 {
2446 /*
2447 * if on an empty line, skip upto a non-empty line
2448 */
2449 if (gchar_pos(&pos) == NUL)
2450 {
2451 do
2452 if ((*func)(&pos) == -1)
2453 break;
2454 while (gchar_pos(&pos) == NUL);
2455 if (dir == FORWARD)
2456 goto found;
2457 }
2458 /*
2459 * if on the start of a paragraph or a section and searching forward,
2460 * go to the next line
2461 */
2462 else if (dir == FORWARD && pos.col == 0 &&
2463 startPS(pos.lnum, NUL, FALSE))
2464 {
2465 if (pos.lnum == curbuf->b_ml.ml_line_count)
2466 return FAIL;
2467 ++pos.lnum;
2468 goto found;
2469 }
2470 else if (dir == BACKWARD)
2471 decl(&pos);
2472
2473 /* go back to the previous non-blank char */
Bram Moolenaardef9e822004-12-31 20:58:58 +00002474 found_dot = FALSE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002475 while ((c = gchar_pos(&pos)) == ' ' || c == '\t' ||
2476 (dir == BACKWARD && vim_strchr((char_u *)".!?)]\"'", c) != NULL))
2477 {
Bram Moolenaardef9e822004-12-31 20:58:58 +00002478 if (vim_strchr((char_u *)".!?", c) != NULL)
2479 {
2480 /* Only skip over a '.', '!' and '?' once. */
2481 if (found_dot)
2482 break;
2483 found_dot = TRUE;
2484 }
Bram Moolenaar071d4272004-06-13 20:20:40 +00002485 if (decl(&pos) == -1)
2486 break;
2487 /* when going forward: Stop in front of empty line */
2488 if (lineempty(pos.lnum) && dir == FORWARD)
2489 {
2490 incl(&pos);
2491 goto found;
2492 }
2493 }
2494
2495 /* remember the line where the search started */
2496 startlnum = pos.lnum;
2497 cpo_J = vim_strchr(p_cpo, CPO_ENDOFSENT) != NULL;
2498
2499 for (;;) /* find end of sentence */
2500 {
2501 c = gchar_pos(&pos);
2502 if (c == NUL || (pos.col == 0 && startPS(pos.lnum, NUL, FALSE)))
2503 {
2504 if (dir == BACKWARD && pos.lnum != startlnum)
2505 ++pos.lnum;
2506 break;
2507 }
2508 if (c == '.' || c == '!' || c == '?')
2509 {
2510 tpos = pos;
2511 do
2512 if ((c = inc(&tpos)) == -1)
2513 break;
2514 while (vim_strchr((char_u *)")]\"'", c = gchar_pos(&tpos))
2515 != NULL);
2516 if (c == -1 || (!cpo_J && (c == ' ' || c == '\t')) || c == NUL
2517 || (cpo_J && (c == ' ' && inc(&tpos) >= 0
2518 && gchar_pos(&tpos) == ' ')))
2519 {
2520 pos = tpos;
2521 if (gchar_pos(&pos) == NUL) /* skip NUL at EOL */
2522 inc(&pos);
2523 break;
2524 }
2525 }
2526 if ((*func)(&pos) == -1)
2527 {
2528 if (count)
2529 return FAIL;
2530 noskip = TRUE;
2531 break;
2532 }
2533 }
2534found:
2535 /* skip white space */
2536 while (!noskip && ((c = gchar_pos(&pos)) == ' ' || c == '\t'))
2537 if (incl(&pos) == -1)
2538 break;
2539 }
2540
2541 setpcmark();
2542 curwin->w_cursor = pos;
2543 return OK;
2544}
2545
2546/*
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002547 * Find the next paragraph or section in direction 'dir'.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002548 * Paragraphs are currently supposed to be separated by empty lines.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002549 * If 'what' is NUL we go to the next paragraph.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002550 * If 'what' is '{' or '}' we go to the next section.
2551 * If 'both' is TRUE also stop at '}'.
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002552 * Return TRUE if the next paragraph or section was found.
Bram Moolenaar071d4272004-06-13 20:20:40 +00002553 */
2554 int
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002555findpar(pincl, dir, count, what, both)
2556 int *pincl; /* Return: TRUE if last char is to be included */
2557 int dir;
2558 long count;
2559 int what;
2560 int both;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002561{
2562 linenr_T curr;
2563 int did_skip; /* TRUE after separating lines have been skipped */
2564 int first; /* TRUE on first line */
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002565 int posix = (vim_strchr(p_cpo, CPO_PARA) != NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00002566#ifdef FEAT_FOLDING
2567 linenr_T fold_first; /* first line of a closed fold */
2568 linenr_T fold_last; /* last line of a closed fold */
2569 int fold_skipped; /* TRUE if a closed fold was skipped this
2570 iteration */
2571#endif
2572
2573 curr = curwin->w_cursor.lnum;
2574
2575 while (count--)
2576 {
2577 did_skip = FALSE;
2578 for (first = TRUE; ; first = FALSE)
2579 {
2580 if (*ml_get(curr) != NUL)
2581 did_skip = TRUE;
2582
2583#ifdef FEAT_FOLDING
2584 /* skip folded lines */
2585 fold_skipped = FALSE;
2586 if (first && hasFolding(curr, &fold_first, &fold_last))
2587 {
2588 curr = ((dir > 0) ? fold_last : fold_first) + dir;
2589 fold_skipped = TRUE;
2590 }
2591#endif
2592
Bram Moolenaar4399ef42005-02-12 14:29:27 +00002593 /* POSIX has it's own ideas of what a paragraph boundary is and it
2594 * doesn't match historical Vi: It also stops at a "{" in the
2595 * first column and at an empty line. */
2596 if (!first && did_skip && (startPS(curr, what, both)
2597 || (posix && what == NUL && *ml_get(curr) == '{')))
Bram Moolenaar071d4272004-06-13 20:20:40 +00002598 break;
2599
2600#ifdef FEAT_FOLDING
2601 if (fold_skipped)
2602 curr -= dir;
2603#endif
2604 if ((curr += dir) < 1 || curr > curbuf->b_ml.ml_line_count)
2605 {
2606 if (count)
2607 return FALSE;
2608 curr -= dir;
2609 break;
2610 }
2611 }
2612 }
2613 setpcmark();
2614 if (both && *ml_get(curr) == '}') /* include line with '}' */
2615 ++curr;
2616 curwin->w_cursor.lnum = curr;
2617 if (curr == curbuf->b_ml.ml_line_count && what != '}')
2618 {
2619 if ((curwin->w_cursor.col = (colnr_T)STRLEN(ml_get(curr))) != 0)
2620 {
2621 --curwin->w_cursor.col;
Bram Moolenaar92d640f2005-09-05 22:11:52 +00002622 *pincl = TRUE;
Bram Moolenaar071d4272004-06-13 20:20:40 +00002623 }
2624 }
2625 else
2626 curwin->w_cursor.col = 0;
2627 return TRUE;
2628}
2629
2630/*
2631 * check if the string 's' is a nroff macro that is in option 'opt'
2632 */
2633 static int
2634inmacro(opt, s)
2635 char_u *opt;
2636 char_u *s;
2637{
2638 char_u *macro;
2639
2640 for (macro = opt; macro[0]; ++macro)
2641 {
2642 /* Accept two characters in the option being equal to two characters
2643 * in the line. A space in the option matches with a space in the
2644 * line or the line having ended. */
2645 if ( (macro[0] == s[0]
2646 || (macro[0] == ' '
2647 && (s[0] == NUL || s[0] == ' ')))
2648 && (macro[1] == s[1]
2649 || ((macro[1] == NUL || macro[1] == ' ')
2650 && (s[0] == NUL || s[1] == NUL || s[1] == ' '))))
2651 break;
2652 ++macro;
2653 if (macro[0] == NUL)
2654 break;
2655 }
2656 return (macro[0] != NUL);
2657}
2658
2659/*
2660 * startPS: return TRUE if line 'lnum' is the start of a section or paragraph.
2661 * If 'para' is '{' or '}' only check for sections.
2662 * If 'both' is TRUE also stop at '}'
2663 */
2664 int
2665startPS(lnum, para, both)
2666 linenr_T lnum;
2667 int para;
2668 int both;
2669{
2670 char_u *s;
2671
2672 s = ml_get(lnum);
2673 if (*s == para || *s == '\f' || (both && *s == '}'))
2674 return TRUE;
2675 if (*s == '.' && (inmacro(p_sections, s + 1) ||
2676 (!para && inmacro(p_para, s + 1))))
2677 return TRUE;
2678 return FALSE;
2679}
2680
2681/*
2682 * The following routines do the word searches performed by the 'w', 'W',
2683 * 'b', 'B', 'e', and 'E' commands.
2684 */
2685
2686/*
2687 * To perform these searches, characters are placed into one of three
2688 * classes, and transitions between classes determine word boundaries.
2689 *
2690 * The classes are:
2691 *
2692 * 0 - white space
2693 * 1 - punctuation
2694 * 2 or higher - keyword characters (letters, digits and underscore)
2695 */
2696
2697static int cls_bigword; /* TRUE for "W", "B" or "E" */
2698
2699/*
2700 * cls() - returns the class of character at curwin->w_cursor
2701 *
2702 * If a 'W', 'B', or 'E' motion is being done (cls_bigword == TRUE), chars
2703 * from class 2 and higher are reported as class 1 since only white space
2704 * boundaries are of interest.
2705 */
2706 static int
2707cls()
2708{
2709 int c;
2710
2711 c = gchar_cursor();
2712#ifdef FEAT_FKMAP /* when 'akm' (Farsi mode), take care of Farsi blank */
2713 if (p_altkeymap && c == F_BLANK)
2714 return 0;
2715#endif
2716 if (c == ' ' || c == '\t' || c == NUL)
2717 return 0;
2718#ifdef FEAT_MBYTE
2719 if (enc_dbcs != 0 && c > 0xFF)
2720 {
2721 /* If cls_bigword, report multi-byte chars as class 1. */
2722 if (enc_dbcs == DBCS_KOR && cls_bigword)
2723 return 1;
2724
2725 /* process code leading/trailing bytes */
2726 return dbcs_class(((unsigned)c >> 8), (c & 0xFF));
2727 }
2728 if (enc_utf8)
2729 {
2730 c = utf_class(c);
2731 if (c != 0 && cls_bigword)
2732 return 1;
2733 return c;
2734 }
2735#endif
2736
2737 /* If cls_bigword is TRUE, report all non-blanks as class 1. */
2738 if (cls_bigword)
2739 return 1;
2740
2741 if (vim_iswordc(c))
2742 return 2;
2743 return 1;
2744}
2745
2746
2747/*
2748 * fwd_word(count, type, eol) - move forward one word
2749 *
2750 * Returns FAIL if the cursor was already at the end of the file.
2751 * If eol is TRUE, last word stops at end of line (for operators).
2752 */
2753 int
2754fwd_word(count, bigword, eol)
2755 long count;
2756 int bigword; /* "W", "E" or "B" */
2757 int eol;
2758{
2759 int sclass; /* starting class */
2760 int i;
2761 int last_line;
2762
2763#ifdef FEAT_VIRTUALEDIT
2764 curwin->w_cursor.coladd = 0;
2765#endif
2766 cls_bigword = bigword;
2767 while (--count >= 0)
2768 {
2769#ifdef FEAT_FOLDING
2770 /* When inside a range of folded lines, move to the last char of the
2771 * last line. */
2772 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2773 coladvance((colnr_T)MAXCOL);
2774#endif
2775 sclass = cls();
2776
2777 /*
2778 * We always move at least one character, unless on the last
2779 * character in the buffer.
2780 */
2781 last_line = (curwin->w_cursor.lnum == curbuf->b_ml.ml_line_count);
2782 i = inc_cursor();
2783 if (i == -1 || (i >= 1 && last_line)) /* started at last char in file */
2784 return FAIL;
2785 if (i == 1 && eol && count == 0) /* started at last char in line */
2786 return OK;
2787
2788 /*
2789 * Go one char past end of current word (if any)
2790 */
2791 if (sclass != 0)
2792 while (cls() == sclass)
2793 {
2794 i = inc_cursor();
2795 if (i == -1 || (i >= 1 && eol && count == 0))
2796 return OK;
2797 }
2798
2799 /*
2800 * go to next non-white
2801 */
2802 while (cls() == 0)
2803 {
2804 /*
2805 * We'll stop if we land on a blank line
2806 */
2807 if (curwin->w_cursor.col == 0 && *ml_get_curline() == NUL)
2808 break;
2809
2810 i = inc_cursor();
2811 if (i == -1 || (i >= 1 && eol && count == 0))
2812 return OK;
2813 }
2814 }
2815 return OK;
2816}
2817
2818/*
2819 * bck_word() - move backward 'count' words
2820 *
2821 * If stop is TRUE and we are already on the start of a word, move one less.
2822 *
2823 * Returns FAIL if top of the file was reached.
2824 */
2825 int
2826bck_word(count, bigword, stop)
2827 long count;
2828 int bigword;
2829 int stop;
2830{
2831 int sclass; /* starting class */
2832
2833#ifdef FEAT_VIRTUALEDIT
2834 curwin->w_cursor.coladd = 0;
2835#endif
2836 cls_bigword = bigword;
2837 while (--count >= 0)
2838 {
2839#ifdef FEAT_FOLDING
2840 /* When inside a range of folded lines, move to the first char of the
2841 * first line. */
2842 if (hasFolding(curwin->w_cursor.lnum, &curwin->w_cursor.lnum, NULL))
2843 curwin->w_cursor.col = 0;
2844#endif
2845 sclass = cls();
2846 if (dec_cursor() == -1) /* started at start of file */
2847 return FAIL;
2848
2849 if (!stop || sclass == cls() || sclass == 0)
2850 {
2851 /*
2852 * Skip white space before the word.
2853 * Stop on an empty line.
2854 */
2855 while (cls() == 0)
2856 {
2857 if (curwin->w_cursor.col == 0
2858 && lineempty(curwin->w_cursor.lnum))
2859 goto finished;
2860 if (dec_cursor() == -1) /* hit start of file, stop here */
2861 return OK;
2862 }
2863
2864 /*
2865 * Move backward to start of this word.
2866 */
2867 if (skip_chars(cls(), BACKWARD))
2868 return OK;
2869 }
2870
2871 inc_cursor(); /* overshot - forward one */
2872finished:
2873 stop = FALSE;
2874 }
2875 return OK;
2876}
2877
2878/*
2879 * end_word() - move to the end of the word
2880 *
2881 * There is an apparent bug in the 'e' motion of the real vi. At least on the
2882 * System V Release 3 version for the 80386. Unlike 'b' and 'w', the 'e'
2883 * motion crosses blank lines. When the real vi crosses a blank line in an
2884 * 'e' motion, the cursor is placed on the FIRST character of the next
2885 * non-blank line. The 'E' command, however, works correctly. Since this
2886 * appears to be a bug, I have not duplicated it here.
2887 *
2888 * Returns FAIL if end of the file was reached.
2889 *
2890 * If stop is TRUE and we are already on the end of a word, move one less.
2891 * If empty is TRUE stop on an empty line.
2892 */
2893 int
2894end_word(count, bigword, stop, empty)
2895 long count;
2896 int bigword;
2897 int stop;
2898 int empty;
2899{
2900 int sclass; /* starting class */
2901
2902#ifdef FEAT_VIRTUALEDIT
2903 curwin->w_cursor.coladd = 0;
2904#endif
2905 cls_bigword = bigword;
2906 while (--count >= 0)
2907 {
2908#ifdef FEAT_FOLDING
2909 /* When inside a range of folded lines, move to the last char of the
2910 * last line. */
2911 if (hasFolding(curwin->w_cursor.lnum, NULL, &curwin->w_cursor.lnum))
2912 coladvance((colnr_T)MAXCOL);
2913#endif
2914 sclass = cls();
2915 if (inc_cursor() == -1)
2916 return FAIL;
2917
2918 /*
2919 * If we're in the middle of a word, we just have to move to the end
2920 * of it.
2921 */
2922 if (cls() == sclass && sclass != 0)
2923 {
2924 /*
2925 * Move forward to end of the current word
2926 */
2927 if (skip_chars(sclass, FORWARD))
2928 return FAIL;
2929 }
2930 else if (!stop || sclass == 0)
2931 {
2932 /*
2933 * We were at the end of a word. Go to the end of the next word.
2934 * First skip white space, if 'empty' is TRUE, stop at empty line.
2935 */
2936 while (cls() == 0)
2937 {
2938 if (empty && curwin->w_cursor.col == 0
2939 && lineempty(curwin->w_cursor.lnum))
2940 goto finished;
2941 if (inc_cursor() == -1) /* hit end of file, stop here */
2942 return FAIL;
2943 }
2944
2945 /*
2946 * Move forward to the end of this word.
2947 */
2948 if (skip_chars(cls(), FORWARD))
2949 return FAIL;
2950 }
2951 dec_cursor(); /* overshot - one char backward */
2952finished:
2953 stop = FALSE; /* we move only one word less */
2954 }
2955 return OK;
2956}
2957
2958/*
2959 * Move back to the end of the word.
2960 *
2961 * Returns FAIL if start of the file was reached.
2962 */
2963 int
2964bckend_word(count, bigword, eol)
2965 long count;
2966 int bigword; /* TRUE for "B" */
2967 int eol; /* TRUE: stop at end of line. */
2968{
2969 int sclass; /* starting class */
2970 int i;
2971
2972#ifdef FEAT_VIRTUALEDIT
2973 curwin->w_cursor.coladd = 0;
2974#endif
2975 cls_bigword = bigword;
2976 while (--count >= 0)
2977 {
2978 sclass = cls();
2979 if ((i = dec_cursor()) == -1)
2980 return FAIL;
2981 if (eol && i == 1)
2982 return OK;
2983
2984 /*
2985 * Move backward to before the start of this word.
2986 */
2987 if (sclass != 0)
2988 {
2989 while (cls() == sclass)
2990 if ((i = dec_cursor()) == -1 || (eol && i == 1))
2991 return OK;
2992 }
2993
2994 /*
2995 * Move backward to end of the previous word
2996 */
2997 while (cls() == 0)
2998 {
2999 if (curwin->w_cursor.col == 0 && lineempty(curwin->w_cursor.lnum))
3000 break;
3001 if ((i = dec_cursor()) == -1 || (eol && i == 1))
3002 return OK;
3003 }
3004 }
3005 return OK;
3006}
3007
3008/*
3009 * Skip a row of characters of the same class.
3010 * Return TRUE when end-of-file reached, FALSE otherwise.
3011 */
3012 static int
3013skip_chars(cclass, dir)
3014 int cclass;
3015 int dir;
3016{
3017 while (cls() == cclass)
3018 if ((dir == FORWARD ? inc_cursor() : dec_cursor()) == -1)
3019 return TRUE;
3020 return FALSE;
3021}
3022
3023#ifdef FEAT_TEXTOBJ
3024/*
3025 * Go back to the start of the word or the start of white space
3026 */
3027 static void
3028back_in_line()
3029{
3030 int sclass; /* starting class */
3031
3032 sclass = cls();
3033 for (;;)
3034 {
3035 if (curwin->w_cursor.col == 0) /* stop at start of line */
3036 break;
3037 dec_cursor();
3038 if (cls() != sclass) /* stop at start of word */
3039 {
3040 inc_cursor();
3041 break;
3042 }
3043 }
3044}
3045
3046 static void
3047find_first_blank(posp)
3048 pos_T *posp;
3049{
3050 int c;
3051
3052 while (decl(posp) != -1)
3053 {
3054 c = gchar_pos(posp);
3055 if (!vim_iswhite(c))
3056 {
3057 incl(posp);
3058 break;
3059 }
3060 }
3061}
3062
3063/*
3064 * Skip count/2 sentences and count/2 separating white spaces.
3065 */
3066 static void
3067findsent_forward(count, at_start_sent)
3068 long count;
3069 int at_start_sent; /* cursor is at start of sentence */
3070{
3071 while (count--)
3072 {
3073 findsent(FORWARD, 1L);
3074 if (at_start_sent)
3075 find_first_blank(&curwin->w_cursor);
3076 if (count == 0 || at_start_sent)
3077 decl(&curwin->w_cursor);
3078 at_start_sent = !at_start_sent;
3079 }
3080}
3081
3082/*
3083 * Find word under cursor, cursor at end.
3084 * Used while an operator is pending, and in Visual mode.
3085 */
3086 int
3087current_word(oap, count, include, bigword)
3088 oparg_T *oap;
3089 long count;
3090 int include; /* TRUE: include word and white space */
3091 int bigword; /* FALSE == word, TRUE == WORD */
3092{
3093 pos_T start_pos;
3094 pos_T pos;
3095 int inclusive = TRUE;
3096 int include_white = FALSE;
3097
3098 cls_bigword = bigword;
3099
3100#ifdef FEAT_VISUAL
3101 /* Correct cursor when 'selection' is exclusive */
3102 if (VIsual_active && *p_sel == 'e' && lt(VIsual, curwin->w_cursor))
3103 dec_cursor();
3104
3105 /*
3106 * When Visual mode is not active, or when the VIsual area is only one
3107 * character, select the word and/or white space under the cursor.
3108 */
3109 if (!VIsual_active || equalpos(curwin->w_cursor, VIsual))
3110#endif
3111 {
3112 /*
3113 * Go to start of current word or white space.
3114 */
3115 back_in_line();
3116 start_pos = curwin->w_cursor;
3117
3118 /*
3119 * If the start is on white space, and white space should be included
3120 * (" word"), or start is not on white space, and white space should
3121 * not be included ("word"), find end of word.
3122 */
3123 if ((cls() == 0) == include)
3124 {
3125 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3126 return FAIL;
3127 }
3128 else
3129 {
3130 /*
3131 * If the start is not on white space, and white space should be
3132 * included ("word "), or start is on white space and white
3133 * space should not be included (" "), find start of word.
3134 * If we end up in the first column of the next line (single char
3135 * word) back up to end of the line.
3136 */
3137 fwd_word(1L, bigword, TRUE);
3138 if (curwin->w_cursor.col == 0)
3139 decl(&curwin->w_cursor);
3140 else
3141 oneleft();
3142
3143 if (include)
3144 include_white = TRUE;
3145 }
3146
3147#ifdef FEAT_VISUAL
3148 if (VIsual_active)
3149 {
3150 /* should do something when inclusive == FALSE ! */
3151 VIsual = start_pos;
3152 redraw_curbuf_later(INVERTED); /* update the inversion */
3153 }
3154 else
3155#endif
3156 {
3157 oap->start = start_pos;
3158 oap->motion_type = MCHAR;
3159 }
3160 --count;
3161 }
3162
3163 /*
3164 * When count is still > 0, extend with more objects.
3165 */
3166 while (count > 0)
3167 {
3168 inclusive = TRUE;
3169#ifdef FEAT_VISUAL
3170 if (VIsual_active && lt(curwin->w_cursor, VIsual))
3171 {
3172 /*
3173 * In Visual mode, with cursor at start: move cursor back.
3174 */
3175 if (decl(&curwin->w_cursor) == -1)
3176 return FAIL;
3177 if (include != (cls() != 0))
3178 {
3179 if (bck_word(1L, bigword, TRUE) == FAIL)
3180 return FAIL;
3181 }
3182 else
3183 {
3184 if (bckend_word(1L, bigword, TRUE) == FAIL)
3185 return FAIL;
3186 (void)incl(&curwin->w_cursor);
3187 }
3188 }
3189 else
3190#endif
3191 {
3192 /*
3193 * Move cursor forward one word and/or white area.
3194 */
3195 if (incl(&curwin->w_cursor) == -1)
3196 return FAIL;
3197 if (include != (cls() == 0))
3198 {
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003199 if (fwd_word(1L, bigword, TRUE) == FAIL && count > 1)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003200 return FAIL;
3201 /*
3202 * If end is just past a new-line, we don't want to include
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003203 * the first character on the line.
3204 * Put cursor on last char of white.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003205 */
Bram Moolenaar2a41f3a2005-01-11 21:30:59 +00003206 if (oneleft() == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003207 inclusive = FALSE;
3208 }
3209 else
3210 {
3211 if (end_word(1L, bigword, TRUE, TRUE) == FAIL)
3212 return FAIL;
3213 }
3214 }
3215 --count;
3216 }
3217
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003218 if (include_white && (cls() != 0
3219 || (curwin->w_cursor.col == 0 && !inclusive)))
Bram Moolenaar071d4272004-06-13 20:20:40 +00003220 {
3221 /*
3222 * If we don't include white space at the end, move the start
3223 * to include some white space there. This makes "daw" work
3224 * better on the last word in a sentence (and "2daw" on last-but-one
Bram Moolenaar69a7cb42004-06-20 12:51:53 +00003225 * word). Also when "2daw" deletes "word." at the end of the line
3226 * (cursor is at start of next line).
3227 * But don't delete white space at start of line (indent).
Bram Moolenaar071d4272004-06-13 20:20:40 +00003228 */
3229 pos = curwin->w_cursor; /* save cursor position */
3230 curwin->w_cursor = start_pos;
3231 if (oneleft() == OK)
3232 {
3233 back_in_line();
3234 if (cls() == 0 && curwin->w_cursor.col > 0)
3235 {
3236#ifdef FEAT_VISUAL
3237 if (VIsual_active)
3238 VIsual = curwin->w_cursor;
3239 else
3240#endif
3241 oap->start = curwin->w_cursor;
3242 }
3243 }
3244 curwin->w_cursor = pos; /* put cursor back at end */
3245 }
3246
3247#ifdef FEAT_VISUAL
3248 if (VIsual_active)
3249 {
3250 if (*p_sel == 'e' && inclusive && ltoreq(VIsual, curwin->w_cursor))
3251 inc_cursor();
3252 if (VIsual_mode == 'V')
3253 {
3254 VIsual_mode = 'v';
3255 redraw_cmdline = TRUE; /* show mode later */
3256 }
3257 }
3258 else
3259#endif
3260 oap->inclusive = inclusive;
3261
3262 return OK;
3263}
3264
3265/*
3266 * Find sentence(s) under the cursor, cursor at end.
3267 * When Visual active, extend it by one or more sentences.
3268 */
3269 int
3270current_sent(oap, count, include)
3271 oparg_T *oap;
3272 long count;
3273 int include;
3274{
3275 pos_T start_pos;
3276 pos_T pos;
3277 int start_blank;
3278 int c;
3279 int at_start_sent;
3280 long ncount;
3281
3282 start_pos = curwin->w_cursor;
3283 pos = start_pos;
3284 findsent(FORWARD, 1L); /* Find start of next sentence. */
3285
3286#ifdef FEAT_VISUAL
3287 /*
3288 * When visual area is bigger than one character: Extend it.
3289 */
3290 if (VIsual_active && !equalpos(start_pos, VIsual))
3291 {
3292extend:
3293 if (lt(start_pos, VIsual))
3294 {
3295 /*
3296 * Cursor at start of Visual area.
3297 * Find out where we are:
3298 * - in the white space before a sentence
3299 * - in a sentence or just after it
3300 * - at the start of a sentence
3301 */
3302 at_start_sent = TRUE;
3303 decl(&pos);
3304 while (lt(pos, curwin->w_cursor))
3305 {
3306 c = gchar_pos(&pos);
3307 if (!vim_iswhite(c))
3308 {
3309 at_start_sent = FALSE;
3310 break;
3311 }
3312 incl(&pos);
3313 }
3314 if (!at_start_sent)
3315 {
3316 findsent(BACKWARD, 1L);
3317 if (equalpos(curwin->w_cursor, start_pos))
3318 at_start_sent = TRUE; /* exactly at start of sentence */
3319 else
3320 /* inside a sentence, go to its end (start of next) */
3321 findsent(FORWARD, 1L);
3322 }
3323 if (include) /* "as" gets twice as much as "is" */
3324 count *= 2;
3325 while (count--)
3326 {
3327 if (at_start_sent)
3328 find_first_blank(&curwin->w_cursor);
3329 c = gchar_cursor();
3330 if (!at_start_sent || (!include && !vim_iswhite(c)))
3331 findsent(BACKWARD, 1L);
3332 at_start_sent = !at_start_sent;
3333 }
3334 }
3335 else
3336 {
3337 /*
3338 * Cursor at end of Visual area.
3339 * Find out where we are:
3340 * - just before a sentence
3341 * - just before or in the white space before a sentence
3342 * - in a sentence
3343 */
3344 incl(&pos);
3345 at_start_sent = TRUE;
3346 if (!equalpos(pos, curwin->w_cursor)) /* not just before a sentence */
3347 {
3348 at_start_sent = FALSE;
3349 while (lt(pos, curwin->w_cursor))
3350 {
3351 c = gchar_pos(&pos);
3352 if (!vim_iswhite(c))
3353 {
3354 at_start_sent = TRUE;
3355 break;
3356 }
3357 incl(&pos);
3358 }
3359 if (at_start_sent) /* in the sentence */
3360 findsent(BACKWARD, 1L);
3361 else /* in/before white before a sentence */
3362 curwin->w_cursor = start_pos;
3363 }
3364
3365 if (include) /* "as" gets twice as much as "is" */
3366 count *= 2;
3367 findsent_forward(count, at_start_sent);
3368 if (*p_sel == 'e')
3369 ++curwin->w_cursor.col;
3370 }
3371 return OK;
3372 }
3373#endif
3374
3375 /*
3376 * If cursor started on blank, check if it is just before the start of the
3377 * next sentence.
3378 */
3379 while (c = gchar_pos(&pos), vim_iswhite(c)) /* vim_iswhite() is a macro */
3380 incl(&pos);
3381 if (equalpos(pos, curwin->w_cursor))
3382 {
3383 start_blank = TRUE;
3384 find_first_blank(&start_pos); /* go back to first blank */
3385 }
3386 else
3387 {
3388 start_blank = FALSE;
3389 findsent(BACKWARD, 1L);
3390 start_pos = curwin->w_cursor;
3391 }
3392 if (include)
3393 ncount = count * 2;
3394 else
3395 {
3396 ncount = count;
3397 if (start_blank)
3398 --ncount;
3399 }
Bram Moolenaardef9e822004-12-31 20:58:58 +00003400 if (ncount > 0)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003401 findsent_forward(ncount, TRUE);
3402 else
3403 decl(&curwin->w_cursor);
3404
3405 if (include)
3406 {
3407 /*
3408 * If the blank in front of the sentence is included, exclude the
3409 * blanks at the end of the sentence, go back to the first blank.
3410 * If there are no trailing blanks, try to include leading blanks.
3411 */
3412 if (start_blank)
3413 {
3414 find_first_blank(&curwin->w_cursor);
3415 c = gchar_pos(&curwin->w_cursor); /* vim_iswhite() is a macro */
3416 if (vim_iswhite(c))
3417 decl(&curwin->w_cursor);
3418 }
3419 else if (c = gchar_cursor(), !vim_iswhite(c))
3420 find_first_blank(&start_pos);
3421 }
3422
3423#ifdef FEAT_VISUAL
3424 if (VIsual_active)
3425 {
3426 /* avoid getting stuck with "is" on a single space before a sent. */
3427 if (equalpos(start_pos, curwin->w_cursor))
3428 goto extend;
3429 if (*p_sel == 'e')
3430 ++curwin->w_cursor.col;
3431 VIsual = start_pos;
3432 VIsual_mode = 'v';
3433 redraw_curbuf_later(INVERTED); /* update the inversion */
3434 }
3435 else
3436#endif
3437 {
3438 /* include a newline after the sentence, if there is one */
3439 if (incl(&curwin->w_cursor) == -1)
3440 oap->inclusive = TRUE;
3441 else
3442 oap->inclusive = FALSE;
3443 oap->start = start_pos;
3444 oap->motion_type = MCHAR;
3445 }
3446 return OK;
3447}
3448
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003449/*
3450 * Find block under the cursor, cursor at end.
3451 * "what" and "other" are two matching parenthesis/paren/etc.
3452 */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003453 int
3454current_block(oap, count, include, what, other)
3455 oparg_T *oap;
3456 long count;
3457 int include; /* TRUE == include white space */
3458 int what; /* '(', '{', etc. */
3459 int other; /* ')', '}', etc. */
3460{
3461 pos_T old_pos;
3462 pos_T *pos = NULL;
3463 pos_T start_pos;
3464 pos_T *end_pos;
3465 pos_T old_start, old_end;
3466 char_u *save_cpo;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003467 int sol = FALSE; /* '{' at start of line */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003468
3469 old_pos = curwin->w_cursor;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003470 old_end = curwin->w_cursor; /* remember where we started */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003471 old_start = old_end;
3472
3473 /*
3474 * If we start on '(', '{', ')', '}', etc., use the whole block inclusive.
3475 */
3476#ifdef FEAT_VISUAL
3477 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3478#endif
3479 {
3480 setpcmark();
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003481 if (what == '{') /* ignore indent */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003482 while (inindent(1))
3483 if (inc_cursor() != 0)
3484 break;
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003485 if (gchar_cursor() == what)
3486 /* cursor on '(' or '{', move cursor just after it */
Bram Moolenaar071d4272004-06-13 20:20:40 +00003487 ++curwin->w_cursor.col;
3488 }
3489#ifdef FEAT_VISUAL
3490 else if (lt(VIsual, curwin->w_cursor))
3491 {
3492 old_start = VIsual;
3493 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3494 }
3495 else
3496 old_end = VIsual;
3497#endif
3498
3499 /*
3500 * Search backwards for unclosed '(', '{', etc..
3501 * Put this position in start_pos.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003502 * Ignore quotes here.
Bram Moolenaar071d4272004-06-13 20:20:40 +00003503 */
3504 save_cpo = p_cpo;
3505 p_cpo = (char_u *)"%";
3506 while (count-- > 0)
3507 {
3508 if ((pos = findmatch(NULL, what)) == NULL)
3509 break;
3510 curwin->w_cursor = *pos;
3511 start_pos = *pos; /* the findmatch for end_pos will overwrite *pos */
3512 }
3513 p_cpo = save_cpo;
3514
3515 /*
3516 * Search for matching ')', '}', etc.
3517 * Put this position in curwin->w_cursor.
3518 */
3519 if (pos == NULL || (end_pos = findmatch(NULL, other)) == NULL)
3520 {
3521 curwin->w_cursor = old_pos;
3522 return FAIL;
3523 }
3524 curwin->w_cursor = *end_pos;
3525
3526 /*
3527 * Try to exclude the '(', '{', ')', '}', etc. when "include" is FALSE.
3528 * If the ending '}' is only preceded by indent, skip that indent.
3529 * But only if the resulting area is not smaller than what we started with.
3530 */
3531 while (!include)
3532 {
3533 incl(&start_pos);
3534 sol = (curwin->w_cursor.col == 0);
3535 decl(&curwin->w_cursor);
3536 if (what == '{')
3537 while (inindent(1))
3538 {
3539 sol = TRUE;
3540 if (decl(&curwin->w_cursor) != 0)
3541 break;
3542 }
3543#ifdef FEAT_VISUAL
3544 /*
3545 * In Visual mode, when the resulting area is not bigger than what we
3546 * started with, extend it to the next block, and then exclude again.
3547 */
3548 if (!lt(start_pos, old_start) && !lt(old_end, curwin->w_cursor)
3549 && VIsual_active)
3550 {
3551 curwin->w_cursor = old_start;
3552 decl(&curwin->w_cursor);
3553 if ((pos = findmatch(NULL, what)) == NULL)
3554 {
3555 curwin->w_cursor = old_pos;
3556 return FAIL;
3557 }
3558 start_pos = *pos;
3559 curwin->w_cursor = *pos;
3560 if ((end_pos = findmatch(NULL, other)) == NULL)
3561 {
3562 curwin->w_cursor = old_pos;
3563 return FAIL;
3564 }
3565 curwin->w_cursor = *end_pos;
3566 }
3567 else
3568#endif
3569 break;
3570 }
3571
3572#ifdef FEAT_VISUAL
3573 if (VIsual_active)
3574 {
3575 if (*p_sel == 'e')
3576 ++curwin->w_cursor.col;
Bram Moolenaara5792f52005-11-23 21:25:05 +00003577 if (sol && gchar_cursor() != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00003578 inc(&curwin->w_cursor); /* include the line break */
3579 VIsual = start_pos;
3580 VIsual_mode = 'v';
3581 redraw_curbuf_later(INVERTED); /* update the inversion */
3582 showmode();
3583 }
3584 else
3585#endif
3586 {
3587 oap->start = start_pos;
3588 oap->motion_type = MCHAR;
3589 if (sol)
3590 {
3591 incl(&curwin->w_cursor);
3592 oap->inclusive = FALSE;
3593 }
3594 else
3595 oap->inclusive = TRUE;
3596 }
3597
3598 return OK;
3599}
3600
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003601static int in_html_tag __ARGS((int));
3602
3603/*
3604 * Return TRUE if the cursor is on a "<aaa>" tag. Ignore "<aaa/>".
3605 * When "end_tag" is TRUE return TRUE if the cursor is on "</aaa>".
3606 */
3607 static int
3608in_html_tag(end_tag)
3609 int end_tag;
3610{
3611 char_u *line = ml_get_curline();
3612 char_u *p;
3613 int c;
3614 int lc = NUL;
3615 pos_T pos;
3616
3617#ifdef FEAT_MBYTE
3618 if (enc_dbcs)
3619 {
3620 char_u *lp = NULL;
3621
3622 /* We search forward until the cursor, because searching backwards is
3623 * very slow for DBCS encodings. */
3624 for (p = line; p < line + curwin->w_cursor.col; mb_ptr_adv(p))
3625 if (*p == '>' || *p == '<')
3626 {
3627 lc = *p;
3628 lp = p;
3629 }
3630 if (*p != '<') /* check for '<' under cursor */
3631 {
3632 if (lc != '<')
3633 return FALSE;
3634 p = lp;
3635 }
3636 }
3637 else
3638#endif
3639 {
3640 for (p = line + curwin->w_cursor.col; p > line; )
3641 {
3642 if (*p == '<') /* find '<' under/before cursor */
3643 break;
3644 mb_ptr_back(line, p);
3645 if (*p == '>') /* find '>' before cursor */
3646 break;
3647 }
3648 if (*p != '<')
3649 return FALSE;
3650 }
3651
3652 pos.lnum = curwin->w_cursor.lnum;
3653 pos.col = p - line;
3654
3655 mb_ptr_adv(p);
3656 if (end_tag)
3657 /* check that there is a '/' after the '<' */
3658 return *p == '/';
3659
3660 /* check that there is no '/' after the '<' */
3661 if (*p == '/')
3662 return FALSE;
3663
3664 /* check that the matching '>' is not preceded by '/' */
3665 for (;;)
3666 {
3667 if (inc(&pos) < 0)
3668 return FALSE;
3669 c = *ml_get_pos(&pos);
3670 if (c == '>')
3671 break;
3672 lc = c;
3673 }
3674 return lc != '/';
3675}
3676
3677/*
3678 * Find tag block under the cursor, cursor at end.
3679 */
3680 int
3681current_tagblock(oap, count_arg, include)
3682 oparg_T *oap;
3683 long count_arg;
3684 int include; /* TRUE == include white space */
3685{
3686 long count = count_arg;
3687 long n;
3688 pos_T old_pos;
3689 pos_T start_pos;
3690 pos_T end_pos;
3691 pos_T old_start, old_end;
3692 char_u *spat, *epat;
3693 char_u *p;
3694 char_u *cp;
3695 int len;
3696 int r;
3697 int do_include = include;
3698 int save_p_ws = p_ws;
3699 int retval = FAIL;
3700
3701 p_ws = FALSE;
3702
3703 old_pos = curwin->w_cursor;
3704 old_end = curwin->w_cursor; /* remember where we started */
3705 old_start = old_end;
3706
3707 /*
Bram Moolenaar45360022005-07-21 21:08:21 +00003708 * If we start on "<aaa>" select that block.
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003709 */
3710#ifdef FEAT_VISUAL
3711 if (!VIsual_active || equalpos(VIsual, curwin->w_cursor))
3712#endif
3713 {
3714 setpcmark();
3715
3716 /* ignore indent */
3717 while (inindent(1))
3718 if (inc_cursor() != 0)
3719 break;
3720
3721 if (in_html_tag(FALSE))
3722 {
3723 /* cursor on start tag, move to just after it */
3724 while (*ml_get_cursor() != '>')
3725 if (inc_cursor() < 0)
3726 break;
3727 }
3728 else if (in_html_tag(TRUE))
3729 {
3730 /* cursor on end tag, move to just before it */
3731 while (*ml_get_cursor() != '<')
3732 if (dec_cursor() < 0)
3733 break;
3734 dec_cursor();
3735 old_end = curwin->w_cursor;
3736 }
3737 }
3738#ifdef FEAT_VISUAL
3739 else if (lt(VIsual, curwin->w_cursor))
3740 {
3741 old_start = VIsual;
3742 curwin->w_cursor = VIsual; /* cursor at low end of Visual */
3743 }
3744 else
3745 old_end = VIsual;
3746#endif
3747
3748again:
3749 /*
3750 * Search backwards for unclosed "<aaa>".
3751 * Put this position in start_pos.
3752 */
3753 for (n = 0; n < count; ++n)
3754 {
Bram Moolenaar45360022005-07-21 21:08:21 +00003755 if (do_searchpair((char_u *)"<[^ \t>/!]\\+\\%(\\_s\\_[^>]\\{-}[^/]>\\|$\\|\\_s\\=>\\)",
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003756 (char_u *)"",
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003757 (char_u *)"</[^>]*>", BACKWARD, (char_u *)"", 0,
3758 NULL, (linenr_T)0) <= 0)
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003759 {
3760 curwin->w_cursor = old_pos;
3761 goto theend;
3762 }
3763 }
3764 start_pos = curwin->w_cursor;
3765
3766 /*
3767 * Search for matching "</aaa>". First isolate the "aaa".
3768 */
3769 inc_cursor();
3770 p = ml_get_cursor();
3771 for (cp = p; *cp != NUL && *cp != '>' && !vim_iswhite(*cp); mb_ptr_adv(cp))
3772 ;
3773 len = cp - p;
3774 if (len == 0)
3775 {
3776 curwin->w_cursor = old_pos;
3777 goto theend;
3778 }
3779 spat = alloc(len + 29);
3780 epat = alloc(len + 9);
3781 if (spat == NULL || epat == NULL)
3782 {
3783 vim_free(spat);
3784 vim_free(epat);
3785 curwin->w_cursor = old_pos;
3786 goto theend;
3787 }
3788 sprintf((char *)spat, "<%.*s\\%%(\\_[^>]\\{-}[^/]>\\|>\\)\\c", len, p);
3789 sprintf((char *)epat, "</%.*s>\\c", len, p);
3790
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003791 r = do_searchpair(spat, (char_u *)"", epat, FORWARD, (char_u *)"",
3792 0, NULL, (linenr_T)0);
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003793
3794 vim_free(spat);
3795 vim_free(epat);
3796
3797 if (r < 1 || lt(curwin->w_cursor, old_end))
3798 {
3799 /* Can't find other end or it's before the previous end. Could be a
3800 * HTML tag that doesn't have a matching end. Search backwards for
3801 * another starting tag. */
3802 count = 1;
3803 curwin->w_cursor = start_pos;
3804 goto again;
3805 }
3806
3807 if (do_include || r < 1)
3808 {
3809 /* Include up to the '>'. */
3810 while (*ml_get_cursor() != '>')
3811 if (inc_cursor() < 0)
3812 break;
3813 }
3814 else
3815 {
3816 /* Exclude the '<' of the end tag. */
3817 if (*ml_get_cursor() == '<')
3818 dec_cursor();
3819 }
3820 end_pos = curwin->w_cursor;
3821
3822 if (!do_include)
3823 {
3824 /* Exclude the start tag. */
3825 curwin->w_cursor = start_pos;
3826 while (inc_cursor() >= 0)
3827 if (*ml_get_cursor() == '>' && lt(curwin->w_cursor, end_pos))
3828 {
3829 inc_cursor();
3830 start_pos = curwin->w_cursor;
3831 break;
3832 }
3833 curwin->w_cursor = end_pos;
3834
Bram Moolenaar45360022005-07-21 21:08:21 +00003835 /* If we now have the same text as before reset "do_include" and try
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003836 * again. */
Bram Moolenaar45360022005-07-21 21:08:21 +00003837 if (equalpos(start_pos, old_start) && equalpos(end_pos, old_end))
Bram Moolenaard6c04cd2005-07-19 22:18:49 +00003838 {
3839 do_include = TRUE;
3840 curwin->w_cursor = old_start;
3841 count = count_arg;
3842 goto again;
3843 }
3844 }
3845
3846#ifdef FEAT_VISUAL
3847 if (VIsual_active)
3848 {
3849 if (*p_sel == 'e')
3850 ++curwin->w_cursor.col;
3851 VIsual = start_pos;
3852 VIsual_mode = 'v';
3853 redraw_curbuf_later(INVERTED); /* update the inversion */
3854 showmode();
3855 }
3856 else
3857#endif
3858 {
3859 oap->start = start_pos;
3860 oap->motion_type = MCHAR;
3861 oap->inclusive = TRUE;
3862 }
3863 retval = OK;
3864
3865theend:
3866 p_ws = save_p_ws;
3867 return retval;
3868}
3869
Bram Moolenaar071d4272004-06-13 20:20:40 +00003870 int
3871current_par(oap, count, include, type)
3872 oparg_T *oap;
3873 long count;
3874 int include; /* TRUE == include white space */
3875 int type; /* 'p' for paragraph, 'S' for section */
3876{
3877 linenr_T start_lnum;
3878 linenr_T end_lnum;
3879 int white_in_front;
3880 int dir;
3881 int start_is_white;
3882 int prev_start_is_white;
3883 int retval = OK;
3884 int do_white = FALSE;
3885 int t;
3886 int i;
3887
3888 if (type == 'S') /* not implemented yet */
3889 return FAIL;
3890
3891 start_lnum = curwin->w_cursor.lnum;
3892
3893#ifdef FEAT_VISUAL
3894 /*
3895 * When visual area is more than one line: extend it.
3896 */
3897 if (VIsual_active && start_lnum != VIsual.lnum)
3898 {
3899extend:
3900 if (start_lnum < VIsual.lnum)
3901 dir = BACKWARD;
3902 else
3903 dir = FORWARD;
3904 for (i = count; --i >= 0; )
3905 {
3906 if (start_lnum ==
3907 (dir == BACKWARD ? 1 : curbuf->b_ml.ml_line_count))
3908 {
3909 retval = FAIL;
3910 break;
3911 }
3912
3913 prev_start_is_white = -1;
3914 for (t = 0; t < 2; ++t)
3915 {
3916 start_lnum += dir;
3917 start_is_white = linewhite(start_lnum);
3918 if (prev_start_is_white == start_is_white)
3919 {
3920 start_lnum -= dir;
3921 break;
3922 }
3923 for (;;)
3924 {
3925 if (start_lnum == (dir == BACKWARD
3926 ? 1 : curbuf->b_ml.ml_line_count))
3927 break;
3928 if (start_is_white != linewhite(start_lnum + dir)
3929 || (!start_is_white
3930 && startPS(start_lnum + (dir > 0
3931 ? 1 : 0), 0, 0)))
3932 break;
3933 start_lnum += dir;
3934 }
3935 if (!include)
3936 break;
3937 if (start_lnum == (dir == BACKWARD
3938 ? 1 : curbuf->b_ml.ml_line_count))
3939 break;
3940 prev_start_is_white = start_is_white;
3941 }
3942 }
3943 curwin->w_cursor.lnum = start_lnum;
3944 curwin->w_cursor.col = 0;
3945 return retval;
3946 }
3947#endif
3948
3949 /*
3950 * First move back to the start_lnum of the paragraph or white lines
3951 */
3952 white_in_front = linewhite(start_lnum);
3953 while (start_lnum > 1)
3954 {
3955 if (white_in_front) /* stop at first white line */
3956 {
3957 if (!linewhite(start_lnum - 1))
3958 break;
3959 }
3960 else /* stop at first non-white line of start of paragraph */
3961 {
3962 if (linewhite(start_lnum - 1) || startPS(start_lnum, 0, 0))
3963 break;
3964 }
3965 --start_lnum;
3966 }
3967
3968 /*
3969 * Move past the end of any white lines.
3970 */
3971 end_lnum = start_lnum;
Bram Moolenaara23ccb82006-02-27 00:08:02 +00003972 while (end_lnum <= curbuf->b_ml.ml_line_count && linewhite(end_lnum))
3973 ++end_lnum;
Bram Moolenaar071d4272004-06-13 20:20:40 +00003974
3975 --end_lnum;
3976 i = count;
3977 if (!include && white_in_front)
3978 --i;
3979 while (i--)
3980 {
3981 if (end_lnum == curbuf->b_ml.ml_line_count)
3982 return FAIL;
3983
3984 if (!include)
3985 do_white = linewhite(end_lnum + 1);
3986
3987 if (include || !do_white)
3988 {
3989 ++end_lnum;
3990 /*
3991 * skip to end of paragraph
3992 */
3993 while (end_lnum < curbuf->b_ml.ml_line_count
3994 && !linewhite(end_lnum + 1)
3995 && !startPS(end_lnum + 1, 0, 0))
3996 ++end_lnum;
3997 }
3998
3999 if (i == 0 && white_in_front && include)
4000 break;
4001
4002 /*
4003 * skip to end of white lines after paragraph
4004 */
4005 if (include || do_white)
4006 while (end_lnum < curbuf->b_ml.ml_line_count
4007 && linewhite(end_lnum + 1))
4008 ++end_lnum;
4009 }
4010
4011 /*
4012 * If there are no empty lines at the end, try to find some empty lines at
4013 * the start (unless that has been done already).
4014 */
4015 if (!white_in_front && !linewhite(end_lnum) && include)
4016 while (start_lnum > 1 && linewhite(start_lnum - 1))
4017 --start_lnum;
4018
4019#ifdef FEAT_VISUAL
4020 if (VIsual_active)
4021 {
4022 /* Problem: when doing "Vipipip" nothing happens in a single white
4023 * line, we get stuck there. Trap this here. */
4024 if (VIsual_mode == 'V' && start_lnum == curwin->w_cursor.lnum)
4025 goto extend;
4026 VIsual.lnum = start_lnum;
4027 VIsual_mode = 'V';
4028 redraw_curbuf_later(INVERTED); /* update the inversion */
4029 showmode();
4030 }
4031 else
4032#endif
4033 {
4034 oap->start.lnum = start_lnum;
4035 oap->start.col = 0;
4036 oap->motion_type = MLINE;
4037 }
4038 curwin->w_cursor.lnum = end_lnum;
4039 curwin->w_cursor.col = 0;
4040
4041 return OK;
4042}
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004043
4044static int find_next_quote __ARGS((char_u *top_ptr, int col, int quotechar, char_u *escape));
4045static int find_prev_quote __ARGS((char_u *line, int col_start, int quotechar, char_u *escape));
4046
4047/*
4048 * Search quote char from string line[col].
4049 * Quote character escaped by one of the characters in "escape" is not counted
4050 * as a quote.
4051 * Returns column number of "quotechar" or -1 when not found.
4052 */
4053 static int
4054find_next_quote(line, col, quotechar, escape)
4055 char_u *line;
4056 int col;
4057 int quotechar;
4058 char_u *escape; /* escape characters, can be NULL */
4059{
4060 int c;
4061
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004062 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004063 {
4064 c = line[col];
4065 if (c == NUL)
4066 return -1;
4067 else if (escape != NULL && vim_strchr(escape, c))
4068 ++col;
4069 else if (c == quotechar)
4070 break;
4071#ifdef FEAT_MBYTE
4072 if (has_mbyte)
Bram Moolenaar0fa313a2005-08-10 21:07:57 +00004073 col += (*mb_ptr2len)(line + col);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004074 else
Bram Moolenaar071d4272004-06-13 20:20:40 +00004075#endif
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004076 ++col;
4077 }
4078 return col;
4079}
4080
4081/*
4082 * Search backwards in "line" from column "col_start" to find "quotechar".
4083 * Quote character escaped by one of the characters in "escape" is not counted
4084 * as a quote.
4085 * Return the found column or zero.
4086 */
4087 static int
4088find_prev_quote(line, col_start, quotechar, escape)
4089 char_u *line;
4090 int col_start;
4091 int quotechar;
4092 char_u *escape; /* escape characters, can be NULL */
4093{
4094 int n;
4095
4096 while (col_start > 0)
4097 {
4098 --col_start;
4099#ifdef FEAT_MBYTE
4100 col_start -= (*mb_head_off)(line, line + col_start);
4101#endif
4102 n = 0;
4103 if (escape != NULL)
4104 while (col_start - n > 0 && vim_strchr(escape,
4105 line[col_start - n - 1]) != NULL)
4106 ++n;
4107 if (n & 1)
4108 col_start -= n; /* uneven number of escape chars, skip it */
4109 else if (line[col_start] == quotechar)
4110 break;
4111 }
4112 return col_start;
4113}
4114
4115/*
4116 * Find quote under the cursor, cursor at end.
4117 * Returns TRUE if found, else FALSE.
4118 */
4119 int
4120current_quote(oap, count, include, quotechar)
4121 oparg_T *oap;
Bram Moolenaarab194812005-09-14 21:40:12 +00004122 long count;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004123 int include; /* TRUE == include quote char */
4124 int quotechar; /* Quote character */
4125{
4126 char_u *line = ml_get_curline();
4127 int col_end;
4128 int col_start = curwin->w_cursor.col;
4129 int inclusive = FALSE;
4130#ifdef FEAT_VISUAL
4131 int vis_empty = TRUE; /* Visual selection <= 1 char */
4132 int vis_bef_curs = FALSE; /* Visual starts before cursor */
Bram Moolenaarab194812005-09-14 21:40:12 +00004133 int inside_quotes = FALSE; /* Looks like "i'" done before */
4134 int selected_quote = FALSE; /* Has quote inside selection */
4135 int i;
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004136
4137 /* Correct cursor when 'selection' is exclusive */
4138 if (VIsual_active)
4139 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004140 vis_bef_curs = lt(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004141 if (*p_sel == 'e' && vis_bef_curs)
4142 dec_cursor();
4143 vis_empty = equalpos(VIsual, curwin->w_cursor);
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004144 }
Bram Moolenaarab194812005-09-14 21:40:12 +00004145
4146 if (!vis_empty)
4147 {
4148 /* Check if the existing selection exactly spans the text inside
4149 * quotes. */
4150 if (vis_bef_curs)
4151 {
4152 inside_quotes = VIsual.col > 0
4153 && line[VIsual.col - 1] == quotechar
4154 && line[curwin->w_cursor.col] != NUL
4155 && line[curwin->w_cursor.col + 1] == quotechar;
4156 i = VIsual.col;
4157 col_end = curwin->w_cursor.col;
4158 }
4159 else
4160 {
4161 inside_quotes = curwin->w_cursor.col > 0
4162 && line[curwin->w_cursor.col - 1] == quotechar
4163 && line[VIsual.col] != NUL
4164 && line[VIsual.col + 1] == quotechar;
4165 i = curwin->w_cursor.col;
4166 col_end = VIsual.col;
4167 }
4168
4169 /* Find out if we have a quote in the selection. */
4170 while (i <= col_end)
4171 if (line[i++] == quotechar)
4172 {
4173 selected_quote = TRUE;
4174 break;
4175 }
4176 }
4177
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004178 if (!vis_empty && line[col_start] == quotechar)
4179 {
4180 /* Already selecting something and on a quote character. Find the
4181 * next quoted string. */
4182 if (vis_bef_curs)
4183 {
4184 /* Assume we are on a closing quote: move to after the next
4185 * opening quote. */
4186 col_start = find_next_quote(line, col_start + 1, quotechar, NULL);
4187 if (col_start < 0)
4188 return FALSE;
4189 col_end = find_next_quote(line, col_start + 1, quotechar,
4190 curbuf->b_p_qe);
4191 if (col_end < 0)
4192 {
4193 /* We were on a starting quote perhaps? */
4194 col_end = col_start;
4195 col_start = curwin->w_cursor.col;
4196 }
4197 }
4198 else
4199 {
4200 col_end = find_prev_quote(line, col_start, quotechar, NULL);
4201 if (line[col_end] != quotechar)
4202 return FALSE;
4203 col_start = find_prev_quote(line, col_end, quotechar,
4204 curbuf->b_p_qe);
4205 if (line[col_start] != quotechar)
4206 {
4207 /* We were on an ending quote perhaps? */
4208 col_start = col_end;
4209 col_end = curwin->w_cursor.col;
4210 }
4211 }
4212 }
4213 else
4214#endif
4215
4216 if (line[col_start] == quotechar
4217#ifdef FEAT_VISUAL
4218 || !vis_empty
4219#endif
4220 )
4221 {
4222 int first_col = col_start;
4223
4224#ifdef FEAT_VISUAL
4225 if (!vis_empty)
4226 {
4227 if (vis_bef_curs)
4228 first_col = find_next_quote(line, col_start, quotechar, NULL);
4229 else
4230 first_col = find_prev_quote(line, col_start, quotechar, NULL);
4231 }
4232#endif
4233 /* The cursor is on a quote, we don't know if it's the opening or
4234 * closing quote. Search from the start of the line to find out.
4235 * Also do this when there is a Visual area, a' may leave the cursor
4236 * in between two strings. */
4237 col_start = 0;
Bram Moolenaard8e9bb22005-07-09 21:14:46 +00004238 for (;;)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004239 {
4240 /* Find open quote character. */
4241 col_start = find_next_quote(line, col_start, quotechar, NULL);
4242 if (col_start < 0 || col_start > first_col)
4243 return FALSE;
4244 /* Find close quote character. */
4245 col_end = find_next_quote(line, col_start + 1, quotechar,
4246 curbuf->b_p_qe);
4247 if (col_end < 0)
4248 return FALSE;
4249 /* If is cursor between start and end quote character, it is
4250 * target text object. */
4251 if (col_start <= first_col && first_col <= col_end)
4252 break;
4253 col_start = col_end + 1;
4254 }
4255 }
4256 else
4257 {
4258 /* Search backward for a starting quote. */
4259 col_start = find_prev_quote(line, col_start, quotechar, curbuf->b_p_qe);
4260 if (line[col_start] != quotechar)
4261 {
4262 /* No quote before the cursor, look after the cursor. */
4263 col_start = find_next_quote(line, col_start, quotechar, NULL);
4264 if (col_start < 0)
4265 return FALSE;
4266 }
4267
4268 /* Find close quote character. */
4269 col_end = find_next_quote(line, col_start + 1, quotechar,
4270 curbuf->b_p_qe);
4271 if (col_end < 0)
4272 return FALSE;
4273 }
4274
4275 /* When "include" is TRUE, include spaces after closing quote or before
4276 * the starting quote. */
4277 if (include)
4278 {
4279 if (vim_iswhite(line[col_end + 1]))
4280 while (vim_iswhite(line[col_end + 1]))
4281 ++col_end;
4282 else
4283 while (col_start > 0 && vim_iswhite(line[col_start - 1]))
4284 --col_start;
4285 }
4286
Bram Moolenaarab194812005-09-14 21:40:12 +00004287 /* Set start position. After vi" another i" must include the ".
4288 * For v2i" include the quotes. */
4289 if (!include && count < 2
4290#ifdef FEAT_VISUAL
4291 && (vis_empty || !inside_quotes)
4292#endif
4293 )
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004294 ++col_start;
4295 curwin->w_cursor.col = col_start;
4296#ifdef FEAT_VISUAL
4297 if (VIsual_active)
4298 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004299 /* Set the start of the Visual area when the Visual area was empty, we
4300 * were just inside quotes or the Visual area didn't start at a quote
4301 * and didn't include a quote.
4302 */
4303 if (vis_empty
4304 || (vis_bef_curs
4305 && !selected_quote
4306 && (inside_quotes
4307 || (line[VIsual.col] != quotechar
4308 && (VIsual.col == 0
4309 || line[VIsual.col - 1] != quotechar)))))
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004310 {
4311 VIsual = curwin->w_cursor;
4312 redraw_curbuf_later(INVERTED);
4313 }
4314 }
4315 else
4316#endif
4317 {
4318 oap->start = curwin->w_cursor;
4319 oap->motion_type = MCHAR;
4320 }
4321
4322 /* Set end position. */
4323 curwin->w_cursor.col = col_end;
Bram Moolenaarab194812005-09-14 21:40:12 +00004324 if ((include || count > 1
4325#ifdef FEAT_VISUAL
4326 /* After vi" another i" must include the ". */
4327 || (!vis_empty && inside_quotes)
4328#endif
4329 ) && inc_cursor() == 2)
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004330 inclusive = TRUE;
4331#ifdef FEAT_VISUAL
4332 if (VIsual_active)
4333 {
4334 if (vis_empty || vis_bef_curs)
4335 {
4336 /* decrement cursor when 'selection' is not exclusive */
4337 if (*p_sel != 'e')
4338 dec_cursor();
4339 }
4340 else
4341 {
Bram Moolenaarab194812005-09-14 21:40:12 +00004342 /* Cursor is at start of Visual area. Set the end of the Visual
4343 * area when it was just inside quotes or it didn't end at a
4344 * quote. */
4345 if (inside_quotes
4346 || (!selected_quote
4347 && line[VIsual.col] != quotechar
4348 && (line[VIsual.col] == NUL
4349 || line[VIsual.col + 1] != quotechar)))
4350 {
4351 dec_cursor();
4352 VIsual = curwin->w_cursor;
4353 }
Bram Moolenaarcfbc5ee2004-07-02 15:38:35 +00004354 curwin->w_cursor.col = col_start;
4355 }
4356 if (VIsual_mode == 'V')
4357 {
4358 VIsual_mode = 'v';
4359 redraw_cmdline = TRUE; /* show mode later */
4360 }
4361 }
4362 else
4363#endif
4364 {
4365 /* Set inclusive and other oap's flags. */
4366 oap->inclusive = inclusive;
4367 }
4368
4369 return OK;
4370}
4371
4372#endif /* FEAT_TEXTOBJ */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004373
4374#if defined(FEAT_LISP) || defined(FEAT_CINDENT) || defined(FEAT_TEXTOBJ) \
4375 || defined(PROTO)
4376/*
4377 * return TRUE if line 'lnum' is empty or has white chars only.
4378 */
4379 int
4380linewhite(lnum)
4381 linenr_T lnum;
4382{
4383 char_u *p;
4384
4385 p = skipwhite(ml_get(lnum));
4386 return (*p == NUL);
4387}
4388#endif
4389
4390#if defined(FEAT_FIND_ID) || defined(PROTO)
4391/*
4392 * Find identifiers or defines in included files.
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004393 * if p_ic && (compl_cont_status & CONT_SOL) then ptr must be in lowercase.
Bram Moolenaar071d4272004-06-13 20:20:40 +00004394 */
4395/*ARGSUSED*/
4396 void
4397find_pattern_in_path(ptr, dir, len, whole, skip_comments,
4398 type, count, action, start_lnum, end_lnum)
4399 char_u *ptr; /* pointer to search pattern */
4400 int dir; /* direction of expansion */
4401 int len; /* length of search pattern */
4402 int whole; /* match whole words only */
4403 int skip_comments; /* don't match inside comments */
4404 int type; /* Type of search; are we looking for a type?
4405 a macro? */
4406 long count;
4407 int action; /* What to do when we find it */
4408 linenr_T start_lnum; /* first line to start searching */
4409 linenr_T end_lnum; /* last line for searching */
4410{
4411 SearchedFile *files; /* Stack of included files */
4412 SearchedFile *bigger; /* When we need more space */
4413 int max_path_depth = 50;
4414 long match_count = 1;
4415
4416 char_u *pat;
4417 char_u *new_fname;
4418 char_u *curr_fname = curbuf->b_fname;
4419 char_u *prev_fname = NULL;
4420 linenr_T lnum;
4421 int depth;
4422 int depth_displayed; /* For type==CHECK_PATH */
4423 int old_files;
4424 int already_searched;
4425 char_u *file_line;
4426 char_u *line;
4427 char_u *p;
4428 char_u save_char;
4429 int define_matched;
4430 regmatch_T regmatch;
4431 regmatch_T incl_regmatch;
4432 regmatch_T def_regmatch;
4433 int matched = FALSE;
4434 int did_show = FALSE;
4435 int found = FALSE;
4436 int i;
4437 char_u *already = NULL;
4438 char_u *startp = NULL;
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004439 char_u *inc_opt = NULL;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004440#ifdef RISCOS
4441 int previous_munging = __riscosify_control;
4442#endif
4443#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4444 win_T *curwin_save = NULL;
4445#endif
4446
4447 regmatch.regprog = NULL;
4448 incl_regmatch.regprog = NULL;
4449 def_regmatch.regprog = NULL;
4450
4451 file_line = alloc(LSIZE);
4452 if (file_line == NULL)
4453 return;
4454
4455#ifdef RISCOS
4456 /* UnixLib knows best how to munge c file names - turn munging back on. */
Bram Moolenaar293ee4d2004-12-09 21:34:53 +00004457 int __riscosify_control = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004458#endif
4459
4460 if (type != CHECK_PATH && type != FIND_DEFINE
4461#ifdef FEAT_INS_EXPAND
4462 /* when CONT_SOL is set compare "ptr" with the beginning of the line
4463 * is faster than quote_meta/regcomp/regexec "ptr" -- Acevedo */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004464 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004465#endif
4466 )
4467 {
4468 pat = alloc(len + 5);
4469 if (pat == NULL)
4470 goto fpip_end;
4471 sprintf((char *)pat, whole ? "\\<%.*s\\>" : "%.*s", len, ptr);
4472 /* ignore case according to p_ic, p_scs and pat */
4473 regmatch.rm_ic = ignorecase(pat);
4474 regmatch.regprog = vim_regcomp(pat, p_magic ? RE_MAGIC : 0);
4475 vim_free(pat);
4476 if (regmatch.regprog == NULL)
4477 goto fpip_end;
4478 }
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004479 inc_opt = (*curbuf->b_p_inc == NUL) ? p_inc : curbuf->b_p_inc;
4480 if (*inc_opt != NUL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004481 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004482 incl_regmatch.regprog = vim_regcomp(inc_opt, p_magic ? RE_MAGIC : 0);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004483 if (incl_regmatch.regprog == NULL)
4484 goto fpip_end;
4485 incl_regmatch.rm_ic = FALSE; /* don't ignore case in incl. pat. */
4486 }
4487 if (type == FIND_DEFINE && (*curbuf->b_p_def != NUL || *p_def != NUL))
4488 {
4489 def_regmatch.regprog = vim_regcomp(*curbuf->b_p_def == NUL
4490 ? p_def : curbuf->b_p_def, p_magic ? RE_MAGIC : 0);
4491 if (def_regmatch.regprog == NULL)
4492 goto fpip_end;
4493 def_regmatch.rm_ic = FALSE; /* don't ignore case in define pat. */
4494 }
4495 files = (SearchedFile *)lalloc_clear((long_u)
4496 (max_path_depth * sizeof(SearchedFile)), TRUE);
4497 if (files == NULL)
4498 goto fpip_end;
4499 old_files = max_path_depth;
4500 depth = depth_displayed = -1;
4501
4502 lnum = start_lnum;
4503 if (end_lnum > curbuf->b_ml.ml_line_count)
4504 end_lnum = curbuf->b_ml.ml_line_count;
4505 if (lnum > end_lnum) /* do at least one line */
4506 lnum = end_lnum;
4507 line = ml_get(lnum);
4508
4509 for (;;)
4510 {
4511 if (incl_regmatch.regprog != NULL
4512 && vim_regexec(&incl_regmatch, line, (colnr_T)0))
4513 {
Bram Moolenaar482aaeb2005-09-29 18:26:07 +00004514 char_u *p_fname = (curr_fname == curbuf->b_fname)
4515 ? curbuf->b_ffname : curr_fname;
4516
4517 if (inc_opt != NULL && strstr((char *)inc_opt, "\\zs") != NULL)
4518 /* Use text from '\zs' to '\ze' (or end) of 'include'. */
4519 new_fname = find_file_name_in_path(incl_regmatch.startp[0],
4520 incl_regmatch.endp[0] - incl_regmatch.startp[0],
4521 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname);
4522 else
4523 /* Use text after match with 'include'. */
4524 new_fname = file_name_in_line(incl_regmatch.endp[0], 0,
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004525 FNAME_EXP|FNAME_INCL|FNAME_REL, 1L, p_fname, NULL);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004526 already_searched = FALSE;
4527 if (new_fname != NULL)
4528 {
4529 /* Check whether we have already searched in this file */
4530 for (i = 0;; i++)
4531 {
4532 if (i == depth + 1)
4533 i = old_files;
4534 if (i == max_path_depth)
4535 break;
4536 if (fullpathcmp(new_fname, files[i].name, TRUE) & FPC_SAME)
4537 {
4538 if (type != CHECK_PATH &&
4539 action == ACTION_SHOW_ALL && files[i].matched)
4540 {
4541 msg_putchar('\n'); /* cursor below last one */
4542 if (!got_int) /* don't display if 'q'
4543 typed at "--more--"
4544 mesage */
4545 {
4546 msg_home_replace_hl(new_fname);
4547 MSG_PUTS(_(" (includes previously listed match)"));
4548 prev_fname = NULL;
4549 }
4550 }
4551 vim_free(new_fname);
4552 new_fname = NULL;
4553 already_searched = TRUE;
4554 break;
4555 }
4556 }
4557 }
4558
4559 if (type == CHECK_PATH && (action == ACTION_SHOW_ALL
4560 || (new_fname == NULL && !already_searched)))
4561 {
4562 if (did_show)
4563 msg_putchar('\n'); /* cursor below last one */
4564 else
4565 {
4566 gotocmdline(TRUE); /* cursor at status line */
4567 MSG_PUTS_TITLE(_("--- Included files "));
4568 if (action != ACTION_SHOW_ALL)
4569 MSG_PUTS_TITLE(_("not found "));
4570 MSG_PUTS_TITLE(_("in path ---\n"));
4571 }
4572 did_show = TRUE;
4573 while (depth_displayed < depth && !got_int)
4574 {
4575 ++depth_displayed;
4576 for (i = 0; i < depth_displayed; i++)
4577 MSG_PUTS(" ");
4578 msg_home_replace(files[depth_displayed].name);
4579 MSG_PUTS(" -->\n");
4580 }
4581 if (!got_int) /* don't display if 'q' typed
4582 for "--more--" message */
4583 {
4584 for (i = 0; i <= depth_displayed; i++)
4585 MSG_PUTS(" ");
4586 if (new_fname != NULL)
4587 {
4588 /* using "new_fname" is more reliable, e.g., when
4589 * 'includeexpr' is set. */
4590 msg_outtrans_attr(new_fname, hl_attr(HLF_D));
4591 }
4592 else
4593 {
4594 /*
4595 * Isolate the file name.
4596 * Include the surrounding "" or <> if present.
4597 */
4598 for (p = incl_regmatch.endp[0]; !vim_isfilec(*p); p++)
4599 ;
4600 for (i = 0; vim_isfilec(p[i]); i++)
4601 ;
4602 if (i == 0)
4603 {
4604 /* Nothing found, use the rest of the line. */
4605 p = incl_regmatch.endp[0];
4606 i = STRLEN(p);
4607 }
4608 else
4609 {
4610 if (p[-1] == '"' || p[-1] == '<')
4611 {
4612 --p;
4613 ++i;
4614 }
4615 if (p[i] == '"' || p[i] == '>')
4616 ++i;
4617 }
4618 save_char = p[i];
4619 p[i] = NUL;
4620 msg_outtrans_attr(p, hl_attr(HLF_D));
4621 p[i] = save_char;
4622 }
4623
4624 if (new_fname == NULL && action == ACTION_SHOW_ALL)
4625 {
4626 if (already_searched)
4627 MSG_PUTS(_(" (Already listed)"));
4628 else
4629 MSG_PUTS(_(" NOT FOUND"));
4630 }
4631 }
4632 out_flush(); /* output each line directly */
4633 }
4634
4635 if (new_fname != NULL)
4636 {
4637 /* Push the new file onto the file stack */
4638 if (depth + 1 == old_files)
4639 {
4640 bigger = (SearchedFile *)lalloc((long_u)(
4641 max_path_depth * 2 * sizeof(SearchedFile)), TRUE);
4642 if (bigger != NULL)
4643 {
4644 for (i = 0; i <= depth; i++)
4645 bigger[i] = files[i];
4646 for (i = depth + 1; i < old_files + max_path_depth; i++)
4647 {
4648 bigger[i].fp = NULL;
4649 bigger[i].name = NULL;
4650 bigger[i].lnum = 0;
4651 bigger[i].matched = FALSE;
4652 }
4653 for (i = old_files; i < max_path_depth; i++)
4654 bigger[i + max_path_depth] = files[i];
4655 old_files += max_path_depth;
4656 max_path_depth *= 2;
4657 vim_free(files);
4658 files = bigger;
4659 }
4660 }
4661 if ((files[depth + 1].fp = mch_fopen((char *)new_fname, "r"))
4662 == NULL)
4663 vim_free(new_fname);
4664 else
4665 {
4666 if (++depth == old_files)
4667 {
4668 /*
4669 * lalloc() for 'bigger' must have failed above. We
4670 * will forget one of our already visited files now.
4671 */
4672 vim_free(files[old_files].name);
4673 ++old_files;
4674 }
4675 files[depth].name = curr_fname = new_fname;
4676 files[depth].lnum = 0;
4677 files[depth].matched = FALSE;
4678#ifdef FEAT_INS_EXPAND
4679 if (action == ACTION_EXPAND)
4680 {
Bram Moolenaar555b2802005-05-19 21:08:39 +00004681 vim_snprintf((char*)IObuff, IOSIZE,
4682 _("Scanning included file: %s"),
4683 (char *)new_fname);
Bram Moolenaar071d4272004-06-13 20:20:40 +00004684 msg_trunc_attr(IObuff, TRUE, hl_attr(HLF_R));
4685 }
4686#endif
4687 }
4688 }
4689 }
4690 else
4691 {
4692 /*
4693 * Check if the line is a define (type == FIND_DEFINE)
4694 */
4695 p = line;
4696search_line:
4697 define_matched = FALSE;
4698 if (def_regmatch.regprog != NULL
4699 && vim_regexec(&def_regmatch, line, (colnr_T)0))
4700 {
4701 /*
4702 * Pattern must be first identifier after 'define', so skip
4703 * to that position before checking for match of pattern. Also
4704 * don't let it match beyond the end of this identifier.
4705 */
4706 p = def_regmatch.endp[0];
4707 while (*p && !vim_iswordc(*p))
4708 p++;
4709 define_matched = TRUE;
4710 }
4711
4712 /*
4713 * Look for a match. Don't do this if we are looking for a
4714 * define and this line didn't match define_prog above.
4715 */
4716 if (def_regmatch.regprog == NULL || define_matched)
4717 {
4718 if (define_matched
4719#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004720 || (compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004721#endif
4722 )
4723 {
4724 /* compare the first "len" chars from "ptr" */
4725 startp = skipwhite(p);
4726 if (p_ic)
4727 matched = !MB_STRNICMP(startp, ptr, len);
4728 else
4729 matched = !STRNCMP(startp, ptr, len);
4730 if (matched && define_matched && whole
4731 && vim_iswordc(startp[len]))
4732 matched = FALSE;
4733 }
4734 else if (regmatch.regprog != NULL
4735 && vim_regexec(&regmatch, line, (colnr_T)(p - line)))
4736 {
4737 matched = TRUE;
4738 startp = regmatch.startp[0];
4739 /*
4740 * Check if the line is not a comment line (unless we are
4741 * looking for a define). A line starting with "# define"
4742 * is not considered to be a comment line.
4743 */
4744 if (!define_matched && skip_comments)
4745 {
4746#ifdef FEAT_COMMENTS
4747 if ((*line != '#' ||
4748 STRNCMP(skipwhite(line + 1), "define", 6) != 0)
4749 && get_leader_len(line, NULL, FALSE))
4750 matched = FALSE;
4751
4752 /*
4753 * Also check for a "/ *" or "/ /" before the match.
4754 * Skips lines like "int backwards; / * normal index
4755 * * /" when looking for "normal".
4756 * Note: Doesn't skip "/ *" in comments.
4757 */
4758 p = skipwhite(line);
4759 if (matched
4760 || (p[0] == '/' && p[1] == '*') || p[0] == '*')
4761#endif
4762 for (p = line; *p && p < startp; ++p)
4763 {
4764 if (matched
4765 && p[0] == '/'
4766 && (p[1] == '*' || p[1] == '/'))
4767 {
4768 matched = FALSE;
4769 /* After "//" all text is comment */
4770 if (p[1] == '/')
4771 break;
4772 ++p;
4773 }
4774 else if (!matched && p[0] == '*' && p[1] == '/')
4775 {
4776 /* Can find match after "* /". */
4777 matched = TRUE;
4778 ++p;
4779 }
4780 }
4781 }
4782 }
4783 }
4784 }
4785 if (matched)
4786 {
4787#ifdef FEAT_INS_EXPAND
4788 if (action == ACTION_EXPAND)
4789 {
4790 int reuse = 0;
4791 int add_r;
4792 char_u *aux;
4793
4794 if (depth == -1 && lnum == curwin->w_cursor.lnum)
4795 break;
4796 found = TRUE;
4797 aux = p = startp;
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004798 if (compl_cont_status & CONT_ADDING)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004799 {
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004800 p += compl_length;
Bram Moolenaar071d4272004-06-13 20:20:40 +00004801 if (vim_iswordp(p))
4802 goto exit_matched;
4803 p = find_word_start(p);
4804 }
4805 p = find_word_end(p);
4806 i = (int)(p - aux);
4807
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004808 if ((compl_cont_status & CONT_ADDING) && i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004809 {
4810 /* get the next line */
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004811 /* IOSIZE > compl_length, so the STRNCPY works */
Bram Moolenaar071d4272004-06-13 20:20:40 +00004812 STRNCPY(IObuff, aux, i);
4813 if (!( depth < 0
4814 && lnum < end_lnum
4815 && (line = ml_get(++lnum)) != NULL)
4816 && !( depth >= 0
4817 && !vim_fgets(line = file_line,
4818 LSIZE, files[depth].fp)))
4819 goto exit_matched;
4820
4821 /* we read a line, set "already" to check this "line" later
4822 * if depth >= 0 we'll increase files[depth].lnum far
4823 * bellow -- Acevedo */
4824 already = aux = p = skipwhite(line);
4825 p = find_word_start(p);
4826 p = find_word_end(p);
4827 if (p > aux)
4828 {
4829 if (*aux != ')' && IObuff[i-1] != TAB)
4830 {
4831 if (IObuff[i-1] != ' ')
4832 IObuff[i++] = ' ';
4833 /* IObuf =~ "\(\k\|\i\).* ", thus i >= 2*/
4834 if (p_js
4835 && (IObuff[i-2] == '.'
4836 || (vim_strchr(p_cpo, CPO_JOINSP) == NULL
4837 && (IObuff[i-2] == '?'
4838 || IObuff[i-2] == '!'))))
4839 IObuff[i++] = ' ';
4840 }
4841 /* copy as much as posible of the new word */
4842 if (p - aux >= IOSIZE - i)
4843 p = aux + IOSIZE - i - 1;
4844 STRNCPY(IObuff + i, aux, p - aux);
4845 i += (int)(p - aux);
4846 reuse |= CONT_S_IPOS;
4847 }
4848 IObuff[i] = NUL;
4849 aux = IObuff;
4850
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004851 if (i == compl_length)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004852 goto exit_matched;
4853 }
4854
Bram Moolenaard1f56e62006-02-22 21:25:37 +00004855 add_r = ins_compl_add_infercase(aux, i, p_ic,
Bram Moolenaar071d4272004-06-13 20:20:40 +00004856 curr_fname == curbuf->b_fname ? NULL : curr_fname,
4857 dir, reuse);
4858 if (add_r == OK)
4859 /* if dir was BACKWARD then honor it just once */
4860 dir = FORWARD;
Bram Moolenaar572cb562005-08-05 21:35:02 +00004861 else if (add_r == FAIL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004862 break;
4863 }
4864 else
4865#endif
4866 if (action == ACTION_SHOW_ALL)
4867 {
4868 found = TRUE;
4869 if (!did_show)
4870 gotocmdline(TRUE); /* cursor at status line */
4871 if (curr_fname != prev_fname)
4872 {
4873 if (did_show)
4874 msg_putchar('\n'); /* cursor below last one */
4875 if (!got_int) /* don't display if 'q' typed
4876 at "--more--" mesage */
4877 msg_home_replace_hl(curr_fname);
4878 prev_fname = curr_fname;
4879 }
4880 did_show = TRUE;
4881 if (!got_int)
4882 show_pat_in_path(line, type, TRUE, action,
4883 (depth == -1) ? NULL : files[depth].fp,
4884 (depth == -1) ? &lnum : &files[depth].lnum,
4885 match_count++);
4886
4887 /* Set matched flag for this file and all the ones that
4888 * include it */
4889 for (i = 0; i <= depth; ++i)
4890 files[i].matched = TRUE;
4891 }
4892 else if (--count <= 0)
4893 {
4894 found = TRUE;
4895 if (depth == -1 && lnum == curwin->w_cursor.lnum
4896#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4897 && g_do_tagpreview == 0
4898#endif
4899 )
4900 EMSG(_("E387: Match is on current line"));
4901 else if (action == ACTION_SHOW)
4902 {
4903 show_pat_in_path(line, type, did_show, action,
4904 (depth == -1) ? NULL : files[depth].fp,
4905 (depth == -1) ? &lnum : &files[depth].lnum, 1L);
4906 did_show = TRUE;
4907 }
4908 else
4909 {
4910#ifdef FEAT_GUI
4911 need_mouse_correct = TRUE;
4912#endif
4913#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4914 /* ":psearch" uses the preview window */
4915 if (g_do_tagpreview != 0)
4916 {
4917 curwin_save = curwin;
4918 prepare_tagpreview();
4919 }
4920#endif
4921 if (action == ACTION_SPLIT)
4922 {
4923#ifdef FEAT_WINDOWS
4924 if (win_split(0, 0) == FAIL)
4925#endif
4926 break;
4927#ifdef FEAT_SCROLLBIND
4928 curwin->w_p_scb = FALSE;
4929#endif
4930 }
4931 if (depth == -1)
4932 {
4933 /* match in current file */
4934#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4935 if (g_do_tagpreview != 0)
4936 {
4937 if (getfile(0, curwin_save->w_buffer->b_fname,
4938 NULL, TRUE, lnum, FALSE) > 0)
4939 break; /* failed to jump to file */
4940 }
4941 else
4942#endif
4943 setpcmark();
4944 curwin->w_cursor.lnum = lnum;
4945 }
4946 else
4947 {
4948 if (getfile(0, files[depth].name, NULL, TRUE,
4949 files[depth].lnum, FALSE) > 0)
4950 break; /* failed to jump to file */
4951 /* autocommands may have changed the lnum, we don't
4952 * want that here */
4953 curwin->w_cursor.lnum = files[depth].lnum;
4954 }
4955 }
4956 if (action != ACTION_SHOW)
4957 {
4958 curwin->w_cursor.col = (colnr_T) (startp - line);
4959 curwin->w_set_curswant = TRUE;
4960 }
4961
4962#if defined(FEAT_WINDOWS) && defined(FEAT_QUICKFIX)
4963 if (g_do_tagpreview != 0
Bram Moolenaar997fb4b2006-02-17 21:53:23 +00004964 && curwin != curwin_save && win_valid(curwin_save))
Bram Moolenaar071d4272004-06-13 20:20:40 +00004965 {
4966 /* Return cursor to where we were */
4967 validate_cursor();
4968 redraw_later(VALID);
4969 win_enter(curwin_save, TRUE);
4970 }
4971#endif
4972 break;
4973 }
4974#ifdef FEAT_INS_EXPAND
4975exit_matched:
4976#endif
4977 matched = FALSE;
4978 /* look for other matches in the rest of the line if we
4979 * are not at the end of it already */
4980 if (def_regmatch.regprog == NULL
4981#ifdef FEAT_INS_EXPAND
4982 && action == ACTION_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004983 && !(compl_cont_status & CONT_SOL)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004984#endif
4985 && *(p = startp + 1))
4986 goto search_line;
4987 }
4988 line_breakcheck();
4989#ifdef FEAT_INS_EXPAND
4990 if (action == ACTION_EXPAND)
Bram Moolenaar572cb562005-08-05 21:35:02 +00004991 ins_compl_check_keys(30);
Bram Moolenaar4be06f92005-07-29 22:36:03 +00004992 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00004993#else
4994 if (got_int)
4995#endif
4996 break;
4997
4998 /*
4999 * Read the next line. When reading an included file and encountering
5000 * end-of-file, close the file and continue in the file that included
5001 * it.
5002 */
5003 while (depth >= 0 && !already
5004 && vim_fgets(line = file_line, LSIZE, files[depth].fp))
5005 {
5006 fclose(files[depth].fp);
5007 --old_files;
5008 files[old_files].name = files[depth].name;
5009 files[old_files].matched = files[depth].matched;
5010 --depth;
5011 curr_fname = (depth == -1) ? curbuf->b_fname
5012 : files[depth].name;
5013 if (depth < depth_displayed)
5014 depth_displayed = depth;
5015 }
5016 if (depth >= 0) /* we could read the line */
5017 files[depth].lnum++;
5018 else if (!already)
5019 {
5020 if (++lnum > end_lnum)
5021 break;
5022 line = ml_get(lnum);
5023 }
5024 already = NULL;
5025 }
5026 /* End of big for (;;) loop. */
5027
5028 /* Close any files that are still open. */
5029 for (i = 0; i <= depth; i++)
5030 {
5031 fclose(files[i].fp);
5032 vim_free(files[i].name);
5033 }
5034 for (i = old_files; i < max_path_depth; i++)
5035 vim_free(files[i].name);
5036 vim_free(files);
5037
5038 if (type == CHECK_PATH)
5039 {
5040 if (!did_show)
5041 {
5042 if (action != ACTION_SHOW_ALL)
5043 MSG(_("All included files were found"));
5044 else
5045 MSG(_("No included files"));
5046 }
5047 }
5048 else if (!found
5049#ifdef FEAT_INS_EXPAND
5050 && action != ACTION_EXPAND
5051#endif
5052 )
5053 {
5054#ifdef FEAT_INS_EXPAND
Bram Moolenaar4be06f92005-07-29 22:36:03 +00005055 if (got_int || compl_interrupted)
Bram Moolenaar071d4272004-06-13 20:20:40 +00005056#else
5057 if (got_int)
5058#endif
5059 EMSG(_(e_interr));
5060 else if (type == FIND_DEFINE)
5061 EMSG(_("E388: Couldn't find definition"));
5062 else
5063 EMSG(_("E389: Couldn't find pattern"));
5064 }
5065 if (action == ACTION_SHOW || action == ACTION_SHOW_ALL)
5066 msg_end();
5067
5068fpip_end:
5069 vim_free(file_line);
5070 vim_free(regmatch.regprog);
5071 vim_free(incl_regmatch.regprog);
5072 vim_free(def_regmatch.regprog);
5073
5074#ifdef RISCOS
5075 /* Restore previous file munging state. */
5076 __riscosify_control = previous_munging;
5077#endif
5078}
5079
5080 static void
5081show_pat_in_path(line, type, did_show, action, fp, lnum, count)
5082 char_u *line;
5083 int type;
5084 int did_show;
5085 int action;
5086 FILE *fp;
5087 linenr_T *lnum;
5088 long count;
5089{
5090 char_u *p;
5091
5092 if (did_show)
5093 msg_putchar('\n'); /* cursor below last one */
5094 else
5095 gotocmdline(TRUE); /* cursor at status line */
5096 if (got_int) /* 'q' typed at "--more--" message */
5097 return;
5098 for (;;)
5099 {
5100 p = line + STRLEN(line) - 1;
5101 if (fp != NULL)
5102 {
5103 /* We used fgets(), so get rid of newline at end */
5104 if (p >= line && *p == '\n')
5105 --p;
5106 if (p >= line && *p == '\r')
5107 --p;
5108 *(p + 1) = NUL;
5109 }
5110 if (action == ACTION_SHOW_ALL)
5111 {
5112 sprintf((char *)IObuff, "%3ld: ", count); /* show match nr */
5113 msg_puts(IObuff);
5114 sprintf((char *)IObuff, "%4ld", *lnum); /* show line nr */
5115 /* Highlight line numbers */
5116 msg_puts_attr(IObuff, hl_attr(HLF_N));
5117 MSG_PUTS(" ");
5118 }
Bram Moolenaar26a60b42005-02-22 08:49:11 +00005119 msg_prt_line(line, FALSE);
Bram Moolenaar071d4272004-06-13 20:20:40 +00005120 out_flush(); /* show one line at a time */
5121
5122 /* Definition continues until line that doesn't end with '\' */
5123 if (got_int || type != FIND_DEFINE || p < line || *p != '\\')
5124 break;
5125
5126 if (fp != NULL)
5127 {
5128 if (vim_fgets(line, LSIZE, fp)) /* end of file */
5129 break;
5130 ++*lnum;
5131 }
5132 else
5133 {
5134 if (++*lnum > curbuf->b_ml.ml_line_count)
5135 break;
5136 line = ml_get(*lnum);
5137 }
5138 msg_putchar('\n');
5139 }
5140}
5141#endif
5142
5143#ifdef FEAT_VIMINFO
5144 int
5145read_viminfo_search_pattern(virp, force)
5146 vir_T *virp;
5147 int force;
5148{
5149 char_u *lp;
5150 int idx = -1;
5151 int magic = FALSE;
5152 int no_scs = FALSE;
5153 int off_line = FALSE;
Bram Moolenaar943d2b52005-12-02 00:50:49 +00005154 int off_end = 0;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005155 long off = 0;
5156 int setlast = FALSE;
5157#ifdef FEAT_SEARCH_EXTRA
5158 static int hlsearch_on = FALSE;
5159#endif
5160 char_u *val;
5161
5162 /*
5163 * Old line types:
5164 * "/pat", "&pat": search/subst. pat
5165 * "~/pat", "~&pat": last used search/subst. pat
5166 * New line types:
5167 * "~h", "~H": hlsearch highlighting off/on
5168 * "~<magic><smartcase><line><end><off><last><which>pat"
5169 * <magic>: 'm' off, 'M' on
5170 * <smartcase>: 's' off, 'S' on
5171 * <line>: 'L' line offset, 'l' char offset
5172 * <end>: 'E' from end, 'e' from start
5173 * <off>: decimal, offset
5174 * <last>: '~' last used pattern
5175 * <which>: '/' search pat, '&' subst. pat
5176 */
5177 lp = virp->vir_line;
5178 if (lp[0] == '~' && (lp[1] == 'm' || lp[1] == 'M')) /* new line type */
5179 {
5180 if (lp[1] == 'M') /* magic on */
5181 magic = TRUE;
5182 if (lp[2] == 's')
5183 no_scs = TRUE;
5184 if (lp[3] == 'L')
5185 off_line = TRUE;
5186 if (lp[4] == 'E')
Bram Moolenaared203462004-06-16 11:19:22 +00005187 off_end = SEARCH_END;
Bram Moolenaar071d4272004-06-13 20:20:40 +00005188 lp += 5;
5189 off = getdigits(&lp);
5190 }
5191 if (lp[0] == '~') /* use this pattern for last-used pattern */
5192 {
5193 setlast = TRUE;
5194 lp++;
5195 }
5196 if (lp[0] == '/')
5197 idx = RE_SEARCH;
5198 else if (lp[0] == '&')
5199 idx = RE_SUBST;
5200#ifdef FEAT_SEARCH_EXTRA
5201 else if (lp[0] == 'h') /* ~h: 'hlsearch' highlighting off */
5202 hlsearch_on = FALSE;
5203 else if (lp[0] == 'H') /* ~H: 'hlsearch' highlighting on */
5204 hlsearch_on = TRUE;
5205#endif
5206 if (idx >= 0)
5207 {
5208 if (force || spats[idx].pat == NULL)
5209 {
5210 val = viminfo_readstring(virp, (int)(lp - virp->vir_line + 1),
5211 TRUE);
5212 if (val != NULL)
5213 {
5214 set_last_search_pat(val, idx, magic, setlast);
5215 vim_free(val);
5216 spats[idx].no_scs = no_scs;
5217 spats[idx].off.line = off_line;
5218 spats[idx].off.end = off_end;
5219 spats[idx].off.off = off;
5220#ifdef FEAT_SEARCH_EXTRA
5221 if (setlast)
5222 no_hlsearch = !hlsearch_on;
5223#endif
5224 }
5225 }
5226 }
5227 return viminfo_readline(virp);
5228}
5229
5230 void
5231write_viminfo_search_pattern(fp)
5232 FILE *fp;
5233{
5234 if (get_viminfo_parameter('/') != 0)
5235 {
5236#ifdef FEAT_SEARCH_EXTRA
5237 fprintf(fp, "\n# hlsearch on (H) or off (h):\n~%c",
5238 (no_hlsearch || find_viminfo_parameter('h') != NULL) ? 'h' : 'H');
5239#endif
5240 wvsp_one(fp, RE_SEARCH, "", '/');
5241 wvsp_one(fp, RE_SUBST, "Substitute ", '&');
5242 }
5243}
5244
5245 static void
5246wvsp_one(fp, idx, s, sc)
5247 FILE *fp; /* file to write to */
5248 int idx; /* spats[] index */
5249 char *s; /* search pat */
5250 int sc; /* dir char */
5251{
5252 if (spats[idx].pat != NULL)
5253 {
5254 fprintf(fp, "\n# Last %sSearch Pattern:\n~", s);
5255 /* off.dir is not stored, it's reset to forward */
5256 fprintf(fp, "%c%c%c%c%ld%s%c",
5257 spats[idx].magic ? 'M' : 'm', /* magic */
5258 spats[idx].no_scs ? 's' : 'S', /* smartcase */
5259 spats[idx].off.line ? 'L' : 'l', /* line offset */
5260 spats[idx].off.end ? 'E' : 'e', /* offset from end */
5261 spats[idx].off.off, /* offset */
5262 last_idx == idx ? "~" : "", /* last used pat */
5263 sc);
5264 viminfo_writestring(fp, spats[idx].pat);
5265 }
5266}
5267#endif /* FEAT_VIMINFO */