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