Commit | Line | Data |
---|---|---|
ca1d1d23 | 1 | /* String search routines for GNU Emacs. |
73b0cd50 | 2 | Copyright (C) 1985-1987, 1993-1994, 1997-1999, 2001-2011 |
8cabe764 | 3 | Free Software Foundation, Inc. |
ca1d1d23 JB |
4 | |
5 | This file is part of GNU Emacs. | |
6 | ||
9ec0b715 | 7 | GNU Emacs is free software: you can redistribute it and/or modify |
ca1d1d23 | 8 | it under the terms of the GNU General Public License as published by |
9ec0b715 GM |
9 | the Free Software Foundation, either version 3 of the License, or |
10 | (at your option) any later version. | |
ca1d1d23 JB |
11 | |
12 | GNU Emacs is distributed in the hope that it will be useful, | |
13 | but WITHOUT ANY WARRANTY; without even the implied warranty of | |
14 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
15 | GNU General Public License for more details. | |
16 | ||
17 | You should have received a copy of the GNU General Public License | |
9ec0b715 | 18 | along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>. */ |
ca1d1d23 JB |
19 | |
20 | ||
18160b98 | 21 | #include <config.h> |
d7306fe6 | 22 | #include <setjmp.h> |
ca1d1d23 JB |
23 | #include "lisp.h" |
24 | #include "syntax.h" | |
5679531d | 25 | #include "category.h" |
ca1d1d23 | 26 | #include "buffer.h" |
76eb0881 | 27 | #include "character.h" |
89ad6492 | 28 | #include "charset.h" |
9169c321 | 29 | #include "region-cache.h" |
ca1d1d23 | 30 | #include "commands.h" |
9ac0d9e0 | 31 | #include "blockinput.h" |
bf1760bb | 32 | #include "intervals.h" |
4746118a | 33 | |
ca1d1d23 JB |
34 | #include <sys/types.h> |
35 | #include "regex.h" | |
36 | ||
1d288aef | 37 | #define REGEXP_CACHE_SIZE 20 |
ca1d1d23 | 38 | |
487282dc KH |
39 | /* If the regexp is non-nil, then the buffer contains the compiled form |
40 | of that regexp, suitable for searching. */ | |
1d288aef RS |
41 | struct regexp_cache |
42 | { | |
487282dc | 43 | struct regexp_cache *next; |
ecdb561e | 44 | Lisp_Object regexp, whitespace_regexp; |
b69e3c18 | 45 | /* Syntax table for which the regexp applies. We need this because |
58e95211 SM |
46 | of character classes. If this is t, then the compiled pattern is valid |
47 | for any syntax-table. */ | |
b69e3c18 | 48 | Lisp_Object syntax_table; |
487282dc KH |
49 | struct re_pattern_buffer buf; |
50 | char fastmap[0400]; | |
b819a390 RS |
51 | /* Nonzero means regexp was compiled to do full POSIX backtracking. */ |
52 | char posix; | |
487282dc | 53 | }; |
ca1d1d23 | 54 | |
487282dc KH |
55 | /* The instances of that struct. */ |
56 | struct regexp_cache searchbufs[REGEXP_CACHE_SIZE]; | |
ca1d1d23 | 57 | |
487282dc KH |
58 | /* The head of the linked list; points to the most recently used buffer. */ |
59 | struct regexp_cache *searchbuf_head; | |
ca1d1d23 | 60 | |
ca1d1d23 | 61 | |
4746118a JB |
62 | /* Every call to re_match, etc., must pass &search_regs as the regs |
63 | argument unless you can show it is unnecessary (i.e., if re_match | |
64 | is certainly going to be called again before region-around-match | |
65 | can be called). | |
66 | ||
67 | Since the registers are now dynamically allocated, we need to make | |
68 | sure not to refer to the Nth register before checking that it has | |
1113d9db JB |
69 | been allocated by checking search_regs.num_regs. |
70 | ||
71 | The regex code keeps track of whether it has allocated the search | |
487282dc KH |
72 | buffer using bits in the re_pattern_buffer. This means that whenever |
73 | you compile a new pattern, it completely forgets whether it has | |
1113d9db JB |
74 | allocated any registers, and will allocate new registers the next |
75 | time you call a searching or matching function. Therefore, we need | |
76 | to call re_set_registers after compiling a new pattern or after | |
77 | setting the match registers, so that the regex functions will be | |
78 | able to free or re-allocate it properly. */ | |
ca1d1d23 JB |
79 | static struct re_registers search_regs; |
80 | ||
daa37602 JB |
81 | /* The buffer in which the last search was performed, or |
82 | Qt if the last search was done in a string; | |
83 | Qnil if no searching has been done yet. */ | |
84 | static Lisp_Object last_thing_searched; | |
ca1d1d23 | 85 | |
8e6208c5 | 86 | /* error condition signaled when regexp compile_pattern fails */ |
ca1d1d23 JB |
87 | |
88 | Lisp_Object Qinvalid_regexp; | |
89 | ||
06f77a8a KS |
90 | /* Error condition used for failing searches */ |
91 | Lisp_Object Qsearch_failed; | |
92 | ||
f57e2426 J |
93 | static void set_search_regs (EMACS_INT, EMACS_INT); |
94 | static void save_search_regs (void); | |
c098fdb8 EZ |
95 | static EMACS_INT simple_search (EMACS_INT, unsigned char *, EMACS_INT, |
96 | EMACS_INT, Lisp_Object, EMACS_INT, EMACS_INT, | |
f57e2426 | 97 | EMACS_INT, EMACS_INT); |
c098fdb8 | 98 | static EMACS_INT boyer_moore (EMACS_INT, unsigned char *, EMACS_INT, EMACS_INT, |
f57e2426 J |
99 | Lisp_Object, Lisp_Object, |
100 | EMACS_INT, EMACS_INT, | |
101 | EMACS_INT, EMACS_INT, int); | |
102 | static EMACS_INT search_buffer (Lisp_Object, EMACS_INT, EMACS_INT, | |
c098fdb8 | 103 | EMACS_INT, EMACS_INT, EMACS_INT, int, |
f57e2426 | 104 | Lisp_Object, Lisp_Object, int); |
971de7fb | 105 | static void matcher_overflow (void) NO_RETURN; |
b819a390 | 106 | |
ca1d1d23 | 107 | static void |
971de7fb | 108 | matcher_overflow (void) |
ca1d1d23 JB |
109 | { |
110 | error ("Stack overflow in regexp matcher"); | |
111 | } | |
112 | ||
b819a390 RS |
113 | /* Compile a regexp and signal a Lisp error if anything goes wrong. |
114 | PATTERN is the pattern to compile. | |
115 | CP is the place to put the result. | |
facdc750 | 116 | TRANSLATE is a translation table for ignoring case, or nil for none. |
b819a390 RS |
117 | REGP is the structure that says where to store the "register" |
118 | values that will result from matching this pattern. | |
119 | If it is 0, we should compile the pattern not to record any | |
120 | subexpression bounds. | |
121 | POSIX is nonzero if we want full backtracking (POSIX style) | |
5679531d | 122 | for this pattern. 0 means backtrack only enough to get a valid match. |
bbc73b48 KH |
123 | |
124 | The behavior also depends on Vsearch_spaces_regexp. */ | |
ca1d1d23 | 125 | |
487282dc | 126 | static void |
971de7fb | 127 | compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern, Lisp_Object translate, struct re_registers *regp, int posix) |
ca1d1d23 | 128 | { |
d451e4db | 129 | char *val; |
b819a390 | 130 | reg_syntax_t old; |
ca1d1d23 | 131 | |
487282dc | 132 | cp->regexp = Qnil; |
59fab369 | 133 | cp->buf.translate = (! NILP (translate) ? translate : make_number (0)); |
b819a390 | 134 | cp->posix = posix; |
93daa011 | 135 | cp->buf.multibyte = STRING_MULTIBYTE (pattern); |
89ad6492 | 136 | cp->buf.charset_unibyte = charset_unibyte; |
d8c85250 CY |
137 | if (STRINGP (Vsearch_spaces_regexp)) |
138 | cp->whitespace_regexp = Vsearch_spaces_regexp; | |
139 | else | |
140 | cp->whitespace_regexp = Qnil; | |
141 | ||
e7b2dd2e RS |
142 | /* rms: I think BLOCK_INPUT is not needed here any more, |
143 | because regex.c defines malloc to call xmalloc. | |
144 | Using BLOCK_INPUT here means the debugger won't run if an error occurs. | |
145 | So let's turn it off. */ | |
146 | /* BLOCK_INPUT; */ | |
fb4a568d | 147 | old = re_set_syntax (RE_SYNTAX_EMACS |
b819a390 | 148 | | (posix ? 0 : RE_NO_POSIX_BACKTRACKING)); |
d8c85250 CY |
149 | |
150 | if (STRINGP (Vsearch_spaces_regexp)) | |
42a5b22f | 151 | re_set_whitespace_regexp (SSDATA (Vsearch_spaces_regexp)); |
d8c85250 CY |
152 | else |
153 | re_set_whitespace_regexp (NULL); | |
bbc73b48 | 154 | |
51b59d79 | 155 | val = (char *) re_compile_pattern (SSDATA (pattern), |
8f924df7 | 156 | SBYTES (pattern), &cp->buf); |
bbc73b48 | 157 | |
58e95211 SM |
158 | /* If the compiled pattern hard codes some of the contents of the |
159 | syntax-table, it can only be reused with *this* syntax table. */ | |
4b4deea2 | 160 | cp->syntax_table = cp->buf.used_syntax ? BVAR (current_buffer, syntax_table) : Qt; |
58e95211 | 161 | |
bbc73b48 KH |
162 | re_set_whitespace_regexp (NULL); |
163 | ||
b819a390 | 164 | re_set_syntax (old); |
e7b2dd2e | 165 | /* UNBLOCK_INPUT; */ |
ca1d1d23 | 166 | if (val) |
06f77a8a | 167 | xsignal1 (Qinvalid_regexp, build_string (val)); |
1113d9db | 168 | |
487282dc | 169 | cp->regexp = Fcopy_sequence (pattern); |
487282dc KH |
170 | } |
171 | ||
6efc7887 RS |
172 | /* Shrink each compiled regexp buffer in the cache |
173 | to the size actually used right now. | |
174 | This is called from garbage collection. */ | |
175 | ||
176 | void | |
971de7fb | 177 | shrink_regexp_cache (void) |
6efc7887 | 178 | { |
a968f437 | 179 | struct regexp_cache *cp; |
6efc7887 RS |
180 | |
181 | for (cp = searchbuf_head; cp != 0; cp = cp->next) | |
182 | { | |
183 | cp->buf.allocated = cp->buf.used; | |
184 | cp->buf.buffer | |
b23c0a83 | 185 | = (unsigned char *) xrealloc (cp->buf.buffer, cp->buf.used); |
6efc7887 RS |
186 | } |
187 | } | |
188 | ||
54dd3310 SM |
189 | /* Clear the regexp cache w.r.t. a particular syntax table, |
190 | because it was changed. | |
e5b94d44 CY |
191 | There is no danger of memory leak here because re_compile_pattern |
192 | automagically manages the memory in each re_pattern_buffer struct, | |
193 | based on its `allocated' and `buffer' values. */ | |
194 | void | |
971de7fb | 195 | clear_regexp_cache (void) |
e5b94d44 CY |
196 | { |
197 | int i; | |
198 | ||
199 | for (i = 0; i < REGEXP_CACHE_SIZE; ++i) | |
920fd1fc EZ |
200 | /* It's tempting to compare with the syntax-table we've actually changed, |
201 | but it's not sufficient because char-table inheritance means that | |
54dd3310 SM |
202 | modifying one syntax-table can change others at the same time. */ |
203 | if (!EQ (searchbufs[i].syntax_table, Qt)) | |
204 | searchbufs[i].regexp = Qnil; | |
e5b94d44 CY |
205 | } |
206 | ||
487282dc | 207 | /* Compile a regexp if necessary, but first check to see if there's one in |
b819a390 RS |
208 | the cache. |
209 | PATTERN is the pattern to compile. | |
facdc750 | 210 | TRANSLATE is a translation table for ignoring case, or nil for none. |
b819a390 RS |
211 | REGP is the structure that says where to store the "register" |
212 | values that will result from matching this pattern. | |
213 | If it is 0, we should compile the pattern not to record any | |
214 | subexpression bounds. | |
215 | POSIX is nonzero if we want full backtracking (POSIX style) | |
216 | for this pattern. 0 means backtrack only enough to get a valid match. */ | |
487282dc KH |
217 | |
218 | struct re_pattern_buffer * | |
971de7fb | 219 | compile_pattern (Lisp_Object pattern, struct re_registers *regp, Lisp_Object translate, int posix, int multibyte) |
487282dc KH |
220 | { |
221 | struct regexp_cache *cp, **cpp; | |
222 | ||
223 | for (cpp = &searchbuf_head; ; cpp = &cp->next) | |
224 | { | |
225 | cp = *cpp; | |
f1b9c7c1 KR |
226 | /* Entries are initialized to nil, and may be set to nil by |
227 | compile_pattern_1 if the pattern isn't valid. Don't apply | |
49a5f770 KR |
228 | string accessors in those cases. However, compile_pattern_1 |
229 | is only applied to the cache entry we pick here to reuse. So | |
230 | nil should never appear before a non-nil entry. */ | |
7c752c80 | 231 | if (NILP (cp->regexp)) |
f1b9c7c1 | 232 | goto compile_it; |
d5db4077 | 233 | if (SCHARS (cp->regexp) == SCHARS (pattern) |
cf69b13e | 234 | && STRING_MULTIBYTE (cp->regexp) == STRING_MULTIBYTE (pattern) |
1d288aef | 235 | && !NILP (Fstring_equal (cp->regexp, pattern)) |
59fab369 | 236 | && EQ (cp->buf.translate, (! NILP (translate) ? translate : make_number (0))) |
5679531d | 237 | && cp->posix == posix |
58e95211 | 238 | && (EQ (cp->syntax_table, Qt) |
4b4deea2 | 239 | || EQ (cp->syntax_table, BVAR (current_buffer, syntax_table))) |
89ad6492 KH |
240 | && !NILP (Fequal (cp->whitespace_regexp, Vsearch_spaces_regexp)) |
241 | && cp->buf.charset_unibyte == charset_unibyte) | |
487282dc KH |
242 | break; |
243 | ||
f1b9c7c1 KR |
244 | /* If we're at the end of the cache, compile into the nil cell |
245 | we found, or the last (least recently used) cell with a | |
246 | string value. */ | |
487282dc KH |
247 | if (cp->next == 0) |
248 | { | |
f1b9c7c1 | 249 | compile_it: |
89ad6492 | 250 | compile_pattern_1 (cp, pattern, translate, regp, posix); |
487282dc KH |
251 | break; |
252 | } | |
253 | } | |
254 | ||
255 | /* When we get here, cp (aka *cpp) contains the compiled pattern, | |
256 | either because we found it in the cache or because we just compiled it. | |
257 | Move it to the front of the queue to mark it as most recently used. */ | |
258 | *cpp = cp->next; | |
259 | cp->next = searchbuf_head; | |
260 | searchbuf_head = cp; | |
1113d9db | 261 | |
6639708c RS |
262 | /* Advise the searching functions about the space we have allocated |
263 | for register data. */ | |
264 | if (regp) | |
265 | re_set_registers (&cp->buf, regp, regp->num_regs, regp->start, regp->end); | |
266 | ||
78edd3b7 | 267 | /* The compiled pattern can be used both for multibyte and unibyte |
89ad6492 KH |
268 | target. But, we have to tell which the pattern is used for. */ |
269 | cp->buf.target_multibyte = multibyte; | |
270 | ||
487282dc | 271 | return &cp->buf; |
ca1d1d23 JB |
272 | } |
273 | ||
ca1d1d23 | 274 | \f |
b819a390 | 275 | static Lisp_Object |
971de7fb | 276 | looking_at_1 (Lisp_Object string, int posix) |
ca1d1d23 JB |
277 | { |
278 | Lisp_Object val; | |
279 | unsigned char *p1, *p2; | |
e7deaab0 | 280 | EMACS_INT s1, s2; |
c098fdb8 | 281 | register EMACS_INT i; |
487282dc | 282 | struct re_pattern_buffer *bufp; |
ca1d1d23 | 283 | |
7074fde6 FP |
284 | if (running_asynch_code) |
285 | save_search_regs (); | |
286 | ||
910c747a | 287 | /* This is so set_image_of_range_1 in regex.c can find the EQV table. */ |
4b4deea2 TT |
288 | XCHAR_TABLE (BVAR (current_buffer, case_canon_table))->extras[2] |
289 | = BVAR (current_buffer, case_eqv_table); | |
910c747a | 290 | |
b7826503 | 291 | CHECK_STRING (string); |
ef887810 RS |
292 | bufp = compile_pattern (string, |
293 | (NILP (Vinhibit_changing_match_data) | |
294 | ? &search_regs : NULL), | |
4b4deea2 TT |
295 | (!NILP (BVAR (current_buffer, case_fold_search)) |
296 | ? BVAR (current_buffer, case_canon_table) : Qnil), | |
0c8533c6 | 297 | posix, |
4b4deea2 | 298 | !NILP (BVAR (current_buffer, enable_multibyte_characters))); |
ca1d1d23 JB |
299 | |
300 | immediate_quit = 1; | |
301 | QUIT; /* Do a pending quit right away, to avoid paradoxical behavior */ | |
302 | ||
303 | /* Get pointers and sizes of the two strings | |
304 | that make up the visible portion of the buffer. */ | |
305 | ||
306 | p1 = BEGV_ADDR; | |
fa8ed3e0 | 307 | s1 = GPT_BYTE - BEGV_BYTE; |
ca1d1d23 | 308 | p2 = GAP_END_ADDR; |
fa8ed3e0 | 309 | s2 = ZV_BYTE - GPT_BYTE; |
ca1d1d23 JB |
310 | if (s1 < 0) |
311 | { | |
312 | p2 = p1; | |
fa8ed3e0 | 313 | s2 = ZV_BYTE - BEGV_BYTE; |
ca1d1d23 JB |
314 | s1 = 0; |
315 | } | |
316 | if (s2 < 0) | |
317 | { | |
fa8ed3e0 | 318 | s1 = ZV_BYTE - BEGV_BYTE; |
ca1d1d23 JB |
319 | s2 = 0; |
320 | } | |
8bb43c28 RS |
321 | |
322 | re_match_object = Qnil; | |
177c0ea7 | 323 | |
487282dc | 324 | i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2, |
ef887810 RS |
325 | PT_BYTE - BEGV_BYTE, |
326 | (NILP (Vinhibit_changing_match_data) | |
327 | ? &search_regs : NULL), | |
fa8ed3e0 | 328 | ZV_BYTE - BEGV_BYTE); |
de182d70 | 329 | immediate_quit = 0; |
177c0ea7 | 330 | |
ca1d1d23 JB |
331 | if (i == -2) |
332 | matcher_overflow (); | |
333 | ||
334 | val = (0 <= i ? Qt : Qnil); | |
ef887810 | 335 | if (NILP (Vinhibit_changing_match_data) && i >= 0) |
fa8ed3e0 RS |
336 | for (i = 0; i < search_regs.num_regs; i++) |
337 | if (search_regs.start[i] >= 0) | |
338 | { | |
339 | search_regs.start[i] | |
340 | = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE); | |
341 | search_regs.end[i] | |
342 | = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE); | |
343 | } | |
ef887810 RS |
344 | |
345 | /* Set last_thing_searched only when match data is changed. */ | |
346 | if (NILP (Vinhibit_changing_match_data)) | |
347 | XSETBUFFER (last_thing_searched, current_buffer); | |
348 | ||
ca1d1d23 JB |
349 | return val; |
350 | } | |
351 | ||
b819a390 | 352 | DEFUN ("looking-at", Flooking_at, Slooking_at, 1, 1, 0, |
8c1a1077 PJ |
353 | doc: /* Return t if text after point matches regular expression REGEXP. |
354 | This function modifies the match data that `match-beginning', | |
355 | `match-end' and `match-data' access; save and restore the match | |
356 | data if you want to preserve them. */) | |
5842a27b | 357 | (Lisp_Object regexp) |
b819a390 | 358 | { |
94f94972 | 359 | return looking_at_1 (regexp, 0); |
b819a390 RS |
360 | } |
361 | ||
362 | DEFUN ("posix-looking-at", Fposix_looking_at, Sposix_looking_at, 1, 1, 0, | |
8c1a1077 PJ |
363 | doc: /* Return t if text after point matches regular expression REGEXP. |
364 | Find the longest match, in accord with Posix regular expression rules. | |
365 | This function modifies the match data that `match-beginning', | |
366 | `match-end' and `match-data' access; save and restore the match | |
367 | data if you want to preserve them. */) | |
5842a27b | 368 | (Lisp_Object regexp) |
b819a390 | 369 | { |
94f94972 | 370 | return looking_at_1 (regexp, 1); |
b819a390 RS |
371 | } |
372 | \f | |
373 | static Lisp_Object | |
971de7fb | 374 | string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start, int posix) |
ca1d1d23 JB |
375 | { |
376 | int val; | |
487282dc | 377 | struct re_pattern_buffer *bufp; |
e7deaab0 | 378 | EMACS_INT pos, pos_byte; |
0c8533c6 | 379 | int i; |
ca1d1d23 | 380 | |
7074fde6 FP |
381 | if (running_asynch_code) |
382 | save_search_regs (); | |
383 | ||
b7826503 PJ |
384 | CHECK_STRING (regexp); |
385 | CHECK_STRING (string); | |
ca1d1d23 JB |
386 | |
387 | if (NILP (start)) | |
0c8533c6 | 388 | pos = 0, pos_byte = 0; |
ca1d1d23 JB |
389 | else |
390 | { | |
c098fdb8 | 391 | EMACS_INT len = SCHARS (string); |
ca1d1d23 | 392 | |
b7826503 | 393 | CHECK_NUMBER (start); |
0c8533c6 RS |
394 | pos = XINT (start); |
395 | if (pos < 0 && -pos <= len) | |
396 | pos = len + pos; | |
397 | else if (0 > pos || pos > len) | |
ca1d1d23 | 398 | args_out_of_range (string, start); |
0c8533c6 | 399 | pos_byte = string_char_to_byte (string, pos); |
ca1d1d23 JB |
400 | } |
401 | ||
910c747a | 402 | /* This is so set_image_of_range_1 in regex.c can find the EQV table. */ |
4b4deea2 TT |
403 | XCHAR_TABLE (BVAR (current_buffer, case_canon_table))->extras[2] |
404 | = BVAR (current_buffer, case_eqv_table); | |
910c747a | 405 | |
ef887810 RS |
406 | bufp = compile_pattern (regexp, |
407 | (NILP (Vinhibit_changing_match_data) | |
408 | ? &search_regs : NULL), | |
4b4deea2 TT |
409 | (!NILP (BVAR (current_buffer, case_fold_search)) |
410 | ? BVAR (current_buffer, case_canon_table) : Qnil), | |
0c8533c6 RS |
411 | posix, |
412 | STRING_MULTIBYTE (string)); | |
ca1d1d23 | 413 | immediate_quit = 1; |
8bb43c28 | 414 | re_match_object = string; |
177c0ea7 | 415 | |
51b59d79 | 416 | val = re_search (bufp, SSDATA (string), |
d5db4077 KR |
417 | SBYTES (string), pos_byte, |
418 | SBYTES (string) - pos_byte, | |
ef887810 RS |
419 | (NILP (Vinhibit_changing_match_data) |
420 | ? &search_regs : NULL)); | |
ca1d1d23 | 421 | immediate_quit = 0; |
ef887810 RS |
422 | |
423 | /* Set last_thing_searched only when match data is changed. */ | |
424 | if (NILP (Vinhibit_changing_match_data)) | |
425 | last_thing_searched = Qt; | |
426 | ||
ca1d1d23 JB |
427 | if (val == -2) |
428 | matcher_overflow (); | |
429 | if (val < 0) return Qnil; | |
0c8533c6 | 430 | |
ef887810 RS |
431 | if (NILP (Vinhibit_changing_match_data)) |
432 | for (i = 0; i < search_regs.num_regs; i++) | |
433 | if (search_regs.start[i] >= 0) | |
434 | { | |
435 | search_regs.start[i] | |
436 | = string_byte_to_char (string, search_regs.start[i]); | |
437 | search_regs.end[i] | |
438 | = string_byte_to_char (string, search_regs.end[i]); | |
439 | } | |
0c8533c6 RS |
440 | |
441 | return make_number (string_byte_to_char (string, val)); | |
ca1d1d23 | 442 | } |
e59a8453 | 443 | |
b819a390 | 444 | DEFUN ("string-match", Fstring_match, Sstring_match, 2, 3, 0, |
8c1a1077 | 445 | doc: /* Return index of start of first match for REGEXP in STRING, or nil. |
b85acc4b | 446 | Matching ignores case if `case-fold-search' is non-nil. |
8c1a1077 PJ |
447 | If third arg START is non-nil, start search at that index in STRING. |
448 | For index of first char beyond the match, do (match-end 0). | |
449 | `match-end' and `match-beginning' also give indices of substrings | |
2bd2f32d RS |
450 | matched by parenthesis constructs in the pattern. |
451 | ||
452 | You can use the function `match-string' to extract the substrings | |
453 | matched by the parenthesis constructions in REGEXP. */) | |
5842a27b | 454 | (Lisp_Object regexp, Lisp_Object string, Lisp_Object start) |
b819a390 RS |
455 | { |
456 | return string_match_1 (regexp, string, start, 0); | |
457 | } | |
458 | ||
459 | DEFUN ("posix-string-match", Fposix_string_match, Sposix_string_match, 2, 3, 0, | |
8c1a1077 PJ |
460 | doc: /* Return index of start of first match for REGEXP in STRING, or nil. |
461 | Find the longest match, in accord with Posix regular expression rules. | |
462 | Case is ignored if `case-fold-search' is non-nil in the current buffer. | |
463 | If third arg START is non-nil, start search at that index in STRING. | |
464 | For index of first char beyond the match, do (match-end 0). | |
465 | `match-end' and `match-beginning' also give indices of substrings | |
466 | matched by parenthesis constructs in the pattern. */) | |
5842a27b | 467 | (Lisp_Object regexp, Lisp_Object string, Lisp_Object start) |
b819a390 RS |
468 | { |
469 | return string_match_1 (regexp, string, start, 1); | |
470 | } | |
471 | ||
e59a8453 RS |
472 | /* Match REGEXP against STRING, searching all of STRING, |
473 | and return the index of the match, or negative on failure. | |
474 | This does not clobber the match data. */ | |
475 | ||
476 | int | |
971de7fb | 477 | fast_string_match (Lisp_Object regexp, Lisp_Object string) |
e59a8453 RS |
478 | { |
479 | int val; | |
487282dc | 480 | struct re_pattern_buffer *bufp; |
e59a8453 | 481 | |
facdc750 RS |
482 | bufp = compile_pattern (regexp, 0, Qnil, |
483 | 0, STRING_MULTIBYTE (string)); | |
e59a8453 | 484 | immediate_quit = 1; |
8bb43c28 | 485 | re_match_object = string; |
177c0ea7 | 486 | |
51b59d79 | 487 | val = re_search (bufp, SSDATA (string), |
d5db4077 KR |
488 | SBYTES (string), 0, |
489 | SBYTES (string), 0); | |
e59a8453 RS |
490 | immediate_quit = 0; |
491 | return val; | |
492 | } | |
5679531d KH |
493 | |
494 | /* Match REGEXP against STRING, searching all of STRING ignoring case, | |
495 | and return the index of the match, or negative on failure. | |
0c8533c6 RS |
496 | This does not clobber the match data. |
497 | We assume that STRING contains single-byte characters. */ | |
5679531d | 498 | |
5679531d | 499 | int |
971de7fb | 500 | fast_c_string_match_ignore_case (Lisp_Object regexp, const char *string) |
5679531d KH |
501 | { |
502 | int val; | |
503 | struct re_pattern_buffer *bufp; | |
c098fdb8 | 504 | size_t len = strlen (string); |
5679531d | 505 | |
0c8533c6 | 506 | regexp = string_make_unibyte (regexp); |
b4577c63 | 507 | re_match_object = Qt; |
5679531d | 508 | bufp = compile_pattern (regexp, 0, |
0190922f | 509 | Vascii_canon_table, 0, |
f8bd51c4 | 510 | 0); |
5679531d KH |
511 | immediate_quit = 1; |
512 | val = re_search (bufp, string, len, 0, len, 0); | |
513 | immediate_quit = 0; | |
514 | return val; | |
515 | } | |
be5f4dfb KH |
516 | |
517 | /* Like fast_string_match but ignore case. */ | |
518 | ||
519 | int | |
971de7fb | 520 | fast_string_match_ignore_case (Lisp_Object regexp, Lisp_Object string) |
be5f4dfb KH |
521 | { |
522 | int val; | |
523 | struct re_pattern_buffer *bufp; | |
524 | ||
0190922f | 525 | bufp = compile_pattern (regexp, 0, Vascii_canon_table, |
be5f4dfb KH |
526 | 0, STRING_MULTIBYTE (string)); |
527 | immediate_quit = 1; | |
528 | re_match_object = string; | |
529 | ||
51b59d79 | 530 | val = re_search (bufp, SSDATA (string), |
be5f4dfb KH |
531 | SBYTES (string), 0, |
532 | SBYTES (string), 0); | |
533 | immediate_quit = 0; | |
534 | return val; | |
535 | } | |
ca1d1d23 | 536 | \f |
a80ce213 | 537 | /* Match REGEXP against the characters after POS to LIMIT, and return |
8b13507a KH |
538 | the number of matched characters. If STRING is non-nil, match |
539 | against the characters in it. In that case, POS and LIMIT are | |
540 | indices into the string. This function doesn't modify the match | |
541 | data. */ | |
542 | ||
543 | EMACS_INT | |
971de7fb | 544 | fast_looking_at (Lisp_Object regexp, EMACS_INT pos, EMACS_INT pos_byte, EMACS_INT limit, EMACS_INT limit_byte, Lisp_Object string) |
8b13507a KH |
545 | { |
546 | int multibyte; | |
547 | struct re_pattern_buffer *buf; | |
548 | unsigned char *p1, *p2; | |
e7deaab0 | 549 | EMACS_INT s1, s2; |
8b13507a | 550 | EMACS_INT len; |
78edd3b7 | 551 | |
8b13507a KH |
552 | if (STRINGP (string)) |
553 | { | |
554 | if (pos_byte < 0) | |
555 | pos_byte = string_char_to_byte (string, pos); | |
556 | if (limit_byte < 0) | |
557 | limit_byte = string_char_to_byte (string, limit); | |
558 | p1 = NULL; | |
559 | s1 = 0; | |
560 | p2 = SDATA (string); | |
561 | s2 = SBYTES (string); | |
562 | re_match_object = string; | |
563 | multibyte = STRING_MULTIBYTE (string); | |
564 | } | |
565 | else | |
566 | { | |
567 | if (pos_byte < 0) | |
568 | pos_byte = CHAR_TO_BYTE (pos); | |
569 | if (limit_byte < 0) | |
570 | limit_byte = CHAR_TO_BYTE (limit); | |
571 | pos_byte -= BEGV_BYTE; | |
572 | limit_byte -= BEGV_BYTE; | |
573 | p1 = BEGV_ADDR; | |
574 | s1 = GPT_BYTE - BEGV_BYTE; | |
575 | p2 = GAP_END_ADDR; | |
576 | s2 = ZV_BYTE - GPT_BYTE; | |
577 | if (s1 < 0) | |
578 | { | |
579 | p2 = p1; | |
580 | s2 = ZV_BYTE - BEGV_BYTE; | |
581 | s1 = 0; | |
582 | } | |
583 | if (s2 < 0) | |
584 | { | |
585 | s1 = ZV_BYTE - BEGV_BYTE; | |
586 | s2 = 0; | |
587 | } | |
588 | re_match_object = Qnil; | |
4b4deea2 | 589 | multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
8b13507a KH |
590 | } |
591 | ||
592 | buf = compile_pattern (regexp, 0, Qnil, 0, multibyte); | |
593 | immediate_quit = 1; | |
594 | len = re_match_2 (buf, (char *) p1, s1, (char *) p2, s2, | |
595 | pos_byte, NULL, limit_byte); | |
596 | immediate_quit = 0; | |
597 | ||
598 | return len; | |
599 | } | |
600 | ||
601 | \f | |
9169c321 JB |
602 | /* The newline cache: remembering which sections of text have no newlines. */ |
603 | ||
604 | /* If the user has requested newline caching, make sure it's on. | |
605 | Otherwise, make sure it's off. | |
606 | This is our cheezy way of associating an action with the change of | |
607 | state of a buffer-local variable. */ | |
608 | static void | |
971de7fb | 609 | newline_cache_on_off (struct buffer *buf) |
9169c321 | 610 | { |
4b4deea2 | 611 | if (NILP (BVAR (buf, cache_long_line_scans))) |
9169c321 JB |
612 | { |
613 | /* It should be off. */ | |
614 | if (buf->newline_cache) | |
615 | { | |
616 | free_region_cache (buf->newline_cache); | |
617 | buf->newline_cache = 0; | |
618 | } | |
619 | } | |
620 | else | |
621 | { | |
622 | /* It should be on. */ | |
623 | if (buf->newline_cache == 0) | |
624 | buf->newline_cache = new_region_cache (); | |
625 | } | |
626 | } | |
627 | ||
628 | \f | |
629 | /* Search for COUNT instances of the character TARGET between START and END. | |
630 | ||
631 | If COUNT is positive, search forwards; END must be >= START. | |
632 | If COUNT is negative, search backwards for the -COUNTth instance; | |
633 | END must be <= START. | |
634 | If COUNT is zero, do anything you please; run rogue, for all I care. | |
635 | ||
636 | If END is zero, use BEGV or ZV instead, as appropriate for the | |
637 | direction indicated by COUNT. | |
ffd56f97 JB |
638 | |
639 | If we find COUNT instances, set *SHORTAGE to zero, and return the | |
a9f2a45f | 640 | position past the COUNTth match. Note that for reverse motion |
5bfe95c9 | 641 | this is not the same as the usual convention for Emacs motion commands. |
ffd56f97 | 642 | |
9169c321 JB |
643 | If we don't find COUNT instances before reaching END, set *SHORTAGE |
644 | to the number of TARGETs left unfound, and return END. | |
ffd56f97 | 645 | |
087a5f81 RS |
646 | If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do |
647 | except when inside redisplay. */ | |
648 | ||
c098fdb8 EZ |
649 | EMACS_INT |
650 | scan_buffer (register int target, EMACS_INT start, EMACS_INT end, | |
651 | EMACS_INT count, int *shortage, int allow_quit) | |
ca1d1d23 | 652 | { |
9169c321 | 653 | struct region_cache *newline_cache; |
177c0ea7 | 654 | int direction; |
ffd56f97 | 655 | |
9169c321 JB |
656 | if (count > 0) |
657 | { | |
658 | direction = 1; | |
659 | if (! end) end = ZV; | |
660 | } | |
661 | else | |
662 | { | |
663 | direction = -1; | |
664 | if (! end) end = BEGV; | |
665 | } | |
ffd56f97 | 666 | |
9169c321 JB |
667 | newline_cache_on_off (current_buffer); |
668 | newline_cache = current_buffer->newline_cache; | |
ca1d1d23 JB |
669 | |
670 | if (shortage != 0) | |
671 | *shortage = 0; | |
672 | ||
087a5f81 | 673 | immediate_quit = allow_quit; |
ca1d1d23 | 674 | |
ffd56f97 | 675 | if (count > 0) |
9169c321 | 676 | while (start != end) |
ca1d1d23 | 677 | { |
9169c321 JB |
678 | /* Our innermost scanning loop is very simple; it doesn't know |
679 | about gaps, buffer ends, or the newline cache. ceiling is | |
680 | the position of the last character before the next such | |
681 | obstacle --- the last character the dumb search loop should | |
682 | examine. */ | |
e7deaab0 AS |
683 | EMACS_INT ceiling_byte = CHAR_TO_BYTE (end) - 1; |
684 | EMACS_INT start_byte = CHAR_TO_BYTE (start); | |
685 | EMACS_INT tem; | |
9169c321 JB |
686 | |
687 | /* If we're looking for a newline, consult the newline cache | |
688 | to see where we can avoid some scanning. */ | |
689 | if (target == '\n' && newline_cache) | |
690 | { | |
c098fdb8 | 691 | EMACS_INT next_change; |
9169c321 JB |
692 | immediate_quit = 0; |
693 | while (region_cache_forward | |
fa8ed3e0 RS |
694 | (current_buffer, newline_cache, start_byte, &next_change)) |
695 | start_byte = next_change; | |
cbe0db0d | 696 | immediate_quit = allow_quit; |
9169c321 | 697 | |
fa8ed3e0 RS |
698 | /* START should never be after END. */ |
699 | if (start_byte > ceiling_byte) | |
700 | start_byte = ceiling_byte; | |
9169c321 JB |
701 | |
702 | /* Now the text after start is an unknown region, and | |
703 | next_change is the position of the next known region. */ | |
fa8ed3e0 | 704 | ceiling_byte = min (next_change - 1, ceiling_byte); |
9169c321 JB |
705 | } |
706 | ||
707 | /* The dumb loop can only scan text stored in contiguous | |
708 | bytes. BUFFER_CEILING_OF returns the last character | |
709 | position that is contiguous, so the ceiling is the | |
710 | position after that. */ | |
67ce527d KH |
711 | tem = BUFFER_CEILING_OF (start_byte); |
712 | ceiling_byte = min (tem, ceiling_byte); | |
9169c321 JB |
713 | |
714 | { | |
177c0ea7 | 715 | /* The termination address of the dumb loop. */ |
fa8ed3e0 RS |
716 | register unsigned char *ceiling_addr |
717 | = BYTE_POS_ADDR (ceiling_byte) + 1; | |
718 | register unsigned char *cursor | |
719 | = BYTE_POS_ADDR (start_byte); | |
9169c321 JB |
720 | unsigned char *base = cursor; |
721 | ||
722 | while (cursor < ceiling_addr) | |
723 | { | |
724 | unsigned char *scan_start = cursor; | |
725 | ||
726 | /* The dumb loop. */ | |
727 | while (*cursor != target && ++cursor < ceiling_addr) | |
728 | ; | |
729 | ||
730 | /* If we're looking for newlines, cache the fact that | |
731 | the region from start to cursor is free of them. */ | |
732 | if (target == '\n' && newline_cache) | |
733 | know_region_cache (current_buffer, newline_cache, | |
fa8ed3e0 RS |
734 | start_byte + scan_start - base, |
735 | start_byte + cursor - base); | |
9169c321 JB |
736 | |
737 | /* Did we find the target character? */ | |
738 | if (cursor < ceiling_addr) | |
739 | { | |
740 | if (--count == 0) | |
741 | { | |
742 | immediate_quit = 0; | |
fa8ed3e0 | 743 | return BYTE_TO_CHAR (start_byte + cursor - base + 1); |
9169c321 JB |
744 | } |
745 | cursor++; | |
746 | } | |
747 | } | |
748 | ||
fa8ed3e0 | 749 | start = BYTE_TO_CHAR (start_byte + cursor - base); |
9169c321 | 750 | } |
ca1d1d23 JB |
751 | } |
752 | else | |
9169c321 JB |
753 | while (start > end) |
754 | { | |
755 | /* The last character to check before the next obstacle. */ | |
e7deaab0 AS |
756 | EMACS_INT ceiling_byte = CHAR_TO_BYTE (end); |
757 | EMACS_INT start_byte = CHAR_TO_BYTE (start); | |
758 | EMACS_INT tem; | |
9169c321 JB |
759 | |
760 | /* Consult the newline cache, if appropriate. */ | |
761 | if (target == '\n' && newline_cache) | |
762 | { | |
c098fdb8 | 763 | EMACS_INT next_change; |
9169c321 JB |
764 | immediate_quit = 0; |
765 | while (region_cache_backward | |
fa8ed3e0 RS |
766 | (current_buffer, newline_cache, start_byte, &next_change)) |
767 | start_byte = next_change; | |
cbe0db0d | 768 | immediate_quit = allow_quit; |
9169c321 JB |
769 | |
770 | /* Start should never be at or before end. */ | |
fa8ed3e0 RS |
771 | if (start_byte <= ceiling_byte) |
772 | start_byte = ceiling_byte + 1; | |
9169c321 JB |
773 | |
774 | /* Now the text before start is an unknown region, and | |
775 | next_change is the position of the next known region. */ | |
fa8ed3e0 | 776 | ceiling_byte = max (next_change, ceiling_byte); |
9169c321 JB |
777 | } |
778 | ||
779 | /* Stop scanning before the gap. */ | |
67ce527d KH |
780 | tem = BUFFER_FLOOR_OF (start_byte - 1); |
781 | ceiling_byte = max (tem, ceiling_byte); | |
9169c321 JB |
782 | |
783 | { | |
784 | /* The termination address of the dumb loop. */ | |
fa8ed3e0 RS |
785 | register unsigned char *ceiling_addr = BYTE_POS_ADDR (ceiling_byte); |
786 | register unsigned char *cursor = BYTE_POS_ADDR (start_byte - 1); | |
9169c321 JB |
787 | unsigned char *base = cursor; |
788 | ||
789 | while (cursor >= ceiling_addr) | |
790 | { | |
791 | unsigned char *scan_start = cursor; | |
792 | ||
793 | while (*cursor != target && --cursor >= ceiling_addr) | |
794 | ; | |
795 | ||
796 | /* If we're looking for newlines, cache the fact that | |
797 | the region from after the cursor to start is free of them. */ | |
798 | if (target == '\n' && newline_cache) | |
799 | know_region_cache (current_buffer, newline_cache, | |
fa8ed3e0 RS |
800 | start_byte + cursor - base, |
801 | start_byte + scan_start - base); | |
9169c321 JB |
802 | |
803 | /* Did we find the target character? */ | |
804 | if (cursor >= ceiling_addr) | |
805 | { | |
806 | if (++count >= 0) | |
807 | { | |
808 | immediate_quit = 0; | |
fa8ed3e0 | 809 | return BYTE_TO_CHAR (start_byte + cursor - base); |
9169c321 JB |
810 | } |
811 | cursor--; | |
812 | } | |
813 | } | |
814 | ||
fa8ed3e0 | 815 | start = BYTE_TO_CHAR (start_byte + cursor - base); |
9169c321 JB |
816 | } |
817 | } | |
818 | ||
ca1d1d23 JB |
819 | immediate_quit = 0; |
820 | if (shortage != 0) | |
ffd56f97 | 821 | *shortage = count * direction; |
9169c321 | 822 | return start; |
ca1d1d23 | 823 | } |
fa8ed3e0 RS |
824 | \f |
825 | /* Search for COUNT instances of a line boundary, which means either a | |
826 | newline or (if selective display enabled) a carriage return. | |
827 | Start at START. If COUNT is negative, search backwards. | |
828 | ||
829 | We report the resulting position by calling TEMP_SET_PT_BOTH. | |
830 | ||
831 | If we find COUNT instances. we position after (always after, | |
832 | even if scanning backwards) the COUNTth match, and return 0. | |
833 | ||
834 | If we don't find COUNT instances before reaching the end of the | |
835 | buffer (or the beginning, if scanning backwards), we return | |
836 | the number of line boundaries left unfound, and position at | |
837 | the limit we bumped up against. | |
838 | ||
839 | If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do | |
d5d57b92 | 840 | except in special cases. */ |
ca1d1d23 | 841 | |
c098fdb8 EZ |
842 | EMACS_INT |
843 | scan_newline (EMACS_INT start, EMACS_INT start_byte, | |
844 | EMACS_INT limit, EMACS_INT limit_byte, | |
845 | register EMACS_INT count, int allow_quit) | |
63fa018d | 846 | { |
fa8ed3e0 RS |
847 | int direction = ((count > 0) ? 1 : -1); |
848 | ||
849 | register unsigned char *cursor; | |
850 | unsigned char *base; | |
851 | ||
e7deaab0 | 852 | EMACS_INT ceiling; |
fa8ed3e0 RS |
853 | register unsigned char *ceiling_addr; |
854 | ||
d5d57b92 RS |
855 | int old_immediate_quit = immediate_quit; |
856 | ||
fa8ed3e0 RS |
857 | /* The code that follows is like scan_buffer |
858 | but checks for either newline or carriage return. */ | |
859 | ||
d5d57b92 RS |
860 | if (allow_quit) |
861 | immediate_quit++; | |
fa8ed3e0 RS |
862 | |
863 | start_byte = CHAR_TO_BYTE (start); | |
864 | ||
865 | if (count > 0) | |
866 | { | |
867 | while (start_byte < limit_byte) | |
868 | { | |
869 | ceiling = BUFFER_CEILING_OF (start_byte); | |
870 | ceiling = min (limit_byte - 1, ceiling); | |
871 | ceiling_addr = BYTE_POS_ADDR (ceiling) + 1; | |
872 | base = (cursor = BYTE_POS_ADDR (start_byte)); | |
873 | while (1) | |
874 | { | |
875 | while (*cursor != '\n' && ++cursor != ceiling_addr) | |
876 | ; | |
877 | ||
878 | if (cursor != ceiling_addr) | |
879 | { | |
880 | if (--count == 0) | |
881 | { | |
d5d57b92 | 882 | immediate_quit = old_immediate_quit; |
fa8ed3e0 RS |
883 | start_byte = start_byte + cursor - base + 1; |
884 | start = BYTE_TO_CHAR (start_byte); | |
885 | TEMP_SET_PT_BOTH (start, start_byte); | |
886 | return 0; | |
887 | } | |
888 | else | |
889 | if (++cursor == ceiling_addr) | |
890 | break; | |
891 | } | |
892 | else | |
893 | break; | |
894 | } | |
895 | start_byte += cursor - base; | |
896 | } | |
897 | } | |
898 | else | |
899 | { | |
fa8ed3e0 RS |
900 | while (start_byte > limit_byte) |
901 | { | |
902 | ceiling = BUFFER_FLOOR_OF (start_byte - 1); | |
903 | ceiling = max (limit_byte, ceiling); | |
904 | ceiling_addr = BYTE_POS_ADDR (ceiling) - 1; | |
905 | base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1); | |
906 | while (1) | |
907 | { | |
908 | while (--cursor != ceiling_addr && *cursor != '\n') | |
909 | ; | |
910 | ||
911 | if (cursor != ceiling_addr) | |
912 | { | |
913 | if (++count == 0) | |
914 | { | |
d5d57b92 | 915 | immediate_quit = old_immediate_quit; |
fa8ed3e0 RS |
916 | /* Return the position AFTER the match we found. */ |
917 | start_byte = start_byte + cursor - base + 1; | |
918 | start = BYTE_TO_CHAR (start_byte); | |
919 | TEMP_SET_PT_BOTH (start, start_byte); | |
920 | return 0; | |
921 | } | |
922 | } | |
923 | else | |
924 | break; | |
925 | } | |
926 | /* Here we add 1 to compensate for the last decrement | |
927 | of CURSOR, which took it past the valid range. */ | |
928 | start_byte += cursor - base + 1; | |
929 | } | |
930 | } | |
931 | ||
932 | TEMP_SET_PT_BOTH (limit, limit_byte); | |
d5d57b92 | 933 | immediate_quit = old_immediate_quit; |
fa8ed3e0 RS |
934 | |
935 | return count * direction; | |
63fa018d RS |
936 | } |
937 | ||
c098fdb8 EZ |
938 | EMACS_INT |
939 | find_next_newline_no_quit (EMACS_INT from, EMACS_INT cnt) | |
ca1d1d23 | 940 | { |
fa8ed3e0 | 941 | return scan_buffer ('\n', from, 0, cnt, (int *) 0, 0); |
9169c321 JB |
942 | } |
943 | ||
9169c321 JB |
944 | /* Like find_next_newline, but returns position before the newline, |
945 | not after, and only search up to TO. This isn't just | |
946 | find_next_newline (...)-1, because you might hit TO. */ | |
fa8ed3e0 | 947 | |
c098fdb8 EZ |
948 | EMACS_INT |
949 | find_before_next_newline (EMACS_INT from, EMACS_INT to, EMACS_INT cnt) | |
9169c321 JB |
950 | { |
951 | int shortage; | |
c098fdb8 | 952 | EMACS_INT pos = scan_buffer ('\n', from, to, cnt, &shortage, 1); |
9169c321 JB |
953 | |
954 | if (shortage == 0) | |
955 | pos--; | |
177c0ea7 | 956 | |
9169c321 | 957 | return pos; |
ca1d1d23 JB |
958 | } |
959 | \f | |
ca1d1d23 JB |
960 | /* Subroutines of Lisp buffer search functions. */ |
961 | ||
962 | static Lisp_Object | |
c098fdb8 EZ |
963 | search_command (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, |
964 | Lisp_Object count, int direction, int RE, int posix) | |
ca1d1d23 JB |
965 | { |
966 | register int np; | |
c098fdb8 | 967 | EMACS_INT lim, lim_byte; |
ca1d1d23 JB |
968 | int n = direction; |
969 | ||
970 | if (!NILP (count)) | |
971 | { | |
b7826503 | 972 | CHECK_NUMBER (count); |
ca1d1d23 JB |
973 | n *= XINT (count); |
974 | } | |
975 | ||
b7826503 | 976 | CHECK_STRING (string); |
ca1d1d23 | 977 | if (NILP (bound)) |
9f43ad85 RS |
978 | { |
979 | if (n > 0) | |
980 | lim = ZV, lim_byte = ZV_BYTE; | |
981 | else | |
982 | lim = BEGV, lim_byte = BEGV_BYTE; | |
983 | } | |
ca1d1d23 JB |
984 | else |
985 | { | |
b7826503 | 986 | CHECK_NUMBER_COERCE_MARKER (bound); |
ca1d1d23 | 987 | lim = XINT (bound); |
6ec8bbd2 | 988 | if (n > 0 ? lim < PT : lim > PT) |
ca1d1d23 JB |
989 | error ("Invalid search bound (wrong side of point)"); |
990 | if (lim > ZV) | |
9f43ad85 | 991 | lim = ZV, lim_byte = ZV_BYTE; |
588d2fd5 | 992 | else if (lim < BEGV) |
9f43ad85 | 993 | lim = BEGV, lim_byte = BEGV_BYTE; |
588d2fd5 KH |
994 | else |
995 | lim_byte = CHAR_TO_BYTE (lim); | |
ca1d1d23 JB |
996 | } |
997 | ||
910c747a | 998 | /* This is so set_image_of_range_1 in regex.c can find the EQV table. */ |
4b4deea2 TT |
999 | XCHAR_TABLE (BVAR (current_buffer, case_canon_table))->extras[2] |
1000 | = BVAR (current_buffer, case_eqv_table); | |
910c747a | 1001 | |
9f43ad85 | 1002 | np = search_buffer (string, PT, PT_BYTE, lim, lim_byte, n, RE, |
4b4deea2 TT |
1003 | (!NILP (BVAR (current_buffer, case_fold_search)) |
1004 | ? BVAR (current_buffer, case_canon_table) | |
3135e9fd | 1005 | : Qnil), |
4b4deea2 TT |
1006 | (!NILP (BVAR (current_buffer, case_fold_search)) |
1007 | ? BVAR (current_buffer, case_eqv_table) | |
3135e9fd | 1008 | : Qnil), |
b819a390 | 1009 | posix); |
ca1d1d23 JB |
1010 | if (np <= 0) |
1011 | { | |
1012 | if (NILP (noerror)) | |
06f77a8a KS |
1013 | xsignal1 (Qsearch_failed, string); |
1014 | ||
ca1d1d23 JB |
1015 | if (!EQ (noerror, Qt)) |
1016 | { | |
1017 | if (lim < BEGV || lim > ZV) | |
1018 | abort (); | |
9f43ad85 | 1019 | SET_PT_BOTH (lim, lim_byte); |
a5f217b8 RS |
1020 | return Qnil; |
1021 | #if 0 /* This would be clean, but maybe programs depend on | |
1022 | a value of nil here. */ | |
481399bf | 1023 | np = lim; |
a5f217b8 | 1024 | #endif |
ca1d1d23 | 1025 | } |
481399bf RS |
1026 | else |
1027 | return Qnil; | |
ca1d1d23 JB |
1028 | } |
1029 | ||
1030 | if (np < BEGV || np > ZV) | |
1031 | abort (); | |
1032 | ||
1033 | SET_PT (np); | |
1034 | ||
1035 | return make_number (np); | |
1036 | } | |
1037 | \f | |
fa8ed3e0 RS |
1038 | /* Return 1 if REGEXP it matches just one constant string. */ |
1039 | ||
b6d6a51c | 1040 | static int |
971de7fb | 1041 | trivial_regexp_p (Lisp_Object regexp) |
b6d6a51c | 1042 | { |
c098fdb8 | 1043 | EMACS_INT len = SBYTES (regexp); |
d5db4077 | 1044 | unsigned char *s = SDATA (regexp); |
b6d6a51c KH |
1045 | while (--len >= 0) |
1046 | { | |
1047 | switch (*s++) | |
1048 | { | |
1049 | case '.': case '*': case '+': case '?': case '[': case '^': case '$': | |
1050 | return 0; | |
1051 | case '\\': | |
1052 | if (--len < 0) | |
1053 | return 0; | |
1054 | switch (*s++) | |
1055 | { | |
1056 | case '|': case '(': case ')': case '`': case '\'': case 'b': | |
1057 | case 'B': case '<': case '>': case 'w': case 'W': case 's': | |
29f89fe7 | 1058 | case 'S': case '=': case '{': case '}': case '_': |
5679531d | 1059 | case 'c': case 'C': /* for categoryspec and notcategoryspec */ |
866f60fd | 1060 | case '1': case '2': case '3': case '4': case '5': |
b6d6a51c KH |
1061 | case '6': case '7': case '8': case '9': |
1062 | return 0; | |
1063 | } | |
1064 | } | |
1065 | } | |
1066 | return 1; | |
1067 | } | |
1068 | ||
ca325161 | 1069 | /* Search for the n'th occurrence of STRING in the current buffer, |
ca1d1d23 | 1070 | starting at position POS and stopping at position LIM, |
b819a390 | 1071 | treating STRING as a literal string if RE is false or as |
ca1d1d23 JB |
1072 | a regular expression if RE is true. |
1073 | ||
1074 | If N is positive, searching is forward and LIM must be greater than POS. | |
1075 | If N is negative, searching is backward and LIM must be less than POS. | |
1076 | ||
facdc750 | 1077 | Returns -x if x occurrences remain to be found (x > 0), |
ca1d1d23 | 1078 | or else the position at the beginning of the Nth occurrence |
b819a390 RS |
1079 | (if searching backward) or the end (if searching forward). |
1080 | ||
1081 | POSIX is nonzero if we want full backtracking (POSIX style) | |
1082 | for this pattern. 0 means backtrack only enough to get a valid match. */ | |
ca1d1d23 | 1083 | |
aff2ce94 RS |
1084 | #define TRANSLATE(out, trt, d) \ |
1085 | do \ | |
1086 | { \ | |
1087 | if (! NILP (trt)) \ | |
1088 | { \ | |
1089 | Lisp_Object temp; \ | |
1090 | temp = Faref (trt, make_number (d)); \ | |
1091 | if (INTEGERP (temp)) \ | |
1092 | out = XINT (temp); \ | |
1093 | else \ | |
1094 | out = d; \ | |
1095 | } \ | |
1096 | else \ | |
1097 | out = d; \ | |
1098 | } \ | |
1099 | while (0) | |
facdc750 | 1100 | |
ef887810 RS |
1101 | /* Only used in search_buffer, to record the end position of the match |
1102 | when searching regexps and SEARCH_REGS should not be changed | |
1103 | (i.e. Vinhibit_changing_match_data is non-nil). */ | |
1104 | static struct re_registers search_regs_1; | |
1105 | ||
e7deaab0 | 1106 | static EMACS_INT |
d5a3eaaf | 1107 | search_buffer (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte, |
c098fdb8 | 1108 | EMACS_INT lim, EMACS_INT lim_byte, EMACS_INT n, |
d5a3eaaf | 1109 | int RE, Lisp_Object trt, Lisp_Object inverse_trt, int posix) |
ca1d1d23 | 1110 | { |
c098fdb8 EZ |
1111 | EMACS_INT len = SCHARS (string); |
1112 | EMACS_INT len_byte = SBYTES (string); | |
facdc750 | 1113 | register int i; |
ca1d1d23 | 1114 | |
7074fde6 FP |
1115 | if (running_asynch_code) |
1116 | save_search_regs (); | |
1117 | ||
a7e4cdde | 1118 | /* Searching 0 times means don't move. */ |
ca1d1d23 | 1119 | /* Null string is found at starting position. */ |
a7e4cdde | 1120 | if (len == 0 || n == 0) |
ca325161 | 1121 | { |
0353b28f | 1122 | set_search_regs (pos_byte, 0); |
ca325161 RS |
1123 | return pos; |
1124 | } | |
3f57a499 | 1125 | |
41a33295 | 1126 | if (RE && !(trivial_regexp_p (string) && NILP (Vsearch_spaces_regexp))) |
ca1d1d23 | 1127 | { |
facdc750 | 1128 | unsigned char *p1, *p2; |
c098fdb8 | 1129 | EMACS_INT s1, s2; |
487282dc KH |
1130 | struct re_pattern_buffer *bufp; |
1131 | ||
ef887810 RS |
1132 | bufp = compile_pattern (string, |
1133 | (NILP (Vinhibit_changing_match_data) | |
1134 | ? &search_regs : &search_regs_1), | |
1135 | trt, posix, | |
4b4deea2 | 1136 | !NILP (BVAR (current_buffer, enable_multibyte_characters))); |
ca1d1d23 | 1137 | |
ca1d1d23 JB |
1138 | immediate_quit = 1; /* Quit immediately if user types ^G, |
1139 | because letting this function finish | |
1140 | can take too long. */ | |
1141 | QUIT; /* Do a pending quit right away, | |
1142 | to avoid paradoxical behavior */ | |
1143 | /* Get pointers and sizes of the two strings | |
1144 | that make up the visible portion of the buffer. */ | |
1145 | ||
1146 | p1 = BEGV_ADDR; | |
fa8ed3e0 | 1147 | s1 = GPT_BYTE - BEGV_BYTE; |
ca1d1d23 | 1148 | p2 = GAP_END_ADDR; |
fa8ed3e0 | 1149 | s2 = ZV_BYTE - GPT_BYTE; |
ca1d1d23 JB |
1150 | if (s1 < 0) |
1151 | { | |
1152 | p2 = p1; | |
fa8ed3e0 | 1153 | s2 = ZV_BYTE - BEGV_BYTE; |
ca1d1d23 JB |
1154 | s1 = 0; |
1155 | } | |
1156 | if (s2 < 0) | |
1157 | { | |
fa8ed3e0 | 1158 | s1 = ZV_BYTE - BEGV_BYTE; |
ca1d1d23 JB |
1159 | s2 = 0; |
1160 | } | |
8bb43c28 | 1161 | re_match_object = Qnil; |
177c0ea7 | 1162 | |
ca1d1d23 JB |
1163 | while (n < 0) |
1164 | { | |
c098fdb8 | 1165 | EMACS_INT val; |
487282dc | 1166 | val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2, |
4996330b | 1167 | pos_byte - BEGV_BYTE, lim_byte - pos_byte, |
ef887810 RS |
1168 | (NILP (Vinhibit_changing_match_data) |
1169 | ? &search_regs : &search_regs_1), | |
42db823b | 1170 | /* Don't allow match past current point */ |
4996330b | 1171 | pos_byte - BEGV_BYTE); |
ca1d1d23 | 1172 | if (val == -2) |
b6d6a51c KH |
1173 | { |
1174 | matcher_overflow (); | |
1175 | } | |
ca1d1d23 JB |
1176 | if (val >= 0) |
1177 | { | |
ef887810 RS |
1178 | if (NILP (Vinhibit_changing_match_data)) |
1179 | { | |
1180 | pos_byte = search_regs.start[0] + BEGV_BYTE; | |
1181 | for (i = 0; i < search_regs.num_regs; i++) | |
1182 | if (search_regs.start[i] >= 0) | |
1183 | { | |
1184 | search_regs.start[i] | |
1185 | = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE); | |
1186 | search_regs.end[i] | |
1187 | = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE); | |
1188 | } | |
1189 | XSETBUFFER (last_thing_searched, current_buffer); | |
1190 | /* Set pos to the new position. */ | |
1191 | pos = search_regs.start[0]; | |
1192 | } | |
1193 | else | |
1194 | { | |
1195 | pos_byte = search_regs_1.start[0] + BEGV_BYTE; | |
1196 | /* Set pos to the new position. */ | |
1197 | pos = BYTE_TO_CHAR (search_regs_1.start[0] + BEGV_BYTE); | |
1198 | } | |
ca1d1d23 JB |
1199 | } |
1200 | else | |
1201 | { | |
1202 | immediate_quit = 0; | |
1203 | return (n); | |
1204 | } | |
1205 | n++; | |
1206 | } | |
1207 | while (n > 0) | |
1208 | { | |
c098fdb8 | 1209 | EMACS_INT val; |
487282dc | 1210 | val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2, |
4996330b | 1211 | pos_byte - BEGV_BYTE, lim_byte - pos_byte, |
ef887810 RS |
1212 | (NILP (Vinhibit_changing_match_data) |
1213 | ? &search_regs : &search_regs_1), | |
4996330b | 1214 | lim_byte - BEGV_BYTE); |
ca1d1d23 | 1215 | if (val == -2) |
b6d6a51c KH |
1216 | { |
1217 | matcher_overflow (); | |
1218 | } | |
ca1d1d23 JB |
1219 | if (val >= 0) |
1220 | { | |
ef887810 RS |
1221 | if (NILP (Vinhibit_changing_match_data)) |
1222 | { | |
1223 | pos_byte = search_regs.end[0] + BEGV_BYTE; | |
1224 | for (i = 0; i < search_regs.num_regs; i++) | |
1225 | if (search_regs.start[i] >= 0) | |
1226 | { | |
1227 | search_regs.start[i] | |
1228 | = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE); | |
1229 | search_regs.end[i] | |
1230 | = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE); | |
1231 | } | |
1232 | XSETBUFFER (last_thing_searched, current_buffer); | |
1233 | pos = search_regs.end[0]; | |
1234 | } | |
1235 | else | |
1236 | { | |
1237 | pos_byte = search_regs_1.end[0] + BEGV_BYTE; | |
1238 | pos = BYTE_TO_CHAR (search_regs_1.end[0] + BEGV_BYTE); | |
1239 | } | |
ca1d1d23 JB |
1240 | } |
1241 | else | |
1242 | { | |
1243 | immediate_quit = 0; | |
1244 | return (0 - n); | |
1245 | } | |
1246 | n--; | |
1247 | } | |
1248 | immediate_quit = 0; | |
1249 | return (pos); | |
1250 | } | |
1251 | else /* non-RE case */ | |
1252 | { | |
facdc750 | 1253 | unsigned char *raw_pattern, *pat; |
c098fdb8 EZ |
1254 | EMACS_INT raw_pattern_size; |
1255 | EMACS_INT raw_pattern_size_byte; | |
facdc750 | 1256 | unsigned char *patbuf; |
4b4deea2 | 1257 | int multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); |
a967ed62 | 1258 | unsigned char *base_pat; |
49e44e81 KH |
1259 | /* Set to positive if we find a non-ASCII char that need |
1260 | translation. Otherwise set to zero later. */ | |
1261 | int char_base = -1; | |
040272ce | 1262 | int boyer_moore_ok = 1; |
facdc750 RS |
1263 | |
1264 | /* MULTIBYTE says whether the text to be searched is multibyte. | |
1265 | We must convert PATTERN to match that, or we will not really | |
1266 | find things right. */ | |
1267 | ||
1268 | if (multibyte == STRING_MULTIBYTE (string)) | |
1269 | { | |
51b59d79 | 1270 | raw_pattern = SDATA (string); |
d5db4077 KR |
1271 | raw_pattern_size = SCHARS (string); |
1272 | raw_pattern_size_byte = SBYTES (string); | |
facdc750 RS |
1273 | } |
1274 | else if (multibyte) | |
1275 | { | |
d5db4077 | 1276 | raw_pattern_size = SCHARS (string); |
facdc750 | 1277 | raw_pattern_size_byte |
d5db4077 | 1278 | = count_size_as_multibyte (SDATA (string), |
facdc750 | 1279 | raw_pattern_size); |
7276d3d8 | 1280 | raw_pattern = (unsigned char *) alloca (raw_pattern_size_byte + 1); |
d5db4077 KR |
1281 | copy_text (SDATA (string), raw_pattern, |
1282 | SCHARS (string), 0, 1); | |
facdc750 RS |
1283 | } |
1284 | else | |
1285 | { | |
1286 | /* Converting multibyte to single-byte. | |
1287 | ||
1288 | ??? Perhaps this conversion should be done in a special way | |
1289 | by subtracting nonascii-insert-offset from each non-ASCII char, | |
1290 | so that only the multibyte chars which really correspond to | |
1291 | the chosen single-byte character set can possibly match. */ | |
d5db4077 KR |
1292 | raw_pattern_size = SCHARS (string); |
1293 | raw_pattern_size_byte = SCHARS (string); | |
7276d3d8 | 1294 | raw_pattern = (unsigned char *) alloca (raw_pattern_size + 1); |
d5db4077 KR |
1295 | copy_text (SDATA (string), raw_pattern, |
1296 | SBYTES (string), 1, 0); | |
facdc750 RS |
1297 | } |
1298 | ||
1299 | /* Copy and optionally translate the pattern. */ | |
1300 | len = raw_pattern_size; | |
1301 | len_byte = raw_pattern_size_byte; | |
f5f7578b | 1302 | patbuf = (unsigned char *) alloca (len * MAX_MULTIBYTE_LENGTH); |
facdc750 RS |
1303 | pat = patbuf; |
1304 | base_pat = raw_pattern; | |
1305 | if (multibyte) | |
1306 | { | |
a17ae96a KH |
1307 | /* Fill patbuf by translated characters in STRING while |
1308 | checking if we can use boyer-moore search. If TRT is | |
1309 | non-nil, we can use boyer-moore search only if TRT can be | |
1310 | represented by the byte array of 256 elements. For that, | |
1311 | all non-ASCII case-equivalents of all case-senstive | |
1312 | characters in STRING must belong to the same charset and | |
1313 | row. */ | |
1314 | ||
facdc750 RS |
1315 | while (--len >= 0) |
1316 | { | |
a17ae96a | 1317 | unsigned char str_base[MAX_MULTIBYTE_LENGTH], *str; |
aff2ce94 | 1318 | int c, translated, inverse; |
a17ae96a | 1319 | int in_charlen, charlen; |
facdc750 RS |
1320 | |
1321 | /* If we got here and the RE flag is set, it's because we're | |
1322 | dealing with a regexp known to be trivial, so the backslash | |
1323 | just quotes the next character. */ | |
1324 | if (RE && *base_pat == '\\') | |
1325 | { | |
1326 | len--; | |
62221f22 | 1327 | raw_pattern_size--; |
facdc750 RS |
1328 | len_byte--; |
1329 | base_pat++; | |
1330 | } | |
1331 | ||
62a6e103 | 1332 | c = STRING_CHAR_AND_LENGTH (base_pat, in_charlen); |
040272ce | 1333 | |
a17ae96a | 1334 | if (NILP (trt)) |
facdc750 | 1335 | { |
a17ae96a KH |
1336 | str = base_pat; |
1337 | charlen = in_charlen; | |
1338 | } | |
1339 | else | |
1340 | { | |
1341 | /* Translate the character. */ | |
1342 | TRANSLATE (translated, trt, c); | |
1343 | charlen = CHAR_STRING (translated, str_base); | |
1344 | str = str_base; | |
1345 | ||
1346 | /* Check if C has any other case-equivalents. */ | |
1347 | TRANSLATE (inverse, inverse_trt, c); | |
1348 | /* If so, check if we can use boyer-moore. */ | |
1349 | if (c != inverse && boyer_moore_ok) | |
1350 | { | |
1351 | /* Check if all equivalents belong to the same | |
1352 | group of characters. Note that the check of C | |
49e44e81 KH |
1353 | itself is done by the last iteration. */ |
1354 | int this_char_base = -1; | |
1355 | ||
1356 | while (boyer_moore_ok) | |
a17ae96a | 1357 | { |
49e44e81 | 1358 | if (ASCII_BYTE_P (inverse)) |
a17ae96a | 1359 | { |
49e44e81 KH |
1360 | if (this_char_base > 0) |
1361 | boyer_moore_ok = 0; | |
1362 | else | |
dafaabd1 | 1363 | this_char_base = 0; |
a17ae96a | 1364 | } |
49e44e81 KH |
1365 | else if (CHAR_BYTE8_P (inverse)) |
1366 | /* Boyer-moore search can't handle a | |
1367 | translation of an eight-bit | |
1368 | character. */ | |
1369 | boyer_moore_ok = 0; | |
1370 | else if (this_char_base < 0) | |
1371 | { | |
1372 | this_char_base = inverse & ~0x3F; | |
1373 | if (char_base < 0) | |
1374 | char_base = this_char_base; | |
dafaabd1 | 1375 | else if (this_char_base != char_base) |
49e44e81 KH |
1376 | boyer_moore_ok = 0; |
1377 | } | |
1378 | else if ((inverse & ~0x3F) != this_char_base) | |
1379 | boyer_moore_ok = 0; | |
a17ae96a KH |
1380 | if (c == inverse) |
1381 | break; | |
1382 | TRANSLATE (inverse, inverse_trt, inverse); | |
1383 | } | |
1384 | } | |
aff2ce94 | 1385 | } |
facdc750 RS |
1386 | |
1387 | /* Store this character into the translated pattern. */ | |
72af86bd | 1388 | memcpy (pat, str, charlen); |
a17ae96a | 1389 | pat += charlen; |
facdc750 RS |
1390 | base_pat += in_charlen; |
1391 | len_byte -= in_charlen; | |
1392 | } | |
dafaabd1 AS |
1393 | |
1394 | /* If char_base is still negative we didn't find any translated | |
1395 | non-ASCII characters. */ | |
1396 | if (char_base < 0) | |
1397 | char_base = 0; | |
facdc750 RS |
1398 | } |
1399 | else | |
1400 | { | |
040272ce | 1401 | /* Unibyte buffer. */ |
a17ae96a | 1402 | char_base = 0; |
facdc750 RS |
1403 | while (--len >= 0) |
1404 | { | |
040272ce | 1405 | int c, translated; |
facdc750 RS |
1406 | |
1407 | /* If we got here and the RE flag is set, it's because we're | |
1408 | dealing with a regexp known to be trivial, so the backslash | |
1409 | just quotes the next character. */ | |
1410 | if (RE && *base_pat == '\\') | |
1411 | { | |
1412 | len--; | |
0190922f | 1413 | raw_pattern_size--; |
facdc750 RS |
1414 | base_pat++; |
1415 | } | |
1416 | c = *base_pat++; | |
aff2ce94 | 1417 | TRANSLATE (translated, trt, c); |
facdc750 RS |
1418 | *pat++ = translated; |
1419 | } | |
1420 | } | |
1421 | ||
1422 | len_byte = pat - patbuf; | |
1423 | len = raw_pattern_size; | |
1424 | pat = base_pat = patbuf; | |
1425 | ||
040272ce | 1426 | if (boyer_moore_ok) |
facdc750 | 1427 | return boyer_moore (n, pat, len, len_byte, trt, inverse_trt, |
aff2ce94 | 1428 | pos, pos_byte, lim, lim_byte, |
a17ae96a | 1429 | char_base); |
facdc750 RS |
1430 | else |
1431 | return simple_search (n, pat, len, len_byte, trt, | |
1432 | pos, pos_byte, lim, lim_byte); | |
1433 | } | |
1434 | } | |
1435 | \f | |
1436 | /* Do a simple string search N times for the string PAT, | |
1437 | whose length is LEN/LEN_BYTE, | |
1438 | from buffer position POS/POS_BYTE until LIM/LIM_BYTE. | |
1439 | TRT is the translation table. | |
f8bd51c4 | 1440 | |
facdc750 RS |
1441 | Return the character position where the match is found. |
1442 | Otherwise, if M matches remained to be found, return -M. | |
f8bd51c4 | 1443 | |
facdc750 RS |
1444 | This kind of search works regardless of what is in PAT and |
1445 | regardless of what is in TRT. It is used in cases where | |
1446 | boyer_moore cannot work. */ | |
1447 | ||
e7deaab0 | 1448 | static EMACS_INT |
c098fdb8 EZ |
1449 | simple_search (EMACS_INT n, unsigned char *pat, |
1450 | EMACS_INT len, EMACS_INT len_byte, Lisp_Object trt, | |
1451 | EMACS_INT pos, EMACS_INT pos_byte, | |
1452 | EMACS_INT lim, EMACS_INT lim_byte) | |
facdc750 | 1453 | { |
4b4deea2 | 1454 | int multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
ab228c24 | 1455 | int forward = n > 0; |
49e44e81 KH |
1456 | /* Number of buffer bytes matched. Note that this may be different |
1457 | from len_byte in a multibyte buffer. */ | |
c098fdb8 | 1458 | EMACS_INT match_byte; |
facdc750 RS |
1459 | |
1460 | if (lim > pos && multibyte) | |
1461 | while (n > 0) | |
1462 | { | |
1463 | while (1) | |
f8bd51c4 | 1464 | { |
facdc750 | 1465 | /* Try matching at position POS. */ |
e7deaab0 AS |
1466 | EMACS_INT this_pos = pos; |
1467 | EMACS_INT this_pos_byte = pos_byte; | |
c098fdb8 | 1468 | EMACS_INT this_len = len; |
facdc750 | 1469 | unsigned char *p = pat; |
a9469498 | 1470 | if (pos + len > lim || pos_byte + len_byte > lim_byte) |
facdc750 RS |
1471 | goto stop; |
1472 | ||
1473 | while (this_len > 0) | |
1474 | { | |
1475 | int charlen, buf_charlen; | |
ab228c24 | 1476 | int pat_ch, buf_ch; |
facdc750 | 1477 | |
62a6e103 | 1478 | pat_ch = STRING_CHAR_AND_LENGTH (p, charlen); |
facdc750 | 1479 | buf_ch = STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte), |
facdc750 | 1480 | buf_charlen); |
aff2ce94 | 1481 | TRANSLATE (buf_ch, trt, buf_ch); |
facdc750 RS |
1482 | |
1483 | if (buf_ch != pat_ch) | |
1484 | break; | |
ab228c24 | 1485 | |
ab228c24 RS |
1486 | this_len--; |
1487 | p += charlen; | |
1488 | ||
1489 | this_pos_byte += buf_charlen; | |
1490 | this_pos++; | |
facdc750 RS |
1491 | } |
1492 | ||
1493 | if (this_len == 0) | |
1494 | { | |
49e44e81 | 1495 | match_byte = this_pos_byte - pos_byte; |
facdc750 | 1496 | pos += len; |
49e44e81 | 1497 | pos_byte += match_byte; |
facdc750 RS |
1498 | break; |
1499 | } | |
1500 | ||
1501 | INC_BOTH (pos, pos_byte); | |
f8bd51c4 | 1502 | } |
facdc750 RS |
1503 | |
1504 | n--; | |
1505 | } | |
1506 | else if (lim > pos) | |
1507 | while (n > 0) | |
1508 | { | |
1509 | while (1) | |
f8bd51c4 | 1510 | { |
facdc750 | 1511 | /* Try matching at position POS. */ |
e7deaab0 | 1512 | EMACS_INT this_pos = pos; |
c098fdb8 | 1513 | EMACS_INT this_len = len; |
facdc750 RS |
1514 | unsigned char *p = pat; |
1515 | ||
1516 | if (pos + len > lim) | |
1517 | goto stop; | |
1518 | ||
1519 | while (this_len > 0) | |
1520 | { | |
1521 | int pat_ch = *p++; | |
1522 | int buf_ch = FETCH_BYTE (this_pos); | |
aff2ce94 | 1523 | TRANSLATE (buf_ch, trt, buf_ch); |
facdc750 RS |
1524 | |
1525 | if (buf_ch != pat_ch) | |
1526 | break; | |
ab228c24 RS |
1527 | |
1528 | this_len--; | |
1529 | this_pos++; | |
facdc750 RS |
1530 | } |
1531 | ||
1532 | if (this_len == 0) | |
1533 | { | |
49e44e81 | 1534 | match_byte = len; |
facdc750 RS |
1535 | pos += len; |
1536 | break; | |
1537 | } | |
1538 | ||
1539 | pos++; | |
f8bd51c4 | 1540 | } |
facdc750 RS |
1541 | |
1542 | n--; | |
1543 | } | |
1544 | /* Backwards search. */ | |
1545 | else if (lim < pos && multibyte) | |
1546 | while (n < 0) | |
1547 | { | |
1548 | while (1) | |
f8bd51c4 | 1549 | { |
facdc750 | 1550 | /* Try matching at position POS. */ |
8b264ecb AS |
1551 | EMACS_INT this_pos = pos; |
1552 | EMACS_INT this_pos_byte = pos_byte; | |
c098fdb8 | 1553 | EMACS_INT this_len = len; |
c525b3f2 | 1554 | const unsigned char *p = pat + len_byte; |
facdc750 | 1555 | |
8b264ecb | 1556 | if (this_pos - len < lim || (pos_byte - len_byte) < lim_byte) |
facdc750 RS |
1557 | goto stop; |
1558 | ||
1559 | while (this_len > 0) | |
1560 | { | |
8b264ecb | 1561 | int charlen; |
ab228c24 | 1562 | int pat_ch, buf_ch; |
facdc750 | 1563 | |
8b264ecb AS |
1564 | DEC_BOTH (this_pos, this_pos_byte); |
1565 | PREV_CHAR_BOUNDARY (p, pat); | |
1566 | pat_ch = STRING_CHAR (p); | |
1567 | buf_ch = STRING_CHAR (BYTE_POS_ADDR (this_pos_byte)); | |
aff2ce94 | 1568 | TRANSLATE (buf_ch, trt, buf_ch); |
facdc750 RS |
1569 | |
1570 | if (buf_ch != pat_ch) | |
1571 | break; | |
ab228c24 | 1572 | |
ab228c24 | 1573 | this_len--; |
facdc750 RS |
1574 | } |
1575 | ||
1576 | if (this_len == 0) | |
1577 | { | |
8b264ecb AS |
1578 | match_byte = pos_byte - this_pos_byte; |
1579 | pos = this_pos; | |
1580 | pos_byte = this_pos_byte; | |
facdc750 RS |
1581 | break; |
1582 | } | |
1583 | ||
1584 | DEC_BOTH (pos, pos_byte); | |
f8bd51c4 KH |
1585 | } |
1586 | ||
facdc750 RS |
1587 | n++; |
1588 | } | |
1589 | else if (lim < pos) | |
1590 | while (n < 0) | |
1591 | { | |
1592 | while (1) | |
b6d6a51c | 1593 | { |
facdc750 | 1594 | /* Try matching at position POS. */ |
e7deaab0 | 1595 | EMACS_INT this_pos = pos - len; |
c098fdb8 | 1596 | EMACS_INT this_len = len; |
facdc750 RS |
1597 | unsigned char *p = pat; |
1598 | ||
a9469498 | 1599 | if (this_pos < lim) |
facdc750 RS |
1600 | goto stop; |
1601 | ||
1602 | while (this_len > 0) | |
1603 | { | |
1604 | int pat_ch = *p++; | |
1605 | int buf_ch = FETCH_BYTE (this_pos); | |
aff2ce94 | 1606 | TRANSLATE (buf_ch, trt, buf_ch); |
facdc750 RS |
1607 | |
1608 | if (buf_ch != pat_ch) | |
1609 | break; | |
ab228c24 RS |
1610 | this_len--; |
1611 | this_pos++; | |
facdc750 RS |
1612 | } |
1613 | ||
1614 | if (this_len == 0) | |
b6d6a51c | 1615 | { |
49e44e81 | 1616 | match_byte = len; |
facdc750 RS |
1617 | pos -= len; |
1618 | break; | |
b6d6a51c | 1619 | } |
facdc750 RS |
1620 | |
1621 | pos--; | |
b6d6a51c | 1622 | } |
facdc750 RS |
1623 | |
1624 | n++; | |
b6d6a51c | 1625 | } |
facdc750 RS |
1626 | |
1627 | stop: | |
1628 | if (n == 0) | |
aff2ce94 | 1629 | { |
ab228c24 | 1630 | if (forward) |
49e44e81 | 1631 | set_search_regs ((multibyte ? pos_byte : pos) - match_byte, match_byte); |
ab228c24 | 1632 | else |
49e44e81 | 1633 | set_search_regs (multibyte ? pos_byte : pos, match_byte); |
aff2ce94 RS |
1634 | |
1635 | return pos; | |
1636 | } | |
facdc750 RS |
1637 | else if (n > 0) |
1638 | return -n; | |
1639 | else | |
1640 | return n; | |
1641 | } | |
1642 | \f | |
0190922f | 1643 | /* Do Boyer-Moore search N times for the string BASE_PAT, |
facdc750 RS |
1644 | whose length is LEN/LEN_BYTE, |
1645 | from buffer position POS/POS_BYTE until LIM/LIM_BYTE. | |
1646 | DIRECTION says which direction we search in. | |
1647 | TRT and INVERSE_TRT are translation tables. | |
0190922f | 1648 | Characters in PAT are already translated by TRT. |
facdc750 | 1649 | |
0190922f KH |
1650 | This kind of search works if all the characters in BASE_PAT that |
1651 | have nontrivial translation are the same aside from the last byte. | |
1652 | This makes it possible to translate just the last byte of a | |
1653 | character, and do so after just a simple test of the context. | |
b2e6b10f | 1654 | CHAR_BASE is nonzero if there is such a non-ASCII character. |
facdc750 RS |
1655 | |
1656 | If that criterion is not satisfied, do not call this function. */ | |
1657 | ||
e7deaab0 | 1658 | static EMACS_INT |
c098fdb8 EZ |
1659 | boyer_moore (EMACS_INT n, unsigned char *base_pat, |
1660 | EMACS_INT len, EMACS_INT len_byte, | |
d5a3eaaf AS |
1661 | Lisp_Object trt, Lisp_Object inverse_trt, |
1662 | EMACS_INT pos, EMACS_INT pos_byte, | |
1663 | EMACS_INT lim, EMACS_INT lim_byte, int char_base) | |
facdc750 RS |
1664 | { |
1665 | int direction = ((n > 0) ? 1 : -1); | |
c098fdb8 | 1666 | register EMACS_INT dirlen; |
e7deaab0 AS |
1667 | EMACS_INT limit; |
1668 | int stride_for_teases = 0; | |
f4646fff | 1669 | int BM_tab[0400]; |
177c0ea7 | 1670 | register unsigned char *cursor, *p_limit; |
c098fdb8 EZ |
1671 | register EMACS_INT i; |
1672 | register int j; | |
cb6792d2 | 1673 | unsigned char *pat, *pat_end; |
4b4deea2 | 1674 | int multibyte = ! NILP (BVAR (current_buffer, enable_multibyte_characters)); |
facdc750 RS |
1675 | |
1676 | unsigned char simple_translate[0400]; | |
0190922f | 1677 | /* These are set to the preceding bytes of a byte to be translated |
49e44e81 | 1678 | if char_base is nonzero. As the maximum byte length of a |
a17ae96a | 1679 | multibyte character is 5, we have to check at most four previous |
0190922f KH |
1680 | bytes. */ |
1681 | int translate_prev_byte1 = 0; | |
1682 | int translate_prev_byte2 = 0; | |
1683 | int translate_prev_byte3 = 0; | |
a17ae96a | 1684 | int translate_prev_byte4 = 0; |
facdc750 | 1685 | |
f4646fff AS |
1686 | /* The general approach is that we are going to maintain that we know |
1687 | the first (closest to the present position, in whatever direction | |
1688 | we're searching) character that could possibly be the last | |
1689 | (furthest from present position) character of a valid match. We | |
1690 | advance the state of our knowledge by looking at that character | |
1691 | and seeing whether it indeed matches the last character of the | |
1692 | pattern. If it does, we take a closer look. If it does not, we | |
1693 | move our pointer (to putative last characters) as far as is | |
1694 | logically possible. This amount of movement, which I call a | |
1695 | stride, will be the length of the pattern if the actual character | |
1696 | appears nowhere in the pattern, otherwise it will be the distance | |
1697 | from the last occurrence of that character to the end of the | |
1698 | pattern. If the amount is zero we have a possible match. */ | |
1699 | ||
1700 | /* Here we make a "mickey mouse" BM table. The stride of the search | |
1701 | is determined only by the last character of the putative match. | |
1702 | If that character does not match, we will stride the proper | |
1703 | distance to propose a match that superimposes it on the last | |
1704 | instance of a character that matches it (per trt), or misses | |
1705 | it entirely if there is none. */ | |
facdc750 RS |
1706 | |
1707 | dirlen = len_byte * direction; | |
cb6792d2 RS |
1708 | |
1709 | /* Record position after the end of the pattern. */ | |
1710 | pat_end = base_pat + len_byte; | |
1711 | /* BASE_PAT points to a character that we start scanning from. | |
1712 | It is the first character in a forward search, | |
1713 | the last character in a backward search. */ | |
facdc750 | 1714 | if (direction < 0) |
cb6792d2 RS |
1715 | base_pat = pat_end - 1; |
1716 | ||
f4646fff AS |
1717 | /* A character that does not appear in the pattern induces a |
1718 | stride equal to the pattern length. */ | |
1719 | for (i = 0; i < 0400; i++) | |
1720 | BM_tab[i] = dirlen; | |
facdc750 RS |
1721 | |
1722 | /* We use this for translation, instead of TRT itself. | |
1723 | We fill this in to handle the characters that actually | |
1724 | occur in the pattern. Others don't matter anyway! */ | |
facdc750 RS |
1725 | for (i = 0; i < 0400; i++) |
1726 | simple_translate[i] = i; | |
1727 | ||
a17ae96a | 1728 | if (char_base) |
0190922f | 1729 | { |
a17ae96a | 1730 | /* Setup translate_prev_byte1/2/3/4 from CHAR_BASE. Only a |
0190922f | 1731 | byte following them are the target of translation. */ |
0190922f | 1732 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
a17ae96a | 1733 | int len = CHAR_STRING (char_base, str); |
0190922f KH |
1734 | |
1735 | translate_prev_byte1 = str[len - 2]; | |
1736 | if (len > 2) | |
1737 | { | |
1738 | translate_prev_byte2 = str[len - 3]; | |
1739 | if (len > 3) | |
a17ae96a KH |
1740 | { |
1741 | translate_prev_byte3 = str[len - 4]; | |
1742 | if (len > 4) | |
1743 | translate_prev_byte4 = str[len - 5]; | |
1744 | } | |
0190922f KH |
1745 | } |
1746 | } | |
1747 | ||
facdc750 | 1748 | i = 0; |
f4646fff | 1749 | while (i != dirlen) |
facdc750 | 1750 | { |
cb6792d2 | 1751 | unsigned char *ptr = base_pat + i; |
facdc750 | 1752 | i += direction; |
facdc750 | 1753 | if (! NILP (trt)) |
ca1d1d23 | 1754 | { |
49e44e81 KH |
1755 | /* If the byte currently looking at is the last of a |
1756 | character to check case-equivalents, set CH to that | |
1757 | character. An ASCII character and a non-ASCII character | |
1758 | matching with CHAR_BASE are to be checked. */ | |
a17ae96a KH |
1759 | int ch = -1; |
1760 | ||
1761 | if (ASCII_BYTE_P (*ptr) || ! multibyte) | |
1762 | ch = *ptr; | |
49e44e81 | 1763 | else if (char_base |
86dc6ccb | 1764 | && ((pat_end - ptr) == 1 || CHAR_HEAD_P (ptr[1]))) |
ca1d1d23 | 1765 | { |
49e44e81 KH |
1766 | unsigned char *charstart = ptr - 1; |
1767 | ||
1768 | while (! (CHAR_HEAD_P (*charstart))) | |
1769 | charstart--; | |
62a6e103 | 1770 | ch = STRING_CHAR (charstart); |
a17ae96a KH |
1771 | if (char_base != (ch & ~0x3F)) |
1772 | ch = -1; | |
ca1d1d23 | 1773 | } |
facdc750 | 1774 | |
f51995fa | 1775 | if (ch >= 0200) |
49e44e81 KH |
1776 | j = (ch & 0x3F) | 0200; |
1777 | else | |
1778 | j = *ptr; | |
1779 | ||
f4646fff | 1780 | if (i == dirlen) |
facdc750 | 1781 | stride_for_teases = BM_tab[j]; |
ab228c24 | 1782 | |
facdc750 RS |
1783 | BM_tab[j] = dirlen - i; |
1784 | /* A translation table is accompanied by its inverse -- see */ | |
177c0ea7 | 1785 | /* comment following downcase_table for details */ |
a17ae96a | 1786 | if (ch >= 0) |
ab228c24 RS |
1787 | { |
1788 | int starting_ch = ch; | |
49e44e81 | 1789 | int starting_j = j; |
a17ae96a | 1790 | |
ab228c24 RS |
1791 | while (1) |
1792 | { | |
1793 | TRANSLATE (ch, inverse_trt, ch); | |
f51995fa | 1794 | if (ch >= 0200) |
49e44e81 | 1795 | j = (ch & 0x3F) | 0200; |
ab228c24 | 1796 | else |
a17ae96a | 1797 | j = ch; |
ab228c24 RS |
1798 | |
1799 | /* For all the characters that map into CH, | |
1800 | set up simple_translate to map the last byte | |
1801 | into STARTING_J. */ | |
1802 | simple_translate[j] = starting_j; | |
1803 | if (ch == starting_ch) | |
1804 | break; | |
1805 | BM_tab[j] = dirlen - i; | |
1806 | } | |
1807 | } | |
facdc750 RS |
1808 | } |
1809 | else | |
1810 | { | |
1811 | j = *ptr; | |
1812 | ||
f4646fff | 1813 | if (i == dirlen) |
facdc750 RS |
1814 | stride_for_teases = BM_tab[j]; |
1815 | BM_tab[j] = dirlen - i; | |
ca1d1d23 | 1816 | } |
f4646fff AS |
1817 | /* stride_for_teases tells how much to stride if we get a |
1818 | match on the far character but are subsequently | |
1819 | disappointed, by recording what the stride would have been | |
1820 | for that character if the last character had been | |
1821 | different. */ | |
facdc750 | 1822 | } |
facdc750 RS |
1823 | pos_byte += dirlen - ((direction > 0) ? direction : 0); |
1824 | /* loop invariant - POS_BYTE points at where last char (first | |
1825 | char if reverse) of pattern would align in a possible match. */ | |
1826 | while (n != 0) | |
1827 | { | |
e7deaab0 | 1828 | EMACS_INT tail_end; |
facdc750 RS |
1829 | unsigned char *tail_end_ptr; |
1830 | ||
1831 | /* It's been reported that some (broken) compiler thinks that | |
1832 | Boolean expressions in an arithmetic context are unsigned. | |
1833 | Using an explicit ?1:0 prevents this. */ | |
1834 | if ((lim_byte - pos_byte - ((direction > 0) ? 1 : 0)) * direction | |
1835 | < 0) | |
1836 | return (n * (0 - direction)); | |
1837 | /* First we do the part we can by pointers (maybe nothing) */ | |
1838 | QUIT; | |
1839 | pat = base_pat; | |
1840 | limit = pos_byte - dirlen + direction; | |
67ce527d KH |
1841 | if (direction > 0) |
1842 | { | |
1843 | limit = BUFFER_CEILING_OF (limit); | |
1844 | /* LIMIT is now the last (not beyond-last!) value POS_BYTE | |
1845 | can take on without hitting edge of buffer or the gap. */ | |
1846 | limit = min (limit, pos_byte + 20000); | |
1847 | limit = min (limit, lim_byte - 1); | |
1848 | } | |
1849 | else | |
1850 | { | |
1851 | limit = BUFFER_FLOOR_OF (limit); | |
1852 | /* LIMIT is now the last (not beyond-last!) value POS_BYTE | |
1853 | can take on without hitting edge of buffer or the gap. */ | |
1854 | limit = max (limit, pos_byte - 20000); | |
1855 | limit = max (limit, lim_byte); | |
1856 | } | |
facdc750 RS |
1857 | tail_end = BUFFER_CEILING_OF (pos_byte) + 1; |
1858 | tail_end_ptr = BYTE_POS_ADDR (tail_end); | |
1859 | ||
1860 | if ((limit - pos_byte) * direction > 20) | |
ca1d1d23 | 1861 | { |
facdc750 RS |
1862 | unsigned char *p2; |
1863 | ||
1864 | p_limit = BYTE_POS_ADDR (limit); | |
1865 | p2 = (cursor = BYTE_POS_ADDR (pos_byte)); | |
f4646fff | 1866 | /* In this loop, pos + cursor - p2 is the surrogate for pos. */ |
facdc750 | 1867 | while (1) /* use one cursor setting as long as i can */ |
ca1d1d23 | 1868 | { |
facdc750 | 1869 | if (direction > 0) /* worth duplicating */ |
ca1d1d23 | 1870 | { |
f4646fff AS |
1871 | while (cursor <= p_limit) |
1872 | { | |
1873 | if (BM_tab[*cursor] == 0) | |
1874 | goto hit; | |
facdc750 | 1875 | cursor += BM_tab[*cursor]; |
f4646fff | 1876 | } |
facdc750 RS |
1877 | } |
1878 | else | |
1879 | { | |
f4646fff AS |
1880 | while (cursor >= p_limit) |
1881 | { | |
1882 | if (BM_tab[*cursor] == 0) | |
1883 | goto hit; | |
facdc750 | 1884 | cursor += BM_tab[*cursor]; |
f4646fff | 1885 | } |
facdc750 | 1886 | } |
f4646fff AS |
1887 | /* If you are here, cursor is beyond the end of the |
1888 | searched region. You fail to match within the | |
1889 | permitted region and would otherwise try a character | |
1890 | beyond that region. */ | |
1891 | break; | |
1892 | ||
1893 | hit: | |
facdc750 RS |
1894 | i = dirlen - direction; |
1895 | if (! NILP (trt)) | |
1896 | { | |
1897 | while ((i -= direction) + direction != 0) | |
ca1d1d23 | 1898 | { |
facdc750 RS |
1899 | int ch; |
1900 | cursor -= direction; | |
1901 | /* Translate only the last byte of a character. */ | |
1902 | if (! multibyte | |
1903 | || ((cursor == tail_end_ptr | |
1904 | || CHAR_HEAD_P (cursor[1])) | |
1905 | && (CHAR_HEAD_P (cursor[0]) | |
0190922f KH |
1906 | /* Check if this is the last byte of |
1907 | a translable character. */ | |
1908 | || (translate_prev_byte1 == cursor[-1] | |
1909 | && (CHAR_HEAD_P (translate_prev_byte1) | |
1910 | || (translate_prev_byte2 == cursor[-2] | |
1911 | && (CHAR_HEAD_P (translate_prev_byte2) | |
1912 | || (translate_prev_byte3 == cursor[-3])))))))) | |
facdc750 RS |
1913 | ch = simple_translate[*cursor]; |
1914 | else | |
1915 | ch = *cursor; | |
1916 | if (pat[i] != ch) | |
1917 | break; | |
ca1d1d23 | 1918 | } |
facdc750 RS |
1919 | } |
1920 | else | |
1921 | { | |
1922 | while ((i -= direction) + direction != 0) | |
ca1d1d23 | 1923 | { |
facdc750 RS |
1924 | cursor -= direction; |
1925 | if (pat[i] != *cursor) | |
1926 | break; | |
ca1d1d23 | 1927 | } |
facdc750 RS |
1928 | } |
1929 | cursor += dirlen - i - direction; /* fix cursor */ | |
1930 | if (i + direction == 0) | |
1931 | { | |
e7deaab0 | 1932 | EMACS_INT position, start, end; |
0c8533c6 | 1933 | |
facdc750 | 1934 | cursor -= direction; |
1113d9db | 1935 | |
facdc750 RS |
1936 | position = pos_byte + cursor - p2 + ((direction > 0) |
1937 | ? 1 - len_byte : 0); | |
1938 | set_search_regs (position, len_byte); | |
ca325161 | 1939 | |
ef887810 RS |
1940 | if (NILP (Vinhibit_changing_match_data)) |
1941 | { | |
1942 | start = search_regs.start[0]; | |
1943 | end = search_regs.end[0]; | |
1944 | } | |
1945 | else | |
1946 | /* If Vinhibit_changing_match_data is non-nil, | |
1947 | search_regs will not be changed. So let's | |
1948 | compute start and end here. */ | |
1949 | { | |
1950 | start = BYTE_TO_CHAR (position); | |
1951 | end = BYTE_TO_CHAR (position + len_byte); | |
1952 | } | |
1953 | ||
facdc750 RS |
1954 | if ((n -= direction) != 0) |
1955 | cursor += dirlen; /* to resume search */ | |
ca1d1d23 | 1956 | else |
ef887810 | 1957 | return direction > 0 ? end : start; |
ca1d1d23 | 1958 | } |
facdc750 RS |
1959 | else |
1960 | cursor += stride_for_teases; /* <sigh> we lose - */ | |
ca1d1d23 | 1961 | } |
facdc750 RS |
1962 | pos_byte += cursor - p2; |
1963 | } | |
1964 | else | |
f4646fff AS |
1965 | /* Now we'll pick up a clump that has to be done the hard |
1966 | way because it covers a discontinuity. */ | |
facdc750 RS |
1967 | { |
1968 | limit = ((direction > 0) | |
1969 | ? BUFFER_CEILING_OF (pos_byte - dirlen + 1) | |
1970 | : BUFFER_FLOOR_OF (pos_byte - dirlen - 1)); | |
1971 | limit = ((direction > 0) | |
1972 | ? min (limit + len_byte, lim_byte - 1) | |
1973 | : max (limit - len_byte, lim_byte)); | |
1974 | /* LIMIT is now the last value POS_BYTE can have | |
1975 | and still be valid for a possible match. */ | |
1976 | while (1) | |
ca1d1d23 | 1977 | { |
f4646fff AS |
1978 | /* This loop can be coded for space rather than |
1979 | speed because it will usually run only once. | |
1980 | (the reach is at most len + 21, and typically | |
1981 | does not exceed len). */ | |
facdc750 | 1982 | while ((limit - pos_byte) * direction >= 0) |
f4646fff AS |
1983 | { |
1984 | int ch = FETCH_BYTE (pos_byte); | |
1985 | if (BM_tab[ch] == 0) | |
1986 | goto hit2; | |
1987 | pos_byte += BM_tab[ch]; | |
1988 | } | |
1989 | break; /* ran off the end */ | |
1990 | ||
1991 | hit2: | |
1992 | /* Found what might be a match. */ | |
facdc750 RS |
1993 | i = dirlen - direction; |
1994 | while ((i -= direction) + direction != 0) | |
ca1d1d23 | 1995 | { |
facdc750 RS |
1996 | int ch; |
1997 | unsigned char *ptr; | |
1998 | pos_byte -= direction; | |
1999 | ptr = BYTE_POS_ADDR (pos_byte); | |
2000 | /* Translate only the last byte of a character. */ | |
2001 | if (! multibyte | |
2002 | || ((ptr == tail_end_ptr | |
2003 | || CHAR_HEAD_P (ptr[1])) | |
2004 | && (CHAR_HEAD_P (ptr[0]) | |
0190922f KH |
2005 | /* Check if this is the last byte of a |
2006 | translable character. */ | |
2007 | || (translate_prev_byte1 == ptr[-1] | |
2008 | && (CHAR_HEAD_P (translate_prev_byte1) | |
2009 | || (translate_prev_byte2 == ptr[-2] | |
2010 | && (CHAR_HEAD_P (translate_prev_byte2) | |
2011 | || translate_prev_byte3 == ptr[-3]))))))) | |
facdc750 RS |
2012 | ch = simple_translate[*ptr]; |
2013 | else | |
2014 | ch = *ptr; | |
2015 | if (pat[i] != ch) | |
2016 | break; | |
2017 | } | |
2018 | /* Above loop has moved POS_BYTE part or all the way | |
2019 | back to the first pos (last pos if reverse). | |
2020 | Set it once again at the last (first if reverse) char. */ | |
f4646fff | 2021 | pos_byte += dirlen - i - direction; |
facdc750 RS |
2022 | if (i + direction == 0) |
2023 | { | |
e7deaab0 | 2024 | EMACS_INT position, start, end; |
facdc750 | 2025 | pos_byte -= direction; |
1113d9db | 2026 | |
facdc750 | 2027 | position = pos_byte + ((direction > 0) ? 1 - len_byte : 0); |
facdc750 | 2028 | set_search_regs (position, len_byte); |
ca325161 | 2029 | |
ef887810 RS |
2030 | if (NILP (Vinhibit_changing_match_data)) |
2031 | { | |
2032 | start = search_regs.start[0]; | |
2033 | end = search_regs.end[0]; | |
2034 | } | |
2035 | else | |
2036 | /* If Vinhibit_changing_match_data is non-nil, | |
2037 | search_regs will not be changed. So let's | |
2038 | compute start and end here. */ | |
2039 | { | |
2040 | start = BYTE_TO_CHAR (position); | |
2041 | end = BYTE_TO_CHAR (position + len_byte); | |
2042 | } | |
2043 | ||
facdc750 RS |
2044 | if ((n -= direction) != 0) |
2045 | pos_byte += dirlen; /* to resume search */ | |
ca1d1d23 | 2046 | else |
ef887810 | 2047 | return direction > 0 ? end : start; |
ca1d1d23 | 2048 | } |
facdc750 RS |
2049 | else |
2050 | pos_byte += stride_for_teases; | |
2051 | } | |
2052 | } | |
2053 | /* We have done one clump. Can we continue? */ | |
2054 | if ((lim_byte - pos_byte) * direction < 0) | |
2055 | return ((0 - n) * direction); | |
ca1d1d23 | 2056 | } |
facdc750 | 2057 | return BYTE_TO_CHAR (pos_byte); |
ca1d1d23 | 2058 | } |
ca325161 | 2059 | |
fa8ed3e0 | 2060 | /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES |
a7e4cdde RS |
2061 | for the overall match just found in the current buffer. |
2062 | Also clear out the match data for registers 1 and up. */ | |
ca325161 RS |
2063 | |
2064 | static void | |
971de7fb | 2065 | set_search_regs (EMACS_INT beg_byte, EMACS_INT nbytes) |
ca325161 | 2066 | { |
a7e4cdde RS |
2067 | int i; |
2068 | ||
ef887810 RS |
2069 | if (!NILP (Vinhibit_changing_match_data)) |
2070 | return; | |
2071 | ||
ca325161 RS |
2072 | /* Make sure we have registers in which to store |
2073 | the match position. */ | |
2074 | if (search_regs.num_regs == 0) | |
2075 | { | |
2d4a771a RS |
2076 | search_regs.start = (regoff_t *) xmalloc (2 * sizeof (regoff_t)); |
2077 | search_regs.end = (regoff_t *) xmalloc (2 * sizeof (regoff_t)); | |
487282dc | 2078 | search_regs.num_regs = 2; |
ca325161 RS |
2079 | } |
2080 | ||
a7e4cdde RS |
2081 | /* Clear out the other registers. */ |
2082 | for (i = 1; i < search_regs.num_regs; i++) | |
2083 | { | |
2084 | search_regs.start[i] = -1; | |
2085 | search_regs.end[i] = -1; | |
2086 | } | |
2087 | ||
fa8ed3e0 RS |
2088 | search_regs.start[0] = BYTE_TO_CHAR (beg_byte); |
2089 | search_regs.end[0] = BYTE_TO_CHAR (beg_byte + nbytes); | |
a3668d92 | 2090 | XSETBUFFER (last_thing_searched, current_buffer); |
ca325161 | 2091 | } |
ca1d1d23 | 2092 | \f |
c6e0e1cc CY |
2093 | /* Given STRING, a string of words separated by word delimiters, |
2094 | compute a regexp that matches those exact words separated by | |
2095 | arbitrary punctuation. If LAX is nonzero, the end of the string | |
2096 | need not match a word boundary unless it ends in whitespace. */ | |
ca1d1d23 JB |
2097 | |
2098 | static Lisp_Object | |
971de7fb | 2099 | wordify (Lisp_Object string, int lax) |
ca1d1d23 JB |
2100 | { |
2101 | register unsigned char *p, *o; | |
c098fdb8 | 2102 | register EMACS_INT i, i_byte, len, punct_count = 0, word_count = 0; |
ca1d1d23 | 2103 | Lisp_Object val; |
0c8533c6 | 2104 | int prev_c = 0; |
c098fdb8 EZ |
2105 | EMACS_INT adjust; |
2106 | int whitespace_at_end; | |
ca1d1d23 | 2107 | |
b7826503 | 2108 | CHECK_STRING (string); |
d5db4077 KR |
2109 | p = SDATA (string); |
2110 | len = SCHARS (string); | |
ca1d1d23 | 2111 | |
0c8533c6 RS |
2112 | for (i = 0, i_byte = 0; i < len; ) |
2113 | { | |
2114 | int c; | |
177c0ea7 | 2115 | |
93daa011 | 2116 | FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c, string, i, i_byte); |
0c8533c6 RS |
2117 | |
2118 | if (SYNTAX (c) != Sword) | |
2119 | { | |
2120 | punct_count++; | |
2121 | if (i > 0 && SYNTAX (prev_c) == Sword) | |
2122 | word_count++; | |
2123 | } | |
ca1d1d23 | 2124 | |
0c8533c6 RS |
2125 | prev_c = c; |
2126 | } | |
2127 | ||
2128 | if (SYNTAX (prev_c) == Sword) | |
c6e0e1cc CY |
2129 | { |
2130 | word_count++; | |
2131 | whitespace_at_end = 0; | |
2132 | } | |
2133 | else | |
2134 | whitespace_at_end = 1; | |
2135 | ||
0c8533c6 | 2136 | if (!word_count) |
c60416e0 | 2137 | return empty_unibyte_string; |
0c8533c6 | 2138 | |
c6e0e1cc CY |
2139 | adjust = - punct_count + 5 * (word_count - 1) |
2140 | + ((lax && !whitespace_at_end) ? 2 : 4); | |
8a2df937 RS |
2141 | if (STRING_MULTIBYTE (string)) |
2142 | val = make_uninit_multibyte_string (len + adjust, | |
d5db4077 | 2143 | SBYTES (string) |
8a2df937 RS |
2144 | + adjust); |
2145 | else | |
2146 | val = make_uninit_string (len + adjust); | |
ca1d1d23 | 2147 | |
d5db4077 | 2148 | o = SDATA (val); |
ca1d1d23 JB |
2149 | *o++ = '\\'; |
2150 | *o++ = 'b'; | |
1e9582d4 | 2151 | prev_c = 0; |
ca1d1d23 | 2152 | |
1e9582d4 RS |
2153 | for (i = 0, i_byte = 0; i < len; ) |
2154 | { | |
2155 | int c; | |
c098fdb8 | 2156 | EMACS_INT i_byte_orig = i_byte; |
177c0ea7 | 2157 | |
93daa011 | 2158 | FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c, string, i, i_byte); |
1e9582d4 RS |
2159 | |
2160 | if (SYNTAX (c) == Sword) | |
2161 | { | |
72af86bd | 2162 | memcpy (o, SDATA (string) + i_byte_orig, i_byte - i_byte_orig); |
1e9582d4 RS |
2163 | o += i_byte - i_byte_orig; |
2164 | } | |
2165 | else if (i > 0 && SYNTAX (prev_c) == Sword && --word_count) | |
2166 | { | |
2167 | *o++ = '\\'; | |
2168 | *o++ = 'W'; | |
2169 | *o++ = '\\'; | |
2170 | *o++ = 'W'; | |
2171 | *o++ = '*'; | |
2172 | } | |
2173 | ||
2174 | prev_c = c; | |
2175 | } | |
ca1d1d23 | 2176 | |
c6e0e1cc CY |
2177 | if (!lax || whitespace_at_end) |
2178 | { | |
2179 | *o++ = '\\'; | |
2180 | *o++ = 'b'; | |
2181 | } | |
ca1d1d23 JB |
2182 | |
2183 | return val; | |
2184 | } | |
2185 | \f | |
2186 | DEFUN ("search-backward", Fsearch_backward, Ssearch_backward, 1, 4, | |
8c1a1077 PJ |
2187 | "MSearch backward: ", |
2188 | doc: /* Search backward from point for STRING. | |
2189 | Set point to the beginning of the occurrence found, and return point. | |
2190 | An optional second argument bounds the search; it is a buffer position. | |
2191 | The match found must not extend before that position. | |
2192 | Optional third argument, if t, means if fail just return nil (no error). | |
2193 | If not nil and not t, position at limit of search and return nil. | |
2194 | Optional fourth argument is repeat count--search for successive occurrences. | |
2195 | ||
2196 | Search case-sensitivity is determined by the value of the variable | |
2197 | `case-fold-search', which see. | |
2198 | ||
2199 | See also the functions `match-beginning', `match-end' and `replace-match'. */) | |
5842a27b | 2200 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
ca1d1d23 | 2201 | { |
b819a390 | 2202 | return search_command (string, bound, noerror, count, -1, 0, 0); |
ca1d1d23 JB |
2203 | } |
2204 | ||
6af43974 | 2205 | DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "MSearch: ", |
8c1a1077 PJ |
2206 | doc: /* Search forward from point for STRING. |
2207 | Set point to the end of the occurrence found, and return point. | |
2208 | An optional second argument bounds the search; it is a buffer position. | |
d6aacdcd | 2209 | The match found must not extend after that position. A value of nil is |
48740f01 | 2210 | equivalent to (point-max). |
8c1a1077 PJ |
2211 | Optional third argument, if t, means if fail just return nil (no error). |
2212 | If not nil and not t, move to limit of search and return nil. | |
2213 | Optional fourth argument is repeat count--search for successive occurrences. | |
2214 | ||
2215 | Search case-sensitivity is determined by the value of the variable | |
2216 | `case-fold-search', which see. | |
2217 | ||
2218 | See also the functions `match-beginning', `match-end' and `replace-match'. */) | |
5842a27b | 2219 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
ca1d1d23 | 2220 | { |
b819a390 | 2221 | return search_command (string, bound, noerror, count, 1, 0, 0); |
ca1d1d23 JB |
2222 | } |
2223 | ||
2224 | DEFUN ("word-search-backward", Fword_search_backward, Sword_search_backward, 1, 4, | |
8c1a1077 PJ |
2225 | "sWord search backward: ", |
2226 | doc: /* Search backward from point for STRING, ignoring differences in punctuation. | |
2227 | Set point to the beginning of the occurrence found, and return point. | |
2228 | An optional second argument bounds the search; it is a buffer position. | |
2229 | The match found must not extend before that position. | |
2230 | Optional third argument, if t, means if fail just return nil (no error). | |
2231 | If not nil and not t, move to limit of search and return nil. | |
2232 | Optional fourth argument is repeat count--search for successive occurrences. */) | |
5842a27b | 2233 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
ca1d1d23 | 2234 | { |
c6e0e1cc | 2235 | return search_command (wordify (string, 0), bound, noerror, count, -1, 1, 0); |
ca1d1d23 JB |
2236 | } |
2237 | ||
2238 | DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4, | |
8c1a1077 PJ |
2239 | "sWord search: ", |
2240 | doc: /* Search forward from point for STRING, ignoring differences in punctuation. | |
2241 | Set point to the end of the occurrence found, and return point. | |
2242 | An optional second argument bounds the search; it is a buffer position. | |
2243 | The match found must not extend after that position. | |
2244 | Optional third argument, if t, means if fail just return nil (no error). | |
2245 | If not nil and not t, move to limit of search and return nil. | |
2246 | Optional fourth argument is repeat count--search for successive occurrences. */) | |
5842a27b | 2247 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
ca1d1d23 | 2248 | { |
c6e0e1cc CY |
2249 | return search_command (wordify (string, 0), bound, noerror, count, 1, 1, 0); |
2250 | } | |
2251 | ||
2252 | DEFUN ("word-search-backward-lax", Fword_search_backward_lax, Sword_search_backward_lax, 1, 4, | |
2253 | "sWord search backward: ", | |
2254 | doc: /* Search backward from point for STRING, ignoring differences in punctuation. | |
2255 | Set point to the beginning of the occurrence found, and return point. | |
2256 | ||
2257 | Unlike `word-search-backward', the end of STRING need not match a word | |
2258 | boundary unless it ends in whitespace. | |
2259 | ||
2260 | An optional second argument bounds the search; it is a buffer position. | |
2261 | The match found must not extend before that position. | |
2262 | Optional third argument, if t, means if fail just return nil (no error). | |
2263 | If not nil and not t, move to limit of search and return nil. | |
2264 | Optional fourth argument is repeat count--search for successive occurrences. */) | |
5842a27b | 2265 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
c6e0e1cc CY |
2266 | { |
2267 | return search_command (wordify (string, 1), bound, noerror, count, -1, 1, 0); | |
2268 | } | |
2269 | ||
2270 | DEFUN ("word-search-forward-lax", Fword_search_forward_lax, Sword_search_forward_lax, 1, 4, | |
2271 | "sWord search: ", | |
2272 | doc: /* Search forward from point for STRING, ignoring differences in punctuation. | |
2273 | Set point to the end of the occurrence found, and return point. | |
2274 | ||
2275 | Unlike `word-search-forward', the end of STRING need not match a word | |
2276 | boundary unless it ends in whitespace. | |
2277 | ||
2278 | An optional second argument bounds the search; it is a buffer position. | |
2279 | The match found must not extend after that position. | |
2280 | Optional third argument, if t, means if fail just return nil (no error). | |
2281 | If not nil and not t, move to limit of search and return nil. | |
2282 | Optional fourth argument is repeat count--search for successive occurrences. */) | |
5842a27b | 2283 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
c6e0e1cc CY |
2284 | { |
2285 | return search_command (wordify (string, 1), bound, noerror, count, 1, 1, 0); | |
ca1d1d23 JB |
2286 | } |
2287 | ||
2288 | DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4, | |
8c1a1077 PJ |
2289 | "sRE search backward: ", |
2290 | doc: /* Search backward from point for match for regular expression REGEXP. | |
2291 | Set point to the beginning of the match, and return point. | |
2292 | The match found is the one starting last in the buffer | |
2293 | and yet ending before the origin of the search. | |
2294 | An optional second argument bounds the search; it is a buffer position. | |
2295 | The match found must start at or after that position. | |
2296 | Optional third argument, if t, means if fail just return nil (no error). | |
2297 | If not nil and not t, move to limit of search and return nil. | |
2298 | Optional fourth argument is repeat count--search for successive occurrences. | |
2299 | See also the functions `match-beginning', `match-end', `match-string', | |
2300 | and `replace-match'. */) | |
5842a27b | 2301 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
ca1d1d23 | 2302 | { |
b819a390 | 2303 | return search_command (regexp, bound, noerror, count, -1, 1, 0); |
ca1d1d23 JB |
2304 | } |
2305 | ||
2306 | DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4, | |
8c1a1077 PJ |
2307 | "sRE search: ", |
2308 | doc: /* Search forward from point for regular expression REGEXP. | |
2309 | Set point to the end of the occurrence found, and return point. | |
2310 | An optional second argument bounds the search; it is a buffer position. | |
2311 | The match found must not extend after that position. | |
2312 | Optional third argument, if t, means if fail just return nil (no error). | |
2313 | If not nil and not t, move to limit of search and return nil. | |
2314 | Optional fourth argument is repeat count--search for successive occurrences. | |
2315 | See also the functions `match-beginning', `match-end', `match-string', | |
2316 | and `replace-match'. */) | |
5842a27b | 2317 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
ca1d1d23 | 2318 | { |
b819a390 RS |
2319 | return search_command (regexp, bound, noerror, count, 1, 1, 0); |
2320 | } | |
2321 | ||
2322 | DEFUN ("posix-search-backward", Fposix_search_backward, Sposix_search_backward, 1, 4, | |
8c1a1077 PJ |
2323 | "sPosix search backward: ", |
2324 | doc: /* Search backward from point for match for regular expression REGEXP. | |
2325 | Find the longest match in accord with Posix regular expression rules. | |
2326 | Set point to the beginning of the match, and return point. | |
2327 | The match found is the one starting last in the buffer | |
2328 | and yet ending before the origin of the search. | |
2329 | An optional second argument bounds the search; it is a buffer position. | |
2330 | The match found must start at or after that position. | |
2331 | Optional third argument, if t, means if fail just return nil (no error). | |
2332 | If not nil and not t, move to limit of search and return nil. | |
2333 | Optional fourth argument is repeat count--search for successive occurrences. | |
2334 | See also the functions `match-beginning', `match-end', `match-string', | |
2335 | and `replace-match'. */) | |
5842a27b | 2336 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
b819a390 RS |
2337 | { |
2338 | return search_command (regexp, bound, noerror, count, -1, 1, 1); | |
2339 | } | |
2340 | ||
2341 | DEFUN ("posix-search-forward", Fposix_search_forward, Sposix_search_forward, 1, 4, | |
8c1a1077 PJ |
2342 | "sPosix search: ", |
2343 | doc: /* Search forward from point for regular expression REGEXP. | |
2344 | Find the longest match in accord with Posix regular expression rules. | |
2345 | Set point to the end of the occurrence found, and return point. | |
2346 | An optional second argument bounds the search; it is a buffer position. | |
2347 | The match found must not extend after that position. | |
2348 | Optional third argument, if t, means if fail just return nil (no error). | |
2349 | If not nil and not t, move to limit of search and return nil. | |
2350 | Optional fourth argument is repeat count--search for successive occurrences. | |
2351 | See also the functions `match-beginning', `match-end', `match-string', | |
2352 | and `replace-match'. */) | |
5842a27b | 2353 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
b819a390 RS |
2354 | { |
2355 | return search_command (regexp, bound, noerror, count, 1, 1, 1); | |
ca1d1d23 JB |
2356 | } |
2357 | \f | |
d7a5ad5f | 2358 | DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0, |
8c1a1077 | 2359 | doc: /* Replace text matched by last search with NEWTEXT. |
4dd0c271 RS |
2360 | Leave point at the end of the replacement text. |
2361 | ||
8c1a1077 PJ |
2362 | If second arg FIXEDCASE is non-nil, do not alter case of replacement text. |
2363 | Otherwise maybe capitalize the whole text, or maybe just word initials, | |
2364 | based on the replaced text. | |
2365 | If the replaced text has only capital letters | |
2366 | and has at least one multiletter word, convert NEWTEXT to all caps. | |
4dd0c271 RS |
2367 | Otherwise if all words are capitalized in the replaced text, |
2368 | capitalize each word in NEWTEXT. | |
8c1a1077 PJ |
2369 | |
2370 | If third arg LITERAL is non-nil, insert NEWTEXT literally. | |
2371 | Otherwise treat `\\' as special: | |
2372 | `\\&' in NEWTEXT means substitute original matched text. | |
2373 | `\\N' means substitute what matched the Nth `\\(...\\)'. | |
2374 | If Nth parens didn't match, substitute nothing. | |
2375 | `\\\\' means insert one `\\'. | |
4dd0c271 RS |
2376 | Case conversion does not apply to these substitutions. |
2377 | ||
8c1a1077 | 2378 | FIXEDCASE and LITERAL are optional arguments. |
8c1a1077 PJ |
2379 | |
2380 | The optional fourth argument STRING can be a string to modify. | |
2381 | This is meaningful when the previous match was done against STRING, | |
2382 | using `string-match'. When used this way, `replace-match' | |
2383 | creates and returns a new string made by copying STRING and replacing | |
2384 | the part of STRING that was matched. | |
2385 | ||
2386 | The optional fifth argument SUBEXP specifies a subexpression; | |
2387 | it says to replace just that subexpression with NEWTEXT, | |
2388 | rather than replacing the entire matched text. | |
2389 | This is, in a vague sense, the inverse of using `\\N' in NEWTEXT; | |
2390 | `\\N' copies subexp N into NEWTEXT, but using N as SUBEXP puts | |
2391 | NEWTEXT in place of subexp N. | |
2392 | This is useful only after a regular expression search or match, | |
2393 | since only regular expressions have distinguished subexpressions. */) | |
5842a27b | 2394 | (Lisp_Object newtext, Lisp_Object fixedcase, Lisp_Object literal, Lisp_Object string, Lisp_Object subexp) |
ca1d1d23 JB |
2395 | { |
2396 | enum { nochange, all_caps, cap_initial } case_action; | |
c098fdb8 | 2397 | register EMACS_INT pos, pos_byte; |
ca1d1d23 | 2398 | int some_multiletter_word; |
97832bd0 | 2399 | int some_lowercase; |
73dc8771 | 2400 | int some_uppercase; |
208767c3 | 2401 | int some_nonuppercase_initial; |
ca1d1d23 | 2402 | register int c, prevc; |
d7a5ad5f | 2403 | int sub; |
e7deaab0 | 2404 | EMACS_INT opoint, newpoint; |
ca1d1d23 | 2405 | |
b7826503 | 2406 | CHECK_STRING (newtext); |
ca1d1d23 | 2407 | |
080c45fd | 2408 | if (! NILP (string)) |
b7826503 | 2409 | CHECK_STRING (string); |
080c45fd | 2410 | |
ca1d1d23 JB |
2411 | case_action = nochange; /* We tried an initialization */ |
2412 | /* but some C compilers blew it */ | |
4746118a JB |
2413 | |
2414 | if (search_regs.num_regs <= 0) | |
d72cdbfc | 2415 | error ("`replace-match' called before any match found"); |
4746118a | 2416 | |
d7a5ad5f RS |
2417 | if (NILP (subexp)) |
2418 | sub = 0; | |
2419 | else | |
2420 | { | |
b7826503 | 2421 | CHECK_NUMBER (subexp); |
d7a5ad5f RS |
2422 | sub = XINT (subexp); |
2423 | if (sub < 0 || sub >= search_regs.num_regs) | |
2424 | args_out_of_range (subexp, make_number (search_regs.num_regs)); | |
2425 | } | |
2426 | ||
080c45fd RS |
2427 | if (NILP (string)) |
2428 | { | |
d7a5ad5f RS |
2429 | if (search_regs.start[sub] < BEGV |
2430 | || search_regs.start[sub] > search_regs.end[sub] | |
2431 | || search_regs.end[sub] > ZV) | |
2432 | args_out_of_range (make_number (search_regs.start[sub]), | |
2433 | make_number (search_regs.end[sub])); | |
080c45fd RS |
2434 | } |
2435 | else | |
2436 | { | |
d7a5ad5f RS |
2437 | if (search_regs.start[sub] < 0 |
2438 | || search_regs.start[sub] > search_regs.end[sub] | |
d5db4077 | 2439 | || search_regs.end[sub] > SCHARS (string)) |
d7a5ad5f RS |
2440 | args_out_of_range (make_number (search_regs.start[sub]), |
2441 | make_number (search_regs.end[sub])); | |
080c45fd | 2442 | } |
ca1d1d23 JB |
2443 | |
2444 | if (NILP (fixedcase)) | |
2445 | { | |
2446 | /* Decide how to casify by examining the matched text. */ | |
e7deaab0 | 2447 | EMACS_INT last; |
ca1d1d23 | 2448 | |
ac3b28b1 KH |
2449 | pos = search_regs.start[sub]; |
2450 | last = search_regs.end[sub]; | |
fa8ed3e0 RS |
2451 | |
2452 | if (NILP (string)) | |
ac3b28b1 | 2453 | pos_byte = CHAR_TO_BYTE (pos); |
fa8ed3e0 | 2454 | else |
ac3b28b1 | 2455 | pos_byte = string_char_to_byte (string, pos); |
fa8ed3e0 | 2456 | |
ca1d1d23 JB |
2457 | prevc = '\n'; |
2458 | case_action = all_caps; | |
2459 | ||
2460 | /* some_multiletter_word is set nonzero if any original word | |
2461 | is more than one letter long. */ | |
2462 | some_multiletter_word = 0; | |
97832bd0 | 2463 | some_lowercase = 0; |
208767c3 | 2464 | some_nonuppercase_initial = 0; |
73dc8771 | 2465 | some_uppercase = 0; |
ca1d1d23 | 2466 | |
ac3b28b1 | 2467 | while (pos < last) |
ca1d1d23 | 2468 | { |
080c45fd | 2469 | if (NILP (string)) |
ac3b28b1 | 2470 | { |
93daa011 | 2471 | c = FETCH_CHAR_AS_MULTIBYTE (pos_byte); |
ac3b28b1 KH |
2472 | INC_BOTH (pos, pos_byte); |
2473 | } | |
080c45fd | 2474 | else |
93daa011 | 2475 | FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c, string, pos, pos_byte); |
080c45fd | 2476 | |
ca1d1d23 JB |
2477 | if (LOWERCASEP (c)) |
2478 | { | |
2479 | /* Cannot be all caps if any original char is lower case */ | |
2480 | ||
97832bd0 | 2481 | some_lowercase = 1; |
ca1d1d23 | 2482 | if (SYNTAX (prevc) != Sword) |
208767c3 | 2483 | some_nonuppercase_initial = 1; |
ca1d1d23 JB |
2484 | else |
2485 | some_multiletter_word = 1; | |
2486 | } | |
d16c2b66 | 2487 | else if (UPPERCASEP (c)) |
ca1d1d23 | 2488 | { |
73dc8771 | 2489 | some_uppercase = 1; |
97832bd0 | 2490 | if (SYNTAX (prevc) != Sword) |
c4d460ce | 2491 | ; |
97832bd0 | 2492 | else |
ca1d1d23 JB |
2493 | some_multiletter_word = 1; |
2494 | } | |
208767c3 RS |
2495 | else |
2496 | { | |
2497 | /* If the initial is a caseless word constituent, | |
2498 | treat that like a lowercase initial. */ | |
2499 | if (SYNTAX (prevc) != Sword) | |
2500 | some_nonuppercase_initial = 1; | |
2501 | } | |
ca1d1d23 JB |
2502 | |
2503 | prevc = c; | |
2504 | } | |
2505 | ||
97832bd0 RS |
2506 | /* Convert to all caps if the old text is all caps |
2507 | and has at least one multiletter word. */ | |
2508 | if (! some_lowercase && some_multiletter_word) | |
2509 | case_action = all_caps; | |
c4d460ce | 2510 | /* Capitalize each word, if the old text has all capitalized words. */ |
208767c3 | 2511 | else if (!some_nonuppercase_initial && some_multiletter_word) |
ca1d1d23 | 2512 | case_action = cap_initial; |
208767c3 | 2513 | else if (!some_nonuppercase_initial && some_uppercase) |
73dc8771 KH |
2514 | /* Should x -> yz, operating on X, give Yz or YZ? |
2515 | We'll assume the latter. */ | |
2516 | case_action = all_caps; | |
97832bd0 RS |
2517 | else |
2518 | case_action = nochange; | |
ca1d1d23 JB |
2519 | } |
2520 | ||
080c45fd RS |
2521 | /* Do replacement in a string. */ |
2522 | if (!NILP (string)) | |
2523 | { | |
2524 | Lisp_Object before, after; | |
2525 | ||
2526 | before = Fsubstring (string, make_number (0), | |
d7a5ad5f RS |
2527 | make_number (search_regs.start[sub])); |
2528 | after = Fsubstring (string, make_number (search_regs.end[sub]), Qnil); | |
080c45fd | 2529 | |
636a5e28 RS |
2530 | /* Substitute parts of the match into NEWTEXT |
2531 | if desired. */ | |
080c45fd RS |
2532 | if (NILP (literal)) |
2533 | { | |
e7deaab0 AS |
2534 | EMACS_INT lastpos = 0; |
2535 | EMACS_INT lastpos_byte = 0; | |
080c45fd RS |
2536 | /* We build up the substituted string in ACCUM. */ |
2537 | Lisp_Object accum; | |
2538 | Lisp_Object middle; | |
d5db4077 | 2539 | int length = SBYTES (newtext); |
080c45fd RS |
2540 | |
2541 | accum = Qnil; | |
2542 | ||
ac3b28b1 | 2543 | for (pos_byte = 0, pos = 0; pos_byte < length;) |
080c45fd | 2544 | { |
c098fdb8 EZ |
2545 | EMACS_INT substart = -1; |
2546 | EMACS_INT subend = 0; | |
1e79ec24 | 2547 | int delbackslash = 0; |
080c45fd | 2548 | |
0c8533c6 RS |
2549 | FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte); |
2550 | ||
080c45fd RS |
2551 | if (c == '\\') |
2552 | { | |
0c8533c6 | 2553 | FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte); |
177c0ea7 | 2554 | |
080c45fd RS |
2555 | if (c == '&') |
2556 | { | |
d7a5ad5f RS |
2557 | substart = search_regs.start[sub]; |
2558 | subend = search_regs.end[sub]; | |
080c45fd | 2559 | } |
76fc1ea2 | 2560 | else if (c >= '1' && c <= '9') |
080c45fd | 2561 | { |
76fc1ea2 KH |
2562 | if (search_regs.start[c - '0'] >= 0 |
2563 | && c <= search_regs.num_regs + '0') | |
080c45fd RS |
2564 | { |
2565 | substart = search_regs.start[c - '0']; | |
2566 | subend = search_regs.end[c - '0']; | |
2567 | } | |
76fc1ea2 KH |
2568 | else |
2569 | { | |
2570 | /* If that subexp did not match, | |
2571 | replace \\N with nothing. */ | |
2572 | substart = 0; | |
2573 | subend = 0; | |
2574 | } | |
080c45fd | 2575 | } |
1e79ec24 KH |
2576 | else if (c == '\\') |
2577 | delbackslash = 1; | |
636a5e28 RS |
2578 | else |
2579 | error ("Invalid use of `\\' in replacement text"); | |
080c45fd RS |
2580 | } |
2581 | if (substart >= 0) | |
2582 | { | |
d131e79c RS |
2583 | if (pos - 2 != lastpos) |
2584 | middle = substring_both (newtext, lastpos, | |
2585 | lastpos_byte, | |
2586 | pos - 2, pos_byte - 2); | |
080c45fd RS |
2587 | else |
2588 | middle = Qnil; | |
2589 | accum = concat3 (accum, middle, | |
0c8533c6 RS |
2590 | Fsubstring (string, |
2591 | make_number (substart), | |
080c45fd RS |
2592 | make_number (subend))); |
2593 | lastpos = pos; | |
0c8533c6 | 2594 | lastpos_byte = pos_byte; |
080c45fd | 2595 | } |
1e79ec24 KH |
2596 | else if (delbackslash) |
2597 | { | |
d131e79c RS |
2598 | middle = substring_both (newtext, lastpos, |
2599 | lastpos_byte, | |
2600 | pos - 1, pos_byte - 1); | |
0c8533c6 | 2601 | |
1e79ec24 KH |
2602 | accum = concat2 (accum, middle); |
2603 | lastpos = pos; | |
0c8533c6 | 2604 | lastpos_byte = pos_byte; |
1e79ec24 | 2605 | } |
080c45fd RS |
2606 | } |
2607 | ||
d131e79c RS |
2608 | if (pos != lastpos) |
2609 | middle = substring_both (newtext, lastpos, | |
2610 | lastpos_byte, | |
0c8533c6 | 2611 | pos, pos_byte); |
080c45fd RS |
2612 | else |
2613 | middle = Qnil; | |
2614 | ||
2615 | newtext = concat2 (accum, middle); | |
2616 | } | |
2617 | ||
636a5e28 | 2618 | /* Do case substitution in NEWTEXT if desired. */ |
080c45fd RS |
2619 | if (case_action == all_caps) |
2620 | newtext = Fupcase (newtext); | |
2621 | else if (case_action == cap_initial) | |
2b2eead9 | 2622 | newtext = Fupcase_initials (newtext); |
080c45fd RS |
2623 | |
2624 | return concat3 (before, newtext, after); | |
2625 | } | |
2626 | ||
09c4719e | 2627 | /* Record point, then move (quietly) to the start of the match. */ |
9160906f | 2628 | if (PT >= search_regs.end[sub]) |
b0eba991 | 2629 | opoint = PT - ZV; |
9160906f RS |
2630 | else if (PT > search_regs.start[sub]) |
2631 | opoint = search_regs.end[sub] - ZV; | |
b0eba991 RS |
2632 | else |
2633 | opoint = PT; | |
2634 | ||
886ed6ec RS |
2635 | /* If we want non-literal replacement, |
2636 | perform substitution on the replacement string. */ | |
2637 | if (NILP (literal)) | |
ca1d1d23 | 2638 | { |
c098fdb8 | 2639 | EMACS_INT length = SBYTES (newtext); |
68e69fbd | 2640 | unsigned char *substed; |
c098fdb8 | 2641 | EMACS_INT substed_alloc_size, substed_len; |
4b4deea2 | 2642 | int buf_multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); |
3bc25e52 KH |
2643 | int str_multibyte = STRING_MULTIBYTE (newtext); |
2644 | Lisp_Object rev_tbl; | |
886ed6ec | 2645 | int really_changed = 0; |
3bc25e52 | 2646 | |
8f924df7 | 2647 | rev_tbl = Qnil; |
ac3b28b1 | 2648 | |
68e69fbd RS |
2649 | substed_alloc_size = length * 2 + 100; |
2650 | substed = (unsigned char *) xmalloc (substed_alloc_size + 1); | |
2651 | substed_len = 0; | |
2652 | ||
3bc25e52 KH |
2653 | /* Go thru NEWTEXT, producing the actual text to insert in |
2654 | SUBSTED while adjusting multibyteness to that of the current | |
2655 | buffer. */ | |
ca1d1d23 | 2656 | |
ac3b28b1 | 2657 | for (pos_byte = 0, pos = 0; pos_byte < length;) |
ca1d1d23 | 2658 | { |
68e69fbd | 2659 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
8ea90aa3 | 2660 | const unsigned char *add_stuff = NULL; |
c098fdb8 | 2661 | EMACS_INT add_len = 0; |
68e69fbd | 2662 | int idx = -1; |
9a76659d | 2663 | |
3bc25e52 KH |
2664 | if (str_multibyte) |
2665 | { | |
eb99a8dd | 2666 | FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, newtext, pos, pos_byte); |
3bc25e52 KH |
2667 | if (!buf_multibyte) |
2668 | c = multibyte_char_to_unibyte (c, rev_tbl); | |
2669 | } | |
2670 | else | |
2671 | { | |
2672 | /* Note that we don't have to increment POS. */ | |
5d69fe10 | 2673 | c = SREF (newtext, pos_byte++); |
3bc25e52 | 2674 | if (buf_multibyte) |
4c0354d7 | 2675 | MAKE_CHAR_MULTIBYTE (c); |
3bc25e52 | 2676 | } |
ac3b28b1 | 2677 | |
68e69fbd RS |
2678 | /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED, |
2679 | or set IDX to a match index, which means put that part | |
2680 | of the buffer text into SUBSTED. */ | |
2681 | ||
ca1d1d23 JB |
2682 | if (c == '\\') |
2683 | { | |
886ed6ec RS |
2684 | really_changed = 1; |
2685 | ||
3bc25e52 KH |
2686 | if (str_multibyte) |
2687 | { | |
eb99a8dd KH |
2688 | FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c, newtext, |
2689 | pos, pos_byte); | |
071ce769 | 2690 | if (!buf_multibyte && !ASCII_CHAR_P (c)) |
3bc25e52 KH |
2691 | c = multibyte_char_to_unibyte (c, rev_tbl); |
2692 | } | |
2693 | else | |
2694 | { | |
d5db4077 | 2695 | c = SREF (newtext, pos_byte++); |
3bc25e52 | 2696 | if (buf_multibyte) |
4c0354d7 | 2697 | MAKE_CHAR_MULTIBYTE (c); |
3bc25e52 KH |
2698 | } |
2699 | ||
ca1d1d23 | 2700 | if (c == '&') |
68e69fbd | 2701 | idx = sub; |
78445046 | 2702 | else if (c >= '1' && c <= '9' && c <= search_regs.num_regs + '0') |
ca1d1d23 JB |
2703 | { |
2704 | if (search_regs.start[c - '0'] >= 1) | |
68e69fbd | 2705 | idx = c - '0'; |
ca1d1d23 | 2706 | } |
636a5e28 | 2707 | else if (c == '\\') |
a7e979a4 | 2708 | add_len = 1, add_stuff = (unsigned char *) "\\"; |
636a5e28 | 2709 | else |
3bc25e52 KH |
2710 | { |
2711 | xfree (substed); | |
2712 | error ("Invalid use of `\\' in replacement text"); | |
2713 | } | |
ca1d1d23 JB |
2714 | } |
2715 | else | |
68e69fbd RS |
2716 | { |
2717 | add_len = CHAR_STRING (c, str); | |
2718 | add_stuff = str; | |
2719 | } | |
2720 | ||
2721 | /* If we want to copy part of a previous match, | |
2722 | set up ADD_STUFF and ADD_LEN to point to it. */ | |
2723 | if (idx >= 0) | |
2724 | { | |
e7deaab0 | 2725 | EMACS_INT begbyte = CHAR_TO_BYTE (search_regs.start[idx]); |
68e69fbd RS |
2726 | add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte; |
2727 | if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx]) | |
2728 | move_gap (search_regs.start[idx]); | |
2729 | add_stuff = BYTE_POS_ADDR (begbyte); | |
2730 | } | |
2731 | ||
2732 | /* Now the stuff we want to add to SUBSTED | |
2733 | is invariably ADD_LEN bytes starting at ADD_STUFF. */ | |
2734 | ||
2735 | /* Make sure SUBSTED is big enough. */ | |
2736 | if (substed_len + add_len >= substed_alloc_size) | |
2737 | { | |
2738 | substed_alloc_size = substed_len + add_len + 500; | |
2739 | substed = (unsigned char *) xrealloc (substed, | |
2740 | substed_alloc_size + 1); | |
2741 | } | |
2742 | ||
2743 | /* Now add to the end of SUBSTED. */ | |
f8ce8a0d GM |
2744 | if (add_stuff) |
2745 | { | |
72af86bd | 2746 | memcpy (substed + substed_len, add_stuff, add_len); |
f8ce8a0d GM |
2747 | substed_len += add_len; |
2748 | } | |
ca1d1d23 | 2749 | } |
68e69fbd | 2750 | |
886ed6ec | 2751 | if (really_changed) |
76fc1ea2 KH |
2752 | { |
2753 | if (buf_multibyte) | |
2754 | { | |
c098fdb8 EZ |
2755 | EMACS_INT nchars = |
2756 | multibyte_chars_in_text (substed, substed_len); | |
68e69fbd | 2757 | |
a7e979a4 PE |
2758 | newtext = make_multibyte_string ((char *) substed, nchars, |
2759 | substed_len); | |
76fc1ea2 KH |
2760 | } |
2761 | else | |
a7e979a4 | 2762 | newtext = make_unibyte_string ((char *) substed, substed_len); |
76fc1ea2 | 2763 | } |
68e69fbd | 2764 | xfree (substed); |
ca1d1d23 JB |
2765 | } |
2766 | ||
886ed6ec RS |
2767 | /* Replace the old text with the new in the cleanest possible way. */ |
2768 | replace_range (search_regs.start[sub], search_regs.end[sub], | |
2769 | newtext, 1, 0, 1); | |
d5db4077 | 2770 | newpoint = search_regs.start[sub] + SCHARS (newtext); |
ca1d1d23 JB |
2771 | |
2772 | if (case_action == all_caps) | |
886ed6ec RS |
2773 | Fupcase_region (make_number (search_regs.start[sub]), |
2774 | make_number (newpoint)); | |
ca1d1d23 | 2775 | else if (case_action == cap_initial) |
886ed6ec RS |
2776 | Fupcase_initials_region (make_number (search_regs.start[sub]), |
2777 | make_number (newpoint)); | |
3e18eecf | 2778 | |
98e942e0 RS |
2779 | /* Adjust search data for this change. */ |
2780 | { | |
e7deaab0 AS |
2781 | EMACS_INT oldend = search_regs.end[sub]; |
2782 | EMACS_INT oldstart = search_regs.start[sub]; | |
2783 | EMACS_INT change = newpoint - search_regs.end[sub]; | |
98e942e0 RS |
2784 | int i; |
2785 | ||
2786 | for (i = 0; i < search_regs.num_regs; i++) | |
2787 | { | |
41c01205 | 2788 | if (search_regs.start[i] >= oldend) |
98e942e0 | 2789 | search_regs.start[i] += change; |
41c01205 DK |
2790 | else if (search_regs.start[i] > oldstart) |
2791 | search_regs.start[i] = oldstart; | |
2792 | if (search_regs.end[i] >= oldend) | |
98e942e0 | 2793 | search_regs.end[i] += change; |
41c01205 DK |
2794 | else if (search_regs.end[i] > oldstart) |
2795 | search_regs.end[i] = oldstart; | |
98e942e0 RS |
2796 | } |
2797 | } | |
2798 | ||
b0eba991 | 2799 | /* Put point back where it was in the text. */ |
8d808a65 | 2800 | if (opoint <= 0) |
fa8ed3e0 | 2801 | TEMP_SET_PT (opoint + ZV); |
b0eba991 | 2802 | else |
fa8ed3e0 | 2803 | TEMP_SET_PT (opoint); |
b0eba991 RS |
2804 | |
2805 | /* Now move point "officially" to the start of the inserted replacement. */ | |
3e18eecf | 2806 | move_if_not_intangible (newpoint); |
177c0ea7 | 2807 | |
ca1d1d23 JB |
2808 | return Qnil; |
2809 | } | |
2810 | \f | |
2811 | static Lisp_Object | |
971de7fb | 2812 | match_limit (Lisp_Object num, int beginningp) |
ca1d1d23 JB |
2813 | { |
2814 | register int n; | |
2815 | ||
b7826503 | 2816 | CHECK_NUMBER (num); |
ca1d1d23 | 2817 | n = XINT (num); |
f90a5bf5 | 2818 | if (n < 0) |
bd2cbd56 | 2819 | args_out_of_range (num, make_number (0)); |
f90a5bf5 RS |
2820 | if (search_regs.num_regs <= 0) |
2821 | error ("No match data, because no search succeeded"); | |
9b9ceb61 | 2822 | if (n >= search_regs.num_regs |
4746118a | 2823 | || search_regs.start[n] < 0) |
ca1d1d23 JB |
2824 | return Qnil; |
2825 | return (make_number ((beginningp) ? search_regs.start[n] | |
2826 | : search_regs.end[n])); | |
2827 | } | |
2828 | ||
2829 | DEFUN ("match-beginning", Fmatch_beginning, Smatch_beginning, 1, 1, 0, | |
8c1a1077 PJ |
2830 | doc: /* Return position of start of text matched by last search. |
2831 | SUBEXP, a number, specifies which parenthesized expression in the last | |
2832 | regexp. | |
2833 | Value is nil if SUBEXPth pair didn't match, or there were less than | |
2834 | SUBEXP pairs. | |
2835 | Zero means the entire text matched by the whole regexp or whole string. */) | |
5842a27b | 2836 | (Lisp_Object subexp) |
ca1d1d23 | 2837 | { |
5806161b | 2838 | return match_limit (subexp, 1); |
ca1d1d23 JB |
2839 | } |
2840 | ||
2841 | DEFUN ("match-end", Fmatch_end, Smatch_end, 1, 1, 0, | |
8c1a1077 PJ |
2842 | doc: /* Return position of end of text matched by last search. |
2843 | SUBEXP, a number, specifies which parenthesized expression in the last | |
2844 | regexp. | |
2845 | Value is nil if SUBEXPth pair didn't match, or there were less than | |
2846 | SUBEXP pairs. | |
2847 | Zero means the entire text matched by the whole regexp or whole string. */) | |
5842a27b | 2848 | (Lisp_Object subexp) |
ca1d1d23 | 2849 | { |
5806161b | 2850 | return match_limit (subexp, 0); |
177c0ea7 | 2851 | } |
ca1d1d23 | 2852 | |
abd0071c | 2853 | DEFUN ("match-data", Fmatch_data, Smatch_data, 0, 3, 0, |
8c1a1077 PJ |
2854 | doc: /* Return a list containing all info on what the last search matched. |
2855 | Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'. | |
2856 | All the elements are markers or nil (nil if the Nth pair didn't match) | |
2857 | if the last match was on a buffer; integers or nil if a string was matched. | |
febb8aba | 2858 | Use `set-match-data' to reinstate the data in this list. |
8c1a1077 | 2859 | |
41c01205 DK |
2860 | If INTEGERS (the optional first argument) is non-nil, always use |
2861 | integers \(rather than markers) to represent buffer positions. In | |
2862 | this case, and if the last match was in a buffer, the buffer will get | |
2863 | stored as one additional element at the end of the list. | |
2864 | ||
abd0071c KS |
2865 | If REUSE is a list, reuse it as part of the value. If REUSE is long |
2866 | enough to hold all the values, and if INTEGERS is non-nil, no consing | |
2867 | is done. | |
2868 | ||
2869 | If optional third arg RESEAT is non-nil, any previous markers on the | |
2870 | REUSE list will be modified to point to nowhere. | |
2871 | ||
140a6b7e | 2872 | Return value is undefined if the last search failed. */) |
5842a27b | 2873 | (Lisp_Object integers, Lisp_Object reuse, Lisp_Object reseat) |
ca1d1d23 | 2874 | { |
56256c2a | 2875 | Lisp_Object tail, prev; |
4746118a | 2876 | Lisp_Object *data; |
ca1d1d23 JB |
2877 | int i, len; |
2878 | ||
abd0071c KS |
2879 | if (!NILP (reseat)) |
2880 | for (tail = reuse; CONSP (tail); tail = XCDR (tail)) | |
2881 | if (MARKERP (XCAR (tail))) | |
2882 | { | |
51f10faa | 2883 | unchain_marker (XMARKER (XCAR (tail))); |
abd0071c KS |
2884 | XSETCAR (tail, Qnil); |
2885 | } | |
2886 | ||
daa37602 | 2887 | if (NILP (last_thing_searched)) |
c36bcf1b | 2888 | return Qnil; |
daa37602 | 2889 | |
6bbd7a29 GM |
2890 | prev = Qnil; |
2891 | ||
41c01205 | 2892 | data = (Lisp_Object *) alloca ((2 * search_regs.num_regs + 1) |
4746118a JB |
2893 | * sizeof (Lisp_Object)); |
2894 | ||
41c01205 | 2895 | len = 0; |
4746118a | 2896 | for (i = 0; i < search_regs.num_regs; i++) |
ca1d1d23 JB |
2897 | { |
2898 | int start = search_regs.start[i]; | |
2899 | if (start >= 0) | |
2900 | { | |
56256c2a RS |
2901 | if (EQ (last_thing_searched, Qt) |
2902 | || ! NILP (integers)) | |
ca1d1d23 | 2903 | { |
c235cce7 KH |
2904 | XSETFASTINT (data[2 * i], start); |
2905 | XSETFASTINT (data[2 * i + 1], search_regs.end[i]); | |
ca1d1d23 | 2906 | } |
0ed62dc7 | 2907 | else if (BUFFERP (last_thing_searched)) |
ca1d1d23 JB |
2908 | { |
2909 | data[2 * i] = Fmake_marker (); | |
daa37602 JB |
2910 | Fset_marker (data[2 * i], |
2911 | make_number (start), | |
2912 | last_thing_searched); | |
ca1d1d23 JB |
2913 | data[2 * i + 1] = Fmake_marker (); |
2914 | Fset_marker (data[2 * i + 1], | |
177c0ea7 | 2915 | make_number (search_regs.end[i]), |
daa37602 | 2916 | last_thing_searched); |
ca1d1d23 | 2917 | } |
daa37602 JB |
2918 | else |
2919 | /* last_thing_searched must always be Qt, a buffer, or Qnil. */ | |
2920 | abort (); | |
2921 | ||
abd0071c | 2922 | len = 2 * i + 2; |
ca1d1d23 JB |
2923 | } |
2924 | else | |
abd0071c | 2925 | data[2 * i] = data[2 * i + 1] = Qnil; |
ca1d1d23 | 2926 | } |
56256c2a | 2927 | |
bd2cbd56 | 2928 | if (BUFFERP (last_thing_searched) && !NILP (integers)) |
41c01205 | 2929 | { |
bd2cbd56 | 2930 | data[len] = last_thing_searched; |
41c01205 DK |
2931 | len++; |
2932 | } | |
2933 | ||
56256c2a RS |
2934 | /* If REUSE is not usable, cons up the values and return them. */ |
2935 | if (! CONSP (reuse)) | |
41c01205 | 2936 | return Flist (len, data); |
56256c2a RS |
2937 | |
2938 | /* If REUSE is a list, store as many value elements as will fit | |
2939 | into the elements of REUSE. */ | |
2940 | for (i = 0, tail = reuse; CONSP (tail); | |
c1d497be | 2941 | i++, tail = XCDR (tail)) |
56256c2a | 2942 | { |
41c01205 | 2943 | if (i < len) |
f3fbd155 | 2944 | XSETCAR (tail, data[i]); |
56256c2a | 2945 | else |
f3fbd155 | 2946 | XSETCAR (tail, Qnil); |
56256c2a RS |
2947 | prev = tail; |
2948 | } | |
2949 | ||
2950 | /* If we couldn't fit all value elements into REUSE, | |
2951 | cons up the rest of them and add them to the end of REUSE. */ | |
41c01205 DK |
2952 | if (i < len) |
2953 | XSETCDR (prev, Flist (len - i, data + i)); | |
56256c2a RS |
2954 | |
2955 | return reuse; | |
ca1d1d23 JB |
2956 | } |
2957 | ||
b51d6c92 SM |
2958 | /* We used to have an internal use variant of `reseat' described as: |
2959 | ||
2960 | If RESEAT is `evaporate', put the markers back on the free list | |
2961 | immediately. No other references to the markers must exist in this | |
2962 | case, so it is used only internally on the unwind stack and | |
2963 | save-match-data from Lisp. | |
2964 | ||
2965 | But it was ill-conceived: those supposedly-internal markers get exposed via | |
2966 | the undo-list, so freeing them here is unsafe. */ | |
ca1d1d23 | 2967 | |
abd0071c | 2968 | DEFUN ("set-match-data", Fset_match_data, Sset_match_data, 1, 2, 0, |
8c1a1077 | 2969 | doc: /* Set internal data on last search match from elements of LIST. |
abd0071c KS |
2970 | LIST should have been created by calling `match-data' previously. |
2971 | ||
51f10faa | 2972 | If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */) |
5842a27b | 2973 | (register Lisp_Object list, Lisp_Object reseat) |
ca1d1d23 JB |
2974 | { |
2975 | register int i; | |
2976 | register Lisp_Object marker; | |
2977 | ||
7074fde6 FP |
2978 | if (running_asynch_code) |
2979 | save_search_regs (); | |
2980 | ||
29100cea | 2981 | CHECK_LIST (list); |
ca1d1d23 | 2982 | |
41c01205 DK |
2983 | /* Unless we find a marker with a buffer or an explicit buffer |
2984 | in LIST, assume that this match data came from a string. */ | |
daa37602 JB |
2985 | last_thing_searched = Qt; |
2986 | ||
4746118a JB |
2987 | /* Allocate registers if they don't already exist. */ |
2988 | { | |
d084e942 | 2989 | int length = XFASTINT (Flength (list)) / 2; |
4746118a JB |
2990 | |
2991 | if (length > search_regs.num_regs) | |
2992 | { | |
1113d9db JB |
2993 | if (search_regs.num_regs == 0) |
2994 | { | |
2995 | search_regs.start | |
2996 | = (regoff_t *) xmalloc (length * sizeof (regoff_t)); | |
2997 | search_regs.end | |
2998 | = (regoff_t *) xmalloc (length * sizeof (regoff_t)); | |
2999 | } | |
4746118a | 3000 | else |
1113d9db JB |
3001 | { |
3002 | search_regs.start | |
3003 | = (regoff_t *) xrealloc (search_regs.start, | |
3004 | length * sizeof (regoff_t)); | |
3005 | search_regs.end | |
3006 | = (regoff_t *) xrealloc (search_regs.end, | |
3007 | length * sizeof (regoff_t)); | |
3008 | } | |
4746118a | 3009 | |
e62371e9 KH |
3010 | for (i = search_regs.num_regs; i < length; i++) |
3011 | search_regs.start[i] = -1; | |
3012 | ||
487282dc | 3013 | search_regs.num_regs = length; |
4746118a | 3014 | } |
ca1d1d23 | 3015 | |
abd0071c | 3016 | for (i = 0; CONSP (list); i++) |
41c01205 | 3017 | { |
abd0071c | 3018 | marker = XCAR (list); |
bd2cbd56 | 3019 | if (BUFFERP (marker)) |
c3762cbd | 3020 | { |
bd2cbd56 | 3021 | last_thing_searched = marker; |
c3762cbd DK |
3022 | break; |
3023 | } | |
3024 | if (i >= length) | |
3025 | break; | |
41c01205 DK |
3026 | if (NILP (marker)) |
3027 | { | |
3028 | search_regs.start[i] = -1; | |
abd0071c | 3029 | list = XCDR (list); |
41c01205 DK |
3030 | } |
3031 | else | |
3032 | { | |
e7deaab0 | 3033 | EMACS_INT from; |
abd0071c | 3034 | Lisp_Object m; |
e2811828 | 3035 | |
abd0071c | 3036 | m = marker; |
41c01205 DK |
3037 | if (MARKERP (marker)) |
3038 | { | |
3039 | if (XMARKER (marker)->buffer == 0) | |
3040 | XSETFASTINT (marker, 0); | |
3041 | else | |
3042 | XSETBUFFER (last_thing_searched, XMARKER (marker)->buffer); | |
3043 | } | |
e2811828 | 3044 | |
41c01205 DK |
3045 | CHECK_NUMBER_COERCE_MARKER (marker); |
3046 | from = XINT (marker); | |
e2811828 | 3047 | |
abd0071c KS |
3048 | if (!NILP (reseat) && MARKERP (m)) |
3049 | { | |
b51d6c92 | 3050 | unchain_marker (XMARKER (m)); |
9ad54a7e | 3051 | XSETCAR (list, Qnil); |
abd0071c KS |
3052 | } |
3053 | ||
3054 | if ((list = XCDR (list), !CONSP (list))) | |
3055 | break; | |
3056 | ||
3057 | m = marker = XCAR (list); | |
3058 | ||
41c01205 DK |
3059 | if (MARKERP (marker) && XMARKER (marker)->buffer == 0) |
3060 | XSETFASTINT (marker, 0); | |
e2811828 | 3061 | |
41c01205 DK |
3062 | CHECK_NUMBER_COERCE_MARKER (marker); |
3063 | search_regs.start[i] = from; | |
3064 | search_regs.end[i] = XINT (marker); | |
abd0071c KS |
3065 | |
3066 | if (!NILP (reseat) && MARKERP (m)) | |
3067 | { | |
b51d6c92 | 3068 | unchain_marker (XMARKER (m)); |
9ad54a7e | 3069 | XSETCAR (list, Qnil); |
abd0071c | 3070 | } |
41c01205 | 3071 | } |
abd0071c | 3072 | list = XCDR (list); |
41c01205 | 3073 | } |
ca1d1d23 | 3074 | |
41c01205 DK |
3075 | for (; i < search_regs.num_regs; i++) |
3076 | search_regs.start[i] = -1; | |
3077 | } | |
ca1d1d23 | 3078 | |
177c0ea7 | 3079 | return Qnil; |
ca1d1d23 JB |
3080 | } |
3081 | ||
7074fde6 FP |
3082 | /* If non-zero the match data have been saved in saved_search_regs |
3083 | during the execution of a sentinel or filter. */ | |
75ebf74b | 3084 | static int search_regs_saved; |
7074fde6 | 3085 | static struct re_registers saved_search_regs; |
41c01205 | 3086 | static Lisp_Object saved_last_thing_searched; |
7074fde6 FP |
3087 | |
3088 | /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data | |
3089 | if asynchronous code (filter or sentinel) is running. */ | |
3090 | static void | |
971de7fb | 3091 | save_search_regs (void) |
7074fde6 FP |
3092 | { |
3093 | if (!search_regs_saved) | |
3094 | { | |
3095 | saved_search_regs.num_regs = search_regs.num_regs; | |
3096 | saved_search_regs.start = search_regs.start; | |
3097 | saved_search_regs.end = search_regs.end; | |
41c01205 DK |
3098 | saved_last_thing_searched = last_thing_searched; |
3099 | last_thing_searched = Qnil; | |
7074fde6 | 3100 | search_regs.num_regs = 0; |
2d4a771a RS |
3101 | search_regs.start = 0; |
3102 | search_regs.end = 0; | |
7074fde6 FP |
3103 | |
3104 | search_regs_saved = 1; | |
3105 | } | |
3106 | } | |
3107 | ||
3108 | /* Called upon exit from filters and sentinels. */ | |
3109 | void | |
971de7fb | 3110 | restore_search_regs (void) |
7074fde6 FP |
3111 | { |
3112 | if (search_regs_saved) | |
3113 | { | |
3114 | if (search_regs.num_regs > 0) | |
3115 | { | |
3116 | xfree (search_regs.start); | |
3117 | xfree (search_regs.end); | |
3118 | } | |
3119 | search_regs.num_regs = saved_search_regs.num_regs; | |
3120 | search_regs.start = saved_search_regs.start; | |
3121 | search_regs.end = saved_search_regs.end; | |
41c01205 DK |
3122 | last_thing_searched = saved_last_thing_searched; |
3123 | saved_last_thing_searched = Qnil; | |
7074fde6 FP |
3124 | search_regs_saved = 0; |
3125 | } | |
3126 | } | |
3127 | ||
abd0071c | 3128 | static Lisp_Object |
971de7fb | 3129 | unwind_set_match_data (Lisp_Object list) |
abd0071c | 3130 | { |
b51d6c92 SM |
3131 | /* It is NOT ALWAYS safe to free (evaporate) the markers immediately. */ |
3132 | return Fset_match_data (list, Qt); | |
abd0071c KS |
3133 | } |
3134 | ||
3135 | /* Called to unwind protect the match data. */ | |
3136 | void | |
971de7fb | 3137 | record_unwind_save_match_data (void) |
abd0071c KS |
3138 | { |
3139 | record_unwind_protect (unwind_set_match_data, | |
3140 | Fmatch_data (Qnil, Qnil, Qnil)); | |
3141 | } | |
3142 | ||
ca1d1d23 JB |
3143 | /* Quote a string to inactivate reg-expr chars */ |
3144 | ||
3145 | DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0, | |
8c1a1077 | 3146 | doc: /* Return a regexp string which matches exactly STRING and nothing else. */) |
5842a27b | 3147 | (Lisp_Object string) |
ca1d1d23 | 3148 | { |
a7e979a4 PE |
3149 | register char *in, *out, *end; |
3150 | register char *temp; | |
0c8533c6 | 3151 | int backslashes_added = 0; |
ca1d1d23 | 3152 | |
b7826503 | 3153 | CHECK_STRING (string); |
ca1d1d23 | 3154 | |
a7e979a4 | 3155 | temp = (char *) alloca (SBYTES (string) * 2); |
ca1d1d23 JB |
3156 | |
3157 | /* Now copy the data into the new string, inserting escapes. */ | |
3158 | ||
a7e979a4 | 3159 | in = SSDATA (string); |
d5db4077 | 3160 | end = in + SBYTES (string); |
177c0ea7 | 3161 | out = temp; |
ca1d1d23 JB |
3162 | |
3163 | for (; in != end; in++) | |
3164 | { | |
66bc6082 | 3165 | if (*in == '[' |
ca1d1d23 JB |
3166 | || *in == '*' || *in == '.' || *in == '\\' |
3167 | || *in == '?' || *in == '+' | |
3168 | || *in == '^' || *in == '$') | |
0c8533c6 | 3169 | *out++ = '\\', backslashes_added++; |
ca1d1d23 JB |
3170 | *out++ = *in; |
3171 | } | |
3172 | ||
3f8100f1 | 3173 | return make_specified_string (temp, |
d5db4077 | 3174 | SCHARS (string) + backslashes_added, |
3f8100f1 RS |
3175 | out - temp, |
3176 | STRING_MULTIBYTE (string)); | |
ca1d1d23 | 3177 | } |
177c0ea7 | 3178 | \f |
dfcf069d | 3179 | void |
971de7fb | 3180 | syms_of_search (void) |
ca1d1d23 JB |
3181 | { |
3182 | register int i; | |
3183 | ||
487282dc KH |
3184 | for (i = 0; i < REGEXP_CACHE_SIZE; ++i) |
3185 | { | |
3186 | searchbufs[i].buf.allocated = 100; | |
b23c0a83 | 3187 | searchbufs[i].buf.buffer = (unsigned char *) xmalloc (100); |
487282dc KH |
3188 | searchbufs[i].buf.fastmap = searchbufs[i].fastmap; |
3189 | searchbufs[i].regexp = Qnil; | |
ecdb561e | 3190 | searchbufs[i].whitespace_regexp = Qnil; |
b69e3c18 | 3191 | searchbufs[i].syntax_table = Qnil; |
487282dc | 3192 | staticpro (&searchbufs[i].regexp); |
aa77b5ce | 3193 | staticpro (&searchbufs[i].whitespace_regexp); |
b69e3c18 | 3194 | staticpro (&searchbufs[i].syntax_table); |
487282dc KH |
3195 | searchbufs[i].next = (i == REGEXP_CACHE_SIZE-1 ? 0 : &searchbufs[i+1]); |
3196 | } | |
3197 | searchbuf_head = &searchbufs[0]; | |
ca1d1d23 | 3198 | |
d67b4f80 | 3199 | Qsearch_failed = intern_c_string ("search-failed"); |
ca1d1d23 | 3200 | staticpro (&Qsearch_failed); |
d67b4f80 | 3201 | Qinvalid_regexp = intern_c_string ("invalid-regexp"); |
ca1d1d23 JB |
3202 | staticpro (&Qinvalid_regexp); |
3203 | ||
3204 | Fput (Qsearch_failed, Qerror_conditions, | |
d67b4f80 | 3205 | pure_cons (Qsearch_failed, pure_cons (Qerror, Qnil))); |
ca1d1d23 | 3206 | Fput (Qsearch_failed, Qerror_message, |
d67b4f80 | 3207 | make_pure_c_string ("Search failed")); |
ca1d1d23 JB |
3208 | |
3209 | Fput (Qinvalid_regexp, Qerror_conditions, | |
d67b4f80 | 3210 | pure_cons (Qinvalid_regexp, pure_cons (Qerror, Qnil))); |
ca1d1d23 | 3211 | Fput (Qinvalid_regexp, Qerror_message, |
d67b4f80 | 3212 | make_pure_c_string ("Invalid regexp")); |
ca1d1d23 | 3213 | |
daa37602 JB |
3214 | last_thing_searched = Qnil; |
3215 | staticpro (&last_thing_searched); | |
3216 | ||
0f6af254 DK |
3217 | saved_last_thing_searched = Qnil; |
3218 | staticpro (&saved_last_thing_searched); | |
3219 | ||
29208e82 | 3220 | DEFVAR_LISP ("search-spaces-regexp", Vsearch_spaces_regexp, |
e2811828 | 3221 | doc: /* Regexp to substitute for bunches of spaces in regexp search. |
f31a9a68 RS |
3222 | Some commands use this for user-specified regexps. |
3223 | Spaces that occur inside character classes or repetition operators | |
3224 | or other such regexp constructs are not replaced with this. | |
3225 | A value of nil (which is the normal value) means treat spaces literally. */); | |
41a33295 | 3226 | Vsearch_spaces_regexp = Qnil; |
f31a9a68 | 3227 | |
29208e82 | 3228 | DEFVAR_LISP ("inhibit-changing-match-data", Vinhibit_changing_match_data, |
ef887810 | 3229 | doc: /* Internal use only. |
39d0bf74 RS |
3230 | If non-nil, the primitive searching and matching functions |
3231 | such as `looking-at', `string-match', `re-search-forward', etc., | |
3232 | do not set the match data. The proper way to use this variable | |
3233 | is to bind it with `let' around a small expression. */); | |
ef887810 RS |
3234 | Vinhibit_changing_match_data = Qnil; |
3235 | ||
ca1d1d23 | 3236 | defsubr (&Slooking_at); |
b819a390 RS |
3237 | defsubr (&Sposix_looking_at); |
3238 | defsubr (&Sstring_match); | |
3239 | defsubr (&Sposix_string_match); | |
ca1d1d23 JB |
3240 | defsubr (&Ssearch_forward); |
3241 | defsubr (&Ssearch_backward); | |
3242 | defsubr (&Sword_search_forward); | |
3243 | defsubr (&Sword_search_backward); | |
c6e0e1cc CY |
3244 | defsubr (&Sword_search_forward_lax); |
3245 | defsubr (&Sword_search_backward_lax); | |
ca1d1d23 JB |
3246 | defsubr (&Sre_search_forward); |
3247 | defsubr (&Sre_search_backward); | |
b819a390 RS |
3248 | defsubr (&Sposix_search_forward); |
3249 | defsubr (&Sposix_search_backward); | |
ca1d1d23 JB |
3250 | defsubr (&Sreplace_match); |
3251 | defsubr (&Smatch_beginning); | |
3252 | defsubr (&Smatch_end); | |
3253 | defsubr (&Smatch_data); | |
3f1c005b | 3254 | defsubr (&Sset_match_data); |
ca1d1d23 JB |
3255 | defsubr (&Sregexp_quote); |
3256 | } |