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