1 /* String search routines for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1997, 1998, 1999, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 3, or (at your option)
13 GNU Emacs is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with GNU Emacs; see the file COPYING. If not, write to
20 the Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
21 Boston, MA 02110-1301, USA. */
29 #include "character.h"
31 #include "region-cache.h"
33 #include "blockinput.h"
34 #include "intervals.h"
36 #include <sys/types.h>
39 #define REGEXP_CACHE_SIZE 20
41 /* If the regexp is non-nil, then the buffer contains the compiled form
42 of that regexp, suitable for searching. */
45 struct regexp_cache
*next
;
46 Lisp_Object regexp
, whitespace_regexp
;
47 /* Syntax table for which the regexp applies. We need this because
48 of character classes. If this is t, then the compiled pattern is valid
49 for any syntax-table. */
50 Lisp_Object syntax_table
;
51 struct re_pattern_buffer buf
;
53 /* Nonzero means regexp was compiled to do full POSIX backtracking. */
57 /* The instances of that struct. */
58 struct regexp_cache searchbufs
[REGEXP_CACHE_SIZE
];
60 /* The head of the linked list; points to the most recently used buffer. */
61 struct regexp_cache
*searchbuf_head
;
64 /* Every call to re_match, etc., must pass &search_regs as the regs
65 argument unless you can show it is unnecessary (i.e., if re_match
66 is certainly going to be called again before region-around-match
69 Since the registers are now dynamically allocated, we need to make
70 sure not to refer to the Nth register before checking that it has
71 been allocated by checking search_regs.num_regs.
73 The regex code keeps track of whether it has allocated the search
74 buffer using bits in the re_pattern_buffer. This means that whenever
75 you compile a new pattern, it completely forgets whether it has
76 allocated any registers, and will allocate new registers the next
77 time you call a searching or matching function. Therefore, we need
78 to call re_set_registers after compiling a new pattern or after
79 setting the match registers, so that the regex functions will be
80 able to free or re-allocate it properly. */
81 static struct re_registers search_regs
;
83 /* The buffer in which the last search was performed, or
84 Qt if the last search was done in a string;
85 Qnil if no searching has been done yet. */
86 static Lisp_Object last_thing_searched
;
88 /* error condition signaled when regexp compile_pattern fails */
90 Lisp_Object Qinvalid_regexp
;
92 /* Error condition used for failing searches */
93 Lisp_Object Qsearch_failed
;
95 Lisp_Object Vsearch_spaces_regexp
;
97 /* If non-nil, the match data will not be changed during call to
98 searching or matching functions. This variable is for internal use
100 Lisp_Object Vinhibit_changing_match_data
;
102 static void set_search_regs ();
103 static void save_search_regs ();
104 static int simple_search ();
105 static int boyer_moore ();
106 static int search_buffer ();
107 static void matcher_overflow () NO_RETURN
;
112 error ("Stack overflow in regexp matcher");
115 /* Compile a regexp and signal a Lisp error if anything goes wrong.
116 PATTERN is the pattern to compile.
117 CP is the place to put the result.
118 TRANSLATE is a translation table for ignoring case, or nil for none.
119 REGP is the structure that says where to store the "register"
120 values that will result from matching this pattern.
121 If it is 0, we should compile the pattern not to record any
122 subexpression bounds.
123 POSIX is nonzero if we want full backtracking (POSIX style)
124 for this pattern. 0 means backtrack only enough to get a valid match.
126 The behavior also depends on Vsearch_spaces_regexp. */
129 compile_pattern_1 (cp
, pattern
, translate
, regp
, posix
)
130 struct regexp_cache
*cp
;
132 Lisp_Object translate
;
133 struct re_registers
*regp
;
140 cp
->buf
.translate
= (! NILP (translate
) ? translate
: make_number (0));
142 cp
->buf
.multibyte
= STRING_MULTIBYTE (pattern
);
143 cp
->buf
.charset_unibyte
= charset_unibyte
;
144 cp
->whitespace_regexp
= Vsearch_spaces_regexp
;
145 /* rms: I think BLOCK_INPUT is not needed here any more,
146 because regex.c defines malloc to call xmalloc.
147 Using BLOCK_INPUT here means the debugger won't run if an error occurs.
148 So let's turn it off. */
150 old
= re_set_syntax (RE_SYNTAX_EMACS
151 | (posix
? 0 : RE_NO_POSIX_BACKTRACKING
));
152 re_set_whitespace_regexp (NILP (Vsearch_spaces_regexp
) ? NULL
153 : SDATA (Vsearch_spaces_regexp
));
155 val
= (char *) re_compile_pattern ((char *) SDATA (pattern
),
156 SBYTES (pattern
), &cp
->buf
);
158 /* If the compiled pattern hard codes some of the contents of the
159 syntax-table, it can only be reused with *this* syntax table. */
160 cp
->syntax_table
= cp
->buf
.used_syntax
? current_buffer
->syntax_table
: Qt
;
162 re_set_whitespace_regexp (NULL
);
167 xsignal1 (Qinvalid_regexp
, build_string (val
));
169 cp
->regexp
= Fcopy_sequence (pattern
);
172 /* Shrink each compiled regexp buffer in the cache
173 to the size actually used right now.
174 This is called from garbage collection. */
177 shrink_regexp_cache ()
179 struct regexp_cache
*cp
;
181 for (cp
= searchbuf_head
; cp
!= 0; cp
= cp
->next
)
183 cp
->buf
.allocated
= cp
->buf
.used
;
185 = (unsigned char *) xrealloc (cp
->buf
.buffer
, cp
->buf
.used
);
189 /* Clear the regexp cache w.r.t. a particular syntax table,
190 because it was changed.
191 There is no danger of memory leak here because re_compile_pattern
192 automagically manages the memory in each re_pattern_buffer struct,
193 based on its `allocated' and `buffer' values. */
195 clear_regexp_cache ()
199 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
200 /* It's tempting to compare with the syntax-table we've actually changd,
201 but it's not sufficient because char-table inheritance mewans that
202 modifying one syntax-table can change others at the same time. */
203 if (!EQ (searchbufs
[i
].syntax_table
, Qt
))
204 searchbufs
[i
].regexp
= Qnil
;
207 /* Compile a regexp if necessary, but first check to see if there's one in
209 PATTERN is the pattern to compile.
210 TRANSLATE is a translation table for ignoring case, or nil for none.
211 REGP is the structure that says where to store the "register"
212 values that will result from matching this pattern.
213 If it is 0, we should compile the pattern not to record any
214 subexpression bounds.
215 POSIX is nonzero if we want full backtracking (POSIX style)
216 for this pattern. 0 means backtrack only enough to get a valid match. */
218 struct re_pattern_buffer
*
219 compile_pattern (pattern
, regp
, translate
, posix
, multibyte
)
221 struct re_registers
*regp
;
222 Lisp_Object translate
;
223 int posix
, multibyte
;
225 struct regexp_cache
*cp
, **cpp
;
227 for (cpp
= &searchbuf_head
; ; cpp
= &cp
->next
)
230 /* Entries are initialized to nil, and may be set to nil by
231 compile_pattern_1 if the pattern isn't valid. Don't apply
232 string accessors in those cases. However, compile_pattern_1
233 is only applied to the cache entry we pick here to reuse. So
234 nil should never appear before a non-nil entry. */
235 if (NILP (cp
->regexp
))
237 if (SCHARS (cp
->regexp
) == SCHARS (pattern
)
238 && STRING_MULTIBYTE (cp
->regexp
) == STRING_MULTIBYTE (pattern
)
239 && !NILP (Fstring_equal (cp
->regexp
, pattern
))
240 && EQ (cp
->buf
.translate
, (! NILP (translate
) ? translate
: make_number (0)))
241 && cp
->posix
== posix
242 && (EQ (cp
->syntax_table
, Qt
)
243 || EQ (cp
->syntax_table
, current_buffer
->syntax_table
))
244 && !NILP (Fequal (cp
->whitespace_regexp
, Vsearch_spaces_regexp
))
245 && cp
->buf
.charset_unibyte
== charset_unibyte
)
248 /* If we're at the end of the cache, compile into the nil cell
249 we found, or the last (least recently used) cell with a
254 compile_pattern_1 (cp
, pattern
, translate
, regp
, posix
);
259 /* When we get here, cp (aka *cpp) contains the compiled pattern,
260 either because we found it in the cache or because we just compiled it.
261 Move it to the front of the queue to mark it as most recently used. */
263 cp
->next
= searchbuf_head
;
266 /* Advise the searching functions about the space we have allocated
267 for register data. */
269 re_set_registers (&cp
->buf
, regp
, regp
->num_regs
, regp
->start
, regp
->end
);
271 /* The compiled pattern can be used both for mulitbyte and unibyte
272 target. But, we have to tell which the pattern is used for. */
273 cp
->buf
.target_multibyte
= multibyte
;
280 looking_at_1 (string
, posix
)
285 unsigned char *p1
, *p2
;
288 struct re_pattern_buffer
*bufp
;
290 if (running_asynch_code
)
293 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
294 XCHAR_TABLE (current_buffer
->case_canon_table
)->extras
[2]
295 = current_buffer
->case_eqv_table
;
297 CHECK_STRING (string
);
298 bufp
= compile_pattern (string
,
299 (NILP (Vinhibit_changing_match_data
)
300 ? &search_regs
: NULL
),
301 (!NILP (current_buffer
->case_fold_search
)
302 ? current_buffer
->case_canon_table
: Qnil
),
304 !NILP (current_buffer
->enable_multibyte_characters
));
307 QUIT
; /* Do a pending quit right away, to avoid paradoxical behavior */
309 /* Get pointers and sizes of the two strings
310 that make up the visible portion of the buffer. */
313 s1
= GPT_BYTE
- BEGV_BYTE
;
315 s2
= ZV_BYTE
- GPT_BYTE
;
319 s2
= ZV_BYTE
- BEGV_BYTE
;
324 s1
= ZV_BYTE
- BEGV_BYTE
;
328 re_match_object
= Qnil
;
330 i
= re_match_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
332 (NILP (Vinhibit_changing_match_data
)
333 ? &search_regs
: NULL
),
334 ZV_BYTE
- BEGV_BYTE
);
340 val
= (0 <= i
? Qt
: Qnil
);
341 if (NILP (Vinhibit_changing_match_data
) && i
>= 0)
342 for (i
= 0; i
< search_regs
.num_regs
; i
++)
343 if (search_regs
.start
[i
] >= 0)
346 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
348 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
351 /* Set last_thing_searched only when match data is changed. */
352 if (NILP (Vinhibit_changing_match_data
))
353 XSETBUFFER (last_thing_searched
, current_buffer
);
358 DEFUN ("looking-at", Flooking_at
, Slooking_at
, 1, 1, 0,
359 doc
: /* Return t if text after point matches regular expression REGEXP.
360 This function modifies the match data that `match-beginning',
361 `match-end' and `match-data' access; save and restore the match
362 data if you want to preserve them. */)
366 return looking_at_1 (regexp
, 0);
369 DEFUN ("posix-looking-at", Fposix_looking_at
, Sposix_looking_at
, 1, 1, 0,
370 doc
: /* Return t if text after point matches regular expression REGEXP.
371 Find the longest match, in accord with Posix regular expression rules.
372 This function modifies the match data that `match-beginning',
373 `match-end' and `match-data' access; save and restore the match
374 data if you want to preserve them. */)
378 return looking_at_1 (regexp
, 1);
382 string_match_1 (regexp
, string
, start
, posix
)
383 Lisp_Object regexp
, string
, start
;
387 struct re_pattern_buffer
*bufp
;
391 if (running_asynch_code
)
394 CHECK_STRING (regexp
);
395 CHECK_STRING (string
);
398 pos
= 0, pos_byte
= 0;
401 int len
= SCHARS (string
);
403 CHECK_NUMBER (start
);
405 if (pos
< 0 && -pos
<= len
)
407 else if (0 > pos
|| pos
> len
)
408 args_out_of_range (string
, start
);
409 pos_byte
= string_char_to_byte (string
, pos
);
412 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
413 XCHAR_TABLE (current_buffer
->case_canon_table
)->extras
[2]
414 = current_buffer
->case_eqv_table
;
416 bufp
= compile_pattern (regexp
,
417 (NILP (Vinhibit_changing_match_data
)
418 ? &search_regs
: NULL
),
419 (!NILP (current_buffer
->case_fold_search
)
420 ? current_buffer
->case_canon_table
: Qnil
),
422 STRING_MULTIBYTE (string
));
424 re_match_object
= string
;
426 val
= re_search (bufp
, (char *) SDATA (string
),
427 SBYTES (string
), pos_byte
,
428 SBYTES (string
) - pos_byte
,
429 (NILP (Vinhibit_changing_match_data
)
430 ? &search_regs
: NULL
));
433 /* Set last_thing_searched only when match data is changed. */
434 if (NILP (Vinhibit_changing_match_data
))
435 last_thing_searched
= Qt
;
439 if (val
< 0) return Qnil
;
441 if (NILP (Vinhibit_changing_match_data
))
442 for (i
= 0; i
< search_regs
.num_regs
; i
++)
443 if (search_regs
.start
[i
] >= 0)
446 = string_byte_to_char (string
, search_regs
.start
[i
]);
448 = string_byte_to_char (string
, search_regs
.end
[i
]);
451 return make_number (string_byte_to_char (string
, val
));
454 DEFUN ("string-match", Fstring_match
, Sstring_match
, 2, 3, 0,
455 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
456 Matching ignores case if `case-fold-search' is non-nil.
457 If third arg START is non-nil, start search at that index in STRING.
458 For index of first char beyond the match, do (match-end 0).
459 `match-end' and `match-beginning' also give indices of substrings
460 matched by parenthesis constructs in the pattern.
462 You can use the function `match-string' to extract the substrings
463 matched by the parenthesis constructions in REGEXP. */)
464 (regexp
, string
, start
)
465 Lisp_Object regexp
, string
, start
;
467 return string_match_1 (regexp
, string
, start
, 0);
470 DEFUN ("posix-string-match", Fposix_string_match
, Sposix_string_match
, 2, 3, 0,
471 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
472 Find the longest match, in accord with Posix regular expression rules.
473 Case is ignored if `case-fold-search' is non-nil in the current buffer.
474 If third arg START is non-nil, start search at that index in STRING.
475 For index of first char beyond the match, do (match-end 0).
476 `match-end' and `match-beginning' also give indices of substrings
477 matched by parenthesis constructs in the pattern. */)
478 (regexp
, string
, start
)
479 Lisp_Object regexp
, string
, start
;
481 return string_match_1 (regexp
, string
, start
, 1);
484 /* Match REGEXP against STRING, searching all of STRING,
485 and return the index of the match, or negative on failure.
486 This does not clobber the match data. */
489 fast_string_match (regexp
, string
)
490 Lisp_Object regexp
, string
;
493 struct re_pattern_buffer
*bufp
;
495 bufp
= compile_pattern (regexp
, 0, Qnil
,
496 0, STRING_MULTIBYTE (string
));
498 re_match_object
= string
;
500 val
= re_search (bufp
, (char *) SDATA (string
),
507 /* Match REGEXP against STRING, searching all of STRING ignoring case,
508 and return the index of the match, or negative on failure.
509 This does not clobber the match data.
510 We assume that STRING contains single-byte characters. */
512 extern Lisp_Object Vascii_downcase_table
;
515 fast_c_string_match_ignore_case (regexp
, string
)
520 struct re_pattern_buffer
*bufp
;
521 int len
= strlen (string
);
523 regexp
= string_make_unibyte (regexp
);
524 re_match_object
= Qt
;
525 bufp
= compile_pattern (regexp
, 0,
526 Vascii_canon_table
, 0,
529 val
= re_search (bufp
, string
, len
, 0, len
, 0);
534 /* Like fast_string_match but ignore case. */
537 fast_string_match_ignore_case (regexp
, string
)
538 Lisp_Object regexp
, string
;
541 struct re_pattern_buffer
*bufp
;
543 bufp
= compile_pattern (regexp
, 0, Vascii_canon_table
,
544 0, STRING_MULTIBYTE (string
));
546 re_match_object
= string
;
548 val
= re_search (bufp
, (char *) SDATA (string
),
555 /* The newline cache: remembering which sections of text have no newlines. */
557 /* If the user has requested newline caching, make sure it's on.
558 Otherwise, make sure it's off.
559 This is our cheezy way of associating an action with the change of
560 state of a buffer-local variable. */
562 newline_cache_on_off (buf
)
565 if (NILP (buf
->cache_long_line_scans
))
567 /* It should be off. */
568 if (buf
->newline_cache
)
570 free_region_cache (buf
->newline_cache
);
571 buf
->newline_cache
= 0;
576 /* It should be on. */
577 if (buf
->newline_cache
== 0)
578 buf
->newline_cache
= new_region_cache ();
583 /* Search for COUNT instances of the character TARGET between START and END.
585 If COUNT is positive, search forwards; END must be >= START.
586 If COUNT is negative, search backwards for the -COUNTth instance;
587 END must be <= START.
588 If COUNT is zero, do anything you please; run rogue, for all I care.
590 If END is zero, use BEGV or ZV instead, as appropriate for the
591 direction indicated by COUNT.
593 If we find COUNT instances, set *SHORTAGE to zero, and return the
594 position past the COUNTth match. Note that for reverse motion
595 this is not the same as the usual convention for Emacs motion commands.
597 If we don't find COUNT instances before reaching END, set *SHORTAGE
598 to the number of TARGETs left unfound, and return END.
600 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
601 except when inside redisplay. */
604 scan_buffer (target
, start
, end
, count
, shortage
, allow_quit
)
611 struct region_cache
*newline_cache
;
622 if (! end
) end
= BEGV
;
625 newline_cache_on_off (current_buffer
);
626 newline_cache
= current_buffer
->newline_cache
;
631 immediate_quit
= allow_quit
;
636 /* Our innermost scanning loop is very simple; it doesn't know
637 about gaps, buffer ends, or the newline cache. ceiling is
638 the position of the last character before the next such
639 obstacle --- the last character the dumb search loop should
641 int ceiling_byte
= CHAR_TO_BYTE (end
) - 1;
642 int start_byte
= CHAR_TO_BYTE (start
);
645 /* If we're looking for a newline, consult the newline cache
646 to see where we can avoid some scanning. */
647 if (target
== '\n' && newline_cache
)
651 while (region_cache_forward
652 (current_buffer
, newline_cache
, start_byte
, &next_change
))
653 start_byte
= next_change
;
654 immediate_quit
= allow_quit
;
656 /* START should never be after END. */
657 if (start_byte
> ceiling_byte
)
658 start_byte
= ceiling_byte
;
660 /* Now the text after start is an unknown region, and
661 next_change is the position of the next known region. */
662 ceiling_byte
= min (next_change
- 1, ceiling_byte
);
665 /* The dumb loop can only scan text stored in contiguous
666 bytes. BUFFER_CEILING_OF returns the last character
667 position that is contiguous, so the ceiling is the
668 position after that. */
669 tem
= BUFFER_CEILING_OF (start_byte
);
670 ceiling_byte
= min (tem
, ceiling_byte
);
673 /* The termination address of the dumb loop. */
674 register unsigned char *ceiling_addr
675 = BYTE_POS_ADDR (ceiling_byte
) + 1;
676 register unsigned char *cursor
677 = BYTE_POS_ADDR (start_byte
);
678 unsigned char *base
= cursor
;
680 while (cursor
< ceiling_addr
)
682 unsigned char *scan_start
= cursor
;
685 while (*cursor
!= target
&& ++cursor
< ceiling_addr
)
688 /* If we're looking for newlines, cache the fact that
689 the region from start to cursor is free of them. */
690 if (target
== '\n' && newline_cache
)
691 know_region_cache (current_buffer
, newline_cache
,
692 start_byte
+ scan_start
- base
,
693 start_byte
+ cursor
- base
);
695 /* Did we find the target character? */
696 if (cursor
< ceiling_addr
)
701 return BYTE_TO_CHAR (start_byte
+ cursor
- base
+ 1);
707 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
713 /* The last character to check before the next obstacle. */
714 int ceiling_byte
= CHAR_TO_BYTE (end
);
715 int start_byte
= CHAR_TO_BYTE (start
);
718 /* Consult the newline cache, if appropriate. */
719 if (target
== '\n' && newline_cache
)
723 while (region_cache_backward
724 (current_buffer
, newline_cache
, start_byte
, &next_change
))
725 start_byte
= next_change
;
726 immediate_quit
= allow_quit
;
728 /* Start should never be at or before end. */
729 if (start_byte
<= ceiling_byte
)
730 start_byte
= ceiling_byte
+ 1;
732 /* Now the text before start is an unknown region, and
733 next_change is the position of the next known region. */
734 ceiling_byte
= max (next_change
, ceiling_byte
);
737 /* Stop scanning before the gap. */
738 tem
= BUFFER_FLOOR_OF (start_byte
- 1);
739 ceiling_byte
= max (tem
, ceiling_byte
);
742 /* The termination address of the dumb loop. */
743 register unsigned char *ceiling_addr
= BYTE_POS_ADDR (ceiling_byte
);
744 register unsigned char *cursor
= BYTE_POS_ADDR (start_byte
- 1);
745 unsigned char *base
= cursor
;
747 while (cursor
>= ceiling_addr
)
749 unsigned char *scan_start
= cursor
;
751 while (*cursor
!= target
&& --cursor
>= ceiling_addr
)
754 /* If we're looking for newlines, cache the fact that
755 the region from after the cursor to start is free of them. */
756 if (target
== '\n' && newline_cache
)
757 know_region_cache (current_buffer
, newline_cache
,
758 start_byte
+ cursor
- base
,
759 start_byte
+ scan_start
- base
);
761 /* Did we find the target character? */
762 if (cursor
>= ceiling_addr
)
767 return BYTE_TO_CHAR (start_byte
+ cursor
- base
);
773 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
779 *shortage
= count
* direction
;
783 /* Search for COUNT instances of a line boundary, which means either a
784 newline or (if selective display enabled) a carriage return.
785 Start at START. If COUNT is negative, search backwards.
787 We report the resulting position by calling TEMP_SET_PT_BOTH.
789 If we find COUNT instances. we position after (always after,
790 even if scanning backwards) the COUNTth match, and return 0.
792 If we don't find COUNT instances before reaching the end of the
793 buffer (or the beginning, if scanning backwards), we return
794 the number of line boundaries left unfound, and position at
795 the limit we bumped up against.
797 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
798 except in special cases. */
801 scan_newline (start
, start_byte
, limit
, limit_byte
, count
, allow_quit
)
802 int start
, start_byte
;
803 int limit
, limit_byte
;
807 int direction
= ((count
> 0) ? 1 : -1);
809 register unsigned char *cursor
;
812 register int ceiling
;
813 register unsigned char *ceiling_addr
;
815 int old_immediate_quit
= immediate_quit
;
817 /* The code that follows is like scan_buffer
818 but checks for either newline or carriage return. */
823 start_byte
= CHAR_TO_BYTE (start
);
827 while (start_byte
< limit_byte
)
829 ceiling
= BUFFER_CEILING_OF (start_byte
);
830 ceiling
= min (limit_byte
- 1, ceiling
);
831 ceiling_addr
= BYTE_POS_ADDR (ceiling
) + 1;
832 base
= (cursor
= BYTE_POS_ADDR (start_byte
));
835 while (*cursor
!= '\n' && ++cursor
!= ceiling_addr
)
838 if (cursor
!= ceiling_addr
)
842 immediate_quit
= old_immediate_quit
;
843 start_byte
= start_byte
+ cursor
- base
+ 1;
844 start
= BYTE_TO_CHAR (start_byte
);
845 TEMP_SET_PT_BOTH (start
, start_byte
);
849 if (++cursor
== ceiling_addr
)
855 start_byte
+= cursor
- base
;
860 while (start_byte
> limit_byte
)
862 ceiling
= BUFFER_FLOOR_OF (start_byte
- 1);
863 ceiling
= max (limit_byte
, ceiling
);
864 ceiling_addr
= BYTE_POS_ADDR (ceiling
) - 1;
865 base
= (cursor
= BYTE_POS_ADDR (start_byte
- 1) + 1);
868 while (--cursor
!= ceiling_addr
&& *cursor
!= '\n')
871 if (cursor
!= ceiling_addr
)
875 immediate_quit
= old_immediate_quit
;
876 /* Return the position AFTER the match we found. */
877 start_byte
= start_byte
+ cursor
- base
+ 1;
878 start
= BYTE_TO_CHAR (start_byte
);
879 TEMP_SET_PT_BOTH (start
, start_byte
);
886 /* Here we add 1 to compensate for the last decrement
887 of CURSOR, which took it past the valid range. */
888 start_byte
+= cursor
- base
+ 1;
892 TEMP_SET_PT_BOTH (limit
, limit_byte
);
893 immediate_quit
= old_immediate_quit
;
895 return count
* direction
;
899 find_next_newline_no_quit (from
, cnt
)
900 register int from
, cnt
;
902 return scan_buffer ('\n', from
, 0, cnt
, (int *) 0, 0);
905 /* Like find_next_newline, but returns position before the newline,
906 not after, and only search up to TO. This isn't just
907 find_next_newline (...)-1, because you might hit TO. */
910 find_before_next_newline (from
, to
, cnt
)
914 int pos
= scan_buffer ('\n', from
, to
, cnt
, &shortage
, 1);
922 /* Subroutines of Lisp buffer search functions. */
925 search_command (string
, bound
, noerror
, count
, direction
, RE
, posix
)
926 Lisp_Object string
, bound
, noerror
, count
;
937 CHECK_NUMBER (count
);
941 CHECK_STRING (string
);
945 lim
= ZV
, lim_byte
= ZV_BYTE
;
947 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
951 CHECK_NUMBER_COERCE_MARKER (bound
);
953 if (n
> 0 ? lim
< PT
: lim
> PT
)
954 error ("Invalid search bound (wrong side of point)");
956 lim
= ZV
, lim_byte
= ZV_BYTE
;
958 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
960 lim_byte
= CHAR_TO_BYTE (lim
);
963 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
964 XCHAR_TABLE (current_buffer
->case_canon_table
)->extras
[2]
965 = current_buffer
->case_eqv_table
;
967 np
= search_buffer (string
, PT
, PT_BYTE
, lim
, lim_byte
, n
, RE
,
968 (!NILP (current_buffer
->case_fold_search
)
969 ? current_buffer
->case_canon_table
971 (!NILP (current_buffer
->case_fold_search
)
972 ? current_buffer
->case_eqv_table
978 xsignal1 (Qsearch_failed
, string
);
980 if (!EQ (noerror
, Qt
))
982 if (lim
< BEGV
|| lim
> ZV
)
984 SET_PT_BOTH (lim
, lim_byte
);
986 #if 0 /* This would be clean, but maybe programs depend on
987 a value of nil here. */
995 if (np
< BEGV
|| np
> ZV
)
1000 return make_number (np
);
1003 /* Return 1 if REGEXP it matches just one constant string. */
1006 trivial_regexp_p (regexp
)
1009 int len
= SBYTES (regexp
);
1010 unsigned char *s
= SDATA (regexp
);
1015 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
1022 case '|': case '(': case ')': case '`': case '\'': case 'b':
1023 case 'B': case '<': case '>': case 'w': case 'W': case 's':
1024 case 'S': case '=': case '{': case '}': case '_':
1025 case 'c': case 'C': /* for categoryspec and notcategoryspec */
1026 case '1': case '2': case '3': case '4': case '5':
1027 case '6': case '7': case '8': case '9':
1035 /* Search for the n'th occurrence of STRING in the current buffer,
1036 starting at position POS and stopping at position LIM,
1037 treating STRING as a literal string if RE is false or as
1038 a regular expression if RE is true.
1040 If N is positive, searching is forward and LIM must be greater than POS.
1041 If N is negative, searching is backward and LIM must be less than POS.
1043 Returns -x if x occurrences remain to be found (x > 0),
1044 or else the position at the beginning of the Nth occurrence
1045 (if searching backward) or the end (if searching forward).
1047 POSIX is nonzero if we want full backtracking (POSIX style)
1048 for this pattern. 0 means backtrack only enough to get a valid match. */
1050 #define TRANSLATE(out, trt, d) \
1056 temp = Faref (trt, make_number (d)); \
1057 if (INTEGERP (temp)) \
1058 out = XINT (temp); \
1067 /* Only used in search_buffer, to record the end position of the match
1068 when searching regexps and SEARCH_REGS should not be changed
1069 (i.e. Vinhibit_changing_match_data is non-nil). */
1070 static struct re_registers search_regs_1
;
1073 search_buffer (string
, pos
, pos_byte
, lim
, lim_byte
, n
,
1074 RE
, trt
, inverse_trt
, posix
)
1083 Lisp_Object inverse_trt
;
1086 int len
= SCHARS (string
);
1087 int len_byte
= SBYTES (string
);
1090 if (running_asynch_code
)
1091 save_search_regs ();
1093 /* Searching 0 times means don't move. */
1094 /* Null string is found at starting position. */
1095 if (len
== 0 || n
== 0)
1097 set_search_regs (pos_byte
, 0);
1101 if (RE
&& !(trivial_regexp_p (string
) && NILP (Vsearch_spaces_regexp
)))
1103 unsigned char *p1
, *p2
;
1105 struct re_pattern_buffer
*bufp
;
1107 bufp
= compile_pattern (string
,
1108 (NILP (Vinhibit_changing_match_data
)
1109 ? &search_regs
: &search_regs_1
),
1111 !NILP (current_buffer
->enable_multibyte_characters
));
1113 immediate_quit
= 1; /* Quit immediately if user types ^G,
1114 because letting this function finish
1115 can take too long. */
1116 QUIT
; /* Do a pending quit right away,
1117 to avoid paradoxical behavior */
1118 /* Get pointers and sizes of the two strings
1119 that make up the visible portion of the buffer. */
1122 s1
= GPT_BYTE
- BEGV_BYTE
;
1124 s2
= ZV_BYTE
- GPT_BYTE
;
1128 s2
= ZV_BYTE
- BEGV_BYTE
;
1133 s1
= ZV_BYTE
- BEGV_BYTE
;
1136 re_match_object
= Qnil
;
1141 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1142 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1143 (NILP (Vinhibit_changing_match_data
)
1144 ? &search_regs
: &search_regs_1
),
1145 /* Don't allow match past current point */
1146 pos_byte
- BEGV_BYTE
);
1149 matcher_overflow ();
1153 if (NILP (Vinhibit_changing_match_data
))
1155 pos_byte
= search_regs
.start
[0] + BEGV_BYTE
;
1156 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1157 if (search_regs
.start
[i
] >= 0)
1159 search_regs
.start
[i
]
1160 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1162 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1164 XSETBUFFER (last_thing_searched
, current_buffer
);
1165 /* Set pos to the new position. */
1166 pos
= search_regs
.start
[0];
1170 pos_byte
= search_regs_1
.start
[0] + BEGV_BYTE
;
1171 /* Set pos to the new position. */
1172 pos
= BYTE_TO_CHAR (search_regs_1
.start
[0] + BEGV_BYTE
);
1185 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1186 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1187 (NILP (Vinhibit_changing_match_data
)
1188 ? &search_regs
: &search_regs_1
),
1189 lim_byte
- BEGV_BYTE
);
1192 matcher_overflow ();
1196 if (NILP (Vinhibit_changing_match_data
))
1198 pos_byte
= search_regs
.end
[0] + BEGV_BYTE
;
1199 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1200 if (search_regs
.start
[i
] >= 0)
1202 search_regs
.start
[i
]
1203 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1205 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1207 XSETBUFFER (last_thing_searched
, current_buffer
);
1208 pos
= search_regs
.end
[0];
1212 pos_byte
= search_regs_1
.end
[0] + BEGV_BYTE
;
1213 pos
= BYTE_TO_CHAR (search_regs_1
.end
[0] + BEGV_BYTE
);
1226 else /* non-RE case */
1228 unsigned char *raw_pattern
, *pat
;
1229 int raw_pattern_size
;
1230 int raw_pattern_size_byte
;
1231 unsigned char *patbuf
;
1232 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1233 unsigned char *base_pat
;
1234 /* Set to positive if we find a non-ASCII char that need
1235 translation. Otherwise set to zero later. */
1237 int boyer_moore_ok
= 1;
1239 /* MULTIBYTE says whether the text to be searched is multibyte.
1240 We must convert PATTERN to match that, or we will not really
1241 find things right. */
1243 if (multibyte
== STRING_MULTIBYTE (string
))
1245 raw_pattern
= (unsigned char *) SDATA (string
);
1246 raw_pattern_size
= SCHARS (string
);
1247 raw_pattern_size_byte
= SBYTES (string
);
1251 raw_pattern_size
= SCHARS (string
);
1252 raw_pattern_size_byte
1253 = count_size_as_multibyte (SDATA (string
),
1255 raw_pattern
= (unsigned char *) alloca (raw_pattern_size_byte
+ 1);
1256 copy_text (SDATA (string
), raw_pattern
,
1257 SCHARS (string
), 0, 1);
1261 /* Converting multibyte to single-byte.
1263 ??? Perhaps this conversion should be done in a special way
1264 by subtracting nonascii-insert-offset from each non-ASCII char,
1265 so that only the multibyte chars which really correspond to
1266 the chosen single-byte character set can possibly match. */
1267 raw_pattern_size
= SCHARS (string
);
1268 raw_pattern_size_byte
= SCHARS (string
);
1269 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
1270 copy_text (SDATA (string
), raw_pattern
,
1271 SBYTES (string
), 1, 0);
1274 /* Copy and optionally translate the pattern. */
1275 len
= raw_pattern_size
;
1276 len_byte
= raw_pattern_size_byte
;
1277 patbuf
= (unsigned char *) alloca (len
* MAX_MULTIBYTE_LENGTH
);
1279 base_pat
= raw_pattern
;
1282 /* Fill patbuf by translated characters in STRING while
1283 checking if we can use boyer-moore search. If TRT is
1284 non-nil, we can use boyer-moore search only if TRT can be
1285 represented by the byte array of 256 elements. For that,
1286 all non-ASCII case-equivalents of all case-senstive
1287 characters in STRING must belong to the same charset and
1292 unsigned char str_base
[MAX_MULTIBYTE_LENGTH
], *str
;
1293 int c
, translated
, inverse
;
1294 int in_charlen
, charlen
;
1296 /* If we got here and the RE flag is set, it's because we're
1297 dealing with a regexp known to be trivial, so the backslash
1298 just quotes the next character. */
1299 if (RE
&& *base_pat
== '\\')
1307 c
= STRING_CHAR_AND_LENGTH (base_pat
, len_byte
, in_charlen
);
1312 charlen
= in_charlen
;
1316 /* Translate the character. */
1317 TRANSLATE (translated
, trt
, c
);
1318 charlen
= CHAR_STRING (translated
, str_base
);
1321 /* Check if C has any other case-equivalents. */
1322 TRANSLATE (inverse
, inverse_trt
, c
);
1323 /* If so, check if we can use boyer-moore. */
1324 if (c
!= inverse
&& boyer_moore_ok
)
1326 /* Check if all equivalents belong to the same
1327 group of characters. Note that the check of C
1328 itself is done by the last iteration. */
1329 int this_char_base
= -1;
1331 while (boyer_moore_ok
)
1333 if (ASCII_BYTE_P (inverse
))
1335 if (this_char_base
> 0)
1341 char_base
= this_char_base
;
1344 else if (CHAR_BYTE8_P (inverse
))
1345 /* Boyer-moore search can't handle a
1346 translation of an eight-bit
1349 else if (this_char_base
< 0)
1351 this_char_base
= inverse
& ~0x3F;
1353 char_base
= this_char_base
;
1354 else if (char_base
> 0
1355 && this_char_base
!= char_base
)
1358 else if ((inverse
& ~0x3F) != this_char_base
)
1362 TRANSLATE (inverse
, inverse_trt
, inverse
);
1369 /* Store this character into the translated pattern. */
1370 bcopy (str
, pat
, charlen
);
1372 base_pat
+= in_charlen
;
1373 len_byte
-= in_charlen
;
1378 /* Unibyte buffer. */
1384 /* If we got here and the RE flag is set, it's because we're
1385 dealing with a regexp known to be trivial, so the backslash
1386 just quotes the next character. */
1387 if (RE
&& *base_pat
== '\\')
1394 TRANSLATE (translated
, trt
, c
);
1395 *pat
++ = translated
;
1399 len_byte
= pat
- patbuf
;
1400 len
= raw_pattern_size
;
1401 pat
= base_pat
= patbuf
;
1404 return boyer_moore (n
, pat
, len
, len_byte
, trt
, inverse_trt
,
1405 pos
, pos_byte
, lim
, lim_byte
,
1408 return simple_search (n
, pat
, len
, len_byte
, trt
,
1409 pos
, pos_byte
, lim
, lim_byte
);
1413 /* Do a simple string search N times for the string PAT,
1414 whose length is LEN/LEN_BYTE,
1415 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1416 TRT is the translation table.
1418 Return the character position where the match is found.
1419 Otherwise, if M matches remained to be found, return -M.
1421 This kind of search works regardless of what is in PAT and
1422 regardless of what is in TRT. It is used in cases where
1423 boyer_moore cannot work. */
1426 simple_search (n
, pat
, len
, len_byte
, trt
, pos
, pos_byte
, lim
, lim_byte
)
1434 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1435 int forward
= n
> 0;
1436 /* Number of buffer bytes matched. Note that this may be different
1437 from len_byte in a multibyte buffer. */
1440 if (lim
> pos
&& multibyte
)
1445 /* Try matching at position POS. */
1447 int this_pos_byte
= pos_byte
;
1449 int this_len_byte
= len_byte
;
1450 unsigned char *p
= pat
;
1451 if (pos
+ len
> lim
|| pos_byte
+ len_byte
> lim_byte
)
1454 while (this_len
> 0)
1456 int charlen
, buf_charlen
;
1459 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1460 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1461 ZV_BYTE
- this_pos_byte
,
1463 TRANSLATE (buf_ch
, trt
, buf_ch
);
1465 if (buf_ch
!= pat_ch
)
1468 this_len_byte
-= charlen
;
1472 this_pos_byte
+= buf_charlen
;
1478 match_byte
= this_pos_byte
- pos_byte
;
1480 pos_byte
+= match_byte
;
1484 INC_BOTH (pos
, pos_byte
);
1494 /* Try matching at position POS. */
1497 unsigned char *p
= pat
;
1499 if (pos
+ len
> lim
)
1502 while (this_len
> 0)
1505 int buf_ch
= FETCH_BYTE (this_pos
);
1506 TRANSLATE (buf_ch
, trt
, buf_ch
);
1508 if (buf_ch
!= pat_ch
)
1527 /* Backwards search. */
1528 else if (lim
< pos
&& multibyte
)
1533 /* Try matching at position POS. */
1534 int this_pos
= pos
- len
;
1537 int this_len_byte
= len_byte
;
1538 unsigned char *p
= pat
;
1540 if (this_pos
< lim
|| (pos_byte
- len_byte
) < lim_byte
)
1542 this_pos_byte
= CHAR_TO_BYTE (this_pos
);
1543 match_byte
= pos_byte
- this_pos_byte
;
1545 while (this_len
> 0)
1547 int charlen
, buf_charlen
;
1550 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1551 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1552 ZV_BYTE
- this_pos_byte
,
1554 TRANSLATE (buf_ch
, trt
, buf_ch
);
1556 if (buf_ch
!= pat_ch
)
1559 this_len_byte
-= charlen
;
1562 this_pos_byte
+= buf_charlen
;
1569 pos_byte
-= match_byte
;
1573 DEC_BOTH (pos
, pos_byte
);
1583 /* Try matching at position POS. */
1584 int this_pos
= pos
- len
;
1586 unsigned char *p
= pat
;
1591 while (this_len
> 0)
1594 int buf_ch
= FETCH_BYTE (this_pos
);
1595 TRANSLATE (buf_ch
, trt
, buf_ch
);
1597 if (buf_ch
!= pat_ch
)
1620 set_search_regs ((multibyte
? pos_byte
: pos
) - match_byte
, match_byte
);
1622 set_search_regs (multibyte
? pos_byte
: pos
, match_byte
);
1632 /* Do Boyer-Moore search N times for the string BASE_PAT,
1633 whose length is LEN/LEN_BYTE,
1634 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1635 DIRECTION says which direction we search in.
1636 TRT and INVERSE_TRT are translation tables.
1637 Characters in PAT are already translated by TRT.
1639 This kind of search works if all the characters in BASE_PAT that
1640 have nontrivial translation are the same aside from the last byte.
1641 This makes it possible to translate just the last byte of a
1642 character, and do so after just a simple test of the context.
1643 CHAR_BASE is nonzero if there is such a non-ASCII character.
1645 If that criterion is not satisfied, do not call this function. */
1648 boyer_moore (n
, base_pat
, len
, len_byte
, trt
, inverse_trt
,
1649 pos
, pos_byte
, lim
, lim_byte
, char_base
)
1651 unsigned char *base_pat
;
1654 Lisp_Object inverse_trt
;
1659 int direction
= ((n
> 0) ? 1 : -1);
1660 register int dirlen
;
1661 int infinity
, limit
, stride_for_teases
= 0;
1662 register int *BM_tab
;
1664 register unsigned char *cursor
, *p_limit
;
1666 unsigned char *pat
, *pat_end
;
1667 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1669 unsigned char simple_translate
[0400];
1670 /* These are set to the preceding bytes of a byte to be translated
1671 if char_base is nonzero. As the maximum byte length of a
1672 multibyte character is 5, we have to check at most four previous
1674 int translate_prev_byte1
= 0;
1675 int translate_prev_byte2
= 0;
1676 int translate_prev_byte3
= 0;
1677 int translate_prev_byte4
= 0;
1679 BM_tab
= (int *) alloca (0400 * sizeof (int));
1681 /* The general approach is that we are going to maintain that we know */
1682 /* the first (closest to the present position, in whatever direction */
1683 /* we're searching) character that could possibly be the last */
1684 /* (furthest from present position) character of a valid match. We */
1685 /* advance the state of our knowledge by looking at that character */
1686 /* and seeing whether it indeed matches the last character of the */
1687 /* pattern. If it does, we take a closer look. If it does not, we */
1688 /* move our pointer (to putative last characters) as far as is */
1689 /* logically possible. This amount of movement, which I call a */
1690 /* stride, will be the length of the pattern if the actual character */
1691 /* appears nowhere in the pattern, otherwise it will be the distance */
1692 /* from the last occurrence of that character to the end of the */
1694 /* As a coding trick, an enormous stride is coded into the table for */
1695 /* characters that match the last character. This allows use of only */
1696 /* a single test, a test for having gone past the end of the */
1697 /* permissible match region, to test for both possible matches (when */
1698 /* the stride goes past the end immediately) and failure to */
1699 /* match (where you get nudged past the end one stride at a time). */
1701 /* Here we make a "mickey mouse" BM table. The stride of the search */
1702 /* is determined only by the last character of the putative match. */
1703 /* If that character does not match, we will stride the proper */
1704 /* distance to propose a match that superimposes it on the last */
1705 /* instance of a character that matches it (per trt), or misses */
1706 /* it entirely if there is none. */
1708 dirlen
= len_byte
* direction
;
1709 infinity
= dirlen
- (lim_byte
+ pos_byte
+ len_byte
+ len_byte
) * direction
;
1711 /* Record position after the end of the pattern. */
1712 pat_end
= base_pat
+ len_byte
;
1713 /* BASE_PAT points to a character that we start scanning from.
1714 It is the first character in a forward search,
1715 the last character in a backward search. */
1717 base_pat
= pat_end
- 1;
1719 BM_tab_base
= BM_tab
;
1721 j
= dirlen
; /* to get it in a register */
1722 /* A character that does not appear in the pattern induces a */
1723 /* stride equal to the pattern length. */
1724 while (BM_tab_base
!= BM_tab
)
1732 /* We use this for translation, instead of TRT itself.
1733 We fill this in to handle the characters that actually
1734 occur in the pattern. Others don't matter anyway! */
1735 bzero (simple_translate
, sizeof simple_translate
);
1736 for (i
= 0; i
< 0400; i
++)
1737 simple_translate
[i
] = i
;
1741 /* Setup translate_prev_byte1/2/3/4 from CHAR_BASE. Only a
1742 byte following them are the target of translation. */
1743 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1744 int len
= CHAR_STRING (char_base
, str
);
1746 translate_prev_byte1
= str
[len
- 2];
1749 translate_prev_byte2
= str
[len
- 3];
1752 translate_prev_byte3
= str
[len
- 4];
1754 translate_prev_byte4
= str
[len
- 5];
1760 while (i
!= infinity
)
1762 unsigned char *ptr
= base_pat
+ i
;
1768 /* If the byte currently looking at is the last of a
1769 character to check case-equivalents, set CH to that
1770 character. An ASCII character and a non-ASCII character
1771 matching with CHAR_BASE are to be checked. */
1774 if (ASCII_BYTE_P (*ptr
) || ! multibyte
)
1777 && ((pat_end
- ptr
) == 1 || CHAR_HEAD_P (ptr
[1])))
1779 unsigned char *charstart
= ptr
- 1;
1781 while (! (CHAR_HEAD_P (*charstart
)))
1783 ch
= STRING_CHAR (charstart
, ptr
- charstart
+ 1);
1784 if (char_base
!= (ch
& ~0x3F))
1789 j
= (ch
& 0x3F) | 0200;
1794 stride_for_teases
= BM_tab
[j
];
1796 BM_tab
[j
] = dirlen
- i
;
1797 /* A translation table is accompanied by its inverse -- see */
1798 /* comment following downcase_table for details */
1801 int starting_ch
= ch
;
1806 TRANSLATE (ch
, inverse_trt
, ch
);
1808 j
= (ch
& 0x3F) | 0200;
1812 /* For all the characters that map into CH,
1813 set up simple_translate to map the last byte
1815 simple_translate
[j
] = starting_j
;
1816 if (ch
== starting_ch
)
1818 BM_tab
[j
] = dirlen
- i
;
1827 stride_for_teases
= BM_tab
[j
];
1828 BM_tab
[j
] = dirlen
- i
;
1830 /* stride_for_teases tells how much to stride if we get a */
1831 /* match on the far character but are subsequently */
1832 /* disappointed, by recording what the stride would have been */
1833 /* for that character if the last character had been */
1836 infinity
= dirlen
- infinity
;
1837 pos_byte
+= dirlen
- ((direction
> 0) ? direction
: 0);
1838 /* loop invariant - POS_BYTE points at where last char (first
1839 char if reverse) of pattern would align in a possible match. */
1843 unsigned char *tail_end_ptr
;
1845 /* It's been reported that some (broken) compiler thinks that
1846 Boolean expressions in an arithmetic context are unsigned.
1847 Using an explicit ?1:0 prevents this. */
1848 if ((lim_byte
- pos_byte
- ((direction
> 0) ? 1 : 0)) * direction
1850 return (n
* (0 - direction
));
1851 /* First we do the part we can by pointers (maybe nothing) */
1854 limit
= pos_byte
- dirlen
+ direction
;
1857 limit
= BUFFER_CEILING_OF (limit
);
1858 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1859 can take on without hitting edge of buffer or the gap. */
1860 limit
= min (limit
, pos_byte
+ 20000);
1861 limit
= min (limit
, lim_byte
- 1);
1865 limit
= BUFFER_FLOOR_OF (limit
);
1866 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1867 can take on without hitting edge of buffer or the gap. */
1868 limit
= max (limit
, pos_byte
- 20000);
1869 limit
= max (limit
, lim_byte
);
1871 tail_end
= BUFFER_CEILING_OF (pos_byte
) + 1;
1872 tail_end_ptr
= BYTE_POS_ADDR (tail_end
);
1874 if ((limit
- pos_byte
) * direction
> 20)
1878 p_limit
= BYTE_POS_ADDR (limit
);
1879 p2
= (cursor
= BYTE_POS_ADDR (pos_byte
));
1880 /* In this loop, pos + cursor - p2 is the surrogate for pos */
1881 while (1) /* use one cursor setting as long as i can */
1883 if (direction
> 0) /* worth duplicating */
1885 /* Use signed comparison if appropriate
1886 to make cursor+infinity sure to be > p_limit.
1887 Assuming that the buffer lies in a range of addresses
1888 that are all "positive" (as ints) or all "negative",
1889 either kind of comparison will work as long
1890 as we don't step by infinity. So pick the kind
1891 that works when we do step by infinity. */
1892 if ((EMACS_INT
) (p_limit
+ infinity
) > (EMACS_INT
) p_limit
)
1893 while ((EMACS_INT
) cursor
<= (EMACS_INT
) p_limit
)
1894 cursor
+= BM_tab
[*cursor
];
1896 while ((EMACS_UINT
) cursor
<= (EMACS_UINT
) p_limit
)
1897 cursor
+= BM_tab
[*cursor
];
1901 if ((EMACS_INT
) (p_limit
+ infinity
) < (EMACS_INT
) p_limit
)
1902 while ((EMACS_INT
) cursor
>= (EMACS_INT
) p_limit
)
1903 cursor
+= BM_tab
[*cursor
];
1905 while ((EMACS_UINT
) cursor
>= (EMACS_UINT
) p_limit
)
1906 cursor
+= BM_tab
[*cursor
];
1908 /* If you are here, cursor is beyond the end of the searched region. */
1909 /* This can happen if you match on the far character of the pattern, */
1910 /* because the "stride" of that character is infinity, a number able */
1911 /* to throw you well beyond the end of the search. It can also */
1912 /* happen if you fail to match within the permitted region and would */
1913 /* otherwise try a character beyond that region */
1914 if ((cursor
- p_limit
) * direction
<= len_byte
)
1915 break; /* a small overrun is genuine */
1916 cursor
-= infinity
; /* large overrun = hit */
1917 i
= dirlen
- direction
;
1920 while ((i
-= direction
) + direction
!= 0)
1923 cursor
-= direction
;
1924 /* Translate only the last byte of a character. */
1926 || ((cursor
== tail_end_ptr
1927 || CHAR_HEAD_P (cursor
[1]))
1928 && (CHAR_HEAD_P (cursor
[0])
1929 /* Check if this is the last byte of
1930 a translable character. */
1931 || (translate_prev_byte1
== cursor
[-1]
1932 && (CHAR_HEAD_P (translate_prev_byte1
)
1933 || (translate_prev_byte2
== cursor
[-2]
1934 && (CHAR_HEAD_P (translate_prev_byte2
)
1935 || (translate_prev_byte3
== cursor
[-3]))))))))
1936 ch
= simple_translate
[*cursor
];
1945 while ((i
-= direction
) + direction
!= 0)
1947 cursor
-= direction
;
1948 if (pat
[i
] != *cursor
)
1952 cursor
+= dirlen
- i
- direction
; /* fix cursor */
1953 if (i
+ direction
== 0)
1955 int position
, start
, end
;
1957 cursor
-= direction
;
1959 position
= pos_byte
+ cursor
- p2
+ ((direction
> 0)
1960 ? 1 - len_byte
: 0);
1961 set_search_regs (position
, len_byte
);
1963 if (NILP (Vinhibit_changing_match_data
))
1965 start
= search_regs
.start
[0];
1966 end
= search_regs
.end
[0];
1969 /* If Vinhibit_changing_match_data is non-nil,
1970 search_regs will not be changed. So let's
1971 compute start and end here. */
1973 start
= BYTE_TO_CHAR (position
);
1974 end
= BYTE_TO_CHAR (position
+ len_byte
);
1977 if ((n
-= direction
) != 0)
1978 cursor
+= dirlen
; /* to resume search */
1980 return direction
> 0 ? end
: start
;
1983 cursor
+= stride_for_teases
; /* <sigh> we lose - */
1985 pos_byte
+= cursor
- p2
;
1988 /* Now we'll pick up a clump that has to be done the hard */
1989 /* way because it covers a discontinuity */
1991 limit
= ((direction
> 0)
1992 ? BUFFER_CEILING_OF (pos_byte
- dirlen
+ 1)
1993 : BUFFER_FLOOR_OF (pos_byte
- dirlen
- 1));
1994 limit
= ((direction
> 0)
1995 ? min (limit
+ len_byte
, lim_byte
- 1)
1996 : max (limit
- len_byte
, lim_byte
));
1997 /* LIMIT is now the last value POS_BYTE can have
1998 and still be valid for a possible match. */
2001 /* This loop can be coded for space rather than */
2002 /* speed because it will usually run only once. */
2003 /* (the reach is at most len + 21, and typically */
2004 /* does not exceed len) */
2005 while ((limit
- pos_byte
) * direction
>= 0)
2006 pos_byte
+= BM_tab
[FETCH_BYTE (pos_byte
)];
2007 /* now run the same tests to distinguish going off the */
2008 /* end, a match or a phony match. */
2009 if ((pos_byte
- limit
) * direction
<= len_byte
)
2010 break; /* ran off the end */
2011 /* Found what might be a match.
2012 Set POS_BYTE back to last (first if reverse) pos. */
2013 pos_byte
-= infinity
;
2014 i
= dirlen
- direction
;
2015 while ((i
-= direction
) + direction
!= 0)
2019 pos_byte
-= direction
;
2020 ptr
= BYTE_POS_ADDR (pos_byte
);
2021 /* Translate only the last byte of a character. */
2023 || ((ptr
== tail_end_ptr
2024 || CHAR_HEAD_P (ptr
[1]))
2025 && (CHAR_HEAD_P (ptr
[0])
2026 /* Check if this is the last byte of a
2027 translable character. */
2028 || (translate_prev_byte1
== ptr
[-1]
2029 && (CHAR_HEAD_P (translate_prev_byte1
)
2030 || (translate_prev_byte2
== ptr
[-2]
2031 && (CHAR_HEAD_P (translate_prev_byte2
)
2032 || translate_prev_byte3
== ptr
[-3])))))))
2033 ch
= simple_translate
[*ptr
];
2039 /* Above loop has moved POS_BYTE part or all the way
2040 back to the first pos (last pos if reverse).
2041 Set it once again at the last (first if reverse) char. */
2042 pos_byte
+= dirlen
- i
- direction
;
2043 if (i
+ direction
== 0)
2045 int position
, start
, end
;
2046 pos_byte
-= direction
;
2048 position
= pos_byte
+ ((direction
> 0) ? 1 - len_byte
: 0);
2049 set_search_regs (position
, len_byte
);
2051 if (NILP (Vinhibit_changing_match_data
))
2053 start
= search_regs
.start
[0];
2054 end
= search_regs
.end
[0];
2057 /* If Vinhibit_changing_match_data is non-nil,
2058 search_regs will not be changed. So let's
2059 compute start and end here. */
2061 start
= BYTE_TO_CHAR (position
);
2062 end
= BYTE_TO_CHAR (position
+ len_byte
);
2065 if ((n
-= direction
) != 0)
2066 pos_byte
+= dirlen
; /* to resume search */
2068 return direction
> 0 ? end
: start
;
2071 pos_byte
+= stride_for_teases
;
2074 /* We have done one clump. Can we continue? */
2075 if ((lim_byte
- pos_byte
) * direction
< 0)
2076 return ((0 - n
) * direction
);
2078 return BYTE_TO_CHAR (pos_byte
);
2081 /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES
2082 for the overall match just found in the current buffer.
2083 Also clear out the match data for registers 1 and up. */
2086 set_search_regs (beg_byte
, nbytes
)
2087 int beg_byte
, nbytes
;
2091 if (!NILP (Vinhibit_changing_match_data
))
2094 /* Make sure we have registers in which to store
2095 the match position. */
2096 if (search_regs
.num_regs
== 0)
2098 search_regs
.start
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
2099 search_regs
.end
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
2100 search_regs
.num_regs
= 2;
2103 /* Clear out the other registers. */
2104 for (i
= 1; i
< search_regs
.num_regs
; i
++)
2106 search_regs
.start
[i
] = -1;
2107 search_regs
.end
[i
] = -1;
2110 search_regs
.start
[0] = BYTE_TO_CHAR (beg_byte
);
2111 search_regs
.end
[0] = BYTE_TO_CHAR (beg_byte
+ nbytes
);
2112 XSETBUFFER (last_thing_searched
, current_buffer
);
2115 /* Given a string of words separated by word delimiters,
2116 compute a regexp that matches those exact words
2117 separated by arbitrary punctuation. */
2123 register unsigned char *p
, *o
;
2124 register int i
, i_byte
, len
, punct_count
= 0, word_count
= 0;
2129 CHECK_STRING (string
);
2131 len
= SCHARS (string
);
2133 for (i
= 0, i_byte
= 0; i
< len
; )
2137 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, i
, i_byte
);
2139 if (SYNTAX (c
) != Sword
)
2142 if (i
> 0 && SYNTAX (prev_c
) == Sword
)
2149 if (SYNTAX (prev_c
) == Sword
)
2152 return empty_unibyte_string
;
2154 adjust
= - punct_count
+ 5 * (word_count
- 1) + 4;
2155 if (STRING_MULTIBYTE (string
))
2156 val
= make_uninit_multibyte_string (len
+ adjust
,
2160 val
= make_uninit_string (len
+ adjust
);
2167 for (i
= 0, i_byte
= 0; i
< len
; )
2170 int i_byte_orig
= i_byte
;
2172 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, i
, i_byte
);
2174 if (SYNTAX (c
) == Sword
)
2176 bcopy (SDATA (string
) + i_byte_orig
, o
,
2177 i_byte
- i_byte_orig
);
2178 o
+= i_byte
- i_byte_orig
;
2180 else if (i
> 0 && SYNTAX (prev_c
) == Sword
&& --word_count
)
2198 DEFUN ("search-backward", Fsearch_backward
, Ssearch_backward
, 1, 4,
2199 "MSearch backward: ",
2200 doc
: /* Search backward from point for STRING.
2201 Set point to the beginning of the occurrence found, and return point.
2202 An optional second argument bounds the search; it is a buffer position.
2203 The match found must not extend before that position.
2204 Optional third argument, if t, means if fail just return nil (no error).
2205 If not nil and not t, position at limit of search and return nil.
2206 Optional fourth argument is repeat count--search for successive occurrences.
2208 Search case-sensitivity is determined by the value of the variable
2209 `case-fold-search', which see.
2211 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2212 (string
, bound
, noerror
, count
)
2213 Lisp_Object string
, bound
, noerror
, count
;
2215 return search_command (string
, bound
, noerror
, count
, -1, 0, 0);
2218 DEFUN ("search-forward", Fsearch_forward
, Ssearch_forward
, 1, 4, "MSearch: ",
2219 doc
: /* Search forward from point for STRING.
2220 Set point to the end of the occurrence found, and return point.
2221 An optional second argument bounds the search; it is a buffer position.
2222 The match found must not extend after that position. A value of nil is
2223 equivalent to (point-max).
2224 Optional third argument, if t, means if fail just return nil (no error).
2225 If not nil and not t, move to limit of search and return nil.
2226 Optional fourth argument is repeat count--search for successive occurrences.
2228 Search case-sensitivity is determined by the value of the variable
2229 `case-fold-search', which see.
2231 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2232 (string
, bound
, noerror
, count
)
2233 Lisp_Object string
, bound
, noerror
, count
;
2235 return search_command (string
, bound
, noerror
, count
, 1, 0, 0);
2238 DEFUN ("word-search-backward", Fword_search_backward
, Sword_search_backward
, 1, 4,
2239 "sWord search backward: ",
2240 doc
: /* Search backward from point for STRING, ignoring differences in punctuation.
2241 Set point to the beginning of the occurrence found, and return point.
2242 An optional second argument bounds the search; it is a buffer position.
2243 The match found must not extend before that position.
2244 Optional third argument, if t, means if fail just return nil (no error).
2245 If not nil and not t, move to limit of search and return nil.
2246 Optional fourth argument is repeat count--search for successive occurrences. */)
2247 (string
, bound
, noerror
, count
)
2248 Lisp_Object string
, bound
, noerror
, count
;
2250 return search_command (wordify (string
), bound
, noerror
, count
, -1, 1, 0);
2253 DEFUN ("word-search-forward", Fword_search_forward
, Sword_search_forward
, 1, 4,
2255 doc
: /* Search forward from point for STRING, ignoring differences in punctuation.
2256 Set point to the end of the occurrence found, and return point.
2257 An optional second argument bounds the search; it is a buffer position.
2258 The match found must not extend after that position.
2259 Optional third argument, if t, means if fail just return nil (no error).
2260 If not nil and not t, move to limit of search and return nil.
2261 Optional fourth argument is repeat count--search for successive occurrences. */)
2262 (string
, bound
, noerror
, count
)
2263 Lisp_Object string
, bound
, noerror
, count
;
2265 return search_command (wordify (string
), bound
, noerror
, count
, 1, 1, 0);
2268 DEFUN ("re-search-backward", Fre_search_backward
, Sre_search_backward
, 1, 4,
2269 "sRE search backward: ",
2270 doc
: /* Search backward from point for match for regular expression REGEXP.
2271 Set point to the beginning of the match, and return point.
2272 The match found is the one starting last in the buffer
2273 and yet ending before the origin of the search.
2274 An optional second argument bounds the search; it is a buffer position.
2275 The match found must start at or after that position.
2276 Optional third argument, if t, means if fail just return nil (no error).
2277 If not nil and not t, move to limit of search and return nil.
2278 Optional fourth argument is repeat count--search for successive occurrences.
2279 See also the functions `match-beginning', `match-end', `match-string',
2280 and `replace-match'. */)
2281 (regexp
, bound
, noerror
, count
)
2282 Lisp_Object regexp
, bound
, noerror
, count
;
2284 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 0);
2287 DEFUN ("re-search-forward", Fre_search_forward
, Sre_search_forward
, 1, 4,
2289 doc
: /* Search forward from point for regular expression REGEXP.
2290 Set point to the end of the occurrence found, and return point.
2291 An optional second argument bounds the search; it is a buffer position.
2292 The match found must not extend after that position.
2293 Optional third argument, if t, means if fail just return nil (no error).
2294 If not nil and not t, move to limit of search and return nil.
2295 Optional fourth argument is repeat count--search for successive occurrences.
2296 See also the functions `match-beginning', `match-end', `match-string',
2297 and `replace-match'. */)
2298 (regexp
, bound
, noerror
, count
)
2299 Lisp_Object regexp
, bound
, noerror
, count
;
2301 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 0);
2304 DEFUN ("posix-search-backward", Fposix_search_backward
, Sposix_search_backward
, 1, 4,
2305 "sPosix search backward: ",
2306 doc
: /* Search backward from point for match for regular expression REGEXP.
2307 Find the longest match in accord with Posix regular expression rules.
2308 Set point to the beginning of the match, and return point.
2309 The match found is the one starting last in the buffer
2310 and yet ending before the origin of the search.
2311 An optional second argument bounds the search; it is a buffer position.
2312 The match found must start at or after that position.
2313 Optional third argument, if t, means if fail just return nil (no error).
2314 If not nil and not t, move to limit of search and return nil.
2315 Optional fourth argument is repeat count--search for successive occurrences.
2316 See also the functions `match-beginning', `match-end', `match-string',
2317 and `replace-match'. */)
2318 (regexp
, bound
, noerror
, count
)
2319 Lisp_Object regexp
, bound
, noerror
, count
;
2321 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 1);
2324 DEFUN ("posix-search-forward", Fposix_search_forward
, Sposix_search_forward
, 1, 4,
2326 doc
: /* Search forward from point for regular expression REGEXP.
2327 Find the longest match in accord with Posix regular expression rules.
2328 Set point to the end of the occurrence found, and return point.
2329 An optional second argument bounds the search; it is a buffer position.
2330 The match found must not extend after that position.
2331 Optional third argument, if t, means if fail just return nil (no error).
2332 If not nil and not t, move to limit of search and return nil.
2333 Optional fourth argument is repeat count--search for successive occurrences.
2334 See also the functions `match-beginning', `match-end', `match-string',
2335 and `replace-match'. */)
2336 (regexp
, bound
, noerror
, count
)
2337 Lisp_Object regexp
, bound
, noerror
, count
;
2339 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 1);
2342 DEFUN ("replace-match", Freplace_match
, Sreplace_match
, 1, 5, 0,
2343 doc
: /* Replace text matched by last search with NEWTEXT.
2344 Leave point at the end of the replacement text.
2346 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
2347 Otherwise maybe capitalize the whole text, or maybe just word initials,
2348 based on the replaced text.
2349 If the replaced text has only capital letters
2350 and has at least one multiletter word, convert NEWTEXT to all caps.
2351 Otherwise if all words are capitalized in the replaced text,
2352 capitalize each word in NEWTEXT.
2354 If third arg LITERAL is non-nil, insert NEWTEXT literally.
2355 Otherwise treat `\\' as special:
2356 `\\&' in NEWTEXT means substitute original matched text.
2357 `\\N' means substitute what matched the Nth `\\(...\\)'.
2358 If Nth parens didn't match, substitute nothing.
2359 `\\\\' means insert one `\\'.
2360 Case conversion does not apply to these substitutions.
2362 FIXEDCASE and LITERAL are optional arguments.
2364 The optional fourth argument STRING can be a string to modify.
2365 This is meaningful when the previous match was done against STRING,
2366 using `string-match'. When used this way, `replace-match'
2367 creates and returns a new string made by copying STRING and replacing
2368 the part of STRING that was matched.
2370 The optional fifth argument SUBEXP specifies a subexpression;
2371 it says to replace just that subexpression with NEWTEXT,
2372 rather than replacing the entire matched text.
2373 This is, in a vague sense, the inverse of using `\\N' in NEWTEXT;
2374 `\\N' copies subexp N into NEWTEXT, but using N as SUBEXP puts
2375 NEWTEXT in place of subexp N.
2376 This is useful only after a regular expression search or match,
2377 since only regular expressions have distinguished subexpressions. */)
2378 (newtext
, fixedcase
, literal
, string
, subexp
)
2379 Lisp_Object newtext
, fixedcase
, literal
, string
, subexp
;
2381 enum { nochange
, all_caps
, cap_initial
} case_action
;
2382 register int pos
, pos_byte
;
2383 int some_multiletter_word
;
2386 int some_nonuppercase_initial
;
2387 register int c
, prevc
;
2389 int opoint
, newpoint
;
2391 CHECK_STRING (newtext
);
2393 if (! NILP (string
))
2394 CHECK_STRING (string
);
2396 case_action
= nochange
; /* We tried an initialization */
2397 /* but some C compilers blew it */
2399 if (search_regs
.num_regs
<= 0)
2400 error ("`replace-match' called before any match found");
2406 CHECK_NUMBER (subexp
);
2407 sub
= XINT (subexp
);
2408 if (sub
< 0 || sub
>= search_regs
.num_regs
)
2409 args_out_of_range (subexp
, make_number (search_regs
.num_regs
));
2414 if (search_regs
.start
[sub
] < BEGV
2415 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2416 || search_regs
.end
[sub
] > ZV
)
2417 args_out_of_range (make_number (search_regs
.start
[sub
]),
2418 make_number (search_regs
.end
[sub
]));
2422 if (search_regs
.start
[sub
] < 0
2423 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2424 || search_regs
.end
[sub
] > SCHARS (string
))
2425 args_out_of_range (make_number (search_regs
.start
[sub
]),
2426 make_number (search_regs
.end
[sub
]));
2429 if (NILP (fixedcase
))
2431 /* Decide how to casify by examining the matched text. */
2434 pos
= search_regs
.start
[sub
];
2435 last
= search_regs
.end
[sub
];
2438 pos_byte
= CHAR_TO_BYTE (pos
);
2440 pos_byte
= string_char_to_byte (string
, pos
);
2443 case_action
= all_caps
;
2445 /* some_multiletter_word is set nonzero if any original word
2446 is more than one letter long. */
2447 some_multiletter_word
= 0;
2449 some_nonuppercase_initial
= 0;
2456 c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
);
2457 INC_BOTH (pos
, pos_byte
);
2460 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, pos
, pos_byte
);
2464 /* Cannot be all caps if any original char is lower case */
2467 if (SYNTAX (prevc
) != Sword
)
2468 some_nonuppercase_initial
= 1;
2470 some_multiletter_word
= 1;
2472 else if (UPPERCASEP (c
))
2475 if (SYNTAX (prevc
) != Sword
)
2478 some_multiletter_word
= 1;
2482 /* If the initial is a caseless word constituent,
2483 treat that like a lowercase initial. */
2484 if (SYNTAX (prevc
) != Sword
)
2485 some_nonuppercase_initial
= 1;
2491 /* Convert to all caps if the old text is all caps
2492 and has at least one multiletter word. */
2493 if (! some_lowercase
&& some_multiletter_word
)
2494 case_action
= all_caps
;
2495 /* Capitalize each word, if the old text has all capitalized words. */
2496 else if (!some_nonuppercase_initial
&& some_multiletter_word
)
2497 case_action
= cap_initial
;
2498 else if (!some_nonuppercase_initial
&& some_uppercase
)
2499 /* Should x -> yz, operating on X, give Yz or YZ?
2500 We'll assume the latter. */
2501 case_action
= all_caps
;
2503 case_action
= nochange
;
2506 /* Do replacement in a string. */
2509 Lisp_Object before
, after
;
2511 before
= Fsubstring (string
, make_number (0),
2512 make_number (search_regs
.start
[sub
]));
2513 after
= Fsubstring (string
, make_number (search_regs
.end
[sub
]), Qnil
);
2515 /* Substitute parts of the match into NEWTEXT
2520 int lastpos_byte
= 0;
2521 /* We build up the substituted string in ACCUM. */
2524 int length
= SBYTES (newtext
);
2528 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2532 int delbackslash
= 0;
2534 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2538 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2542 substart
= search_regs
.start
[sub
];
2543 subend
= search_regs
.end
[sub
];
2545 else if (c
>= '1' && c
<= '9')
2547 if (search_regs
.start
[c
- '0'] >= 0
2548 && c
<= search_regs
.num_regs
+ '0')
2550 substart
= search_regs
.start
[c
- '0'];
2551 subend
= search_regs
.end
[c
- '0'];
2555 /* If that subexp did not match,
2556 replace \\N with nothing. */
2564 error ("Invalid use of `\\' in replacement text");
2568 if (pos
- 2 != lastpos
)
2569 middle
= substring_both (newtext
, lastpos
,
2571 pos
- 2, pos_byte
- 2);
2574 accum
= concat3 (accum
, middle
,
2576 make_number (substart
),
2577 make_number (subend
)));
2579 lastpos_byte
= pos_byte
;
2581 else if (delbackslash
)
2583 middle
= substring_both (newtext
, lastpos
,
2585 pos
- 1, pos_byte
- 1);
2587 accum
= concat2 (accum
, middle
);
2589 lastpos_byte
= pos_byte
;
2594 middle
= substring_both (newtext
, lastpos
,
2600 newtext
= concat2 (accum
, middle
);
2603 /* Do case substitution in NEWTEXT if desired. */
2604 if (case_action
== all_caps
)
2605 newtext
= Fupcase (newtext
);
2606 else if (case_action
== cap_initial
)
2607 newtext
= Fupcase_initials (newtext
);
2609 return concat3 (before
, newtext
, after
);
2612 /* Record point, then move (quietly) to the start of the match. */
2613 if (PT
>= search_regs
.end
[sub
])
2615 else if (PT
> search_regs
.start
[sub
])
2616 opoint
= search_regs
.end
[sub
] - ZV
;
2620 /* If we want non-literal replacement,
2621 perform substitution on the replacement string. */
2624 int length
= SBYTES (newtext
);
2625 unsigned char *substed
;
2626 int substed_alloc_size
, substed_len
;
2627 int buf_multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
2628 int str_multibyte
= STRING_MULTIBYTE (newtext
);
2629 Lisp_Object rev_tbl
;
2630 int really_changed
= 0;
2634 substed_alloc_size
= length
* 2 + 100;
2635 substed
= (unsigned char *) xmalloc (substed_alloc_size
+ 1);
2638 /* Go thru NEWTEXT, producing the actual text to insert in
2639 SUBSTED while adjusting multibyteness to that of the current
2642 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2644 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2645 unsigned char *add_stuff
= NULL
;
2651 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
, pos
, pos_byte
);
2653 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2657 /* Note that we don't have to increment POS. */
2658 c
= SREF (newtext
, pos_byte
++);
2660 c
= unibyte_char_to_multibyte (c
);
2663 /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
2664 or set IDX to a match index, which means put that part
2665 of the buffer text into SUBSTED. */
2673 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
,
2675 if (!buf_multibyte
&& !ASCII_CHAR_P (c
))
2676 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2680 c
= SREF (newtext
, pos_byte
++);
2682 c
= unibyte_char_to_multibyte (c
);
2687 else if (c
>= '1' && c
<= '9' && c
<= search_regs
.num_regs
+ '0')
2689 if (search_regs
.start
[c
- '0'] >= 1)
2693 add_len
= 1, add_stuff
= "\\";
2697 error ("Invalid use of `\\' in replacement text");
2702 add_len
= CHAR_STRING (c
, str
);
2706 /* If we want to copy part of a previous match,
2707 set up ADD_STUFF and ADD_LEN to point to it. */
2710 int begbyte
= CHAR_TO_BYTE (search_regs
.start
[idx
]);
2711 add_len
= CHAR_TO_BYTE (search_regs
.end
[idx
]) - begbyte
;
2712 if (search_regs
.start
[idx
] < GPT
&& GPT
< search_regs
.end
[idx
])
2713 move_gap (search_regs
.start
[idx
]);
2714 add_stuff
= BYTE_POS_ADDR (begbyte
);
2717 /* Now the stuff we want to add to SUBSTED
2718 is invariably ADD_LEN bytes starting at ADD_STUFF. */
2720 /* Make sure SUBSTED is big enough. */
2721 if (substed_len
+ add_len
>= substed_alloc_size
)
2723 substed_alloc_size
= substed_len
+ add_len
+ 500;
2724 substed
= (unsigned char *) xrealloc (substed
,
2725 substed_alloc_size
+ 1);
2728 /* Now add to the end of SUBSTED. */
2731 bcopy (add_stuff
, substed
+ substed_len
, add_len
);
2732 substed_len
+= add_len
;
2740 int nchars
= multibyte_chars_in_text (substed
, substed_len
);
2742 newtext
= make_multibyte_string (substed
, nchars
, substed_len
);
2745 newtext
= make_unibyte_string (substed
, substed_len
);
2750 /* Replace the old text with the new in the cleanest possible way. */
2751 replace_range (search_regs
.start
[sub
], search_regs
.end
[sub
],
2753 newpoint
= search_regs
.start
[sub
] + SCHARS (newtext
);
2755 if (case_action
== all_caps
)
2756 Fupcase_region (make_number (search_regs
.start
[sub
]),
2757 make_number (newpoint
));
2758 else if (case_action
== cap_initial
)
2759 Fupcase_initials_region (make_number (search_regs
.start
[sub
]),
2760 make_number (newpoint
));
2762 /* Adjust search data for this change. */
2764 int oldend
= search_regs
.end
[sub
];
2765 int oldstart
= search_regs
.start
[sub
];
2766 int change
= newpoint
- search_regs
.end
[sub
];
2769 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2771 if (search_regs
.start
[i
] >= oldend
)
2772 search_regs
.start
[i
] += change
;
2773 else if (search_regs
.start
[i
] > oldstart
)
2774 search_regs
.start
[i
] = oldstart
;
2775 if (search_regs
.end
[i
] >= oldend
)
2776 search_regs
.end
[i
] += change
;
2777 else if (search_regs
.end
[i
] > oldstart
)
2778 search_regs
.end
[i
] = oldstart
;
2782 /* Put point back where it was in the text. */
2784 TEMP_SET_PT (opoint
+ ZV
);
2786 TEMP_SET_PT (opoint
);
2788 /* Now move point "officially" to the start of the inserted replacement. */
2789 move_if_not_intangible (newpoint
);
2795 match_limit (num
, beginningp
)
2804 args_out_of_range (num
, make_number (0));
2805 if (search_regs
.num_regs
<= 0)
2806 error ("No match data, because no search succeeded");
2807 if (n
>= search_regs
.num_regs
2808 || search_regs
.start
[n
] < 0)
2810 return (make_number ((beginningp
) ? search_regs
.start
[n
]
2811 : search_regs
.end
[n
]));
2814 DEFUN ("match-beginning", Fmatch_beginning
, Smatch_beginning
, 1, 1, 0,
2815 doc
: /* Return position of start of text matched by last search.
2816 SUBEXP, a number, specifies which parenthesized expression in the last
2818 Value is nil if SUBEXPth pair didn't match, or there were less than
2820 Zero means the entire text matched by the whole regexp or whole string. */)
2824 return match_limit (subexp
, 1);
2827 DEFUN ("match-end", Fmatch_end
, Smatch_end
, 1, 1, 0,
2828 doc
: /* Return position of end of text matched by last search.
2829 SUBEXP, a number, specifies which parenthesized expression in the last
2831 Value is nil if SUBEXPth pair didn't match, or there were less than
2833 Zero means the entire text matched by the whole regexp or whole string. */)
2837 return match_limit (subexp
, 0);
2840 DEFUN ("match-data", Fmatch_data
, Smatch_data
, 0, 3, 0,
2841 doc
: /* Return a list containing all info on what the last search matched.
2842 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
2843 All the elements are markers or nil (nil if the Nth pair didn't match)
2844 if the last match was on a buffer; integers or nil if a string was matched.
2845 Use `store-match-data' to reinstate the data in this list.
2847 If INTEGERS (the optional first argument) is non-nil, always use
2848 integers \(rather than markers) to represent buffer positions. In
2849 this case, and if the last match was in a buffer, the buffer will get
2850 stored as one additional element at the end of the list.
2852 If REUSE is a list, reuse it as part of the value. If REUSE is long
2853 enough to hold all the values, and if INTEGERS is non-nil, no consing
2856 If optional third arg RESEAT is non-nil, any previous markers on the
2857 REUSE list will be modified to point to nowhere.
2859 Return value is undefined if the last search failed. */)
2860 (integers
, reuse
, reseat
)
2861 Lisp_Object integers
, reuse
, reseat
;
2863 Lisp_Object tail
, prev
;
2868 for (tail
= reuse
; CONSP (tail
); tail
= XCDR (tail
))
2869 if (MARKERP (XCAR (tail
)))
2871 unchain_marker (XMARKER (XCAR (tail
)));
2872 XSETCAR (tail
, Qnil
);
2875 if (NILP (last_thing_searched
))
2880 data
= (Lisp_Object
*) alloca ((2 * search_regs
.num_regs
+ 1)
2881 * sizeof (Lisp_Object
));
2884 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2886 int start
= search_regs
.start
[i
];
2889 if (EQ (last_thing_searched
, Qt
)
2890 || ! NILP (integers
))
2892 XSETFASTINT (data
[2 * i
], start
);
2893 XSETFASTINT (data
[2 * i
+ 1], search_regs
.end
[i
]);
2895 else if (BUFFERP (last_thing_searched
))
2897 data
[2 * i
] = Fmake_marker ();
2898 Fset_marker (data
[2 * i
],
2899 make_number (start
),
2900 last_thing_searched
);
2901 data
[2 * i
+ 1] = Fmake_marker ();
2902 Fset_marker (data
[2 * i
+ 1],
2903 make_number (search_regs
.end
[i
]),
2904 last_thing_searched
);
2907 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
2913 data
[2 * i
] = data
[2 * i
+ 1] = Qnil
;
2916 if (BUFFERP (last_thing_searched
) && !NILP (integers
))
2918 data
[len
] = last_thing_searched
;
2922 /* If REUSE is not usable, cons up the values and return them. */
2923 if (! CONSP (reuse
))
2924 return Flist (len
, data
);
2926 /* If REUSE is a list, store as many value elements as will fit
2927 into the elements of REUSE. */
2928 for (i
= 0, tail
= reuse
; CONSP (tail
);
2929 i
++, tail
= XCDR (tail
))
2932 XSETCAR (tail
, data
[i
]);
2934 XSETCAR (tail
, Qnil
);
2938 /* If we couldn't fit all value elements into REUSE,
2939 cons up the rest of them and add them to the end of REUSE. */
2941 XSETCDR (prev
, Flist (len
- i
, data
+ i
));
2946 /* We used to have an internal use variant of `reseat' described as:
2948 If RESEAT is `evaporate', put the markers back on the free list
2949 immediately. No other references to the markers must exist in this
2950 case, so it is used only internally on the unwind stack and
2951 save-match-data from Lisp.
2953 But it was ill-conceived: those supposedly-internal markers get exposed via
2954 the undo-list, so freeing them here is unsafe. */
2956 DEFUN ("set-match-data", Fset_match_data
, Sset_match_data
, 1, 2, 0,
2957 doc
: /* Set internal data on last search match from elements of LIST.
2958 LIST should have been created by calling `match-data' previously.
2960 If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */)
2962 register Lisp_Object list
, reseat
;
2965 register Lisp_Object marker
;
2967 if (running_asynch_code
)
2968 save_search_regs ();
2972 /* Unless we find a marker with a buffer or an explicit buffer
2973 in LIST, assume that this match data came from a string. */
2974 last_thing_searched
= Qt
;
2976 /* Allocate registers if they don't already exist. */
2978 int length
= XFASTINT (Flength (list
)) / 2;
2980 if (length
> search_regs
.num_regs
)
2982 if (search_regs
.num_regs
== 0)
2985 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2987 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2992 = (regoff_t
*) xrealloc (search_regs
.start
,
2993 length
* sizeof (regoff_t
));
2995 = (regoff_t
*) xrealloc (search_regs
.end
,
2996 length
* sizeof (regoff_t
));
2999 for (i
= search_regs
.num_regs
; i
< length
; i
++)
3000 search_regs
.start
[i
] = -1;
3002 search_regs
.num_regs
= length
;
3005 for (i
= 0; CONSP (list
); i
++)
3007 marker
= XCAR (list
);
3008 if (BUFFERP (marker
))
3010 last_thing_searched
= marker
;
3017 search_regs
.start
[i
] = -1;
3026 if (MARKERP (marker
))
3028 if (XMARKER (marker
)->buffer
== 0)
3029 XSETFASTINT (marker
, 0);
3031 XSETBUFFER (last_thing_searched
, XMARKER (marker
)->buffer
);
3034 CHECK_NUMBER_COERCE_MARKER (marker
);
3035 from
= XINT (marker
);
3037 if (!NILP (reseat
) && MARKERP (m
))
3039 unchain_marker (XMARKER (m
));
3040 XSETCAR (list
, Qnil
);
3043 if ((list
= XCDR (list
), !CONSP (list
)))
3046 m
= marker
= XCAR (list
);
3048 if (MARKERP (marker
) && XMARKER (marker
)->buffer
== 0)
3049 XSETFASTINT (marker
, 0);
3051 CHECK_NUMBER_COERCE_MARKER (marker
);
3052 search_regs
.start
[i
] = from
;
3053 search_regs
.end
[i
] = XINT (marker
);
3055 if (!NILP (reseat
) && MARKERP (m
))
3057 unchain_marker (XMARKER (m
));
3058 XSETCAR (list
, Qnil
);
3064 for (; i
< search_regs
.num_regs
; i
++)
3065 search_regs
.start
[i
] = -1;
3071 /* If non-zero the match data have been saved in saved_search_regs
3072 during the execution of a sentinel or filter. */
3073 static int search_regs_saved
;
3074 static struct re_registers saved_search_regs
;
3075 static Lisp_Object saved_last_thing_searched
;
3077 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
3078 if asynchronous code (filter or sentinel) is running. */
3082 if (!search_regs_saved
)
3084 saved_search_regs
.num_regs
= search_regs
.num_regs
;
3085 saved_search_regs
.start
= search_regs
.start
;
3086 saved_search_regs
.end
= search_regs
.end
;
3087 saved_last_thing_searched
= last_thing_searched
;
3088 last_thing_searched
= Qnil
;
3089 search_regs
.num_regs
= 0;
3090 search_regs
.start
= 0;
3091 search_regs
.end
= 0;
3093 search_regs_saved
= 1;
3097 /* Called upon exit from filters and sentinels. */
3099 restore_search_regs ()
3101 if (search_regs_saved
)
3103 if (search_regs
.num_regs
> 0)
3105 xfree (search_regs
.start
);
3106 xfree (search_regs
.end
);
3108 search_regs
.num_regs
= saved_search_regs
.num_regs
;
3109 search_regs
.start
= saved_search_regs
.start
;
3110 search_regs
.end
= saved_search_regs
.end
;
3111 last_thing_searched
= saved_last_thing_searched
;
3112 saved_last_thing_searched
= Qnil
;
3113 search_regs_saved
= 0;
3118 unwind_set_match_data (list
)
3121 /* It is NOT ALWAYS safe to free (evaporate) the markers immediately. */
3122 return Fset_match_data (list
, Qt
);
3125 /* Called to unwind protect the match data. */
3127 record_unwind_save_match_data ()
3129 record_unwind_protect (unwind_set_match_data
,
3130 Fmatch_data (Qnil
, Qnil
, Qnil
));
3133 /* Quote a string to inactivate reg-expr chars */
3135 DEFUN ("regexp-quote", Fregexp_quote
, Sregexp_quote
, 1, 1, 0,
3136 doc
: /* Return a regexp string which matches exactly STRING and nothing else. */)
3140 register unsigned char *in
, *out
, *end
;
3141 register unsigned char *temp
;
3142 int backslashes_added
= 0;
3144 CHECK_STRING (string
);
3146 temp
= (unsigned char *) alloca (SBYTES (string
) * 2);
3148 /* Now copy the data into the new string, inserting escapes. */
3150 in
= SDATA (string
);
3151 end
= in
+ SBYTES (string
);
3154 for (; in
!= end
; in
++)
3157 || *in
== '*' || *in
== '.' || *in
== '\\'
3158 || *in
== '?' || *in
== '+'
3159 || *in
== '^' || *in
== '$')
3160 *out
++ = '\\', backslashes_added
++;
3164 return make_specified_string (temp
,
3165 SCHARS (string
) + backslashes_added
,
3167 STRING_MULTIBYTE (string
));
3175 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
3177 searchbufs
[i
].buf
.allocated
= 100;
3178 searchbufs
[i
].buf
.buffer
= (unsigned char *) xmalloc (100);
3179 searchbufs
[i
].buf
.fastmap
= searchbufs
[i
].fastmap
;
3180 searchbufs
[i
].regexp
= Qnil
;
3181 searchbufs
[i
].whitespace_regexp
= Qnil
;
3182 searchbufs
[i
].syntax_table
= Qnil
;
3183 staticpro (&searchbufs
[i
].regexp
);
3184 staticpro (&searchbufs
[i
].whitespace_regexp
);
3185 staticpro (&searchbufs
[i
].syntax_table
);
3186 searchbufs
[i
].next
= (i
== REGEXP_CACHE_SIZE
-1 ? 0 : &searchbufs
[i
+1]);
3188 searchbuf_head
= &searchbufs
[0];
3190 Qsearch_failed
= intern ("search-failed");
3191 staticpro (&Qsearch_failed
);
3192 Qinvalid_regexp
= intern ("invalid-regexp");
3193 staticpro (&Qinvalid_regexp
);
3195 Fput (Qsearch_failed
, Qerror_conditions
,
3196 Fcons (Qsearch_failed
, Fcons (Qerror
, Qnil
)));
3197 Fput (Qsearch_failed
, Qerror_message
,
3198 build_string ("Search failed"));
3200 Fput (Qinvalid_regexp
, Qerror_conditions
,
3201 Fcons (Qinvalid_regexp
, Fcons (Qerror
, Qnil
)));
3202 Fput (Qinvalid_regexp
, Qerror_message
,
3203 build_string ("Invalid regexp"));
3205 last_thing_searched
= Qnil
;
3206 staticpro (&last_thing_searched
);
3208 saved_last_thing_searched
= Qnil
;
3209 staticpro (&saved_last_thing_searched
);
3211 DEFVAR_LISP ("search-spaces-regexp", &Vsearch_spaces_regexp
,
3212 doc
: /* Regexp to substitute for bunches of spaces in regexp search.
3213 Some commands use this for user-specified regexps.
3214 Spaces that occur inside character classes or repetition operators
3215 or other such regexp constructs are not replaced with this.
3216 A value of nil (which is the normal value) means treat spaces literally. */);
3217 Vsearch_spaces_regexp
= Qnil
;
3219 DEFVAR_LISP ("inhibit-changing-match-data", &Vinhibit_changing_match_data
,
3220 doc
: /* Internal use only.
3221 If non-nil, the primitive searching and matching functions
3222 such as `looking-at', `string-match', `re-search-forward', etc.,
3223 do not set the match data. The proper way to use this variable
3224 is to bind it with `let' around a small expression. */);
3225 Vinhibit_changing_match_data
= Qnil
;
3227 defsubr (&Slooking_at
);
3228 defsubr (&Sposix_looking_at
);
3229 defsubr (&Sstring_match
);
3230 defsubr (&Sposix_string_match
);
3231 defsubr (&Ssearch_forward
);
3232 defsubr (&Ssearch_backward
);
3233 defsubr (&Sword_search_forward
);
3234 defsubr (&Sword_search_backward
);
3235 defsubr (&Sre_search_forward
);
3236 defsubr (&Sre_search_backward
);
3237 defsubr (&Sposix_search_forward
);
3238 defsubr (&Sposix_search_backward
);
3239 defsubr (&Sreplace_match
);
3240 defsubr (&Smatch_beginning
);
3241 defsubr (&Smatch_end
);
3242 defsubr (&Smatch_data
);
3243 defsubr (&Sset_match_data
);
3244 defsubr (&Sregexp_quote
);
3247 /* arch-tag: a6059d79-0552-4f14-a2cb-d379a4e3c78f
3248 (do not change this comment) */