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