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