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