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