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