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