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