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