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