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