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