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