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