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. */
27 #include "character.h"
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 *) XSTRING (pattern
)->data
;
132 raw_pattern_size
= STRING_BYTES (XSTRING (pattern
));
136 raw_pattern_size
= count_size_as_multibyte (XSTRING (pattern
)->data
,
137 XSTRING (pattern
)->size
);
138 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
139 copy_text (XSTRING (pattern
)->data
, raw_pattern
,
140 XSTRING (pattern
)->size
, 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
= XSTRING (pattern
)->size
;
151 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
152 copy_text (XSTRING (pattern
)->data
, raw_pattern
,
153 STRING_BYTES (XSTRING (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 *) realloc (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 XSTRING in those cases. However, compile_pattern_1 is only
216 applied to the cache entry we pick here to reuse. So nil
217 should never appear before a non-nil entry. */
218 if (NILP (cp
->regexp
))
220 if (XSTRING (cp
->regexp
)->size
== XSTRING (pattern
)->size
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
= XSTRING (string
)->size
;
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 *) XSTRING (string
)->data
,
395 STRING_BYTES (XSTRING (string
)), pos_byte
,
396 STRING_BYTES (XSTRING (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. */)
423 (regexp
, string
, start
)
424 Lisp_Object regexp
, string
, start
;
426 return string_match_1 (regexp
, string
, start
, 0);
429 DEFUN ("posix-string-match", Fposix_string_match
, Sposix_string_match
, 2, 3, 0,
430 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
431 Find the longest match, in accord with Posix regular expression rules.
432 Case is ignored if `case-fold-search' is non-nil in the current buffer.
433 If third arg START is non-nil, start search at that index in STRING.
434 For index of first char beyond the match, do (match-end 0).
435 `match-end' and `match-beginning' also give indices of substrings
436 matched by parenthesis constructs in the pattern. */)
437 (regexp
, string
, start
)
438 Lisp_Object regexp
, string
, start
;
440 return string_match_1 (regexp
, string
, start
, 1);
443 /* Match REGEXP against STRING, searching all of STRING,
444 and return the index of the match, or negative on failure.
445 This does not clobber the match data. */
448 fast_string_match (regexp
, string
)
449 Lisp_Object regexp
, string
;
452 struct re_pattern_buffer
*bufp
;
454 bufp
= compile_pattern (regexp
, 0, Qnil
,
455 0, STRING_MULTIBYTE (string
));
457 re_match_object
= string
;
459 val
= re_search (bufp
, (char *) XSTRING (string
)->data
,
460 STRING_BYTES (XSTRING (string
)), 0,
461 STRING_BYTES (XSTRING (string
)), 0);
466 /* Match REGEXP against STRING, searching all of STRING ignoring case,
467 and return the index of the match, or negative on failure.
468 This does not clobber the match data.
469 We assume that STRING contains single-byte characters. */
471 extern Lisp_Object Vascii_downcase_table
;
474 fast_c_string_match_ignore_case (regexp
, string
)
479 struct re_pattern_buffer
*bufp
;
480 int len
= strlen (string
);
482 regexp
= string_make_unibyte (regexp
);
483 re_match_object
= Qt
;
484 bufp
= compile_pattern (regexp
, 0,
485 Vascii_downcase_table
, 0,
488 val
= re_search (bufp
, string
, len
, 0, len
, 0);
493 /* The newline cache: remembering which sections of text have no newlines. */
495 /* If the user has requested newline caching, make sure it's on.
496 Otherwise, make sure it's off.
497 This is our cheezy way of associating an action with the change of
498 state of a buffer-local variable. */
500 newline_cache_on_off (buf
)
503 if (NILP (buf
->cache_long_line_scans
))
505 /* It should be off. */
506 if (buf
->newline_cache
)
508 free_region_cache (buf
->newline_cache
);
509 buf
->newline_cache
= 0;
514 /* It should be on. */
515 if (buf
->newline_cache
== 0)
516 buf
->newline_cache
= new_region_cache ();
521 /* Search for COUNT instances of the character TARGET between START and END.
523 If COUNT is positive, search forwards; END must be >= START.
524 If COUNT is negative, search backwards for the -COUNTth instance;
525 END must be <= START.
526 If COUNT is zero, do anything you please; run rogue, for all I care.
528 If END is zero, use BEGV or ZV instead, as appropriate for the
529 direction indicated by COUNT.
531 If we find COUNT instances, set *SHORTAGE to zero, and return the
532 position after the COUNTth match. Note that for reverse motion
533 this is not the same as the usual convention for Emacs motion commands.
535 If we don't find COUNT instances before reaching END, set *SHORTAGE
536 to the number of TARGETs left unfound, and return END.
538 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
539 except when inside redisplay. */
542 scan_buffer (target
, start
, end
, count
, shortage
, allow_quit
)
549 struct region_cache
*newline_cache
;
560 if (! end
) end
= BEGV
;
563 newline_cache_on_off (current_buffer
);
564 newline_cache
= current_buffer
->newline_cache
;
569 immediate_quit
= allow_quit
;
574 /* Our innermost scanning loop is very simple; it doesn't know
575 about gaps, buffer ends, or the newline cache. ceiling is
576 the position of the last character before the next such
577 obstacle --- the last character the dumb search loop should
579 int ceiling_byte
= CHAR_TO_BYTE (end
) - 1;
580 int start_byte
= CHAR_TO_BYTE (start
);
583 /* If we're looking for a newline, consult the newline cache
584 to see where we can avoid some scanning. */
585 if (target
== '\n' && newline_cache
)
589 while (region_cache_forward
590 (current_buffer
, newline_cache
, start_byte
, &next_change
))
591 start_byte
= next_change
;
592 immediate_quit
= allow_quit
;
594 /* START should never be after END. */
595 if (start_byte
> ceiling_byte
)
596 start_byte
= ceiling_byte
;
598 /* Now the text after start is an unknown region, and
599 next_change is the position of the next known region. */
600 ceiling_byte
= min (next_change
- 1, ceiling_byte
);
603 /* The dumb loop can only scan text stored in contiguous
604 bytes. BUFFER_CEILING_OF returns the last character
605 position that is contiguous, so the ceiling is the
606 position after that. */
607 tem
= BUFFER_CEILING_OF (start_byte
);
608 ceiling_byte
= min (tem
, ceiling_byte
);
611 /* The termination address of the dumb loop. */
612 register unsigned char *ceiling_addr
613 = BYTE_POS_ADDR (ceiling_byte
) + 1;
614 register unsigned char *cursor
615 = BYTE_POS_ADDR (start_byte
);
616 unsigned char *base
= cursor
;
618 while (cursor
< ceiling_addr
)
620 unsigned char *scan_start
= cursor
;
623 while (*cursor
!= target
&& ++cursor
< ceiling_addr
)
626 /* If we're looking for newlines, cache the fact that
627 the region from start to cursor is free of them. */
628 if (target
== '\n' && newline_cache
)
629 know_region_cache (current_buffer
, newline_cache
,
630 start_byte
+ scan_start
- base
,
631 start_byte
+ cursor
- base
);
633 /* Did we find the target character? */
634 if (cursor
< ceiling_addr
)
639 return BYTE_TO_CHAR (start_byte
+ cursor
- base
+ 1);
645 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
651 /* The last character to check before the next obstacle. */
652 int ceiling_byte
= CHAR_TO_BYTE (end
);
653 int start_byte
= CHAR_TO_BYTE (start
);
656 /* Consult the newline cache, if appropriate. */
657 if (target
== '\n' && newline_cache
)
661 while (region_cache_backward
662 (current_buffer
, newline_cache
, start_byte
, &next_change
))
663 start_byte
= next_change
;
664 immediate_quit
= allow_quit
;
666 /* Start should never be at or before end. */
667 if (start_byte
<= ceiling_byte
)
668 start_byte
= ceiling_byte
+ 1;
670 /* Now the text before start is an unknown region, and
671 next_change is the position of the next known region. */
672 ceiling_byte
= max (next_change
, ceiling_byte
);
675 /* Stop scanning before the gap. */
676 tem
= BUFFER_FLOOR_OF (start_byte
- 1);
677 ceiling_byte
= max (tem
, ceiling_byte
);
680 /* The termination address of the dumb loop. */
681 register unsigned char *ceiling_addr
= BYTE_POS_ADDR (ceiling_byte
);
682 register unsigned char *cursor
= BYTE_POS_ADDR (start_byte
- 1);
683 unsigned char *base
= cursor
;
685 while (cursor
>= ceiling_addr
)
687 unsigned char *scan_start
= cursor
;
689 while (*cursor
!= target
&& --cursor
>= ceiling_addr
)
692 /* If we're looking for newlines, cache the fact that
693 the region from after the cursor to start is free of them. */
694 if (target
== '\n' && newline_cache
)
695 know_region_cache (current_buffer
, newline_cache
,
696 start_byte
+ cursor
- base
,
697 start_byte
+ scan_start
- base
);
699 /* Did we find the target character? */
700 if (cursor
>= ceiling_addr
)
705 return BYTE_TO_CHAR (start_byte
+ cursor
- base
);
711 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
717 *shortage
= count
* direction
;
721 /* Search for COUNT instances of a line boundary, which means either a
722 newline or (if selective display enabled) a carriage return.
723 Start at START. If COUNT is negative, search backwards.
725 We report the resulting position by calling TEMP_SET_PT_BOTH.
727 If we find COUNT instances. we position after (always after,
728 even if scanning backwards) the COUNTth match, and return 0.
730 If we don't find COUNT instances before reaching the end of the
731 buffer (or the beginning, if scanning backwards), we return
732 the number of line boundaries left unfound, and position at
733 the limit we bumped up against.
735 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
736 except in special cases. */
739 scan_newline (start
, start_byte
, limit
, limit_byte
, count
, allow_quit
)
740 int start
, start_byte
;
741 int limit
, limit_byte
;
745 int direction
= ((count
> 0) ? 1 : -1);
747 register unsigned char *cursor
;
750 register int ceiling
;
751 register unsigned char *ceiling_addr
;
753 int old_immediate_quit
= immediate_quit
;
755 /* The code that follows is like scan_buffer
756 but checks for either newline or carriage return. */
761 start_byte
= CHAR_TO_BYTE (start
);
765 while (start_byte
< limit_byte
)
767 ceiling
= BUFFER_CEILING_OF (start_byte
);
768 ceiling
= min (limit_byte
- 1, ceiling
);
769 ceiling_addr
= BYTE_POS_ADDR (ceiling
) + 1;
770 base
= (cursor
= BYTE_POS_ADDR (start_byte
));
773 while (*cursor
!= '\n' && ++cursor
!= ceiling_addr
)
776 if (cursor
!= ceiling_addr
)
780 immediate_quit
= old_immediate_quit
;
781 start_byte
= start_byte
+ cursor
- base
+ 1;
782 start
= BYTE_TO_CHAR (start_byte
);
783 TEMP_SET_PT_BOTH (start
, start_byte
);
787 if (++cursor
== ceiling_addr
)
793 start_byte
+= cursor
- base
;
798 while (start_byte
> limit_byte
)
800 ceiling
= BUFFER_FLOOR_OF (start_byte
- 1);
801 ceiling
= max (limit_byte
, ceiling
);
802 ceiling_addr
= BYTE_POS_ADDR (ceiling
) - 1;
803 base
= (cursor
= BYTE_POS_ADDR (start_byte
- 1) + 1);
806 while (--cursor
!= ceiling_addr
&& *cursor
!= '\n')
809 if (cursor
!= ceiling_addr
)
813 immediate_quit
= old_immediate_quit
;
814 /* Return the position AFTER the match we found. */
815 start_byte
= start_byte
+ cursor
- base
+ 1;
816 start
= BYTE_TO_CHAR (start_byte
);
817 TEMP_SET_PT_BOTH (start
, start_byte
);
824 /* Here we add 1 to compensate for the last decrement
825 of CURSOR, which took it past the valid range. */
826 start_byte
+= cursor
- base
+ 1;
830 TEMP_SET_PT_BOTH (limit
, limit_byte
);
831 immediate_quit
= old_immediate_quit
;
833 return count
* direction
;
837 find_next_newline_no_quit (from
, cnt
)
838 register int from
, cnt
;
840 return scan_buffer ('\n', from
, 0, cnt
, (int *) 0, 0);
843 /* Like find_next_newline, but returns position before the newline,
844 not after, and only search up to TO. This isn't just
845 find_next_newline (...)-1, because you might hit TO. */
848 find_before_next_newline (from
, to
, cnt
)
852 int pos
= scan_buffer ('\n', from
, to
, cnt
, &shortage
, 1);
860 /* Subroutines of Lisp buffer search functions. */
863 search_command (string
, bound
, noerror
, count
, direction
, RE
, posix
)
864 Lisp_Object string
, bound
, noerror
, count
;
875 CHECK_NUMBER (count
);
879 CHECK_STRING (string
);
883 lim
= ZV
, lim_byte
= ZV_BYTE
;
885 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
889 CHECK_NUMBER_COERCE_MARKER (bound
);
891 if (n
> 0 ? lim
< PT
: lim
> PT
)
892 error ("Invalid search bound (wrong side of point)");
894 lim
= ZV
, lim_byte
= ZV_BYTE
;
896 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
898 lim_byte
= CHAR_TO_BYTE (lim
);
901 np
= search_buffer (string
, PT
, PT_BYTE
, lim
, lim_byte
, n
, RE
,
902 (!NILP (current_buffer
->case_fold_search
)
903 ? current_buffer
->case_canon_table
905 (!NILP (current_buffer
->case_fold_search
)
906 ? current_buffer
->case_eqv_table
912 return signal_failure (string
);
913 if (!EQ (noerror
, Qt
))
915 if (lim
< BEGV
|| lim
> ZV
)
917 SET_PT_BOTH (lim
, lim_byte
);
919 #if 0 /* This would be clean, but maybe programs depend on
920 a value of nil here. */
928 if (np
< BEGV
|| np
> ZV
)
933 return make_number (np
);
936 /* Return 1 if REGEXP it matches just one constant string. */
939 trivial_regexp_p (regexp
)
942 int len
= STRING_BYTES (XSTRING (regexp
));
943 unsigned char *s
= XSTRING (regexp
)->data
;
948 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
955 case '|': case '(': case ')': case '`': case '\'': case 'b':
956 case 'B': case '<': case '>': case 'w': case 'W': case 's':
957 case 'S': case '=': case '{': case '}':
958 case 'c': case 'C': /* for categoryspec and notcategoryspec */
959 case '1': case '2': case '3': case '4': case '5':
960 case '6': case '7': case '8': case '9':
968 /* Search for the n'th occurrence of STRING in the current buffer,
969 starting at position POS and stopping at position LIM,
970 treating STRING as a literal string if RE is false or as
971 a regular expression if RE is true.
973 If N is positive, searching is forward and LIM must be greater than POS.
974 If N is negative, searching is backward and LIM must be less than POS.
976 Returns -x if x occurrences remain to be found (x > 0),
977 or else the position at the beginning of the Nth occurrence
978 (if searching backward) or the end (if searching forward).
980 POSIX is nonzero if we want full backtracking (POSIX style)
981 for this pattern. 0 means backtrack only enough to get a valid match. */
983 #define TRANSLATE(out, trt, d) \
989 temp = Faref (trt, make_number (d)); \
990 if (INTEGERP (temp)) \
1001 search_buffer (string
, pos
, pos_byte
, lim
, lim_byte
, n
,
1002 RE
, trt
, inverse_trt
, posix
)
1011 Lisp_Object inverse_trt
;
1014 int len
= XSTRING (string
)->size
;
1015 int len_byte
= STRING_BYTES (XSTRING (string
));
1018 if (running_asynch_code
)
1019 save_search_regs ();
1021 /* Searching 0 times means don't move. */
1022 /* Null string is found at starting position. */
1023 if (len
== 0 || n
== 0)
1025 set_search_regs (pos_byte
, 0);
1029 if (RE
&& !trivial_regexp_p (string
))
1031 unsigned char *p1
, *p2
;
1033 struct re_pattern_buffer
*bufp
;
1035 bufp
= compile_pattern (string
, &search_regs
, trt
, posix
,
1036 !NILP (current_buffer
->enable_multibyte_characters
));
1038 immediate_quit
= 1; /* Quit immediately if user types ^G,
1039 because letting this function finish
1040 can take too long. */
1041 QUIT
; /* Do a pending quit right away,
1042 to avoid paradoxical behavior */
1043 /* Get pointers and sizes of the two strings
1044 that make up the visible portion of the buffer. */
1047 s1
= GPT_BYTE
- BEGV_BYTE
;
1049 s2
= ZV_BYTE
- GPT_BYTE
;
1053 s2
= ZV_BYTE
- BEGV_BYTE
;
1058 s1
= ZV_BYTE
- BEGV_BYTE
;
1061 re_match_object
= Qnil
;
1066 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1067 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1069 /* Don't allow match past current point */
1070 pos_byte
- BEGV_BYTE
);
1073 matcher_overflow ();
1077 pos_byte
= search_regs
.start
[0] + BEGV_BYTE
;
1078 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1079 if (search_regs
.start
[i
] >= 0)
1081 search_regs
.start
[i
]
1082 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1084 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1086 XSETBUFFER (last_thing_searched
, current_buffer
);
1087 /* Set pos to the new position. */
1088 pos
= search_regs
.start
[0];
1100 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1101 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1103 lim_byte
- BEGV_BYTE
);
1106 matcher_overflow ();
1110 pos_byte
= search_regs
.end
[0] + BEGV_BYTE
;
1111 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1112 if (search_regs
.start
[i
] >= 0)
1114 search_regs
.start
[i
]
1115 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1117 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1119 XSETBUFFER (last_thing_searched
, current_buffer
);
1120 pos
= search_regs
.end
[0];
1132 else /* non-RE case */
1134 unsigned char *raw_pattern
, *pat
;
1135 int raw_pattern_size
;
1136 int raw_pattern_size_byte
;
1137 unsigned char *patbuf
;
1138 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1139 unsigned char *base_pat
= XSTRING (string
)->data
;
1140 int charset_base
= -1;
1141 int boyer_moore_ok
= 1;
1143 /* MULTIBYTE says whether the text to be searched is multibyte.
1144 We must convert PATTERN to match that, or we will not really
1145 find things right. */
1147 if (multibyte
== STRING_MULTIBYTE (string
))
1149 raw_pattern
= (unsigned char *) XSTRING (string
)->data
;
1150 raw_pattern_size
= XSTRING (string
)->size
;
1151 raw_pattern_size_byte
= STRING_BYTES (XSTRING (string
));
1155 raw_pattern_size
= XSTRING (string
)->size
;
1156 raw_pattern_size_byte
1157 = count_size_as_multibyte (XSTRING (string
)->data
,
1159 raw_pattern
= (unsigned char *) alloca (raw_pattern_size_byte
+ 1);
1160 copy_text (XSTRING (string
)->data
, raw_pattern
,
1161 XSTRING (string
)->size
, 0, 1);
1165 /* Converting multibyte to single-byte.
1167 ??? Perhaps this conversion should be done in a special way
1168 by subtracting nonascii-insert-offset from each non-ASCII char,
1169 so that only the multibyte chars which really correspond to
1170 the chosen single-byte character set can possibly match. */
1171 raw_pattern_size
= XSTRING (string
)->size
;
1172 raw_pattern_size_byte
= XSTRING (string
)->size
;
1173 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
1174 copy_text (XSTRING (string
)->data
, raw_pattern
,
1175 STRING_BYTES (XSTRING (string
)), 1, 0);
1178 /* Copy and optionally translate the pattern. */
1179 len
= raw_pattern_size
;
1180 len_byte
= raw_pattern_size_byte
;
1181 patbuf
= (unsigned char *) alloca (len_byte
);
1183 base_pat
= raw_pattern
;
1188 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1189 int c
, translated
, inverse
;
1190 int in_charlen
, charlen
;
1192 /* If we got here and the RE flag is set, it's because we're
1193 dealing with a regexp known to be trivial, so the backslash
1194 just quotes the next character. */
1195 if (RE
&& *base_pat
== '\\')
1202 c
= STRING_CHAR_AND_LENGTH (base_pat
, len_byte
, in_charlen
);
1204 /* Translate the character, if requested. */
1205 TRANSLATE (translated
, trt
, c
);
1206 /* If translation changed the byte-length, go back
1207 to the original character. */
1208 charlen
= CHAR_STRING (translated
, str
);
1209 if (in_charlen
!= charlen
)
1212 charlen
= CHAR_STRING (c
, str
);
1215 /* If we are searching for something strange,
1216 an invalid multibyte code, don't use boyer-moore. */
1217 if (! ASCII_BYTE_P (translated
)
1218 && (charlen
== 1 /* 8bit code */
1219 || charlen
!= in_charlen
/* invalid multibyte code */
1223 TRANSLATE (inverse
, inverse_trt
, c
);
1225 /* Did this char actually get translated?
1226 Would any other char get translated into it? */
1227 if (translated
!= c
|| inverse
!= c
)
1229 /* Keep track of which character set row
1230 contains the characters that need translation. */
1231 int charset_base_code
= c
& ~0x3F;
1232 if (charset_base
== -1)
1233 charset_base
= charset_base_code
;
1234 else if (charset_base
!= charset_base_code
)
1235 /* If two different rows appear, needing translation,
1236 then we cannot use boyer_moore search. */
1240 /* Store this character into the translated pattern. */
1241 bcopy (str
, pat
, charlen
);
1243 base_pat
+= in_charlen
;
1244 len_byte
-= in_charlen
;
1249 /* Unibyte buffer. */
1255 /* If we got here and the RE flag is set, it's because we're
1256 dealing with a regexp known to be trivial, so the backslash
1257 just quotes the next character. */
1258 if (RE
&& *base_pat
== '\\')
1264 TRANSLATE (translated
, trt
, c
);
1265 *pat
++ = translated
;
1269 len_byte
= pat
- patbuf
;
1270 len
= raw_pattern_size
;
1271 pat
= base_pat
= patbuf
;
1274 return boyer_moore (n
, pat
, len
, len_byte
, trt
, inverse_trt
,
1275 pos
, pos_byte
, lim
, lim_byte
,
1278 return simple_search (n
, pat
, len
, len_byte
, trt
,
1279 pos
, pos_byte
, lim
, lim_byte
);
1283 /* Do a simple string search N times for the string PAT,
1284 whose length is LEN/LEN_BYTE,
1285 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1286 TRT is the translation table.
1288 Return the character position where the match is found.
1289 Otherwise, if M matches remained to be found, return -M.
1291 This kind of search works regardless of what is in PAT and
1292 regardless of what is in TRT. It is used in cases where
1293 boyer_moore cannot work. */
1296 simple_search (n
, pat
, len
, len_byte
, trt
, pos
, pos_byte
, lim
, lim_byte
)
1304 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1305 int forward
= n
> 0;
1307 if (lim
> pos
&& multibyte
)
1312 /* Try matching at position POS. */
1314 int this_pos_byte
= pos_byte
;
1316 int this_len_byte
= len_byte
;
1317 unsigned char *p
= pat
;
1318 if (pos
+ len
> lim
)
1321 while (this_len
> 0)
1323 int charlen
, buf_charlen
;
1326 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1327 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1328 ZV_BYTE
- this_pos_byte
,
1330 TRANSLATE (buf_ch
, trt
, buf_ch
);
1332 if (buf_ch
!= pat_ch
)
1335 this_len_byte
-= charlen
;
1339 this_pos_byte
+= buf_charlen
;
1346 pos_byte
+= len_byte
;
1350 INC_BOTH (pos
, pos_byte
);
1360 /* Try matching at position POS. */
1363 unsigned char *p
= pat
;
1365 if (pos
+ len
> lim
)
1368 while (this_len
> 0)
1371 int buf_ch
= FETCH_BYTE (this_pos
);
1372 TRANSLATE (buf_ch
, trt
, buf_ch
);
1374 if (buf_ch
!= pat_ch
)
1392 /* Backwards search. */
1393 else if (lim
< pos
&& multibyte
)
1398 /* Try matching at position POS. */
1399 int this_pos
= pos
- len
;
1400 int this_pos_byte
= pos_byte
- len_byte
;
1402 int this_len_byte
= len_byte
;
1403 unsigned char *p
= pat
;
1405 if (pos
- len
< lim
)
1408 while (this_len
> 0)
1410 int charlen
, buf_charlen
;
1413 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1414 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1415 ZV_BYTE
- this_pos_byte
,
1417 TRANSLATE (buf_ch
, trt
, buf_ch
);
1419 if (buf_ch
!= pat_ch
)
1422 this_len_byte
-= charlen
;
1425 this_pos_byte
+= buf_charlen
;
1432 pos_byte
-= len_byte
;
1436 DEC_BOTH (pos
, pos_byte
);
1446 /* Try matching at position POS. */
1447 int this_pos
= pos
- len
;
1449 unsigned char *p
= pat
;
1451 if (pos
- len
< lim
)
1454 while (this_len
> 0)
1457 int buf_ch
= FETCH_BYTE (this_pos
);
1458 TRANSLATE (buf_ch
, trt
, buf_ch
);
1460 if (buf_ch
!= pat_ch
)
1482 set_search_regs ((multibyte
? pos_byte
: pos
) - len_byte
, len_byte
);
1484 set_search_regs (multibyte
? pos_byte
: pos
, len_byte
);
1494 /* Do Boyer-Moore search N times for the string PAT,
1495 whose length is LEN/LEN_BYTE,
1496 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1497 DIRECTION says which direction we search in.
1498 TRT and INVERSE_TRT are translation tables.
1500 This kind of search works if all the characters in PAT that have
1501 nontrivial translation are the same aside from the last byte. This
1502 makes it possible to translate just the last byte of a character,
1503 and do so after just a simple test of the context.
1505 If that criterion is not satisfied, do not call this function. */
1508 boyer_moore (n
, base_pat
, len
, len_byte
, trt
, inverse_trt
,
1509 pos
, pos_byte
, lim
, lim_byte
, charset_base
)
1511 unsigned char *base_pat
;
1514 Lisp_Object inverse_trt
;
1519 int direction
= ((n
> 0) ? 1 : -1);
1520 register int dirlen
;
1521 int infinity
, limit
, stride_for_teases
= 0;
1522 register int *BM_tab
;
1524 register unsigned char *cursor
, *p_limit
;
1526 unsigned char *pat
, *pat_end
;
1527 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1529 unsigned char simple_translate
[0400];
1530 int translate_prev_byte
= 0;
1531 int translate_anteprev_byte
= 0;
1534 int BM_tab_space
[0400];
1535 BM_tab
= &BM_tab_space
[0];
1537 BM_tab
= (int *) alloca (0400 * sizeof (int));
1539 /* The general approach is that we are going to maintain that we know */
1540 /* the first (closest to the present position, in whatever direction */
1541 /* we're searching) character that could possibly be the last */
1542 /* (furthest from present position) character of a valid match. We */
1543 /* advance the state of our knowledge by looking at that character */
1544 /* and seeing whether it indeed matches the last character of the */
1545 /* pattern. If it does, we take a closer look. If it does not, we */
1546 /* move our pointer (to putative last characters) as far as is */
1547 /* logically possible. This amount of movement, which I call a */
1548 /* stride, will be the length of the pattern if the actual character */
1549 /* appears nowhere in the pattern, otherwise it will be the distance */
1550 /* from the last occurrence of that character to the end of the */
1552 /* As a coding trick, an enormous stride is coded into the table for */
1553 /* characters that match the last character. This allows use of only */
1554 /* a single test, a test for having gone past the end of the */
1555 /* permissible match region, to test for both possible matches (when */
1556 /* the stride goes past the end immediately) and failure to */
1557 /* match (where you get nudged past the end one stride at a time). */
1559 /* Here we make a "mickey mouse" BM table. The stride of the search */
1560 /* is determined only by the last character of the putative match. */
1561 /* If that character does not match, we will stride the proper */
1562 /* distance to propose a match that superimposes it on the last */
1563 /* instance of a character that matches it (per trt), or misses */
1564 /* it entirely if there is none. */
1566 dirlen
= len_byte
* direction
;
1567 infinity
= dirlen
- (lim_byte
+ pos_byte
+ len_byte
+ len_byte
) * direction
;
1569 /* Record position after the end of the pattern. */
1570 pat_end
= base_pat
+ len_byte
;
1571 /* BASE_PAT points to a character that we start scanning from.
1572 It is the first character in a forward search,
1573 the last character in a backward search. */
1575 base_pat
= pat_end
- 1;
1577 BM_tab_base
= BM_tab
;
1579 j
= dirlen
; /* to get it in a register */
1580 /* A character that does not appear in the pattern induces a */
1581 /* stride equal to the pattern length. */
1582 while (BM_tab_base
!= BM_tab
)
1590 /* We use this for translation, instead of TRT itself.
1591 We fill this in to handle the characters that actually
1592 occur in the pattern. Others don't matter anyway! */
1593 bzero (simple_translate
, sizeof simple_translate
);
1594 for (i
= 0; i
< 0400; i
++)
1595 simple_translate
[i
] = i
;
1598 while (i
!= infinity
)
1600 unsigned char *ptr
= base_pat
+ i
;
1608 int this_translated
= 1;
1611 /* Is *PTR the last byte of a character? */
1612 && (pat_end
- ptr
== 1 || CHAR_HEAD_P (ptr
[1])))
1614 unsigned char *charstart
= ptr
;
1615 while (! CHAR_HEAD_P (*charstart
))
1617 untranslated
= STRING_CHAR (charstart
, ptr
- charstart
+ 1);
1618 if (charset_base
== (untranslated
& ~0x3F))
1620 TRANSLATE (ch
, trt
, untranslated
);
1621 if (! CHAR_HEAD_P (*ptr
))
1623 translate_prev_byte
= ptr
[-1];
1624 if (! CHAR_HEAD_P (translate_prev_byte
))
1625 translate_anteprev_byte
= ptr
[-2];
1630 this_translated
= 0;
1634 else if (!multibyte
)
1635 TRANSLATE (ch
, trt
, *ptr
);
1639 this_translated
= 0;
1644 j
= (ch
& 0x3F) | 0200;
1646 j
= (unsigned char) ch
;
1649 stride_for_teases
= BM_tab
[j
];
1651 BM_tab
[j
] = dirlen
- i
;
1652 /* A translation table is accompanied by its inverse -- see */
1653 /* comment following downcase_table for details */
1654 if (this_translated
)
1656 int starting_ch
= ch
;
1660 TRANSLATE (ch
, inverse_trt
, ch
);
1662 j
= (ch
& 0x3F) | 0200;
1664 j
= (unsigned char) ch
;
1666 /* For all the characters that map into CH,
1667 set up simple_translate to map the last byte
1669 simple_translate
[j
] = starting_j
;
1670 if (ch
== starting_ch
)
1672 BM_tab
[j
] = dirlen
- i
;
1681 stride_for_teases
= BM_tab
[j
];
1682 BM_tab
[j
] = dirlen
- i
;
1684 /* stride_for_teases tells how much to stride if we get a */
1685 /* match on the far character but are subsequently */
1686 /* disappointed, by recording what the stride would have been */
1687 /* for that character if the last character had been */
1690 infinity
= dirlen
- infinity
;
1691 pos_byte
+= dirlen
- ((direction
> 0) ? direction
: 0);
1692 /* loop invariant - POS_BYTE points at where last char (first
1693 char if reverse) of pattern would align in a possible match. */
1697 unsigned char *tail_end_ptr
;
1699 /* It's been reported that some (broken) compiler thinks that
1700 Boolean expressions in an arithmetic context are unsigned.
1701 Using an explicit ?1:0 prevents this. */
1702 if ((lim_byte
- pos_byte
- ((direction
> 0) ? 1 : 0)) * direction
1704 return (n
* (0 - direction
));
1705 /* First we do the part we can by pointers (maybe nothing) */
1708 limit
= pos_byte
- dirlen
+ direction
;
1711 limit
= BUFFER_CEILING_OF (limit
);
1712 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1713 can take on without hitting edge of buffer or the gap. */
1714 limit
= min (limit
, pos_byte
+ 20000);
1715 limit
= min (limit
, lim_byte
- 1);
1719 limit
= BUFFER_FLOOR_OF (limit
);
1720 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1721 can take on without hitting edge of buffer or the gap. */
1722 limit
= max (limit
, pos_byte
- 20000);
1723 limit
= max (limit
, lim_byte
);
1725 tail_end
= BUFFER_CEILING_OF (pos_byte
) + 1;
1726 tail_end_ptr
= BYTE_POS_ADDR (tail_end
);
1728 if ((limit
- pos_byte
) * direction
> 20)
1732 p_limit
= BYTE_POS_ADDR (limit
);
1733 p2
= (cursor
= BYTE_POS_ADDR (pos_byte
));
1734 /* In this loop, pos + cursor - p2 is the surrogate for pos */
1735 while (1) /* use one cursor setting as long as i can */
1737 if (direction
> 0) /* worth duplicating */
1739 /* Use signed comparison if appropriate
1740 to make cursor+infinity sure to be > p_limit.
1741 Assuming that the buffer lies in a range of addresses
1742 that are all "positive" (as ints) or all "negative",
1743 either kind of comparison will work as long
1744 as we don't step by infinity. So pick the kind
1745 that works when we do step by infinity. */
1746 if ((EMACS_INT
) (p_limit
+ infinity
) > (EMACS_INT
) p_limit
)
1747 while ((EMACS_INT
) cursor
<= (EMACS_INT
) p_limit
)
1748 cursor
+= BM_tab
[*cursor
];
1750 while ((EMACS_UINT
) cursor
<= (EMACS_UINT
) p_limit
)
1751 cursor
+= BM_tab
[*cursor
];
1755 if ((EMACS_INT
) (p_limit
+ infinity
) < (EMACS_INT
) p_limit
)
1756 while ((EMACS_INT
) cursor
>= (EMACS_INT
) p_limit
)
1757 cursor
+= BM_tab
[*cursor
];
1759 while ((EMACS_UINT
) cursor
>= (EMACS_UINT
) p_limit
)
1760 cursor
+= BM_tab
[*cursor
];
1762 /* If you are here, cursor is beyond the end of the searched region. */
1763 /* This can happen if you match on the far character of the pattern, */
1764 /* because the "stride" of that character is infinity, a number able */
1765 /* to throw you well beyond the end of the search. It can also */
1766 /* happen if you fail to match within the permitted region and would */
1767 /* otherwise try a character beyond that region */
1768 if ((cursor
- p_limit
) * direction
<= len_byte
)
1769 break; /* a small overrun is genuine */
1770 cursor
-= infinity
; /* large overrun = hit */
1771 i
= dirlen
- direction
;
1774 while ((i
-= direction
) + direction
!= 0)
1777 cursor
-= direction
;
1778 /* Translate only the last byte of a character. */
1780 || ((cursor
== tail_end_ptr
1781 || CHAR_HEAD_P (cursor
[1]))
1782 && (CHAR_HEAD_P (cursor
[0])
1783 || (translate_prev_byte
== cursor
[-1]
1784 && (CHAR_HEAD_P (translate_prev_byte
)
1785 || translate_anteprev_byte
== cursor
[-2])))))
1786 ch
= simple_translate
[*cursor
];
1795 while ((i
-= direction
) + direction
!= 0)
1797 cursor
-= direction
;
1798 if (pat
[i
] != *cursor
)
1802 cursor
+= dirlen
- i
- direction
; /* fix cursor */
1803 if (i
+ direction
== 0)
1807 cursor
-= direction
;
1809 position
= pos_byte
+ cursor
- p2
+ ((direction
> 0)
1810 ? 1 - len_byte
: 0);
1811 set_search_regs (position
, len_byte
);
1813 if ((n
-= direction
) != 0)
1814 cursor
+= dirlen
; /* to resume search */
1816 return ((direction
> 0)
1817 ? search_regs
.end
[0] : search_regs
.start
[0]);
1820 cursor
+= stride_for_teases
; /* <sigh> we lose - */
1822 pos_byte
+= cursor
- p2
;
1825 /* Now we'll pick up a clump that has to be done the hard */
1826 /* way because it covers a discontinuity */
1828 limit
= ((direction
> 0)
1829 ? BUFFER_CEILING_OF (pos_byte
- dirlen
+ 1)
1830 : BUFFER_FLOOR_OF (pos_byte
- dirlen
- 1));
1831 limit
= ((direction
> 0)
1832 ? min (limit
+ len_byte
, lim_byte
- 1)
1833 : max (limit
- len_byte
, lim_byte
));
1834 /* LIMIT is now the last value POS_BYTE can have
1835 and still be valid for a possible match. */
1838 /* This loop can be coded for space rather than */
1839 /* speed because it will usually run only once. */
1840 /* (the reach is at most len + 21, and typically */
1841 /* does not exceed len) */
1842 while ((limit
- pos_byte
) * direction
>= 0)
1843 pos_byte
+= BM_tab
[FETCH_BYTE (pos_byte
)];
1844 /* now run the same tests to distinguish going off the */
1845 /* end, a match or a phony match. */
1846 if ((pos_byte
- limit
) * direction
<= len_byte
)
1847 break; /* ran off the end */
1848 /* Found what might be a match.
1849 Set POS_BYTE back to last (first if reverse) pos. */
1850 pos_byte
-= infinity
;
1851 i
= dirlen
- direction
;
1852 while ((i
-= direction
) + direction
!= 0)
1856 pos_byte
-= direction
;
1857 ptr
= BYTE_POS_ADDR (pos_byte
);
1858 /* Translate only the last byte of a character. */
1860 || ((ptr
== tail_end_ptr
1861 || CHAR_HEAD_P (ptr
[1]))
1862 && (CHAR_HEAD_P (ptr
[0])
1863 || (translate_prev_byte
== ptr
[-1]
1864 && (CHAR_HEAD_P (translate_prev_byte
)
1865 || translate_anteprev_byte
== ptr
[-2])))))
1866 ch
= simple_translate
[*ptr
];
1872 /* Above loop has moved POS_BYTE part or all the way
1873 back to the first pos (last pos if reverse).
1874 Set it once again at the last (first if reverse) char. */
1875 pos_byte
+= dirlen
- i
- direction
;
1876 if (i
+ direction
== 0)
1879 pos_byte
-= direction
;
1881 position
= pos_byte
+ ((direction
> 0) ? 1 - len_byte
: 0);
1883 set_search_regs (position
, len_byte
);
1885 if ((n
-= direction
) != 0)
1886 pos_byte
+= dirlen
; /* to resume search */
1888 return ((direction
> 0)
1889 ? search_regs
.end
[0] : search_regs
.start
[0]);
1892 pos_byte
+= stride_for_teases
;
1895 /* We have done one clump. Can we continue? */
1896 if ((lim_byte
- pos_byte
) * direction
< 0)
1897 return ((0 - n
) * direction
);
1899 return BYTE_TO_CHAR (pos_byte
);
1902 /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES
1903 for the overall match just found in the current buffer.
1904 Also clear out the match data for registers 1 and up. */
1907 set_search_regs (beg_byte
, nbytes
)
1908 int beg_byte
, nbytes
;
1912 /* Make sure we have registers in which to store
1913 the match position. */
1914 if (search_regs
.num_regs
== 0)
1916 search_regs
.start
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
1917 search_regs
.end
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
1918 search_regs
.num_regs
= 2;
1921 /* Clear out the other registers. */
1922 for (i
= 1; i
< search_regs
.num_regs
; i
++)
1924 search_regs
.start
[i
] = -1;
1925 search_regs
.end
[i
] = -1;
1928 search_regs
.start
[0] = BYTE_TO_CHAR (beg_byte
);
1929 search_regs
.end
[0] = BYTE_TO_CHAR (beg_byte
+ nbytes
);
1930 XSETBUFFER (last_thing_searched
, current_buffer
);
1933 /* Given a string of words separated by word delimiters,
1934 compute a regexp that matches those exact words
1935 separated by arbitrary punctuation. */
1941 register unsigned char *p
, *o
;
1942 register int i
, i_byte
, len
, punct_count
= 0, word_count
= 0;
1947 CHECK_STRING (string
);
1948 p
= XSTRING (string
)->data
;
1949 len
= XSTRING (string
)->size
;
1951 for (i
= 0, i_byte
= 0; i
< len
; )
1955 FETCH_STRING_CHAR_ADVANCE (c
, string
, i
, i_byte
);
1957 if (SYNTAX (c
) != Sword
)
1960 if (i
> 0 && SYNTAX (prev_c
) == Sword
)
1967 if (SYNTAX (prev_c
) == Sword
)
1970 return empty_string
;
1972 adjust
= - punct_count
+ 5 * (word_count
- 1) + 4;
1973 if (STRING_MULTIBYTE (string
))
1974 val
= make_uninit_multibyte_string (len
+ adjust
,
1975 STRING_BYTES (XSTRING (string
))
1978 val
= make_uninit_string (len
+ adjust
);
1980 o
= XSTRING (val
)->data
;
1985 for (i
= 0, i_byte
= 0; i
< len
; )
1988 int i_byte_orig
= i_byte
;
1990 FETCH_STRING_CHAR_ADVANCE (c
, string
, i
, i_byte
);
1992 if (SYNTAX (c
) == Sword
)
1994 bcopy (&XSTRING (string
)->data
[i_byte_orig
], o
,
1995 i_byte
- i_byte_orig
);
1996 o
+= i_byte
- i_byte_orig
;
1998 else if (i
> 0 && SYNTAX (prev_c
) == Sword
&& --word_count
)
2016 DEFUN ("search-backward", Fsearch_backward
, Ssearch_backward
, 1, 4,
2017 "MSearch backward: ",
2018 doc
: /* Search backward from point for STRING.
2019 Set point to the beginning of the occurrence found, and return point.
2020 An optional second argument bounds the search; it is a buffer position.
2021 The match found must not extend before that position.
2022 Optional third argument, if t, means if fail just return nil (no error).
2023 If not nil and not t, position at limit of search and return nil.
2024 Optional fourth argument is repeat count--search for successive occurrences.
2026 Search case-sensitivity is determined by the value of the variable
2027 `case-fold-search', which see.
2029 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2030 (string
, bound
, noerror
, count
)
2031 Lisp_Object string
, bound
, noerror
, count
;
2033 return search_command (string
, bound
, noerror
, count
, -1, 0, 0);
2036 DEFUN ("search-forward", Fsearch_forward
, Ssearch_forward
, 1, 4, "MSearch: ",
2037 doc
: /* Search forward from point for STRING.
2038 Set point to the end of the occurrence found, and return point.
2039 An optional second argument bounds the search; it is a buffer position.
2040 The match found must not extend after that position. nil is equivalent
2042 Optional third argument, if t, means if fail just return nil (no error).
2043 If not nil and not t, move to limit of search and return nil.
2044 Optional fourth argument is repeat count--search for successive occurrences.
2046 Search case-sensitivity is determined by the value of the variable
2047 `case-fold-search', which see.
2049 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2050 (string
, bound
, noerror
, count
)
2051 Lisp_Object string
, bound
, noerror
, count
;
2053 return search_command (string
, bound
, noerror
, count
, 1, 0, 0);
2056 DEFUN ("word-search-backward", Fword_search_backward
, Sword_search_backward
, 1, 4,
2057 "sWord search backward: ",
2058 doc
: /* Search backward from point for STRING, ignoring differences in punctuation.
2059 Set point to the beginning of the occurrence found, and return point.
2060 An optional second argument bounds the search; it is a buffer position.
2061 The match found must not extend before that position.
2062 Optional third argument, if t, means if fail just return nil (no error).
2063 If not nil and not t, move to limit of search and return nil.
2064 Optional fourth argument is repeat count--search for successive occurrences. */)
2065 (string
, bound
, noerror
, count
)
2066 Lisp_Object string
, bound
, noerror
, count
;
2068 return search_command (wordify (string
), bound
, noerror
, count
, -1, 1, 0);
2071 DEFUN ("word-search-forward", Fword_search_forward
, Sword_search_forward
, 1, 4,
2073 doc
: /* Search forward from point for STRING, ignoring differences in punctuation.
2074 Set point to the end of the occurrence found, and return point.
2075 An optional second argument bounds the search; it is a buffer position.
2076 The match found must not extend after that position.
2077 Optional third argument, if t, means if fail just return nil (no error).
2078 If not nil and not t, move to limit of search and return nil.
2079 Optional fourth argument is repeat count--search for successive occurrences. */)
2080 (string
, bound
, noerror
, count
)
2081 Lisp_Object string
, bound
, noerror
, count
;
2083 return search_command (wordify (string
), bound
, noerror
, count
, 1, 1, 0);
2086 DEFUN ("re-search-backward", Fre_search_backward
, Sre_search_backward
, 1, 4,
2087 "sRE search backward: ",
2088 doc
: /* Search backward from point for match for regular expression REGEXP.
2089 Set point to the beginning of the match, and return point.
2090 The match found is the one starting last in the buffer
2091 and yet ending before the origin of the search.
2092 An optional second argument bounds the search; it is a buffer position.
2093 The match found must start at or after that position.
2094 Optional third argument, if t, means if fail just return nil (no error).
2095 If not nil and not t, move to limit of search and return nil.
2096 Optional fourth argument is repeat count--search for successive occurrences.
2097 See also the functions `match-beginning', `match-end', `match-string',
2098 and `replace-match'. */)
2099 (regexp
, bound
, noerror
, count
)
2100 Lisp_Object regexp
, bound
, noerror
, count
;
2102 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 0);
2105 DEFUN ("re-search-forward", Fre_search_forward
, Sre_search_forward
, 1, 4,
2107 doc
: /* Search forward from point for regular expression REGEXP.
2108 Set point to the end of the occurrence found, and return point.
2109 An optional second argument bounds the search; it is a buffer position.
2110 The match found must not extend after that position.
2111 Optional third argument, if t, means if fail just return nil (no error).
2112 If not nil and not t, move to limit of search and return nil.
2113 Optional fourth argument is repeat count--search for successive occurrences.
2114 See also the functions `match-beginning', `match-end', `match-string',
2115 and `replace-match'. */)
2116 (regexp
, bound
, noerror
, count
)
2117 Lisp_Object regexp
, bound
, noerror
, count
;
2119 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 0);
2122 DEFUN ("posix-search-backward", Fposix_search_backward
, Sposix_search_backward
, 1, 4,
2123 "sPosix search backward: ",
2124 doc
: /* Search backward from point for match for regular expression REGEXP.
2125 Find the longest match in accord with Posix regular expression rules.
2126 Set point to the beginning of the match, and return point.
2127 The match found is the one starting last in the buffer
2128 and yet ending before the origin of the search.
2129 An optional second argument bounds the search; it is a buffer position.
2130 The match found must start at or after that position.
2131 Optional third argument, if t, means if fail just return nil (no error).
2132 If not nil and not t, move to limit of search and return nil.
2133 Optional fourth argument is repeat count--search for successive occurrences.
2134 See also the functions `match-beginning', `match-end', `match-string',
2135 and `replace-match'. */)
2136 (regexp
, bound
, noerror
, count
)
2137 Lisp_Object regexp
, bound
, noerror
, count
;
2139 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 1);
2142 DEFUN ("posix-search-forward", Fposix_search_forward
, Sposix_search_forward
, 1, 4,
2144 doc
: /* Search forward from point for regular expression REGEXP.
2145 Find the longest match in accord with Posix regular expression rules.
2146 Set point to the end of the occurrence found, and return point.
2147 An optional second argument bounds the search; it is a buffer position.
2148 The match found must not extend after that position.
2149 Optional third argument, if t, means if fail just return nil (no error).
2150 If not nil and not t, move to limit of search and return nil.
2151 Optional fourth argument is repeat count--search for successive occurrences.
2152 See also the functions `match-beginning', `match-end', `match-string',
2153 and `replace-match'. */)
2154 (regexp
, bound
, noerror
, count
)
2155 Lisp_Object regexp
, bound
, noerror
, count
;
2157 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 1);
2160 DEFUN ("replace-match", Freplace_match
, Sreplace_match
, 1, 5, 0,
2161 doc
: /* Replace text matched by last search with NEWTEXT.
2162 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
2163 Otherwise maybe capitalize the whole text, or maybe just word initials,
2164 based on the replaced text.
2165 If the replaced text has only capital letters
2166 and has at least one multiletter word, convert NEWTEXT to all caps.
2167 If the replaced text has at least one word starting with a capital letter,
2168 then capitalize each word in NEWTEXT.
2170 If third arg LITERAL is non-nil, insert NEWTEXT literally.
2171 Otherwise treat `\\' as special:
2172 `\\&' in NEWTEXT means substitute original matched text.
2173 `\\N' means substitute what matched the Nth `\\(...\\)'.
2174 If Nth parens didn't match, substitute nothing.
2175 `\\\\' means insert one `\\'.
2176 FIXEDCASE and LITERAL are optional arguments.
2177 Leaves point at end of replacement text.
2179 The optional fourth argument STRING can be a string to modify.
2180 This is meaningful when the previous match was done against STRING,
2181 using `string-match'. When used this way, `replace-match'
2182 creates and returns a new string made by copying STRING and replacing
2183 the part of STRING that was matched.
2185 The optional fifth argument SUBEXP specifies a subexpression;
2186 it says to replace just that subexpression with NEWTEXT,
2187 rather than replacing the entire matched text.
2188 This is, in a vague sense, the inverse of using `\\N' in NEWTEXT;
2189 `\\N' copies subexp N into NEWTEXT, but using N as SUBEXP puts
2190 NEWTEXT in place of subexp N.
2191 This is useful only after a regular expression search or match,
2192 since only regular expressions have distinguished subexpressions. */)
2193 (newtext
, fixedcase
, literal
, string
, subexp
)
2194 Lisp_Object newtext
, fixedcase
, literal
, string
, subexp
;
2196 enum { nochange
, all_caps
, cap_initial
} case_action
;
2197 register int pos
, pos_byte
;
2198 int some_multiletter_word
;
2201 int some_nonuppercase_initial
;
2202 register int c
, prevc
;
2204 int opoint
, newpoint
;
2206 CHECK_STRING (newtext
);
2208 if (! NILP (string
))
2209 CHECK_STRING (string
);
2211 case_action
= nochange
; /* We tried an initialization */
2212 /* but some C compilers blew it */
2214 if (search_regs
.num_regs
<= 0)
2215 error ("replace-match called before any match found");
2221 CHECK_NUMBER (subexp
);
2222 sub
= XINT (subexp
);
2223 if (sub
< 0 || sub
>= search_regs
.num_regs
)
2224 args_out_of_range (subexp
, make_number (search_regs
.num_regs
));
2229 if (search_regs
.start
[sub
] < BEGV
2230 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2231 || search_regs
.end
[sub
] > ZV
)
2232 args_out_of_range (make_number (search_regs
.start
[sub
]),
2233 make_number (search_regs
.end
[sub
]));
2237 if (search_regs
.start
[sub
] < 0
2238 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2239 || search_regs
.end
[sub
] > XSTRING (string
)->size
)
2240 args_out_of_range (make_number (search_regs
.start
[sub
]),
2241 make_number (search_regs
.end
[sub
]));
2244 if (NILP (fixedcase
))
2246 /* Decide how to casify by examining the matched text. */
2249 pos
= search_regs
.start
[sub
];
2250 last
= search_regs
.end
[sub
];
2253 pos_byte
= CHAR_TO_BYTE (pos
);
2255 pos_byte
= string_char_to_byte (string
, pos
);
2258 case_action
= all_caps
;
2260 /* some_multiletter_word is set nonzero if any original word
2261 is more than one letter long. */
2262 some_multiletter_word
= 0;
2264 some_nonuppercase_initial
= 0;
2271 c
= FETCH_CHAR (pos_byte
);
2272 INC_BOTH (pos
, pos_byte
);
2275 FETCH_STRING_CHAR_ADVANCE (c
, string
, pos
, pos_byte
);
2279 /* Cannot be all caps if any original char is lower case */
2282 if (SYNTAX (prevc
) != Sword
)
2283 some_nonuppercase_initial
= 1;
2285 some_multiletter_word
= 1;
2287 else if (!NOCASEP (c
))
2290 if (SYNTAX (prevc
) != Sword
)
2293 some_multiletter_word
= 1;
2297 /* If the initial is a caseless word constituent,
2298 treat that like a lowercase initial. */
2299 if (SYNTAX (prevc
) != Sword
)
2300 some_nonuppercase_initial
= 1;
2306 /* Convert to all caps if the old text is all caps
2307 and has at least one multiletter word. */
2308 if (! some_lowercase
&& some_multiletter_word
)
2309 case_action
= all_caps
;
2310 /* Capitalize each word, if the old text has all capitalized words. */
2311 else if (!some_nonuppercase_initial
&& some_multiletter_word
)
2312 case_action
= cap_initial
;
2313 else if (!some_nonuppercase_initial
&& some_uppercase
)
2314 /* Should x -> yz, operating on X, give Yz or YZ?
2315 We'll assume the latter. */
2316 case_action
= all_caps
;
2318 case_action
= nochange
;
2321 /* Do replacement in a string. */
2324 Lisp_Object before
, after
;
2326 before
= Fsubstring (string
, make_number (0),
2327 make_number (search_regs
.start
[sub
]));
2328 after
= Fsubstring (string
, make_number (search_regs
.end
[sub
]), Qnil
);
2330 /* Substitute parts of the match into NEWTEXT
2335 int lastpos_byte
= 0;
2336 /* We build up the substituted string in ACCUM. */
2339 int length
= STRING_BYTES (XSTRING (newtext
));
2343 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2347 int delbackslash
= 0;
2349 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2353 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2357 substart
= search_regs
.start
[sub
];
2358 subend
= search_regs
.end
[sub
];
2360 else if (c
>= '1' && c
<= '9' && c
<= search_regs
.num_regs
+ '0')
2362 if (search_regs
.start
[c
- '0'] >= 0)
2364 substart
= search_regs
.start
[c
- '0'];
2365 subend
= search_regs
.end
[c
- '0'];
2371 error ("Invalid use of `\\' in replacement text");
2375 if (pos
- 2 != lastpos
)
2376 middle
= substring_both (newtext
, lastpos
,
2378 pos
- 2, pos_byte
- 2);
2381 accum
= concat3 (accum
, middle
,
2383 make_number (substart
),
2384 make_number (subend
)));
2386 lastpos_byte
= pos_byte
;
2388 else if (delbackslash
)
2390 middle
= substring_both (newtext
, lastpos
,
2392 pos
- 1, pos_byte
- 1);
2394 accum
= concat2 (accum
, middle
);
2396 lastpos_byte
= pos_byte
;
2401 middle
= substring_both (newtext
, lastpos
,
2407 newtext
= concat2 (accum
, middle
);
2410 /* Do case substitution in NEWTEXT if desired. */
2411 if (case_action
== all_caps
)
2412 newtext
= Fupcase (newtext
);
2413 else if (case_action
== cap_initial
)
2414 newtext
= Fupcase_initials (newtext
);
2416 return concat3 (before
, newtext
, after
);
2419 /* Record point, then move (quietly) to the start of the match. */
2420 if (PT
>= search_regs
.end
[sub
])
2422 else if (PT
> search_regs
.start
[sub
])
2423 opoint
= search_regs
.end
[sub
] - ZV
;
2427 /* If we want non-literal replacement,
2428 perform substitution on the replacement string. */
2431 int length
= STRING_BYTES (XSTRING (newtext
));
2432 unsigned char *substed
;
2433 int substed_alloc_size
, substed_len
;
2434 int buf_multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
2435 int str_multibyte
= STRING_MULTIBYTE (newtext
);
2436 Lisp_Object rev_tbl
;
2437 int really_changed
= 0;
2441 substed_alloc_size
= length
* 2 + 100;
2442 substed
= (unsigned char *) xmalloc (substed_alloc_size
+ 1);
2445 /* Go thru NEWTEXT, producing the actual text to insert in
2446 SUBSTED while adjusting multibyteness to that of the current
2449 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2451 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2452 unsigned char *add_stuff
= NULL
;
2458 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
, pos
, pos_byte
);
2460 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2464 /* Note that we don't have to increment POS. */
2465 c
= XSTRING (newtext
)->data
[pos_byte
++];
2467 c
= unibyte_char_to_multibyte (c
);
2470 /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
2471 or set IDX to a match index, which means put that part
2472 of the buffer text into SUBSTED. */
2480 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
,
2482 if (!buf_multibyte
&& !SINGLE_BYTE_CHAR_P (c
))
2483 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2487 c
= XSTRING (newtext
)->data
[pos_byte
++];
2489 c
= unibyte_char_to_multibyte (c
);
2494 else if (c
>= '1' && c
<= '9' && c
<= search_regs
.num_regs
+ '0')
2496 if (search_regs
.start
[c
- '0'] >= 1)
2500 add_len
= 1, add_stuff
= "\\";
2504 error ("Invalid use of `\\' in replacement text");
2509 add_len
= CHAR_STRING (c
, str
);
2513 /* If we want to copy part of a previous match,
2514 set up ADD_STUFF and ADD_LEN to point to it. */
2517 int begbyte
= CHAR_TO_BYTE (search_regs
.start
[idx
]);
2518 add_len
= CHAR_TO_BYTE (search_regs
.end
[idx
]) - begbyte
;
2519 if (search_regs
.start
[idx
] < GPT
&& GPT
< search_regs
.end
[idx
])
2520 move_gap (search_regs
.start
[idx
]);
2521 add_stuff
= BYTE_POS_ADDR (begbyte
);
2524 /* Now the stuff we want to add to SUBSTED
2525 is invariably ADD_LEN bytes starting at ADD_STUFF. */
2527 /* Make sure SUBSTED is big enough. */
2528 if (substed_len
+ add_len
>= substed_alloc_size
)
2530 substed_alloc_size
= substed_len
+ add_len
+ 500;
2531 substed
= (unsigned char *) xrealloc (substed
,
2532 substed_alloc_size
+ 1);
2535 /* Now add to the end of SUBSTED. */
2538 bcopy (add_stuff
, substed
+ substed_len
, add_len
);
2539 substed_len
+= add_len
;
2544 newtext
= make_string (substed
, substed_len
);
2549 /* Replace the old text with the new in the cleanest possible way. */
2550 replace_range (search_regs
.start
[sub
], search_regs
.end
[sub
],
2552 newpoint
= search_regs
.start
[sub
] + XSTRING (newtext
)->size
;
2554 if (case_action
== all_caps
)
2555 Fupcase_region (make_number (search_regs
.start
[sub
]),
2556 make_number (newpoint
));
2557 else if (case_action
== cap_initial
)
2558 Fupcase_initials_region (make_number (search_regs
.start
[sub
]),
2559 make_number (newpoint
));
2561 /* Put point back where it was in the text. */
2563 TEMP_SET_PT (opoint
+ ZV
);
2565 TEMP_SET_PT (opoint
);
2567 /* Now move point "officially" to the start of the inserted replacement. */
2568 move_if_not_intangible (newpoint
);
2574 match_limit (num
, beginningp
)
2582 if (n
< 0 || n
>= search_regs
.num_regs
)
2583 args_out_of_range (num
, make_number (search_regs
.num_regs
));
2584 if (search_regs
.num_regs
<= 0
2585 || search_regs
.start
[n
] < 0)
2587 return (make_number ((beginningp
) ? search_regs
.start
[n
]
2588 : search_regs
.end
[n
]));
2591 DEFUN ("match-beginning", Fmatch_beginning
, Smatch_beginning
, 1, 1, 0,
2592 doc
: /* Return position of start of text matched by last search.
2593 SUBEXP, a number, specifies which parenthesized expression in the last
2595 Value is nil if SUBEXPth pair didn't match, or there were less than
2597 Zero means the entire text matched by the whole regexp or whole string. */)
2601 return match_limit (subexp
, 1);
2604 DEFUN ("match-end", Fmatch_end
, Smatch_end
, 1, 1, 0,
2605 doc
: /* Return position of end of text matched by last search.
2606 SUBEXP, a number, specifies which parenthesized expression in the last
2608 Value is nil if SUBEXPth pair didn't match, or there were less than
2610 Zero means the entire text matched by the whole regexp or whole string. */)
2614 return match_limit (subexp
, 0);
2617 DEFUN ("match-data", Fmatch_data
, Smatch_data
, 0, 2, 0,
2618 doc
: /* Return a list containing all info on what the last search matched.
2619 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
2620 All the elements are markers or nil (nil if the Nth pair didn't match)
2621 if the last match was on a buffer; integers or nil if a string was matched.
2622 Use `store-match-data' to reinstate the data in this list.
2624 If INTEGERS (the optional first argument) is non-nil, always use integers
2625 \(rather than markers) to represent buffer positions.
2626 If REUSE is a list, reuse it as part of the value. If REUSE is long enough
2627 to hold all the values, and if INTEGERS is non-nil, no consing is done. */)
2629 Lisp_Object integers
, reuse
;
2631 Lisp_Object tail
, prev
;
2635 if (NILP (last_thing_searched
))
2640 data
= (Lisp_Object
*) alloca ((2 * search_regs
.num_regs
)
2641 * sizeof (Lisp_Object
));
2644 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2646 int start
= search_regs
.start
[i
];
2649 if (EQ (last_thing_searched
, Qt
)
2650 || ! NILP (integers
))
2652 XSETFASTINT (data
[2 * i
], start
);
2653 XSETFASTINT (data
[2 * i
+ 1], search_regs
.end
[i
]);
2655 else if (BUFFERP (last_thing_searched
))
2657 data
[2 * i
] = Fmake_marker ();
2658 Fset_marker (data
[2 * i
],
2659 make_number (start
),
2660 last_thing_searched
);
2661 data
[2 * i
+ 1] = Fmake_marker ();
2662 Fset_marker (data
[2 * i
+ 1],
2663 make_number (search_regs
.end
[i
]),
2664 last_thing_searched
);
2667 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
2673 data
[2 * i
] = data
[2 * i
+ 1] = Qnil
;
2676 /* If REUSE is not usable, cons up the values and return them. */
2677 if (! CONSP (reuse
))
2678 return Flist (2 * len
+ 2, data
);
2680 /* If REUSE is a list, store as many value elements as will fit
2681 into the elements of REUSE. */
2682 for (i
= 0, tail
= reuse
; CONSP (tail
);
2683 i
++, tail
= XCDR (tail
))
2685 if (i
< 2 * len
+ 2)
2686 XSETCAR (tail
, data
[i
]);
2688 XSETCAR (tail
, Qnil
);
2692 /* If we couldn't fit all value elements into REUSE,
2693 cons up the rest of them and add them to the end of REUSE. */
2694 if (i
< 2 * len
+ 2)
2695 XSETCDR (prev
, Flist (2 * len
+ 2 - i
, data
+ i
));
2701 DEFUN ("set-match-data", Fset_match_data
, Sset_match_data
, 1, 1, 0,
2702 doc
: /* Set internal data on last search match from elements of LIST.
2703 LIST should have been created by calling `match-data' previously. */)
2705 register Lisp_Object list
;
2708 register Lisp_Object marker
;
2710 if (running_asynch_code
)
2711 save_search_regs ();
2713 if (!CONSP (list
) && !NILP (list
))
2714 list
= wrong_type_argument (Qconsp
, list
);
2716 /* Unless we find a marker with a buffer in LIST, assume that this
2717 match data came from a string. */
2718 last_thing_searched
= Qt
;
2720 /* Allocate registers if they don't already exist. */
2722 int length
= XFASTINT (Flength (list
)) / 2;
2724 if (length
> search_regs
.num_regs
)
2726 if (search_regs
.num_regs
== 0)
2729 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2731 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2736 = (regoff_t
*) xrealloc (search_regs
.start
,
2737 length
* sizeof (regoff_t
));
2739 = (regoff_t
*) xrealloc (search_regs
.end
,
2740 length
* sizeof (regoff_t
));
2743 for (i
= search_regs
.num_regs
; i
< length
; i
++)
2744 search_regs
.start
[i
] = -1;
2746 search_regs
.num_regs
= length
;
2750 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2752 marker
= Fcar (list
);
2755 search_regs
.start
[i
] = -1;
2762 if (MARKERP (marker
))
2764 if (XMARKER (marker
)->buffer
== 0)
2765 XSETFASTINT (marker
, 0);
2767 XSETBUFFER (last_thing_searched
, XMARKER (marker
)->buffer
);
2770 CHECK_NUMBER_COERCE_MARKER (marker
);
2771 from
= XINT (marker
);
2774 marker
= Fcar (list
);
2775 if (MARKERP (marker
) && XMARKER (marker
)->buffer
== 0)
2776 XSETFASTINT (marker
, 0);
2778 CHECK_NUMBER_COERCE_MARKER (marker
);
2779 search_regs
.start
[i
] = from
;
2780 search_regs
.end
[i
] = XINT (marker
);
2788 /* If non-zero the match data have been saved in saved_search_regs
2789 during the execution of a sentinel or filter. */
2790 static int search_regs_saved
;
2791 static struct re_registers saved_search_regs
;
2793 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
2794 if asynchronous code (filter or sentinel) is running. */
2798 if (!search_regs_saved
)
2800 saved_search_regs
.num_regs
= search_regs
.num_regs
;
2801 saved_search_regs
.start
= search_regs
.start
;
2802 saved_search_regs
.end
= search_regs
.end
;
2803 search_regs
.num_regs
= 0;
2804 search_regs
.start
= 0;
2805 search_regs
.end
= 0;
2807 search_regs_saved
= 1;
2811 /* Called upon exit from filters and sentinels. */
2813 restore_match_data ()
2815 if (search_regs_saved
)
2817 if (search_regs
.num_regs
> 0)
2819 xfree (search_regs
.start
);
2820 xfree (search_regs
.end
);
2822 search_regs
.num_regs
= saved_search_regs
.num_regs
;
2823 search_regs
.start
= saved_search_regs
.start
;
2824 search_regs
.end
= saved_search_regs
.end
;
2826 search_regs_saved
= 0;
2830 /* Quote a string to inactivate reg-expr chars */
2832 DEFUN ("regexp-quote", Fregexp_quote
, Sregexp_quote
, 1, 1, 0,
2833 doc
: /* Return a regexp string which matches exactly STRING and nothing else. */)
2837 register unsigned char *in
, *out
, *end
;
2838 register unsigned char *temp
;
2839 int backslashes_added
= 0;
2841 CHECK_STRING (string
);
2843 temp
= (unsigned char *) alloca (STRING_BYTES (XSTRING (string
)) * 2);
2845 /* Now copy the data into the new string, inserting escapes. */
2847 in
= XSTRING (string
)->data
;
2848 end
= in
+ STRING_BYTES (XSTRING (string
));
2851 for (; in
!= end
; in
++)
2853 if (*in
== '[' || *in
== ']'
2854 || *in
== '*' || *in
== '.' || *in
== '\\'
2855 || *in
== '?' || *in
== '+'
2856 || *in
== '^' || *in
== '$')
2857 *out
++ = '\\', backslashes_added
++;
2861 return make_specified_string (temp
,
2862 XSTRING (string
)->size
+ backslashes_added
,
2864 STRING_MULTIBYTE (string
));
2872 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
2874 searchbufs
[i
].buf
.allocated
= 100;
2875 searchbufs
[i
].buf
.buffer
= (unsigned char *) malloc (100);
2876 searchbufs
[i
].buf
.fastmap
= searchbufs
[i
].fastmap
;
2877 searchbufs
[i
].regexp
= Qnil
;
2878 staticpro (&searchbufs
[i
].regexp
);
2879 searchbufs
[i
].next
= (i
== REGEXP_CACHE_SIZE
-1 ? 0 : &searchbufs
[i
+1]);
2881 searchbuf_head
= &searchbufs
[0];
2883 Qsearch_failed
= intern ("search-failed");
2884 staticpro (&Qsearch_failed
);
2885 Qinvalid_regexp
= intern ("invalid-regexp");
2886 staticpro (&Qinvalid_regexp
);
2888 Fput (Qsearch_failed
, Qerror_conditions
,
2889 Fcons (Qsearch_failed
, Fcons (Qerror
, Qnil
)));
2890 Fput (Qsearch_failed
, Qerror_message
,
2891 build_string ("Search failed"));
2893 Fput (Qinvalid_regexp
, Qerror_conditions
,
2894 Fcons (Qinvalid_regexp
, Fcons (Qerror
, Qnil
)));
2895 Fput (Qinvalid_regexp
, Qerror_message
,
2896 build_string ("Invalid regexp"));
2898 last_thing_searched
= Qnil
;
2899 staticpro (&last_thing_searched
);
2901 defsubr (&Slooking_at
);
2902 defsubr (&Sposix_looking_at
);
2903 defsubr (&Sstring_match
);
2904 defsubr (&Sposix_string_match
);
2905 defsubr (&Ssearch_forward
);
2906 defsubr (&Ssearch_backward
);
2907 defsubr (&Sword_search_forward
);
2908 defsubr (&Sword_search_backward
);
2909 defsubr (&Sre_search_forward
);
2910 defsubr (&Sre_search_backward
);
2911 defsubr (&Sposix_search_forward
);
2912 defsubr (&Sposix_search_backward
);
2913 defsubr (&Sreplace_match
);
2914 defsubr (&Smatch_beginning
);
2915 defsubr (&Smatch_end
);
2916 defsubr (&Smatch_data
);
2917 defsubr (&Sset_match_data
);
2918 defsubr (&Sregexp_quote
);