1 /* String search routines for GNU Emacs.
2 Copyright (C) 1985, 86,87,93,94,97,98, 1999 Free Software Foundation, Inc.
4 This file is part of GNU Emacs.
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)
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.
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. */
28 #include "region-cache.h"
30 #include "blockinput.h"
31 #include "intervals.h"
33 #include <sys/types.h>
36 #define REGEXP_CACHE_SIZE 20
38 /* If the regexp is non-nil, then the buffer contains the compiled form
39 of that regexp, suitable for searching. */
42 struct regexp_cache
*next
;
44 struct re_pattern_buffer buf
;
46 /* Nonzero means regexp was compiled to do full POSIX backtracking. */
50 /* The instances of that struct. */
51 struct regexp_cache searchbufs
[REGEXP_CACHE_SIZE
];
53 /* The head of the linked list; points to the most recently used buffer. */
54 struct regexp_cache
*searchbuf_head
;
57 /* Every call to re_match, etc., must pass &search_regs as the regs
58 argument unless you can show it is unnecessary (i.e., if re_match
59 is certainly going to be called again before region-around-match
62 Since the registers are now dynamically allocated, we need to make
63 sure not to refer to the Nth register before checking that it has
64 been allocated by checking search_regs.num_regs.
66 The regex code keeps track of whether it has allocated the search
67 buffer using bits in the re_pattern_buffer. This means that whenever
68 you compile a new pattern, it completely forgets whether it has
69 allocated any registers, and will allocate new registers the next
70 time you call a searching or matching function. Therefore, we need
71 to call re_set_registers after compiling a new pattern or after
72 setting the match registers, so that the regex functions will be
73 able to free or re-allocate it properly. */
74 static struct re_registers search_regs
;
76 /* The buffer in which the last search was performed, or
77 Qt if the last search was done in a string;
78 Qnil if no searching has been done yet. */
79 static Lisp_Object last_thing_searched
;
81 /* error condition signaled when regexp compile_pattern fails */
83 Lisp_Object Qinvalid_regexp
;
85 static void set_search_regs ();
86 static void save_search_regs ();
87 static int simple_search ();
88 static int boyer_moore ();
89 static int search_buffer ();
94 error ("Stack overflow in regexp matcher");
97 /* Compile a regexp and signal a Lisp error if anything goes wrong.
98 PATTERN is the pattern to compile.
99 CP is the place to put the result.
100 TRANSLATE is a translation table for ignoring case, or nil for none.
101 REGP is the structure that says where to store the "register"
102 values that will result from matching this pattern.
103 If it is 0, we should compile the pattern not to record any
104 subexpression bounds.
105 POSIX is nonzero if we want full backtracking (POSIX style)
106 for this pattern. 0 means backtrack only enough to get a valid match.
107 MULTIBYTE is nonzero if we want to handle multibyte characters in
108 PATTERN. 0 means all multibyte characters are recognized just as
109 sequences of binary data. */
112 compile_pattern_1 (cp
, pattern
, translate
, regp
, posix
, multibyte
)
113 struct regexp_cache
*cp
;
115 Lisp_Object translate
;
116 struct re_registers
*regp
;
120 unsigned char *raw_pattern
;
121 int raw_pattern_size
;
125 /* MULTIBYTE says whether the text to be searched is multibyte.
126 We must convert PATTERN to match that, or we will not really
127 find things right. */
129 if (multibyte
== STRING_MULTIBYTE (pattern
))
131 raw_pattern
= (unsigned char *) SDATA (pattern
);
132 raw_pattern_size
= SBYTES (pattern
);
136 raw_pattern_size
= count_size_as_multibyte (SDATA (pattern
),
138 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
139 copy_text (SDATA (pattern
), raw_pattern
,
140 SCHARS (pattern
), 0, 1);
144 /* Converting multibyte to single-byte.
146 ??? Perhaps this conversion should be done in a special way
147 by subtracting nonascii-insert-offset from each non-ASCII char,
148 so that only the multibyte chars which really correspond to
149 the chosen single-byte character set can possibly match. */
150 raw_pattern_size
= SCHARS (pattern
);
151 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
152 copy_text (SDATA (pattern
), raw_pattern
,
153 SBYTES (pattern
), 1, 0);
157 cp
->buf
.translate
= (! NILP (translate
) ? translate
: make_number (0));
159 cp
->buf
.multibyte
= multibyte
;
161 old
= re_set_syntax (RE_SYNTAX_EMACS
162 | (posix
? 0 : RE_NO_POSIX_BACKTRACKING
));
163 val
= (char *) re_compile_pattern ((char *)raw_pattern
,
164 raw_pattern_size
, &cp
->buf
);
168 Fsignal (Qinvalid_regexp
, Fcons (build_string (val
), Qnil
));
170 cp
->regexp
= Fcopy_sequence (pattern
);
173 /* Shrink each compiled regexp buffer in the cache
174 to the size actually used right now.
175 This is called from garbage collection. */
178 shrink_regexp_cache ()
180 struct regexp_cache
*cp
;
182 for (cp
= searchbuf_head
; cp
!= 0; cp
= cp
->next
)
184 cp
->buf
.allocated
= cp
->buf
.used
;
186 = (unsigned char *) xrealloc (cp
->buf
.buffer
, cp
->buf
.used
);
190 /* Compile a regexp if necessary, but first check to see if there's one in
192 PATTERN is the pattern to compile.
193 TRANSLATE is a translation table for ignoring case, or nil for none.
194 REGP is the structure that says where to store the "register"
195 values that will result from matching this pattern.
196 If it is 0, we should compile the pattern not to record any
197 subexpression bounds.
198 POSIX is nonzero if we want full backtracking (POSIX style)
199 for this pattern. 0 means backtrack only enough to get a valid match. */
201 struct re_pattern_buffer
*
202 compile_pattern (pattern
, regp
, translate
, posix
, multibyte
)
204 struct re_registers
*regp
;
205 Lisp_Object translate
;
206 int posix
, multibyte
;
208 struct regexp_cache
*cp
, **cpp
;
210 for (cpp
= &searchbuf_head
; ; cpp
= &cp
->next
)
213 /* Entries are initialized to nil, and may be set to nil by
214 compile_pattern_1 if the pattern isn't valid. Don't apply
215 string accessors in those cases. However, compile_pattern_1
216 is only applied to the cache entry we pick here to reuse. So
217 nil should never appear before a non-nil entry. */
218 if (NILP (cp
->regexp
))
220 if (SCHARS (cp
->regexp
) == SCHARS (pattern
)
221 && STRING_MULTIBYTE (cp
->regexp
) == STRING_MULTIBYTE (pattern
)
222 && !NILP (Fstring_equal (cp
->regexp
, pattern
))
223 && EQ (cp
->buf
.translate
, (! NILP (translate
) ? translate
: make_number (0)))
224 && cp
->posix
== posix
225 && cp
->buf
.multibyte
== multibyte
)
228 /* If we're at the end of the cache, compile into the nil cell
229 we found, or the last (least recently used) cell with a
234 compile_pattern_1 (cp
, pattern
, translate
, regp
, posix
, multibyte
);
239 /* When we get here, cp (aka *cpp) contains the compiled pattern,
240 either because we found it in the cache or because we just compiled it.
241 Move it to the front of the queue to mark it as most recently used. */
243 cp
->next
= searchbuf_head
;
246 /* Advise the searching functions about the space we have allocated
247 for register data. */
249 re_set_registers (&cp
->buf
, regp
, regp
->num_regs
, regp
->start
, regp
->end
);
254 /* Error condition used for failing searches */
255 Lisp_Object Qsearch_failed
;
261 Fsignal (Qsearch_failed
, Fcons (arg
, Qnil
));
266 looking_at_1 (string
, posix
)
271 unsigned char *p1
, *p2
;
274 struct re_pattern_buffer
*bufp
;
276 if (running_asynch_code
)
279 CHECK_STRING (string
);
280 bufp
= compile_pattern (string
, &search_regs
,
281 (!NILP (current_buffer
->case_fold_search
)
282 ? DOWNCASE_TABLE
: Qnil
),
284 !NILP (current_buffer
->enable_multibyte_characters
));
287 QUIT
; /* Do a pending quit right away, to avoid paradoxical behavior */
289 /* Get pointers and sizes of the two strings
290 that make up the visible portion of the buffer. */
293 s1
= GPT_BYTE
- BEGV_BYTE
;
295 s2
= ZV_BYTE
- GPT_BYTE
;
299 s2
= ZV_BYTE
- BEGV_BYTE
;
304 s1
= ZV_BYTE
- BEGV_BYTE
;
308 re_match_object
= Qnil
;
310 i
= re_match_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
311 PT_BYTE
- BEGV_BYTE
, &search_regs
,
312 ZV_BYTE
- BEGV_BYTE
);
318 val
= (0 <= i
? Qt
: Qnil
);
320 for (i
= 0; i
< search_regs
.num_regs
; i
++)
321 if (search_regs
.start
[i
] >= 0)
324 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
326 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
328 XSETBUFFER (last_thing_searched
, current_buffer
);
332 DEFUN ("looking-at", Flooking_at
, Slooking_at
, 1, 1, 0,
333 doc
: /* Return t if text after point matches regular expression REGEXP.
334 This function modifies the match data that `match-beginning',
335 `match-end' and `match-data' access; save and restore the match
336 data if you want to preserve them. */)
340 return looking_at_1 (regexp
, 0);
343 DEFUN ("posix-looking-at", Fposix_looking_at
, Sposix_looking_at
, 1, 1, 0,
344 doc
: /* Return t if text after point matches regular expression REGEXP.
345 Find the longest match, in accord with Posix regular expression rules.
346 This function modifies the match data that `match-beginning',
347 `match-end' and `match-data' access; save and restore the match
348 data if you want to preserve them. */)
352 return looking_at_1 (regexp
, 1);
356 string_match_1 (regexp
, string
, start
, posix
)
357 Lisp_Object regexp
, string
, start
;
361 struct re_pattern_buffer
*bufp
;
365 if (running_asynch_code
)
368 CHECK_STRING (regexp
);
369 CHECK_STRING (string
);
372 pos
= 0, pos_byte
= 0;
375 int len
= SCHARS (string
);
377 CHECK_NUMBER (start
);
379 if (pos
< 0 && -pos
<= len
)
381 else if (0 > pos
|| pos
> len
)
382 args_out_of_range (string
, start
);
383 pos_byte
= string_char_to_byte (string
, pos
);
386 bufp
= compile_pattern (regexp
, &search_regs
,
387 (!NILP (current_buffer
->case_fold_search
)
388 ? DOWNCASE_TABLE
: Qnil
),
390 STRING_MULTIBYTE (string
));
392 re_match_object
= string
;
394 val
= re_search (bufp
, (char *) SDATA (string
),
395 SBYTES (string
), pos_byte
,
396 SBYTES (string
) - pos_byte
,
399 last_thing_searched
= Qt
;
402 if (val
< 0) return Qnil
;
404 for (i
= 0; i
< search_regs
.num_regs
; i
++)
405 if (search_regs
.start
[i
] >= 0)
408 = string_byte_to_char (string
, search_regs
.start
[i
]);
410 = string_byte_to_char (string
, search_regs
.end
[i
]);
413 return make_number (string_byte_to_char (string
, val
));
416 DEFUN ("string-match", Fstring_match
, Sstring_match
, 2, 3, 0,
417 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
418 Case is ignored if `case-fold-search' is non-nil in the current buffer.
419 If third arg START is non-nil, start search at that index in STRING.
420 For index of first char beyond the match, do (match-end 0).
421 `match-end' and `match-beginning' also give indices of substrings
422 matched by parenthesis constructs in the pattern.
424 You can use the function `match-string' to extract the substrings
425 matched by the parenthesis constructions in REGEXP. */)
426 (regexp
, string
, start
)
427 Lisp_Object regexp
, string
, start
;
429 return string_match_1 (regexp
, string
, start
, 0);
432 DEFUN ("posix-string-match", Fposix_string_match
, Sposix_string_match
, 2, 3, 0,
433 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
434 Find the longest match, in accord with Posix regular expression rules.
435 Case is ignored if `case-fold-search' is non-nil in the current buffer.
436 If third arg START is non-nil, start search at that index in STRING.
437 For index of first char beyond the match, do (match-end 0).
438 `match-end' and `match-beginning' also give indices of substrings
439 matched by parenthesis constructs in the pattern. */)
440 (regexp
, string
, start
)
441 Lisp_Object regexp
, string
, start
;
443 return string_match_1 (regexp
, string
, start
, 1);
446 /* Match REGEXP against STRING, searching all of STRING,
447 and return the index of the match, or negative on failure.
448 This does not clobber the match data. */
451 fast_string_match (regexp
, string
)
452 Lisp_Object regexp
, string
;
455 struct re_pattern_buffer
*bufp
;
457 bufp
= compile_pattern (regexp
, 0, Qnil
,
458 0, STRING_MULTIBYTE (string
));
460 re_match_object
= string
;
462 val
= re_search (bufp
, (char *) SDATA (string
),
469 /* Match REGEXP against STRING, searching all of STRING ignoring case,
470 and return the index of the match, or negative on failure.
471 This does not clobber the match data.
472 We assume that STRING contains single-byte characters. */
474 extern Lisp_Object Vascii_downcase_table
;
477 fast_c_string_match_ignore_case (regexp
, string
)
482 struct re_pattern_buffer
*bufp
;
483 int len
= strlen (string
);
485 regexp
= string_make_unibyte (regexp
);
486 re_match_object
= Qt
;
487 bufp
= compile_pattern (regexp
, 0,
488 Vascii_downcase_table
, 0,
491 val
= re_search (bufp
, string
, len
, 0, len
, 0);
496 /* The newline cache: remembering which sections of text have no newlines. */
498 /* If the user has requested newline caching, make sure it's on.
499 Otherwise, make sure it's off.
500 This is our cheezy way of associating an action with the change of
501 state of a buffer-local variable. */
503 newline_cache_on_off (buf
)
506 if (NILP (buf
->cache_long_line_scans
))
508 /* It should be off. */
509 if (buf
->newline_cache
)
511 free_region_cache (buf
->newline_cache
);
512 buf
->newline_cache
= 0;
517 /* It should be on. */
518 if (buf
->newline_cache
== 0)
519 buf
->newline_cache
= new_region_cache ();
524 /* Search for COUNT instances of the character TARGET between START and END.
526 If COUNT is positive, search forwards; END must be >= START.
527 If COUNT is negative, search backwards for the -COUNTth instance;
528 END must be <= START.
529 If COUNT is zero, do anything you please; run rogue, for all I care.
531 If END is zero, use BEGV or ZV instead, as appropriate for the
532 direction indicated by COUNT.
534 If we find COUNT instances, set *SHORTAGE to zero, and return the
535 position after the COUNTth match. Note that for reverse motion
536 this is not the same as the usual convention for Emacs motion commands.
538 If we don't find COUNT instances before reaching END, set *SHORTAGE
539 to the number of TARGETs left unfound, and return END.
541 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
542 except when inside redisplay. */
545 scan_buffer (target
, start
, end
, count
, shortage
, allow_quit
)
552 struct region_cache
*newline_cache
;
563 if (! end
) end
= BEGV
;
566 newline_cache_on_off (current_buffer
);
567 newline_cache
= current_buffer
->newline_cache
;
572 immediate_quit
= allow_quit
;
577 /* Our innermost scanning loop is very simple; it doesn't know
578 about gaps, buffer ends, or the newline cache. ceiling is
579 the position of the last character before the next such
580 obstacle --- the last character the dumb search loop should
582 int ceiling_byte
= CHAR_TO_BYTE (end
) - 1;
583 int start_byte
= CHAR_TO_BYTE (start
);
586 /* If we're looking for a newline, consult the newline cache
587 to see where we can avoid some scanning. */
588 if (target
== '\n' && newline_cache
)
592 while (region_cache_forward
593 (current_buffer
, newline_cache
, start_byte
, &next_change
))
594 start_byte
= next_change
;
595 immediate_quit
= allow_quit
;
597 /* START should never be after END. */
598 if (start_byte
> ceiling_byte
)
599 start_byte
= ceiling_byte
;
601 /* Now the text after start is an unknown region, and
602 next_change is the position of the next known region. */
603 ceiling_byte
= min (next_change
- 1, ceiling_byte
);
606 /* The dumb loop can only scan text stored in contiguous
607 bytes. BUFFER_CEILING_OF returns the last character
608 position that is contiguous, so the ceiling is the
609 position after that. */
610 tem
= BUFFER_CEILING_OF (start_byte
);
611 ceiling_byte
= min (tem
, ceiling_byte
);
614 /* The termination address of the dumb loop. */
615 register unsigned char *ceiling_addr
616 = BYTE_POS_ADDR (ceiling_byte
) + 1;
617 register unsigned char *cursor
618 = BYTE_POS_ADDR (start_byte
);
619 unsigned char *base
= cursor
;
621 while (cursor
< ceiling_addr
)
623 unsigned char *scan_start
= cursor
;
626 while (*cursor
!= target
&& ++cursor
< ceiling_addr
)
629 /* If we're looking for newlines, cache the fact that
630 the region from start to cursor is free of them. */
631 if (target
== '\n' && newline_cache
)
632 know_region_cache (current_buffer
, newline_cache
,
633 start_byte
+ scan_start
- base
,
634 start_byte
+ cursor
- base
);
636 /* Did we find the target character? */
637 if (cursor
< ceiling_addr
)
642 return BYTE_TO_CHAR (start_byte
+ cursor
- base
+ 1);
648 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
654 /* The last character to check before the next obstacle. */
655 int ceiling_byte
= CHAR_TO_BYTE (end
);
656 int start_byte
= CHAR_TO_BYTE (start
);
659 /* Consult the newline cache, if appropriate. */
660 if (target
== '\n' && newline_cache
)
664 while (region_cache_backward
665 (current_buffer
, newline_cache
, start_byte
, &next_change
))
666 start_byte
= next_change
;
667 immediate_quit
= allow_quit
;
669 /* Start should never be at or before end. */
670 if (start_byte
<= ceiling_byte
)
671 start_byte
= ceiling_byte
+ 1;
673 /* Now the text before start is an unknown region, and
674 next_change is the position of the next known region. */
675 ceiling_byte
= max (next_change
, ceiling_byte
);
678 /* Stop scanning before the gap. */
679 tem
= BUFFER_FLOOR_OF (start_byte
- 1);
680 ceiling_byte
= max (tem
, ceiling_byte
);
683 /* The termination address of the dumb loop. */
684 register unsigned char *ceiling_addr
= BYTE_POS_ADDR (ceiling_byte
);
685 register unsigned char *cursor
= BYTE_POS_ADDR (start_byte
- 1);
686 unsigned char *base
= cursor
;
688 while (cursor
>= ceiling_addr
)
690 unsigned char *scan_start
= cursor
;
692 while (*cursor
!= target
&& --cursor
>= ceiling_addr
)
695 /* If we're looking for newlines, cache the fact that
696 the region from after the cursor to start is free of them. */
697 if (target
== '\n' && newline_cache
)
698 know_region_cache (current_buffer
, newline_cache
,
699 start_byte
+ cursor
- base
,
700 start_byte
+ scan_start
- base
);
702 /* Did we find the target character? */
703 if (cursor
>= ceiling_addr
)
708 return BYTE_TO_CHAR (start_byte
+ cursor
- base
);
714 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
720 *shortage
= count
* direction
;
724 /* Search for COUNT instances of a line boundary, which means either a
725 newline or (if selective display enabled) a carriage return.
726 Start at START. If COUNT is negative, search backwards.
728 We report the resulting position by calling TEMP_SET_PT_BOTH.
730 If we find COUNT instances. we position after (always after,
731 even if scanning backwards) the COUNTth match, and return 0.
733 If we don't find COUNT instances before reaching the end of the
734 buffer (or the beginning, if scanning backwards), we return
735 the number of line boundaries left unfound, and position at
736 the limit we bumped up against.
738 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
739 except in special cases. */
742 scan_newline (start
, start_byte
, limit
, limit_byte
, count
, allow_quit
)
743 int start
, start_byte
;
744 int limit
, limit_byte
;
748 int direction
= ((count
> 0) ? 1 : -1);
750 register unsigned char *cursor
;
753 register int ceiling
;
754 register unsigned char *ceiling_addr
;
756 int old_immediate_quit
= immediate_quit
;
758 /* The code that follows is like scan_buffer
759 but checks for either newline or carriage return. */
764 start_byte
= CHAR_TO_BYTE (start
);
768 while (start_byte
< limit_byte
)
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
));
776 while (*cursor
!= '\n' && ++cursor
!= ceiling_addr
)
779 if (cursor
!= ceiling_addr
)
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
);
790 if (++cursor
== ceiling_addr
)
796 start_byte
+= cursor
- base
;
801 while (start_byte
> limit_byte
)
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);
809 while (--cursor
!= ceiling_addr
&& *cursor
!= '\n')
812 if (cursor
!= ceiling_addr
)
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
);
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;
833 TEMP_SET_PT_BOTH (limit
, limit_byte
);
834 immediate_quit
= old_immediate_quit
;
836 return count
* direction
;
840 find_next_newline_no_quit (from
, cnt
)
841 register int from
, cnt
;
843 return scan_buffer ('\n', from
, 0, cnt
, (int *) 0, 0);
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. */
851 find_before_next_newline (from
, to
, cnt
)
855 int pos
= scan_buffer ('\n', from
, to
, cnt
, &shortage
, 1);
863 /* Subroutines of Lisp buffer search functions. */
866 search_command (string
, bound
, noerror
, count
, direction
, RE
, posix
)
867 Lisp_Object string
, bound
, noerror
, count
;
878 CHECK_NUMBER (count
);
882 CHECK_STRING (string
);
886 lim
= ZV
, lim_byte
= ZV_BYTE
;
888 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
892 CHECK_NUMBER_COERCE_MARKER (bound
);
894 if (n
> 0 ? lim
< PT
: lim
> PT
)
895 error ("Invalid search bound (wrong side of point)");
897 lim
= ZV
, lim_byte
= ZV_BYTE
;
899 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
901 lim_byte
= CHAR_TO_BYTE (lim
);
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
908 (!NILP (current_buffer
->case_fold_search
)
909 ? current_buffer
->case_eqv_table
915 return signal_failure (string
);
916 if (!EQ (noerror
, Qt
))
918 if (lim
< BEGV
|| lim
> ZV
)
920 SET_PT_BOTH (lim
, lim_byte
);
922 #if 0 /* This would be clean, but maybe programs depend on
923 a value of nil here. */
931 if (np
< BEGV
|| np
> ZV
)
936 return make_number (np
);
939 /* Return 1 if REGEXP it matches just one constant string. */
942 trivial_regexp_p (regexp
)
945 int len
= SBYTES (regexp
);
946 unsigned char *s
= SDATA (regexp
);
951 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
958 case '|': case '(': case ')': case '`': case '\'': case 'b':
959 case 'B': case '<': case '>': case 'w': case 'W': case 's':
960 case 'S': case '=': case '{': case '}':
961 case 'c': case 'C': /* for categoryspec and notcategoryspec */
962 case '1': case '2': case '3': case '4': case '5':
963 case '6': case '7': case '8': case '9':
971 /* Search for the n'th occurrence of STRING in the current buffer,
972 starting at position POS and stopping at position LIM,
973 treating STRING as a literal string if RE is false or as
974 a regular expression if RE is true.
976 If N is positive, searching is forward and LIM must be greater than POS.
977 If N is negative, searching is backward and LIM must be less than POS.
979 Returns -x if x occurrences remain to be found (x > 0),
980 or else the position at the beginning of the Nth occurrence
981 (if searching backward) or the end (if searching forward).
983 POSIX is nonzero if we want full backtracking (POSIX style)
984 for this pattern. 0 means backtrack only enough to get a valid match. */
986 #define TRANSLATE(out, trt, d) \
992 temp = Faref (trt, make_number (d)); \
993 if (INTEGERP (temp)) \
1004 search_buffer (string
, pos
, pos_byte
, lim
, lim_byte
, n
,
1005 RE
, trt
, inverse_trt
, posix
)
1014 Lisp_Object inverse_trt
;
1017 int len
= SCHARS (string
);
1018 int len_byte
= SBYTES (string
);
1021 if (running_asynch_code
)
1022 save_search_regs ();
1024 /* Searching 0 times means don't move. */
1025 /* Null string is found at starting position. */
1026 if (len
== 0 || n
== 0)
1028 set_search_regs (pos_byte
, 0);
1032 if (RE
&& !trivial_regexp_p (string
))
1034 unsigned char *p1
, *p2
;
1036 struct re_pattern_buffer
*bufp
;
1038 bufp
= compile_pattern (string
, &search_regs
, trt
, posix
,
1039 !NILP (current_buffer
->enable_multibyte_characters
));
1041 immediate_quit
= 1; /* Quit immediately if user types ^G,
1042 because letting this function finish
1043 can take too long. */
1044 QUIT
; /* Do a pending quit right away,
1045 to avoid paradoxical behavior */
1046 /* Get pointers and sizes of the two strings
1047 that make up the visible portion of the buffer. */
1050 s1
= GPT_BYTE
- BEGV_BYTE
;
1052 s2
= ZV_BYTE
- GPT_BYTE
;
1056 s2
= ZV_BYTE
- BEGV_BYTE
;
1061 s1
= ZV_BYTE
- BEGV_BYTE
;
1064 re_match_object
= Qnil
;
1069 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1070 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1072 /* Don't allow match past current point */
1073 pos_byte
- BEGV_BYTE
);
1076 matcher_overflow ();
1080 pos_byte
= search_regs
.start
[0] + BEGV_BYTE
;
1081 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1082 if (search_regs
.start
[i
] >= 0)
1084 search_regs
.start
[i
]
1085 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1087 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1089 XSETBUFFER (last_thing_searched
, current_buffer
);
1090 /* Set pos to the new position. */
1091 pos
= search_regs
.start
[0];
1103 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1104 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1106 lim_byte
- BEGV_BYTE
);
1109 matcher_overflow ();
1113 pos_byte
= search_regs
.end
[0] + BEGV_BYTE
;
1114 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1115 if (search_regs
.start
[i
] >= 0)
1117 search_regs
.start
[i
]
1118 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1120 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1122 XSETBUFFER (last_thing_searched
, current_buffer
);
1123 pos
= search_regs
.end
[0];
1135 else /* non-RE case */
1137 unsigned char *raw_pattern
, *pat
;
1138 int raw_pattern_size
;
1139 int raw_pattern_size_byte
;
1140 unsigned char *patbuf
;
1141 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1142 unsigned char *base_pat
= SDATA (string
);
1143 int charset_base
= -1;
1144 int boyer_moore_ok
= 1;
1146 /* MULTIBYTE says whether the text to be searched is multibyte.
1147 We must convert PATTERN to match that, or we will not really
1148 find things right. */
1150 if (multibyte
== STRING_MULTIBYTE (string
))
1152 raw_pattern
= (unsigned char *) SDATA (string
);
1153 raw_pattern_size
= SCHARS (string
);
1154 raw_pattern_size_byte
= SBYTES (string
);
1158 raw_pattern_size
= SCHARS (string
);
1159 raw_pattern_size_byte
1160 = count_size_as_multibyte (SDATA (string
),
1162 raw_pattern
= (unsigned char *) alloca (raw_pattern_size_byte
+ 1);
1163 copy_text (SDATA (string
), raw_pattern
,
1164 SCHARS (string
), 0, 1);
1168 /* Converting multibyte to single-byte.
1170 ??? Perhaps this conversion should be done in a special way
1171 by subtracting nonascii-insert-offset from each non-ASCII char,
1172 so that only the multibyte chars which really correspond to
1173 the chosen single-byte character set can possibly match. */
1174 raw_pattern_size
= SCHARS (string
);
1175 raw_pattern_size_byte
= SCHARS (string
);
1176 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
1177 copy_text (SDATA (string
), raw_pattern
,
1178 SBYTES (string
), 1, 0);
1181 /* Copy and optionally translate the pattern. */
1182 len
= raw_pattern_size
;
1183 len_byte
= raw_pattern_size_byte
;
1184 patbuf
= (unsigned char *) alloca (len_byte
);
1186 base_pat
= raw_pattern
;
1191 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1192 int c
, translated
, inverse
;
1193 int in_charlen
, charlen
;
1195 /* If we got here and the RE flag is set, it's because we're
1196 dealing with a regexp known to be trivial, so the backslash
1197 just quotes the next character. */
1198 if (RE
&& *base_pat
== '\\')
1205 c
= STRING_CHAR_AND_LENGTH (base_pat
, len_byte
, in_charlen
);
1207 /* Translate the character, if requested. */
1208 TRANSLATE (translated
, trt
, c
);
1209 /* If translation changed the byte-length, go back
1210 to the original character. */
1211 charlen
= CHAR_STRING (translated
, str
);
1212 if (in_charlen
!= charlen
)
1215 charlen
= CHAR_STRING (c
, str
);
1218 /* If we are searching for something strange,
1219 an invalid multibyte code, don't use boyer-moore. */
1220 if (! ASCII_BYTE_P (translated
)
1221 && (charlen
== 1 /* 8bit code */
1222 || charlen
!= in_charlen
/* invalid multibyte code */
1226 TRANSLATE (inverse
, inverse_trt
, c
);
1228 /* Did this char actually get translated?
1229 Would any other char get translated into it? */
1230 if (translated
!= c
|| inverse
!= c
)
1232 /* Keep track of which character set row
1233 contains the characters that need translation. */
1234 int charset_base_code
= c
& ~CHAR_FIELD3_MASK
;
1235 int inverse_charset_base
= inverse
& ~CHAR_FIELD3_MASK
;
1237 if (charset_base_code
!= inverse_charset_base
)
1239 else if (charset_base
== -1)
1240 charset_base
= charset_base_code
;
1241 else if (charset_base
!= charset_base_code
)
1242 /* If two different rows appear, needing translation,
1243 then we cannot use boyer_moore search. */
1247 /* Store this character into the translated pattern. */
1248 bcopy (str
, pat
, charlen
);
1250 base_pat
+= in_charlen
;
1251 len_byte
-= in_charlen
;
1256 /* Unibyte buffer. */
1262 /* If we got here and the RE flag is set, it's because we're
1263 dealing with a regexp known to be trivial, so the backslash
1264 just quotes the next character. */
1265 if (RE
&& *base_pat
== '\\')
1271 TRANSLATE (translated
, trt
, c
);
1272 *pat
++ = translated
;
1276 len_byte
= pat
- patbuf
;
1277 len
= raw_pattern_size
;
1278 pat
= base_pat
= patbuf
;
1281 return boyer_moore (n
, pat
, len
, len_byte
, trt
, inverse_trt
,
1282 pos
, pos_byte
, lim
, lim_byte
,
1285 return simple_search (n
, pat
, len
, len_byte
, trt
,
1286 pos
, pos_byte
, lim
, lim_byte
);
1290 /* Do a simple string search N times for the string PAT,
1291 whose length is LEN/LEN_BYTE,
1292 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1293 TRT is the translation table.
1295 Return the character position where the match is found.
1296 Otherwise, if M matches remained to be found, return -M.
1298 This kind of search works regardless of what is in PAT and
1299 regardless of what is in TRT. It is used in cases where
1300 boyer_moore cannot work. */
1303 simple_search (n
, pat
, len
, len_byte
, trt
, pos
, pos_byte
, lim
, lim_byte
)
1311 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1312 int forward
= n
> 0;
1314 if (lim
> pos
&& multibyte
)
1319 /* Try matching at position POS. */
1321 int this_pos_byte
= pos_byte
;
1323 int this_len_byte
= len_byte
;
1324 unsigned char *p
= pat
;
1325 if (pos
+ len
> lim
)
1328 while (this_len
> 0)
1330 int charlen
, buf_charlen
;
1333 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1334 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1335 ZV_BYTE
- this_pos_byte
,
1337 TRANSLATE (buf_ch
, trt
, buf_ch
);
1339 if (buf_ch
!= pat_ch
)
1342 this_len_byte
-= charlen
;
1346 this_pos_byte
+= buf_charlen
;
1353 pos_byte
+= len_byte
;
1357 INC_BOTH (pos
, pos_byte
);
1367 /* Try matching at position POS. */
1370 unsigned char *p
= pat
;
1372 if (pos
+ len
> lim
)
1375 while (this_len
> 0)
1378 int buf_ch
= FETCH_BYTE (this_pos
);
1379 TRANSLATE (buf_ch
, trt
, buf_ch
);
1381 if (buf_ch
!= pat_ch
)
1399 /* Backwards search. */
1400 else if (lim
< pos
&& multibyte
)
1405 /* Try matching at position POS. */
1406 int this_pos
= pos
- len
;
1407 int this_pos_byte
= pos_byte
- len_byte
;
1409 int this_len_byte
= len_byte
;
1410 unsigned char *p
= pat
;
1412 if (pos
- len
< lim
)
1415 while (this_len
> 0)
1417 int charlen
, buf_charlen
;
1420 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1421 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1422 ZV_BYTE
- this_pos_byte
,
1424 TRANSLATE (buf_ch
, trt
, buf_ch
);
1426 if (buf_ch
!= pat_ch
)
1429 this_len_byte
-= charlen
;
1432 this_pos_byte
+= buf_charlen
;
1439 pos_byte
-= len_byte
;
1443 DEC_BOTH (pos
, pos_byte
);
1453 /* Try matching at position POS. */
1454 int this_pos
= pos
- len
;
1456 unsigned char *p
= pat
;
1458 if (pos
- len
< lim
)
1461 while (this_len
> 0)
1464 int buf_ch
= FETCH_BYTE (this_pos
);
1465 TRANSLATE (buf_ch
, trt
, buf_ch
);
1467 if (buf_ch
!= pat_ch
)
1489 set_search_regs ((multibyte
? pos_byte
: pos
) - len_byte
, len_byte
);
1491 set_search_regs (multibyte
? pos_byte
: pos
, len_byte
);
1501 /* Do Boyer-Moore search N times for the string PAT,
1502 whose length is LEN/LEN_BYTE,
1503 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1504 DIRECTION says which direction we search in.
1505 TRT and INVERSE_TRT are translation tables.
1507 This kind of search works if all the characters in PAT that have
1508 nontrivial translation are the same aside from the last byte. This
1509 makes it possible to translate just the last byte of a character,
1510 and do so after just a simple test of the context.
1512 If that criterion is not satisfied, do not call this function. */
1515 boyer_moore (n
, base_pat
, len
, len_byte
, trt
, inverse_trt
,
1516 pos
, pos_byte
, lim
, lim_byte
, charset_base
)
1518 unsigned char *base_pat
;
1521 Lisp_Object inverse_trt
;
1526 int direction
= ((n
> 0) ? 1 : -1);
1527 register int dirlen
;
1528 int infinity
, limit
, stride_for_teases
= 0;
1529 register int *BM_tab
;
1531 register unsigned char *cursor
, *p_limit
;
1533 unsigned char *pat
, *pat_end
;
1534 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1536 unsigned char simple_translate
[0400];
1537 int translate_prev_byte
= 0;
1538 int translate_anteprev_byte
= 0;
1541 int BM_tab_space
[0400];
1542 BM_tab
= &BM_tab_space
[0];
1544 BM_tab
= (int *) alloca (0400 * sizeof (int));
1546 /* The general approach is that we are going to maintain that we know */
1547 /* the first (closest to the present position, in whatever direction */
1548 /* we're searching) character that could possibly be the last */
1549 /* (furthest from present position) character of a valid match. We */
1550 /* advance the state of our knowledge by looking at that character */
1551 /* and seeing whether it indeed matches the last character of the */
1552 /* pattern. If it does, we take a closer look. If it does not, we */
1553 /* move our pointer (to putative last characters) as far as is */
1554 /* logically possible. This amount of movement, which I call a */
1555 /* stride, will be the length of the pattern if the actual character */
1556 /* appears nowhere in the pattern, otherwise it will be the distance */
1557 /* from the last occurrence of that character to the end of the */
1559 /* As a coding trick, an enormous stride is coded into the table for */
1560 /* characters that match the last character. This allows use of only */
1561 /* a single test, a test for having gone past the end of the */
1562 /* permissible match region, to test for both possible matches (when */
1563 /* the stride goes past the end immediately) and failure to */
1564 /* match (where you get nudged past the end one stride at a time). */
1566 /* Here we make a "mickey mouse" BM table. The stride of the search */
1567 /* is determined only by the last character of the putative match. */
1568 /* If that character does not match, we will stride the proper */
1569 /* distance to propose a match that superimposes it on the last */
1570 /* instance of a character that matches it (per trt), or misses */
1571 /* it entirely if there is none. */
1573 dirlen
= len_byte
* direction
;
1574 infinity
= dirlen
- (lim_byte
+ pos_byte
+ len_byte
+ len_byte
) * direction
;
1576 /* Record position after the end of the pattern. */
1577 pat_end
= base_pat
+ len_byte
;
1578 /* BASE_PAT points to a character that we start scanning from.
1579 It is the first character in a forward search,
1580 the last character in a backward search. */
1582 base_pat
= pat_end
- 1;
1584 BM_tab_base
= BM_tab
;
1586 j
= dirlen
; /* to get it in a register */
1587 /* A character that does not appear in the pattern induces a */
1588 /* stride equal to the pattern length. */
1589 while (BM_tab_base
!= BM_tab
)
1597 /* We use this for translation, instead of TRT itself.
1598 We fill this in to handle the characters that actually
1599 occur in the pattern. Others don't matter anyway! */
1600 bzero (simple_translate
, sizeof simple_translate
);
1601 for (i
= 0; i
< 0400; i
++)
1602 simple_translate
[i
] = i
;
1605 while (i
!= infinity
)
1607 unsigned char *ptr
= base_pat
+ i
;
1615 int this_translated
= 1;
1618 /* Is *PTR the last byte of a character? */
1619 && (pat_end
- ptr
== 1 || CHAR_HEAD_P (ptr
[1])))
1621 unsigned char *charstart
= ptr
;
1622 while (! CHAR_HEAD_P (*charstart
))
1624 untranslated
= STRING_CHAR (charstart
, ptr
- charstart
+ 1);
1625 if (charset_base
== (untranslated
& ~CHAR_FIELD3_MASK
))
1627 TRANSLATE (ch
, trt
, untranslated
);
1628 if (! CHAR_HEAD_P (*ptr
))
1630 translate_prev_byte
= ptr
[-1];
1631 if (! CHAR_HEAD_P (translate_prev_byte
))
1632 translate_anteprev_byte
= ptr
[-2];
1637 this_translated
= 0;
1641 else if (!multibyte
)
1642 TRANSLATE (ch
, trt
, *ptr
);
1646 this_translated
= 0;
1650 j
= ((unsigned char) ch
) | 0200;
1652 j
= (unsigned char) ch
;
1655 stride_for_teases
= BM_tab
[j
];
1657 BM_tab
[j
] = dirlen
- i
;
1658 /* A translation table is accompanied by its inverse -- see */
1659 /* comment following downcase_table for details */
1660 if (this_translated
)
1662 int starting_ch
= ch
;
1666 TRANSLATE (ch
, inverse_trt
, ch
);
1668 j
= ((unsigned char) ch
) | 0200;
1670 j
= (unsigned char) ch
;
1672 /* For all the characters that map into CH,
1673 set up simple_translate to map the last byte
1675 simple_translate
[j
] = starting_j
;
1676 if (ch
== starting_ch
)
1678 BM_tab
[j
] = dirlen
- i
;
1687 stride_for_teases
= BM_tab
[j
];
1688 BM_tab
[j
] = dirlen
- i
;
1690 /* stride_for_teases tells how much to stride if we get a */
1691 /* match on the far character but are subsequently */
1692 /* disappointed, by recording what the stride would have been */
1693 /* for that character if the last character had been */
1696 infinity
= dirlen
- infinity
;
1697 pos_byte
+= dirlen
- ((direction
> 0) ? direction
: 0);
1698 /* loop invariant - POS_BYTE points at where last char (first
1699 char if reverse) of pattern would align in a possible match. */
1703 unsigned char *tail_end_ptr
;
1705 /* It's been reported that some (broken) compiler thinks that
1706 Boolean expressions in an arithmetic context are unsigned.
1707 Using an explicit ?1:0 prevents this. */
1708 if ((lim_byte
- pos_byte
- ((direction
> 0) ? 1 : 0)) * direction
1710 return (n
* (0 - direction
));
1711 /* First we do the part we can by pointers (maybe nothing) */
1714 limit
= pos_byte
- dirlen
+ direction
;
1717 limit
= BUFFER_CEILING_OF (limit
);
1718 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1719 can take on without hitting edge of buffer or the gap. */
1720 limit
= min (limit
, pos_byte
+ 20000);
1721 limit
= min (limit
, lim_byte
- 1);
1725 limit
= BUFFER_FLOOR_OF (limit
);
1726 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1727 can take on without hitting edge of buffer or the gap. */
1728 limit
= max (limit
, pos_byte
- 20000);
1729 limit
= max (limit
, lim_byte
);
1731 tail_end
= BUFFER_CEILING_OF (pos_byte
) + 1;
1732 tail_end_ptr
= BYTE_POS_ADDR (tail_end
);
1734 if ((limit
- pos_byte
) * direction
> 20)
1738 p_limit
= BYTE_POS_ADDR (limit
);
1739 p2
= (cursor
= BYTE_POS_ADDR (pos_byte
));
1740 /* In this loop, pos + cursor - p2 is the surrogate for pos */
1741 while (1) /* use one cursor setting as long as i can */
1743 if (direction
> 0) /* worth duplicating */
1745 /* Use signed comparison if appropriate
1746 to make cursor+infinity sure to be > p_limit.
1747 Assuming that the buffer lies in a range of addresses
1748 that are all "positive" (as ints) or all "negative",
1749 either kind of comparison will work as long
1750 as we don't step by infinity. So pick the kind
1751 that works when we do step by infinity. */
1752 if ((EMACS_INT
) (p_limit
+ infinity
) > (EMACS_INT
) p_limit
)
1753 while ((EMACS_INT
) cursor
<= (EMACS_INT
) p_limit
)
1754 cursor
+= BM_tab
[*cursor
];
1756 while ((EMACS_UINT
) cursor
<= (EMACS_UINT
) p_limit
)
1757 cursor
+= BM_tab
[*cursor
];
1761 if ((EMACS_INT
) (p_limit
+ infinity
) < (EMACS_INT
) p_limit
)
1762 while ((EMACS_INT
) cursor
>= (EMACS_INT
) p_limit
)
1763 cursor
+= BM_tab
[*cursor
];
1765 while ((EMACS_UINT
) cursor
>= (EMACS_UINT
) p_limit
)
1766 cursor
+= BM_tab
[*cursor
];
1768 /* If you are here, cursor is beyond the end of the searched region. */
1769 /* This can happen if you match on the far character of the pattern, */
1770 /* because the "stride" of that character is infinity, a number able */
1771 /* to throw you well beyond the end of the search. It can also */
1772 /* happen if you fail to match within the permitted region and would */
1773 /* otherwise try a character beyond that region */
1774 if ((cursor
- p_limit
) * direction
<= len_byte
)
1775 break; /* a small overrun is genuine */
1776 cursor
-= infinity
; /* large overrun = hit */
1777 i
= dirlen
- direction
;
1780 while ((i
-= direction
) + direction
!= 0)
1783 cursor
-= direction
;
1784 /* Translate only the last byte of a character. */
1786 || ((cursor
== tail_end_ptr
1787 || CHAR_HEAD_P (cursor
[1]))
1788 && (CHAR_HEAD_P (cursor
[0])
1789 || (translate_prev_byte
== cursor
[-1]
1790 && (CHAR_HEAD_P (translate_prev_byte
)
1791 || translate_anteprev_byte
== cursor
[-2])))))
1792 ch
= simple_translate
[*cursor
];
1801 while ((i
-= direction
) + direction
!= 0)
1803 cursor
-= direction
;
1804 if (pat
[i
] != *cursor
)
1808 cursor
+= dirlen
- i
- direction
; /* fix cursor */
1809 if (i
+ direction
== 0)
1813 cursor
-= direction
;
1815 position
= pos_byte
+ cursor
- p2
+ ((direction
> 0)
1816 ? 1 - len_byte
: 0);
1817 set_search_regs (position
, len_byte
);
1819 if ((n
-= direction
) != 0)
1820 cursor
+= dirlen
; /* to resume search */
1822 return ((direction
> 0)
1823 ? search_regs
.end
[0] : search_regs
.start
[0]);
1826 cursor
+= stride_for_teases
; /* <sigh> we lose - */
1828 pos_byte
+= cursor
- p2
;
1831 /* Now we'll pick up a clump that has to be done the hard */
1832 /* way because it covers a discontinuity */
1834 limit
= ((direction
> 0)
1835 ? BUFFER_CEILING_OF (pos_byte
- dirlen
+ 1)
1836 : BUFFER_FLOOR_OF (pos_byte
- dirlen
- 1));
1837 limit
= ((direction
> 0)
1838 ? min (limit
+ len_byte
, lim_byte
- 1)
1839 : max (limit
- len_byte
, lim_byte
));
1840 /* LIMIT is now the last value POS_BYTE can have
1841 and still be valid for a possible match. */
1844 /* This loop can be coded for space rather than */
1845 /* speed because it will usually run only once. */
1846 /* (the reach is at most len + 21, and typically */
1847 /* does not exceed len) */
1848 while ((limit
- pos_byte
) * direction
>= 0)
1849 pos_byte
+= BM_tab
[FETCH_BYTE (pos_byte
)];
1850 /* now run the same tests to distinguish going off the */
1851 /* end, a match or a phony match. */
1852 if ((pos_byte
- limit
) * direction
<= len_byte
)
1853 break; /* ran off the end */
1854 /* Found what might be a match.
1855 Set POS_BYTE back to last (first if reverse) pos. */
1856 pos_byte
-= infinity
;
1857 i
= dirlen
- direction
;
1858 while ((i
-= direction
) + direction
!= 0)
1862 pos_byte
-= direction
;
1863 ptr
= BYTE_POS_ADDR (pos_byte
);
1864 /* Translate only the last byte of a character. */
1866 || ((ptr
== tail_end_ptr
1867 || CHAR_HEAD_P (ptr
[1]))
1868 && (CHAR_HEAD_P (ptr
[0])
1869 || (translate_prev_byte
== ptr
[-1]
1870 && (CHAR_HEAD_P (translate_prev_byte
)
1871 || translate_anteprev_byte
== ptr
[-2])))))
1872 ch
= simple_translate
[*ptr
];
1878 /* Above loop has moved POS_BYTE part or all the way
1879 back to the first pos (last pos if reverse).
1880 Set it once again at the last (first if reverse) char. */
1881 pos_byte
+= dirlen
- i
- direction
;
1882 if (i
+ direction
== 0)
1885 pos_byte
-= direction
;
1887 position
= pos_byte
+ ((direction
> 0) ? 1 - len_byte
: 0);
1889 set_search_regs (position
, len_byte
);
1891 if ((n
-= direction
) != 0)
1892 pos_byte
+= dirlen
; /* to resume search */
1894 return ((direction
> 0)
1895 ? search_regs
.end
[0] : search_regs
.start
[0]);
1898 pos_byte
+= stride_for_teases
;
1901 /* We have done one clump. Can we continue? */
1902 if ((lim_byte
- pos_byte
) * direction
< 0)
1903 return ((0 - n
) * direction
);
1905 return BYTE_TO_CHAR (pos_byte
);
1908 /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES
1909 for the overall match just found in the current buffer.
1910 Also clear out the match data for registers 1 and up. */
1913 set_search_regs (beg_byte
, nbytes
)
1914 int beg_byte
, nbytes
;
1918 /* Make sure we have registers in which to store
1919 the match position. */
1920 if (search_regs
.num_regs
== 0)
1922 search_regs
.start
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
1923 search_regs
.end
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
1924 search_regs
.num_regs
= 2;
1927 /* Clear out the other registers. */
1928 for (i
= 1; i
< search_regs
.num_regs
; i
++)
1930 search_regs
.start
[i
] = -1;
1931 search_regs
.end
[i
] = -1;
1934 search_regs
.start
[0] = BYTE_TO_CHAR (beg_byte
);
1935 search_regs
.end
[0] = BYTE_TO_CHAR (beg_byte
+ nbytes
);
1936 XSETBUFFER (last_thing_searched
, current_buffer
);
1939 /* Given a string of words separated by word delimiters,
1940 compute a regexp that matches those exact words
1941 separated by arbitrary punctuation. */
1947 register unsigned char *p
, *o
;
1948 register int i
, i_byte
, len
, punct_count
= 0, word_count
= 0;
1953 CHECK_STRING (string
);
1955 len
= SCHARS (string
);
1957 for (i
= 0, i_byte
= 0; i
< len
; )
1961 FETCH_STRING_CHAR_ADVANCE (c
, string
, i
, i_byte
);
1963 if (SYNTAX (c
) != Sword
)
1966 if (i
> 0 && SYNTAX (prev_c
) == Sword
)
1973 if (SYNTAX (prev_c
) == Sword
)
1976 return empty_string
;
1978 adjust
= - punct_count
+ 5 * (word_count
- 1) + 4;
1979 if (STRING_MULTIBYTE (string
))
1980 val
= make_uninit_multibyte_string (len
+ adjust
,
1984 val
= make_uninit_string (len
+ adjust
);
1991 for (i
= 0, i_byte
= 0; i
< len
; )
1994 int i_byte_orig
= i_byte
;
1996 FETCH_STRING_CHAR_ADVANCE (c
, string
, i
, i_byte
);
1998 if (SYNTAX (c
) == Sword
)
2000 bcopy (SDATA (string
) + i_byte_orig
, o
,
2001 i_byte
- i_byte_orig
);
2002 o
+= i_byte
- i_byte_orig
;
2004 else if (i
> 0 && SYNTAX (prev_c
) == Sword
&& --word_count
)
2022 DEFUN ("search-backward", Fsearch_backward
, Ssearch_backward
, 1, 4,
2023 "MSearch backward: ",
2024 doc
: /* Search backward from point for STRING.
2025 Set point to the beginning of the occurrence found, and return point.
2026 An optional second argument bounds the search; it is a buffer position.
2027 The match found must not extend before that position.
2028 Optional third argument, if t, means if fail just return nil (no error).
2029 If not nil and not t, position at limit of search and return nil.
2030 Optional fourth argument is repeat count--search for successive occurrences.
2032 Search case-sensitivity is determined by the value of the variable
2033 `case-fold-search', which see.
2035 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2036 (string
, bound
, noerror
, count
)
2037 Lisp_Object string
, bound
, noerror
, count
;
2039 return search_command (string
, bound
, noerror
, count
, -1, 0, 0);
2042 DEFUN ("search-forward", Fsearch_forward
, Ssearch_forward
, 1, 4, "MSearch: ",
2043 doc
: /* Search forward from point for STRING.
2044 Set point to the end of the occurrence found, and return point.
2045 An optional second argument bounds the search; it is a buffer position.
2046 The match found must not extend after that position. nil is equivalent
2048 Optional third argument, if t, means if fail just return nil (no error).
2049 If not nil and not t, move to limit of search and return nil.
2050 Optional fourth argument is repeat count--search for successive occurrences.
2052 Search case-sensitivity is determined by the value of the variable
2053 `case-fold-search', which see.
2055 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2056 (string
, bound
, noerror
, count
)
2057 Lisp_Object string
, bound
, noerror
, count
;
2059 return search_command (string
, bound
, noerror
, count
, 1, 0, 0);
2062 DEFUN ("word-search-backward", Fword_search_backward
, Sword_search_backward
, 1, 4,
2063 "sWord search backward: ",
2064 doc
: /* Search backward from point for STRING, ignoring differences in punctuation.
2065 Set point to the beginning of the occurrence found, and return point.
2066 An optional second argument bounds the search; it is a buffer position.
2067 The match found must not extend before that position.
2068 Optional third argument, if t, means if fail just return nil (no error).
2069 If not nil and not t, move to limit of search and return nil.
2070 Optional fourth argument is repeat count--search for successive occurrences. */)
2071 (string
, bound
, noerror
, count
)
2072 Lisp_Object string
, bound
, noerror
, count
;
2074 return search_command (wordify (string
), bound
, noerror
, count
, -1, 1, 0);
2077 DEFUN ("word-search-forward", Fword_search_forward
, Sword_search_forward
, 1, 4,
2079 doc
: /* Search forward from point for STRING, ignoring differences in punctuation.
2080 Set point to the end of the occurrence found, and return point.
2081 An optional second argument bounds the search; it is a buffer position.
2082 The match found must not extend after that position.
2083 Optional third argument, if t, means if fail just return nil (no error).
2084 If not nil and not t, move to limit of search and return nil.
2085 Optional fourth argument is repeat count--search for successive occurrences. */)
2086 (string
, bound
, noerror
, count
)
2087 Lisp_Object string
, bound
, noerror
, count
;
2089 return search_command (wordify (string
), bound
, noerror
, count
, 1, 1, 0);
2092 DEFUN ("re-search-backward", Fre_search_backward
, Sre_search_backward
, 1, 4,
2093 "sRE search backward: ",
2094 doc
: /* Search backward from point for match for regular expression REGEXP.
2095 Set point to the beginning of the match, and return point.
2096 The match found is the one starting last in the buffer
2097 and yet ending before the origin of the search.
2098 An optional second argument bounds the search; it is a buffer position.
2099 The match found must start at or after that position.
2100 Optional third argument, if t, means if fail just return nil (no error).
2101 If not nil and not t, move to limit of search and return nil.
2102 Optional fourth argument is repeat count--search for successive occurrences.
2103 See also the functions `match-beginning', `match-end', `match-string',
2104 and `replace-match'. */)
2105 (regexp
, bound
, noerror
, count
)
2106 Lisp_Object regexp
, bound
, noerror
, count
;
2108 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 0);
2111 DEFUN ("re-search-forward", Fre_search_forward
, Sre_search_forward
, 1, 4,
2113 doc
: /* Search forward from point for regular expression REGEXP.
2114 Set point to the end of the occurrence found, and return point.
2115 An optional second argument bounds the search; it is a buffer position.
2116 The match found must not extend after that position.
2117 Optional third argument, if t, means if fail just return nil (no error).
2118 If not nil and not t, move to limit of search and return nil.
2119 Optional fourth argument is repeat count--search for successive occurrences.
2120 See also the functions `match-beginning', `match-end', `match-string',
2121 and `replace-match'. */)
2122 (regexp
, bound
, noerror
, count
)
2123 Lisp_Object regexp
, bound
, noerror
, count
;
2125 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 0);
2128 DEFUN ("posix-search-backward", Fposix_search_backward
, Sposix_search_backward
, 1, 4,
2129 "sPosix search backward: ",
2130 doc
: /* Search backward from point for match for regular expression REGEXP.
2131 Find the longest match in accord with Posix regular expression rules.
2132 Set point to the beginning of the match, and return point.
2133 The match found is the one starting last in the buffer
2134 and yet ending before the origin of the search.
2135 An optional second argument bounds the search; it is a buffer position.
2136 The match found must start at or after that position.
2137 Optional third argument, if t, means if fail just return nil (no error).
2138 If not nil and not t, move to limit of search and return nil.
2139 Optional fourth argument is repeat count--search for successive occurrences.
2140 See also the functions `match-beginning', `match-end', `match-string',
2141 and `replace-match'. */)
2142 (regexp
, bound
, noerror
, count
)
2143 Lisp_Object regexp
, bound
, noerror
, count
;
2145 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 1);
2148 DEFUN ("posix-search-forward", Fposix_search_forward
, Sposix_search_forward
, 1, 4,
2150 doc
: /* Search forward from point for regular expression REGEXP.
2151 Find the longest match in accord with Posix regular expression rules.
2152 Set point to the end of the occurrence found, and return point.
2153 An optional second argument bounds the search; it is a buffer position.
2154 The match found must not extend after that position.
2155 Optional third argument, if t, means if fail just return nil (no error).
2156 If not nil and not t, move to limit of search and return nil.
2157 Optional fourth argument is repeat count--search for successive occurrences.
2158 See also the functions `match-beginning', `match-end', `match-string',
2159 and `replace-match'. */)
2160 (regexp
, bound
, noerror
, count
)
2161 Lisp_Object regexp
, bound
, noerror
, count
;
2163 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 1);
2166 DEFUN ("replace-match", Freplace_match
, Sreplace_match
, 1, 5, 0,
2167 doc
: /* Replace text matched by last search with NEWTEXT.
2168 Leave point at the end of the replacement text.
2170 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
2171 Otherwise maybe capitalize the whole text, or maybe just word initials,
2172 based on the replaced text.
2173 If the replaced text has only capital letters
2174 and has at least one multiletter word, convert NEWTEXT to all caps.
2175 Otherwise if all words are capitalized in the replaced text,
2176 capitalize each word in NEWTEXT.
2178 If third arg LITERAL is non-nil, insert NEWTEXT literally.
2179 Otherwise treat `\\' as special:
2180 `\\&' in NEWTEXT means substitute original matched text.
2181 `\\N' means substitute what matched the Nth `\\(...\\)'.
2182 If Nth parens didn't match, substitute nothing.
2183 `\\\\' means insert one `\\'.
2184 Case conversion does not apply to these substitutions.
2186 FIXEDCASE and LITERAL are optional arguments.
2188 The optional fourth argument STRING can be a string to modify.
2189 This is meaningful when the previous match was done against STRING,
2190 using `string-match'. When used this way, `replace-match'
2191 creates and returns a new string made by copying STRING and replacing
2192 the part of STRING that was matched.
2194 The optional fifth argument SUBEXP specifies a subexpression;
2195 it says to replace just that subexpression with NEWTEXT,
2196 rather than replacing the entire matched text.
2197 This is, in a vague sense, the inverse of using `\\N' in NEWTEXT;
2198 `\\N' copies subexp N into NEWTEXT, but using N as SUBEXP puts
2199 NEWTEXT in place of subexp N.
2200 This is useful only after a regular expression search or match,
2201 since only regular expressions have distinguished subexpressions. */)
2202 (newtext
, fixedcase
, literal
, string
, subexp
)
2203 Lisp_Object newtext
, fixedcase
, literal
, string
, subexp
;
2205 enum { nochange
, all_caps
, cap_initial
} case_action
;
2206 register int pos
, pos_byte
;
2207 int some_multiletter_word
;
2210 int some_nonuppercase_initial
;
2211 register int c
, prevc
;
2213 int opoint
, newpoint
;
2215 CHECK_STRING (newtext
);
2217 if (! NILP (string
))
2218 CHECK_STRING (string
);
2220 case_action
= nochange
; /* We tried an initialization */
2221 /* but some C compilers blew it */
2223 if (search_regs
.num_regs
<= 0)
2224 error ("replace-match called before any match found");
2230 CHECK_NUMBER (subexp
);
2231 sub
= XINT (subexp
);
2232 if (sub
< 0 || sub
>= search_regs
.num_regs
)
2233 args_out_of_range (subexp
, make_number (search_regs
.num_regs
));
2238 if (search_regs
.start
[sub
] < BEGV
2239 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2240 || search_regs
.end
[sub
] > ZV
)
2241 args_out_of_range (make_number (search_regs
.start
[sub
]),
2242 make_number (search_regs
.end
[sub
]));
2246 if (search_regs
.start
[sub
] < 0
2247 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2248 || search_regs
.end
[sub
] > SCHARS (string
))
2249 args_out_of_range (make_number (search_regs
.start
[sub
]),
2250 make_number (search_regs
.end
[sub
]));
2253 if (NILP (fixedcase
))
2255 /* Decide how to casify by examining the matched text. */
2258 pos
= search_regs
.start
[sub
];
2259 last
= search_regs
.end
[sub
];
2262 pos_byte
= CHAR_TO_BYTE (pos
);
2264 pos_byte
= string_char_to_byte (string
, pos
);
2267 case_action
= all_caps
;
2269 /* some_multiletter_word is set nonzero if any original word
2270 is more than one letter long. */
2271 some_multiletter_word
= 0;
2273 some_nonuppercase_initial
= 0;
2280 c
= FETCH_CHAR (pos_byte
);
2281 INC_BOTH (pos
, pos_byte
);
2284 FETCH_STRING_CHAR_ADVANCE (c
, string
, pos
, pos_byte
);
2288 /* Cannot be all caps if any original char is lower case */
2291 if (SYNTAX (prevc
) != Sword
)
2292 some_nonuppercase_initial
= 1;
2294 some_multiletter_word
= 1;
2296 else if (!NOCASEP (c
))
2299 if (SYNTAX (prevc
) != Sword
)
2302 some_multiletter_word
= 1;
2306 /* If the initial is a caseless word constituent,
2307 treat that like a lowercase initial. */
2308 if (SYNTAX (prevc
) != Sword
)
2309 some_nonuppercase_initial
= 1;
2315 /* Convert to all caps if the old text is all caps
2316 and has at least one multiletter word. */
2317 if (! some_lowercase
&& some_multiletter_word
)
2318 case_action
= all_caps
;
2319 /* Capitalize each word, if the old text has all capitalized words. */
2320 else if (!some_nonuppercase_initial
&& some_multiletter_word
)
2321 case_action
= cap_initial
;
2322 else if (!some_nonuppercase_initial
&& some_uppercase
)
2323 /* Should x -> yz, operating on X, give Yz or YZ?
2324 We'll assume the latter. */
2325 case_action
= all_caps
;
2327 case_action
= nochange
;
2330 /* Do replacement in a string. */
2333 Lisp_Object before
, after
;
2335 before
= Fsubstring (string
, make_number (0),
2336 make_number (search_regs
.start
[sub
]));
2337 after
= Fsubstring (string
, make_number (search_regs
.end
[sub
]), Qnil
);
2339 /* Substitute parts of the match into NEWTEXT
2344 int lastpos_byte
= 0;
2345 /* We build up the substituted string in ACCUM. */
2348 int length
= SBYTES (newtext
);
2352 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2356 int delbackslash
= 0;
2358 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2362 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2366 substart
= search_regs
.start
[sub
];
2367 subend
= search_regs
.end
[sub
];
2369 else if (c
>= '1' && c
<= '9' && c
<= search_regs
.num_regs
+ '0')
2371 if (search_regs
.start
[c
- '0'] >= 0)
2373 substart
= search_regs
.start
[c
- '0'];
2374 subend
= search_regs
.end
[c
- '0'];
2380 error ("Invalid use of `\\' in replacement text");
2384 if (pos
- 2 != lastpos
)
2385 middle
= substring_both (newtext
, lastpos
,
2387 pos
- 2, pos_byte
- 2);
2390 accum
= concat3 (accum
, middle
,
2392 make_number (substart
),
2393 make_number (subend
)));
2395 lastpos_byte
= pos_byte
;
2397 else if (delbackslash
)
2399 middle
= substring_both (newtext
, lastpos
,
2401 pos
- 1, pos_byte
- 1);
2403 accum
= concat2 (accum
, middle
);
2405 lastpos_byte
= pos_byte
;
2410 middle
= substring_both (newtext
, lastpos
,
2416 newtext
= concat2 (accum
, middle
);
2419 /* Do case substitution in NEWTEXT if desired. */
2420 if (case_action
== all_caps
)
2421 newtext
= Fupcase (newtext
);
2422 else if (case_action
== cap_initial
)
2423 newtext
= Fupcase_initials (newtext
);
2425 return concat3 (before
, newtext
, after
);
2428 /* Record point, then move (quietly) to the start of the match. */
2429 if (PT
>= search_regs
.end
[sub
])
2431 else if (PT
> search_regs
.start
[sub
])
2432 opoint
= search_regs
.end
[sub
] - ZV
;
2436 /* If we want non-literal replacement,
2437 perform substitution on the replacement string. */
2440 int length
= SBYTES (newtext
);
2441 unsigned char *substed
;
2442 int substed_alloc_size
, substed_len
;
2443 int buf_multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
2444 int str_multibyte
= STRING_MULTIBYTE (newtext
);
2445 Lisp_Object rev_tbl
;
2446 int really_changed
= 0;
2448 rev_tbl
= (!buf_multibyte
&& CHAR_TABLE_P (Vnonascii_translation_table
)
2449 ? Fchar_table_extra_slot (Vnonascii_translation_table
,
2453 substed_alloc_size
= length
* 2 + 100;
2454 substed
= (unsigned char *) xmalloc (substed_alloc_size
+ 1);
2457 /* Go thru NEWTEXT, producing the actual text to insert in
2458 SUBSTED while adjusting multibyteness to that of the current
2461 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2463 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2464 unsigned char *add_stuff
= NULL
;
2470 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
, pos
, pos_byte
);
2472 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2476 /* Note that we don't have to increment POS. */
2477 c
= SREF (newtext
, pos_byte
++);
2479 c
= unibyte_char_to_multibyte (c
);
2482 /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
2483 or set IDX to a match index, which means put that part
2484 of the buffer text into SUBSTED. */
2492 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
,
2494 if (!buf_multibyte
&& !SINGLE_BYTE_CHAR_P (c
))
2495 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2499 c
= SREF (newtext
, pos_byte
++);
2501 c
= unibyte_char_to_multibyte (c
);
2506 else if (c
>= '1' && c
<= '9' && c
<= search_regs
.num_regs
+ '0')
2508 if (search_regs
.start
[c
- '0'] >= 1)
2512 add_len
= 1, add_stuff
= "\\";
2516 error ("Invalid use of `\\' in replacement text");
2521 add_len
= CHAR_STRING (c
, str
);
2525 /* If we want to copy part of a previous match,
2526 set up ADD_STUFF and ADD_LEN to point to it. */
2529 int begbyte
= CHAR_TO_BYTE (search_regs
.start
[idx
]);
2530 add_len
= CHAR_TO_BYTE (search_regs
.end
[idx
]) - begbyte
;
2531 if (search_regs
.start
[idx
] < GPT
&& GPT
< search_regs
.end
[idx
])
2532 move_gap (search_regs
.start
[idx
]);
2533 add_stuff
= BYTE_POS_ADDR (begbyte
);
2536 /* Now the stuff we want to add to SUBSTED
2537 is invariably ADD_LEN bytes starting at ADD_STUFF. */
2539 /* Make sure SUBSTED is big enough. */
2540 if (substed_len
+ add_len
>= substed_alloc_size
)
2542 substed_alloc_size
= substed_len
+ add_len
+ 500;
2543 substed
= (unsigned char *) xrealloc (substed
,
2544 substed_alloc_size
+ 1);
2547 /* Now add to the end of SUBSTED. */
2550 bcopy (add_stuff
, substed
+ substed_len
, add_len
);
2551 substed_len
+= add_len
;
2556 newtext
= make_string (substed
, substed_len
);
2561 /* Replace the old text with the new in the cleanest possible way. */
2562 replace_range (search_regs
.start
[sub
], search_regs
.end
[sub
],
2564 newpoint
= search_regs
.start
[sub
] + SCHARS (newtext
);
2566 if (case_action
== all_caps
)
2567 Fupcase_region (make_number (search_regs
.start
[sub
]),
2568 make_number (newpoint
));
2569 else if (case_action
== cap_initial
)
2570 Fupcase_initials_region (make_number (search_regs
.start
[sub
]),
2571 make_number (newpoint
));
2573 /* Adjust search data for this change. */
2575 int oldend
= search_regs
.end
[sub
];
2576 int change
= newpoint
- search_regs
.end
[sub
];
2579 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2581 if (search_regs
.start
[i
] > oldend
)
2582 search_regs
.start
[i
] += change
;
2583 if (search_regs
.end
[i
] > oldend
)
2584 search_regs
.end
[i
] += change
;
2588 /* Put point back where it was in the text. */
2590 TEMP_SET_PT (opoint
+ ZV
);
2592 TEMP_SET_PT (opoint
);
2594 /* Now move point "officially" to the start of the inserted replacement. */
2595 move_if_not_intangible (newpoint
);
2601 match_limit (num
, beginningp
)
2609 if (n
< 0 || n
>= search_regs
.num_regs
)
2610 args_out_of_range (num
, make_number (search_regs
.num_regs
));
2611 if (search_regs
.num_regs
<= 0
2612 || search_regs
.start
[n
] < 0)
2614 return (make_number ((beginningp
) ? search_regs
.start
[n
]
2615 : search_regs
.end
[n
]));
2618 DEFUN ("match-beginning", Fmatch_beginning
, Smatch_beginning
, 1, 1, 0,
2619 doc
: /* Return position of start of text matched by last search.
2620 SUBEXP, a number, specifies which parenthesized expression in the last
2622 Value is nil if SUBEXPth pair didn't match, or there were less than
2624 Zero means the entire text matched by the whole regexp or whole string. */)
2628 return match_limit (subexp
, 1);
2631 DEFUN ("match-end", Fmatch_end
, Smatch_end
, 1, 1, 0,
2632 doc
: /* Return position of end of text matched by last search.
2633 SUBEXP, a number, specifies which parenthesized expression in the last
2635 Value is nil if SUBEXPth pair didn't match, or there were less than
2637 Zero means the entire text matched by the whole regexp or whole string. */)
2641 return match_limit (subexp
, 0);
2644 DEFUN ("match-data", Fmatch_data
, Smatch_data
, 0, 2, 0,
2645 doc
: /* Return a list containing all info on what the last search matched.
2646 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
2647 All the elements are markers or nil (nil if the Nth pair didn't match)
2648 if the last match was on a buffer; integers or nil if a string was matched.
2649 Use `store-match-data' to reinstate the data in this list.
2651 If INTEGERS (the optional first argument) is non-nil, always use integers
2652 \(rather than markers) to represent buffer positions.
2653 If REUSE is a list, reuse it as part of the value. If REUSE is long enough
2654 to hold all the values, and if INTEGERS is non-nil, no consing is done.
2656 Return value is undefined if the last search failed. */)
2658 Lisp_Object integers
, reuse
;
2660 Lisp_Object tail
, prev
;
2664 if (NILP (last_thing_searched
))
2669 data
= (Lisp_Object
*) alloca ((2 * search_regs
.num_regs
)
2670 * sizeof (Lisp_Object
));
2673 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2675 int start
= search_regs
.start
[i
];
2678 if (EQ (last_thing_searched
, Qt
)
2679 || ! NILP (integers
))
2681 XSETFASTINT (data
[2 * i
], start
);
2682 XSETFASTINT (data
[2 * i
+ 1], search_regs
.end
[i
]);
2684 else if (BUFFERP (last_thing_searched
))
2686 data
[2 * i
] = Fmake_marker ();
2687 Fset_marker (data
[2 * i
],
2688 make_number (start
),
2689 last_thing_searched
);
2690 data
[2 * i
+ 1] = Fmake_marker ();
2691 Fset_marker (data
[2 * i
+ 1],
2692 make_number (search_regs
.end
[i
]),
2693 last_thing_searched
);
2696 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
2702 data
[2 * i
] = data
[2 * i
+ 1] = Qnil
;
2705 /* If REUSE is not usable, cons up the values and return them. */
2706 if (! CONSP (reuse
))
2707 return Flist (2 * len
+ 2, data
);
2709 /* If REUSE is a list, store as many value elements as will fit
2710 into the elements of REUSE. */
2711 for (i
= 0, tail
= reuse
; CONSP (tail
);
2712 i
++, tail
= XCDR (tail
))
2714 if (i
< 2 * len
+ 2)
2715 XSETCAR (tail
, data
[i
]);
2717 XSETCAR (tail
, Qnil
);
2721 /* If we couldn't fit all value elements into REUSE,
2722 cons up the rest of them and add them to the end of REUSE. */
2723 if (i
< 2 * len
+ 2)
2724 XSETCDR (prev
, Flist (2 * len
+ 2 - i
, data
+ i
));
2730 DEFUN ("set-match-data", Fset_match_data
, Sset_match_data
, 1, 1, 0,
2731 doc
: /* Set internal data on last search match from elements of LIST.
2732 LIST should have been created by calling `match-data' previously. */)
2734 register Lisp_Object list
;
2737 register Lisp_Object marker
;
2739 if (running_asynch_code
)
2740 save_search_regs ();
2742 if (!CONSP (list
) && !NILP (list
))
2743 list
= wrong_type_argument (Qconsp
, list
);
2745 /* Unless we find a marker with a buffer in LIST, assume that this
2746 match data came from a string. */
2747 last_thing_searched
= Qt
;
2749 /* Allocate registers if they don't already exist. */
2751 int length
= XFASTINT (Flength (list
)) / 2;
2753 if (length
> search_regs
.num_regs
)
2755 if (search_regs
.num_regs
== 0)
2758 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2760 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2765 = (regoff_t
*) xrealloc (search_regs
.start
,
2766 length
* sizeof (regoff_t
));
2768 = (regoff_t
*) xrealloc (search_regs
.end
,
2769 length
* sizeof (regoff_t
));
2772 for (i
= search_regs
.num_regs
; i
< length
; i
++)
2773 search_regs
.start
[i
] = -1;
2775 search_regs
.num_regs
= length
;
2779 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2781 marker
= Fcar (list
);
2784 search_regs
.start
[i
] = -1;
2791 if (MARKERP (marker
))
2793 if (XMARKER (marker
)->buffer
== 0)
2794 XSETFASTINT (marker
, 0);
2796 XSETBUFFER (last_thing_searched
, XMARKER (marker
)->buffer
);
2799 CHECK_NUMBER_COERCE_MARKER (marker
);
2800 from
= XINT (marker
);
2803 marker
= Fcar (list
);
2804 if (MARKERP (marker
) && XMARKER (marker
)->buffer
== 0)
2805 XSETFASTINT (marker
, 0);
2807 CHECK_NUMBER_COERCE_MARKER (marker
);
2808 search_regs
.start
[i
] = from
;
2809 search_regs
.end
[i
] = XINT (marker
);
2817 /* If non-zero the match data have been saved in saved_search_regs
2818 during the execution of a sentinel or filter. */
2819 static int search_regs_saved
;
2820 static struct re_registers saved_search_regs
;
2822 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
2823 if asynchronous code (filter or sentinel) is running. */
2827 if (!search_regs_saved
)
2829 saved_search_regs
.num_regs
= search_regs
.num_regs
;
2830 saved_search_regs
.start
= search_regs
.start
;
2831 saved_search_regs
.end
= search_regs
.end
;
2832 search_regs
.num_regs
= 0;
2833 search_regs
.start
= 0;
2834 search_regs
.end
= 0;
2836 search_regs_saved
= 1;
2840 /* Called upon exit from filters and sentinels. */
2842 restore_match_data ()
2844 if (search_regs_saved
)
2846 if (search_regs
.num_regs
> 0)
2848 xfree (search_regs
.start
);
2849 xfree (search_regs
.end
);
2851 search_regs
.num_regs
= saved_search_regs
.num_regs
;
2852 search_regs
.start
= saved_search_regs
.start
;
2853 search_regs
.end
= saved_search_regs
.end
;
2855 search_regs_saved
= 0;
2859 /* Quote a string to inactivate reg-expr chars */
2861 DEFUN ("regexp-quote", Fregexp_quote
, Sregexp_quote
, 1, 1, 0,
2862 doc
: /* Return a regexp string which matches exactly STRING and nothing else. */)
2866 register unsigned char *in
, *out
, *end
;
2867 register unsigned char *temp
;
2868 int backslashes_added
= 0;
2870 CHECK_STRING (string
);
2872 temp
= (unsigned char *) alloca (SBYTES (string
) * 2);
2874 /* Now copy the data into the new string, inserting escapes. */
2876 in
= SDATA (string
);
2877 end
= in
+ SBYTES (string
);
2880 for (; in
!= end
; in
++)
2882 if (*in
== '[' || *in
== ']'
2883 || *in
== '*' || *in
== '.' || *in
== '\\'
2884 || *in
== '?' || *in
== '+'
2885 || *in
== '^' || *in
== '$')
2886 *out
++ = '\\', backslashes_added
++;
2890 return make_specified_string (temp
,
2891 SCHARS (string
) + backslashes_added
,
2893 STRING_MULTIBYTE (string
));
2901 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
2903 searchbufs
[i
].buf
.allocated
= 100;
2904 searchbufs
[i
].buf
.buffer
= (unsigned char *) xmalloc (100);
2905 searchbufs
[i
].buf
.fastmap
= searchbufs
[i
].fastmap
;
2906 searchbufs
[i
].regexp
= Qnil
;
2907 staticpro (&searchbufs
[i
].regexp
);
2908 searchbufs
[i
].next
= (i
== REGEXP_CACHE_SIZE
-1 ? 0 : &searchbufs
[i
+1]);
2910 searchbuf_head
= &searchbufs
[0];
2912 Qsearch_failed
= intern ("search-failed");
2913 staticpro (&Qsearch_failed
);
2914 Qinvalid_regexp
= intern ("invalid-regexp");
2915 staticpro (&Qinvalid_regexp
);
2917 Fput (Qsearch_failed
, Qerror_conditions
,
2918 Fcons (Qsearch_failed
, Fcons (Qerror
, Qnil
)));
2919 Fput (Qsearch_failed
, Qerror_message
,
2920 build_string ("Search failed"));
2922 Fput (Qinvalid_regexp
, Qerror_conditions
,
2923 Fcons (Qinvalid_regexp
, Fcons (Qerror
, Qnil
)));
2924 Fput (Qinvalid_regexp
, Qerror_message
,
2925 build_string ("Invalid regexp"));
2927 last_thing_searched
= Qnil
;
2928 staticpro (&last_thing_searched
);
2930 defsubr (&Slooking_at
);
2931 defsubr (&Sposix_looking_at
);
2932 defsubr (&Sstring_match
);
2933 defsubr (&Sposix_string_match
);
2934 defsubr (&Ssearch_forward
);
2935 defsubr (&Ssearch_backward
);
2936 defsubr (&Sword_search_forward
);
2937 defsubr (&Sword_search_backward
);
2938 defsubr (&Sre_search_forward
);
2939 defsubr (&Sre_search_backward
);
2940 defsubr (&Sposix_search_forward
);
2941 defsubr (&Sposix_search_backward
);
2942 defsubr (&Sreplace_match
);
2943 defsubr (&Smatch_beginning
);
2944 defsubr (&Smatch_end
);
2945 defsubr (&Smatch_data
);
2946 defsubr (&Sset_match_data
);
2947 defsubr (&Sregexp_quote
);
2950 /* arch-tag: a6059d79-0552-4f14-a2cb-d379a4e3c78f
2951 (do not change this comment) */