1 /* String search routines for GNU Emacs.
3 Copyright (C) 1985-1987, 1993-1994, 1997-1999, 2001-2013 Free Software
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 of the License, or
11 (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>. */
27 #include "character.h"
30 #include "region-cache.h"
32 #include "blockinput.h"
33 #include "intervals.h"
35 #include <sys/types.h>
38 #define REGEXP_CACHE_SIZE 20
40 /* If the regexp is non-nil, then the buffer contains the compiled form
41 of that regexp, suitable for searching. */
44 struct regexp_cache
*next
;
45 Lisp_Object regexp
, f_whitespace_regexp
;
46 /* Syntax table for which the regexp applies. We need this because
47 of character classes. If this is t, then the compiled pattern is valid
48 for any syntax-table. */
49 Lisp_Object syntax_table
;
50 struct re_pattern_buffer buf
;
52 /* True means regexp was compiled to do full POSIX backtracking. */
56 /* The instances of that struct. */
57 static struct regexp_cache searchbufs
[REGEXP_CACHE_SIZE
];
59 /* The head of the linked list; points to the most recently used buffer. */
60 static struct regexp_cache
*searchbuf_head
;
63 /* Every call to re_match, etc., must pass &search_regs as the regs
64 argument unless you can show it is unnecessary (i.e., if re_match
65 is certainly going to be called again before region-around-match
68 Since the registers are now dynamically allocated, we need to make
69 sure not to refer to the Nth register before checking that it has
70 been allocated by checking search_regs.num_regs.
72 The regex code keeps track of whether it has allocated the search
73 buffer using bits in the re_pattern_buffer. This means that whenever
74 you compile a new pattern, it completely forgets whether it has
75 allocated any registers, and will allocate new registers the next
76 time you call a searching or matching function. Therefore, we need
77 to call re_set_registers after compiling a new pattern or after
78 setting the match registers, so that the regex functions will be
79 able to free or re-allocate it properly. */
80 /* static struct re_registers search_regs; */
82 /* The buffer in which the last search was performed, or
83 Qt if the last search was done in a string;
84 Qnil if no searching has been done yet. */
85 /* static Lisp_Object last_thing_searched; */
87 /* Error condition signaled when regexp compile_pattern fails. */
88 static Lisp_Object Qinvalid_regexp
;
90 /* Error condition used for failing searches. */
91 static Lisp_Object Qsearch_failed
;
93 static void set_search_regs (ptrdiff_t, ptrdiff_t);
94 static void save_search_regs (void);
95 static EMACS_INT
simple_search (EMACS_INT
, unsigned char *, ptrdiff_t,
96 ptrdiff_t, Lisp_Object
, ptrdiff_t, ptrdiff_t,
97 ptrdiff_t, ptrdiff_t);
98 static EMACS_INT
boyer_moore (EMACS_INT
, unsigned char *, ptrdiff_t,
99 Lisp_Object
, Lisp_Object
, ptrdiff_t,
101 static EMACS_INT
search_buffer (Lisp_Object
, ptrdiff_t, ptrdiff_t,
102 ptrdiff_t, ptrdiff_t, EMACS_INT
, int,
103 Lisp_Object
, Lisp_Object
, bool);
105 static _Noreturn
void
106 matcher_overflow (void)
108 error ("Stack overflow in regexp matcher");
111 /* Compile a regexp and signal a Lisp error if anything goes wrong.
112 PATTERN is the pattern to compile.
113 CP is the place to put the result.
114 TRANSLATE is a translation table for ignoring case, or nil for none.
115 POSIX is true if we want full backtracking (POSIX style) for this pattern.
116 False means backtrack only enough to get a valid match.
118 The behavior also depends on Vsearch_spaces_regexp. */
121 compile_pattern_1 (struct regexp_cache
*cp
, Lisp_Object pattern
,
122 Lisp_Object translate
, bool posix
)
128 cp
->buf
.translate
= (! NILP (translate
) ? translate
: make_number (0));
130 cp
->buf
.multibyte
= STRING_MULTIBYTE (pattern
);
131 cp
->buf
.charset_unibyte
= charset_unibyte
;
132 if (STRINGP (Vsearch_spaces_regexp
))
133 cp
->f_whitespace_regexp
= Vsearch_spaces_regexp
;
135 cp
->f_whitespace_regexp
= Qnil
;
137 /* rms: I think BLOCK_INPUT is not needed here any more,
138 because regex.c defines malloc to call xmalloc.
139 Using BLOCK_INPUT here means the debugger won't run if an error occurs.
140 So let's turn it off. */
142 old
= re_set_syntax (RE_SYNTAX_EMACS
143 | (posix
? 0 : RE_NO_POSIX_BACKTRACKING
));
145 if (STRINGP (Vsearch_spaces_regexp
))
146 re_set_whitespace_regexp (SSDATA (Vsearch_spaces_regexp
));
148 re_set_whitespace_regexp (NULL
);
150 val
= (char *) re_compile_pattern (SSDATA (pattern
),
151 SBYTES (pattern
), &cp
->buf
);
153 /* If the compiled pattern hard codes some of the contents of the
154 syntax-table, it can only be reused with *this* syntax table. */
155 cp
->syntax_table
= cp
->buf
.used_syntax
? BVAR (current_buffer
, syntax_table
) : Qt
;
157 re_set_whitespace_regexp (NULL
);
160 /* unblock_input (); */
162 xsignal1 (Qinvalid_regexp
, build_string (val
));
164 cp
->regexp
= Fcopy_sequence (pattern
);
167 /* Shrink each compiled regexp buffer in the cache
168 to the size actually used right now.
169 This is called from garbage collection. */
172 shrink_regexp_cache (void)
174 struct regexp_cache
*cp
;
176 for (cp
= searchbuf_head
; cp
!= 0; cp
= cp
->next
)
178 cp
->buf
.allocated
= cp
->buf
.used
;
179 cp
->buf
.buffer
= xrealloc (cp
->buf
.buffer
, cp
->buf
.used
);
183 /* Clear the regexp cache w.r.t. a particular syntax table,
184 because it was changed.
185 There is no danger of memory leak here because re_compile_pattern
186 automagically manages the memory in each re_pattern_buffer struct,
187 based on its `allocated' and `buffer' values. */
189 clear_regexp_cache (void)
193 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
194 /* It's tempting to compare with the syntax-table we've actually changed,
195 but it's not sufficient because char-table inheritance means that
196 modifying one syntax-table can change others at the same time. */
197 if (!EQ (searchbufs
[i
].syntax_table
, Qt
))
198 searchbufs
[i
].regexp
= Qnil
;
201 /* Compile a regexp if necessary, but first check to see if there's one in
203 PATTERN is the pattern to compile.
204 TRANSLATE is a translation table for ignoring case, or nil for none.
205 REGP is the structure that says where to store the "register"
206 values that will result from matching this pattern.
207 If it is 0, we should compile the pattern not to record any
208 subexpression bounds.
209 POSIX is true if we want full backtracking (POSIX style) for this pattern.
210 False means backtrack only enough to get a valid match. */
212 struct re_pattern_buffer
*
213 compile_pattern (Lisp_Object pattern
, struct re_registers
*regp
,
214 Lisp_Object translate
, bool posix
, bool multibyte
)
216 struct regexp_cache
*cp
, **cpp
;
218 for (cpp
= &searchbuf_head
; ; cpp
= &cp
->next
)
221 /* Entries are initialized to nil, and may be set to nil by
222 compile_pattern_1 if the pattern isn't valid. Don't apply
223 string accessors in those cases. However, compile_pattern_1
224 is only applied to the cache entry we pick here to reuse. So
225 nil should never appear before a non-nil entry. */
226 if (NILP (cp
->regexp
))
228 if (SCHARS (cp
->regexp
) == SCHARS (pattern
)
229 && STRING_MULTIBYTE (cp
->regexp
) == STRING_MULTIBYTE (pattern
)
230 && !NILP (Fstring_equal (cp
->regexp
, pattern
))
231 && EQ (cp
->buf
.translate
, (! NILP (translate
) ? translate
: make_number (0)))
232 && cp
->posix
== posix
233 && (EQ (cp
->syntax_table
, Qt
)
234 || EQ (cp
->syntax_table
, BVAR (current_buffer
, syntax_table
)))
235 && !NILP (Fequal (cp
->f_whitespace_regexp
, Vsearch_spaces_regexp
))
236 && cp
->buf
.charset_unibyte
== charset_unibyte
)
239 /* If we're at the end of the cache, compile into the nil cell
240 we found, or the last (least recently used) cell with a
245 compile_pattern_1 (cp
, pattern
, translate
, posix
);
250 /* When we get here, cp (aka *cpp) contains the compiled pattern,
251 either because we found it in the cache or because we just compiled it.
252 Move it to the front of the queue to mark it as most recently used. */
254 cp
->next
= searchbuf_head
;
257 /* Advise the searching functions about the space we have allocated
258 for register data. */
260 re_set_registers (&cp
->buf
, regp
, regp
->num_regs
, regp
->start
, regp
->end
);
262 /* The compiled pattern can be used both for multibyte and unibyte
263 target. But, we have to tell which the pattern is used for. */
264 cp
->buf
.target_multibyte
= multibyte
;
271 looking_at_1 (Lisp_Object string
, bool posix
)
274 unsigned char *p1
, *p2
;
276 register ptrdiff_t i
;
277 struct re_pattern_buffer
*bufp
;
279 if (running_asynch_code
)
282 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
283 set_char_table_extras (BVAR (current_buffer
, case_canon_table
), 2,
284 BVAR (current_buffer
, case_eqv_table
));
286 CHECK_STRING (string
);
287 bufp
= compile_pattern (string
,
288 (NILP (Vinhibit_changing_match_data
)
289 ? &search_regs
: NULL
),
290 (!NILP (BVAR (current_buffer
, case_fold_search
))
291 ? BVAR (current_buffer
, case_canon_table
) : Qnil
),
293 !NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
296 QUIT
; /* Do a pending quit right away, to avoid paradoxical behavior */
298 /* Get pointers and sizes of the two strings
299 that make up the visible portion of the buffer. */
302 s1
= GPT_BYTE
- BEGV_BYTE
;
304 s2
= ZV_BYTE
- GPT_BYTE
;
308 s2
= ZV_BYTE
- BEGV_BYTE
;
313 s1
= ZV_BYTE
- BEGV_BYTE
;
317 re_match_object
= Qnil
;
319 i
= re_match_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
321 (NILP (Vinhibit_changing_match_data
)
322 ? &search_regs
: NULL
),
323 ZV_BYTE
- BEGV_BYTE
);
329 val
= (0 <= i
? Qt
: Qnil
);
330 if (NILP (Vinhibit_changing_match_data
) && i
>= 0)
331 for (i
= 0; i
< search_regs
.num_regs
; i
++)
332 if (search_regs
.start
[i
] >= 0)
335 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
337 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
340 /* Set last_thing_searched only when match data is changed. */
341 if (NILP (Vinhibit_changing_match_data
))
342 XSETBUFFER (last_thing_searched
, current_buffer
);
347 DEFUN ("looking-at", Flooking_at
, Slooking_at
, 1, 1, 0,
348 doc
: /* Return t if text after point matches regular expression REGEXP.
349 This function modifies the match data that `match-beginning',
350 `match-end' and `match-data' access; save and restore the match
351 data if you want to preserve them. */)
354 return looking_at_1 (regexp
, 0);
357 DEFUN ("posix-looking-at", Fposix_looking_at
, Sposix_looking_at
, 1, 1, 0,
358 doc
: /* Return t if text after point matches regular expression REGEXP.
359 Find the longest match, in accord with Posix regular expression rules.
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. */)
365 return looking_at_1 (regexp
, 1);
369 string_match_1 (Lisp_Object regexp
, Lisp_Object string
, Lisp_Object start
,
373 struct re_pattern_buffer
*bufp
;
375 ptrdiff_t pos_byte
, i
;
377 if (running_asynch_code
)
380 CHECK_STRING (regexp
);
381 CHECK_STRING (string
);
384 pos
= 0, pos_byte
= 0;
387 ptrdiff_t len
= SCHARS (string
);
389 CHECK_NUMBER (start
);
391 if (pos
< 0 && -pos
<= len
)
393 else if (0 > pos
|| pos
> len
)
394 args_out_of_range (string
, start
);
395 pos_byte
= string_char_to_byte (string
, pos
);
398 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
399 set_char_table_extras (BVAR (current_buffer
, case_canon_table
), 2,
400 BVAR (current_buffer
, case_eqv_table
));
402 bufp
= compile_pattern (regexp
,
403 (NILP (Vinhibit_changing_match_data
)
404 ? &search_regs
: NULL
),
405 (!NILP (BVAR (current_buffer
, case_fold_search
))
406 ? BVAR (current_buffer
, case_canon_table
) : Qnil
),
408 STRING_MULTIBYTE (string
));
410 re_match_object
= string
;
412 val
= re_search (bufp
, SSDATA (string
),
413 SBYTES (string
), pos_byte
,
414 SBYTES (string
) - pos_byte
,
415 (NILP (Vinhibit_changing_match_data
)
416 ? &search_regs
: NULL
));
419 /* Set last_thing_searched only when match data is changed. */
420 if (NILP (Vinhibit_changing_match_data
))
421 last_thing_searched
= Qt
;
425 if (val
< 0) return Qnil
;
427 if (NILP (Vinhibit_changing_match_data
))
428 for (i
= 0; i
< search_regs
.num_regs
; i
++)
429 if (search_regs
.start
[i
] >= 0)
432 = string_byte_to_char (string
, search_regs
.start
[i
]);
434 = string_byte_to_char (string
, search_regs
.end
[i
]);
437 return make_number (string_byte_to_char (string
, val
));
440 DEFUN ("string-match", Fstring_match
, Sstring_match
, 2, 3, 0,
441 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
442 Matching ignores case if `case-fold-search' is non-nil.
443 If third arg START is non-nil, start search at that index in STRING.
444 For index of first char beyond the match, do (match-end 0).
445 `match-end' and `match-beginning' also give indices of substrings
446 matched by parenthesis constructs in the pattern.
448 You can use the function `match-string' to extract the substrings
449 matched by the parenthesis constructions in REGEXP. */)
450 (Lisp_Object regexp
, Lisp_Object string
, Lisp_Object start
)
452 return string_match_1 (regexp
, string
, start
, 0);
455 DEFUN ("posix-string-match", Fposix_string_match
, Sposix_string_match
, 2, 3, 0,
456 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
457 Find the longest match, in accord with Posix regular expression rules.
458 Case is ignored if `case-fold-search' is non-nil in the current buffer.
459 If third arg START is non-nil, start search at that index in STRING.
460 For index of first char beyond the match, do (match-end 0).
461 `match-end' and `match-beginning' also give indices of substrings
462 matched by parenthesis constructs in the pattern. */)
463 (Lisp_Object regexp
, Lisp_Object string
, Lisp_Object start
)
465 return string_match_1 (regexp
, string
, start
, 1);
468 /* Match REGEXP against STRING, searching all of STRING,
469 and return the index of the match, or negative on failure.
470 This does not clobber the match data. */
473 fast_string_match (Lisp_Object regexp
, Lisp_Object string
)
476 struct re_pattern_buffer
*bufp
;
478 bufp
= compile_pattern (regexp
, 0, Qnil
,
479 0, STRING_MULTIBYTE (string
));
481 re_match_object
= string
;
483 val
= re_search (bufp
, SSDATA (string
),
490 /* Match REGEXP against STRING, searching all of STRING ignoring case,
491 and return the index of the match, or negative on failure.
492 This does not clobber the match data.
493 We assume that STRING contains single-byte characters. */
496 fast_c_string_match_ignore_case (Lisp_Object regexp
,
497 const char *string
, ptrdiff_t len
)
500 struct re_pattern_buffer
*bufp
;
502 regexp
= string_make_unibyte (regexp
);
503 re_match_object
= Qt
;
504 bufp
= compile_pattern (regexp
, 0,
505 Vascii_canon_table
, 0,
508 val
= re_search (bufp
, string
, len
, 0, len
, 0);
513 /* Like fast_string_match but ignore case. */
516 fast_string_match_ignore_case (Lisp_Object regexp
, Lisp_Object string
)
519 struct re_pattern_buffer
*bufp
;
521 bufp
= compile_pattern (regexp
, 0, Vascii_canon_table
,
522 0, STRING_MULTIBYTE (string
));
524 re_match_object
= string
;
526 val
= re_search (bufp
, SSDATA (string
),
533 /* Match REGEXP against the characters after POS to LIMIT, and return
534 the number of matched characters. If STRING is non-nil, match
535 against the characters in it. In that case, POS and LIMIT are
536 indices into the string. This function doesn't modify the match
540 fast_looking_at (Lisp_Object regexp
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
541 ptrdiff_t limit
, ptrdiff_t limit_byte
, Lisp_Object string
)
544 struct re_pattern_buffer
*buf
;
545 unsigned char *p1
, *p2
;
549 if (STRINGP (string
))
552 pos_byte
= string_char_to_byte (string
, pos
);
554 limit_byte
= string_char_to_byte (string
, limit
);
558 s2
= SBYTES (string
);
559 re_match_object
= string
;
560 multibyte
= STRING_MULTIBYTE (string
);
565 pos_byte
= CHAR_TO_BYTE (pos
);
567 limit_byte
= CHAR_TO_BYTE (limit
);
568 pos_byte
-= BEGV_BYTE
;
569 limit_byte
-= BEGV_BYTE
;
571 s1
= GPT_BYTE
- BEGV_BYTE
;
573 s2
= ZV_BYTE
- GPT_BYTE
;
577 s2
= ZV_BYTE
- BEGV_BYTE
;
582 s1
= ZV_BYTE
- BEGV_BYTE
;
585 re_match_object
= Qnil
;
586 multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
589 buf
= compile_pattern (regexp
, 0, Qnil
, 0, multibyte
);
591 len
= re_match_2 (buf
, (char *) p1
, s1
, (char *) p2
, s2
,
592 pos_byte
, NULL
, limit_byte
);
599 /* The newline cache: remembering which sections of text have no newlines. */
601 /* If the user has requested newline caching, make sure it's on.
602 Otherwise, make sure it's off.
603 This is our cheezy way of associating an action with the change of
604 state of a buffer-local variable. */
606 newline_cache_on_off (struct buffer
*buf
)
608 if (NILP (BVAR (buf
, cache_long_line_scans
)))
610 /* It should be off. */
611 if (buf
->newline_cache
)
613 free_region_cache (buf
->newline_cache
);
614 buf
->newline_cache
= 0;
619 /* It should be on. */
620 if (buf
->newline_cache
== 0)
621 buf
->newline_cache
= new_region_cache ();
626 /* Search for COUNT newlines between START/START_BYTE and END/END_BYTE.
628 If COUNT is positive, search forwards; END must be >= START.
629 If COUNT is negative, search backwards for the -COUNTth instance;
630 END must be <= START.
631 If COUNT is zero, do anything you please; run rogue, for all I care.
633 If END is zero, use BEGV or ZV instead, as appropriate for the
634 direction indicated by COUNT.
636 If we find COUNT instances, set *SHORTAGE to zero, and return the
637 position past the COUNTth match. Note that for reverse motion
638 this is not the same as the usual convention for Emacs motion commands.
640 If we don't find COUNT instances before reaching END, set *SHORTAGE
641 to the number of newlines left unfound, and return END.
643 If BYTEPOS is not NULL, set *BYTEPOS to the byte position corresponding
644 to the returned character position.
646 If ALLOW_QUIT, set immediate_quit. That's good to do
647 except when inside redisplay. */
650 find_newline (ptrdiff_t start
, ptrdiff_t start_byte
, ptrdiff_t end
,
651 ptrdiff_t end_byte
, ptrdiff_t count
, ptrdiff_t *shortage
,
652 ptrdiff_t *bytepos
, bool allow_quit
)
654 struct region_cache
*newline_cache
;
661 end
= ZV
, end_byte
= ZV_BYTE
;
667 end
= BEGV
, end_byte
= BEGV_BYTE
;
670 end_byte
= CHAR_TO_BYTE (end
);
672 newline_cache_on_off (current_buffer
);
673 newline_cache
= current_buffer
->newline_cache
;
678 immediate_quit
= allow_quit
;
683 /* Our innermost scanning loop is very simple; it doesn't know
684 about gaps, buffer ends, or the newline cache. ceiling is
685 the position of the last character before the next such
686 obstacle --- the last character the dumb search loop should
688 ptrdiff_t tem
, ceiling_byte
= end_byte
- 1;
690 /* If we're looking for a newline, consult the newline cache
691 to see where we can avoid some scanning. */
694 ptrdiff_t next_change
;
696 while (region_cache_forward
697 (current_buffer
, newline_cache
, start
, &next_change
))
699 immediate_quit
= allow_quit
;
701 start_byte
= CHAR_TO_BYTE (start
);
703 /* START should never be after END. */
704 if (start_byte
> ceiling_byte
)
705 start_byte
= ceiling_byte
;
707 /* Now the text after start is an unknown region, and
708 next_change is the position of the next known region. */
709 ceiling_byte
= min (CHAR_TO_BYTE (next_change
) - 1, ceiling_byte
);
711 else if (start_byte
== -1)
712 start_byte
= CHAR_TO_BYTE (start
);
714 /* The dumb loop can only scan text stored in contiguous
715 bytes. BUFFER_CEILING_OF returns the last character
716 position that is contiguous, so the ceiling is the
717 position after that. */
718 tem
= BUFFER_CEILING_OF (start_byte
);
719 ceiling_byte
= min (tem
, ceiling_byte
);
722 /* The termination address of the dumb loop. */
723 register unsigned char *ceiling_addr
724 = BYTE_POS_ADDR (ceiling_byte
) + 1;
725 register unsigned char *cursor
726 = BYTE_POS_ADDR (start_byte
);
727 unsigned char *base
= cursor
;
729 while (cursor
< ceiling_addr
)
732 unsigned char *nl
= memchr (cursor
, '\n', ceiling_addr
- cursor
);
734 /* If we're looking for newlines, cache the fact that
735 the region from start to cursor is free of them. */
738 unsigned char *low
= cursor
;
739 unsigned char *lim
= nl
? nl
: ceiling_addr
;
740 know_region_cache (current_buffer
, newline_cache
,
741 BYTE_TO_CHAR (low
- base
+ start_byte
),
742 BYTE_TO_CHAR (lim
- base
+ start_byte
));
752 *bytepos
= nl
+ 1 - base
+ start_byte
;
753 return BYTE_TO_CHAR (nl
+ 1 - base
+ start_byte
);
758 start_byte
+= ceiling_addr
- base
;
759 start
= BYTE_TO_CHAR (start_byte
);
765 /* The last character to check before the next obstacle. */
766 ptrdiff_t tem
, ceiling_byte
= end_byte
;
768 /* Consult the newline cache, if appropriate. */
771 ptrdiff_t next_change
;
773 while (region_cache_backward
774 (current_buffer
, newline_cache
, start
, &next_change
))
776 immediate_quit
= allow_quit
;
778 start_byte
= CHAR_TO_BYTE (start
);
780 /* Start should never be at or before end. */
781 if (start_byte
<= ceiling_byte
)
782 start_byte
= ceiling_byte
+ 1;
784 /* Now the text before start is an unknown region, and
785 next_change is the position of the next known region. */
786 ceiling_byte
= max (CHAR_TO_BYTE (next_change
), ceiling_byte
);
788 else if (start_byte
== -1)
789 start_byte
= CHAR_TO_BYTE (start
);
791 /* Stop scanning before the gap. */
792 tem
= BUFFER_FLOOR_OF (start_byte
- 1);
793 ceiling_byte
= max (tem
, ceiling_byte
);
796 /* The termination address of the dumb loop. */
797 register unsigned char *ceiling_addr
= BYTE_POS_ADDR (ceiling_byte
);
798 register unsigned char *cursor
= BYTE_POS_ADDR (start_byte
- 1);
799 unsigned char *base
= cursor
;
801 while (cursor
>= ceiling_addr
)
803 unsigned char *nl
= memrchr (ceiling_addr
, '\n',
804 cursor
+ 1 - ceiling_addr
);
806 /* If we're looking for newlines, cache the fact that
807 the region from after the cursor to start is free of them. */
810 unsigned char *low
= nl
? nl
: ceiling_addr
- 1;
811 unsigned char *lim
= cursor
;
812 know_region_cache (current_buffer
, newline_cache
,
813 BYTE_TO_CHAR (low
- base
+ start_byte
),
814 BYTE_TO_CHAR (lim
- base
+ start_byte
));
824 *bytepos
= nl
- base
+ start_byte
;
825 return BYTE_TO_CHAR (nl
- base
+ start_byte
);
830 start_byte
+= ceiling_addr
- 1 - base
;
831 start
= BYTE_TO_CHAR (start_byte
);
837 *shortage
= count
* direction
;
840 *bytepos
= start_byte
== -1 ? CHAR_TO_BYTE (start
) : start_byte
;
841 eassert (*bytepos
== CHAR_TO_BYTE (start
));
846 /* Search for COUNT instances of a line boundary.
847 Start at START. If COUNT is negative, search backwards.
849 We report the resulting position by calling TEMP_SET_PT_BOTH.
851 If we find COUNT instances. we position after (always after,
852 even if scanning backwards) the COUNTth match, and return 0.
854 If we don't find COUNT instances before reaching the end of the
855 buffer (or the beginning, if scanning backwards), we return
856 the number of line boundaries left unfound, and position at
857 the limit we bumped up against.
859 If ALLOW_QUIT, set immediate_quit. That's good to do
860 except in special cases. */
863 scan_newline (ptrdiff_t start
, ptrdiff_t start_byte
,
864 ptrdiff_t limit
, ptrdiff_t limit_byte
,
865 EMACS_INT count
, bool allow_quit
)
867 int direction
= ((count
> 0) ? 1 : -1);
869 unsigned char *cursor
;
873 unsigned char *ceiling_addr
;
875 bool old_immediate_quit
= immediate_quit
;
882 while (start_byte
< limit_byte
)
884 ceiling
= BUFFER_CEILING_OF (start_byte
);
885 ceiling
= min (limit_byte
- 1, ceiling
);
886 ceiling_addr
= BYTE_POS_ADDR (ceiling
) + 1;
887 base
= (cursor
= BYTE_POS_ADDR (start_byte
));
891 unsigned char *nl
= memchr (cursor
, '\n', ceiling_addr
- cursor
);
896 immediate_quit
= old_immediate_quit
;
897 start_byte
+= nl
- base
+ 1;
898 start
= BYTE_TO_CHAR (start_byte
);
899 TEMP_SET_PT_BOTH (start
, start_byte
);
904 while (cursor
< ceiling_addr
);
906 start_byte
+= ceiling_addr
- base
;
911 while (start_byte
> limit_byte
)
913 ceiling
= BUFFER_FLOOR_OF (start_byte
- 1);
914 ceiling
= max (limit_byte
, ceiling
);
915 ceiling_addr
= BYTE_POS_ADDR (ceiling
);
916 base
= (cursor
= BYTE_POS_ADDR (start_byte
- 1) + 1);
919 unsigned char *nl
= memrchr (ceiling_addr
, '\n',
920 cursor
- ceiling_addr
);
926 immediate_quit
= old_immediate_quit
;
927 /* Return the position AFTER the match we found. */
928 start_byte
+= nl
- base
+ 1;
929 start
= BYTE_TO_CHAR (start_byte
);
930 TEMP_SET_PT_BOTH (start
, start_byte
);
936 start_byte
+= ceiling_addr
- base
;
940 TEMP_SET_PT_BOTH (limit
, limit_byte
);
941 immediate_quit
= old_immediate_quit
;
943 return count
* direction
;
946 /* Like find_newline, but doesn't allow QUITting and doesn't return
949 find_newline_no_quit (ptrdiff_t from
, ptrdiff_t frombyte
,
950 ptrdiff_t cnt
, ptrdiff_t *bytepos
)
952 return find_newline (from
, frombyte
, 0, -1, cnt
, NULL
, bytepos
, 0);
955 /* Like find_newline, but returns position before the newline, not
956 after, and only search up to TO.
957 This isn't just find_newline_no_quit (...)-1, because you might hit TO. */
960 find_before_next_newline (ptrdiff_t from
, ptrdiff_t to
,
961 ptrdiff_t cnt
, ptrdiff_t *bytepos
)
964 ptrdiff_t pos
= find_newline (from
, -1, to
, -1, cnt
, &shortage
, bytepos
, 1);
969 DEC_BOTH (pos
, *bytepos
);
976 /* Subroutines of Lisp buffer search functions. */
979 search_command (Lisp_Object string
, Lisp_Object bound
, Lisp_Object noerror
,
980 Lisp_Object count
, int direction
, int RE
, bool posix
)
985 EMACS_INT n
= direction
;
989 CHECK_NUMBER (count
);
993 CHECK_STRING (string
);
997 lim
= ZV
, lim_byte
= ZV_BYTE
;
999 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
1003 CHECK_NUMBER_COERCE_MARKER (bound
);
1005 if (n
> 0 ? lim
< PT
: lim
> PT
)
1006 error ("Invalid search bound (wrong side of point)");
1008 lim
= ZV
, lim_byte
= ZV_BYTE
;
1009 else if (lim
< BEGV
)
1010 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
1012 lim_byte
= CHAR_TO_BYTE (lim
);
1015 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
1016 set_char_table_extras (BVAR (current_buffer
, case_canon_table
), 2,
1017 BVAR (current_buffer
, case_eqv_table
));
1019 np
= search_buffer (string
, PT
, PT_BYTE
, lim
, lim_byte
, n
, RE
,
1020 (!NILP (BVAR (current_buffer
, case_fold_search
))
1021 ? BVAR (current_buffer
, case_canon_table
)
1023 (!NILP (BVAR (current_buffer
, case_fold_search
))
1024 ? BVAR (current_buffer
, case_eqv_table
)
1030 xsignal1 (Qsearch_failed
, string
);
1032 if (!EQ (noerror
, Qt
))
1034 eassert (BEGV
<= lim
&& lim
<= ZV
);
1035 SET_PT_BOTH (lim
, lim_byte
);
1037 #if 0 /* This would be clean, but maybe programs depend on
1038 a value of nil here. */
1046 eassert (BEGV
<= np
&& np
<= ZV
);
1049 return make_number (np
);
1052 /* Return true if REGEXP it matches just one constant string. */
1055 trivial_regexp_p (Lisp_Object regexp
)
1057 ptrdiff_t len
= SBYTES (regexp
);
1058 unsigned char *s
= SDATA (regexp
);
1063 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
1070 case '|': case '(': case ')': case '`': case '\'': case 'b':
1071 case 'B': case '<': case '>': case 'w': case 'W': case 's':
1072 case 'S': case '=': case '{': case '}': case '_':
1073 case 'c': case 'C': /* for categoryspec and notcategoryspec */
1074 case '1': case '2': case '3': case '4': case '5':
1075 case '6': case '7': case '8': case '9':
1083 /* Search for the n'th occurrence of STRING in the current buffer,
1084 starting at position POS and stopping at position LIM,
1085 treating STRING as a literal string if RE is false or as
1086 a regular expression if RE is true.
1088 If N is positive, searching is forward and LIM must be greater than POS.
1089 If N is negative, searching is backward and LIM must be less than POS.
1091 Returns -x if x occurrences remain to be found (x > 0),
1092 or else the position at the beginning of the Nth occurrence
1093 (if searching backward) or the end (if searching forward).
1095 POSIX is nonzero if we want full backtracking (POSIX style)
1096 for this pattern. 0 means backtrack only enough to get a valid match. */
1098 #define TRANSLATE(out, trt, d) \
1104 temp = Faref (trt, make_number (d)); \
1105 if (INTEGERP (temp)) \
1106 out = XINT (temp); \
1115 /* Only used in search_buffer, to record the end position of the match
1116 when searching regexps and SEARCH_REGS should not be changed
1117 (i.e. Vinhibit_changing_match_data is non-nil). */
1118 static struct re_registers search_regs_1
;
1121 search_buffer (Lisp_Object string
, ptrdiff_t pos
, ptrdiff_t pos_byte
,
1122 ptrdiff_t lim
, ptrdiff_t lim_byte
, EMACS_INT n
,
1123 int RE
, Lisp_Object trt
, Lisp_Object inverse_trt
, bool posix
)
1125 ptrdiff_t len
= SCHARS (string
);
1126 ptrdiff_t len_byte
= SBYTES (string
);
1127 register ptrdiff_t i
;
1129 if (running_asynch_code
)
1130 save_search_regs ();
1132 /* Searching 0 times means don't move. */
1133 /* Null string is found at starting position. */
1134 if (len
== 0 || n
== 0)
1136 set_search_regs (pos_byte
, 0);
1140 if (RE
&& !(trivial_regexp_p (string
) && NILP (Vsearch_spaces_regexp
)))
1142 unsigned char *p1
, *p2
;
1144 struct re_pattern_buffer
*bufp
;
1146 bufp
= compile_pattern (string
,
1147 (NILP (Vinhibit_changing_match_data
)
1148 ? &search_regs
: &search_regs_1
),
1150 !NILP (BVAR (current_buffer
, enable_multibyte_characters
)));
1152 immediate_quit
= 1; /* Quit immediately if user types ^G,
1153 because letting this function finish
1154 can take too long. */
1155 QUIT
; /* Do a pending quit right away,
1156 to avoid paradoxical behavior */
1157 /* Get pointers and sizes of the two strings
1158 that make up the visible portion of the buffer. */
1161 s1
= GPT_BYTE
- BEGV_BYTE
;
1163 s2
= ZV_BYTE
- GPT_BYTE
;
1167 s2
= ZV_BYTE
- BEGV_BYTE
;
1172 s1
= ZV_BYTE
- BEGV_BYTE
;
1175 re_match_object
= Qnil
;
1181 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1182 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1183 (NILP (Vinhibit_changing_match_data
)
1184 ? &search_regs
: &search_regs_1
),
1185 /* Don't allow match past current point */
1186 pos_byte
- BEGV_BYTE
);
1189 matcher_overflow ();
1193 if (NILP (Vinhibit_changing_match_data
))
1195 pos_byte
= search_regs
.start
[0] + BEGV_BYTE
;
1196 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1197 if (search_regs
.start
[i
] >= 0)
1199 search_regs
.start
[i
]
1200 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1202 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1204 XSETBUFFER (last_thing_searched
, current_buffer
);
1205 /* Set pos to the new position. */
1206 pos
= search_regs
.start
[0];
1210 pos_byte
= search_regs_1
.start
[0] + BEGV_BYTE
;
1211 /* Set pos to the new position. */
1212 pos
= BYTE_TO_CHAR (search_regs_1
.start
[0] + BEGV_BYTE
);
1226 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1227 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1228 (NILP (Vinhibit_changing_match_data
)
1229 ? &search_regs
: &search_regs_1
),
1230 lim_byte
- BEGV_BYTE
);
1233 matcher_overflow ();
1237 if (NILP (Vinhibit_changing_match_data
))
1239 pos_byte
= search_regs
.end
[0] + BEGV_BYTE
;
1240 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1241 if (search_regs
.start
[i
] >= 0)
1243 search_regs
.start
[i
]
1244 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1246 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1248 XSETBUFFER (last_thing_searched
, current_buffer
);
1249 pos
= search_regs
.end
[0];
1253 pos_byte
= search_regs_1
.end
[0] + BEGV_BYTE
;
1254 pos
= BYTE_TO_CHAR (search_regs_1
.end
[0] + BEGV_BYTE
);
1267 else /* non-RE case */
1269 unsigned char *raw_pattern
, *pat
;
1270 ptrdiff_t raw_pattern_size
;
1271 ptrdiff_t raw_pattern_size_byte
;
1272 unsigned char *patbuf
;
1273 bool multibyte
= !NILP (BVAR (current_buffer
, enable_multibyte_characters
));
1274 unsigned char *base_pat
;
1275 /* Set to positive if we find a non-ASCII char that need
1276 translation. Otherwise set to zero later. */
1278 bool boyer_moore_ok
= 1;
1280 /* MULTIBYTE says whether the text to be searched is multibyte.
1281 We must convert PATTERN to match that, or we will not really
1282 find things right. */
1284 if (multibyte
== STRING_MULTIBYTE (string
))
1286 raw_pattern
= SDATA (string
);
1287 raw_pattern_size
= SCHARS (string
);
1288 raw_pattern_size_byte
= SBYTES (string
);
1292 raw_pattern_size
= SCHARS (string
);
1293 raw_pattern_size_byte
1294 = count_size_as_multibyte (SDATA (string
),
1296 raw_pattern
= alloca (raw_pattern_size_byte
+ 1);
1297 copy_text (SDATA (string
), raw_pattern
,
1298 SCHARS (string
), 0, 1);
1302 /* Converting multibyte to single-byte.
1304 ??? Perhaps this conversion should be done in a special way
1305 by subtracting nonascii-insert-offset from each non-ASCII char,
1306 so that only the multibyte chars which really correspond to
1307 the chosen single-byte character set can possibly match. */
1308 raw_pattern_size
= SCHARS (string
);
1309 raw_pattern_size_byte
= SCHARS (string
);
1310 raw_pattern
= alloca (raw_pattern_size
+ 1);
1311 copy_text (SDATA (string
), raw_pattern
,
1312 SBYTES (string
), 1, 0);
1315 /* Copy and optionally translate the pattern. */
1316 len
= raw_pattern_size
;
1317 len_byte
= raw_pattern_size_byte
;
1318 patbuf
= alloca (len
* MAX_MULTIBYTE_LENGTH
);
1320 base_pat
= raw_pattern
;
1323 /* Fill patbuf by translated characters in STRING while
1324 checking if we can use boyer-moore search. If TRT is
1325 non-nil, we can use boyer-moore search only if TRT can be
1326 represented by the byte array of 256 elements. For that,
1327 all non-ASCII case-equivalents of all case-sensitive
1328 characters in STRING must belong to the same character
1329 group (two characters belong to the same group iff their
1330 multibyte forms are the same except for the last byte;
1331 i.e. every 64 characters form a group; U+0000..U+003F,
1332 U+0040..U+007F, U+0080..U+00BF, ...). */
1336 unsigned char str_base
[MAX_MULTIBYTE_LENGTH
], *str
;
1337 int c
, translated
, inverse
;
1338 int in_charlen
, charlen
;
1340 /* If we got here and the RE flag is set, it's because we're
1341 dealing with a regexp known to be trivial, so the backslash
1342 just quotes the next character. */
1343 if (RE
&& *base_pat
== '\\')
1351 c
= STRING_CHAR_AND_LENGTH (base_pat
, in_charlen
);
1356 charlen
= in_charlen
;
1360 /* Translate the character. */
1361 TRANSLATE (translated
, trt
, c
);
1362 charlen
= CHAR_STRING (translated
, str_base
);
1365 /* Check if C has any other case-equivalents. */
1366 TRANSLATE (inverse
, inverse_trt
, c
);
1367 /* If so, check if we can use boyer-moore. */
1368 if (c
!= inverse
&& boyer_moore_ok
)
1370 /* Check if all equivalents belong to the same
1371 group of characters. Note that the check of C
1372 itself is done by the last iteration. */
1373 int this_char_base
= -1;
1375 while (boyer_moore_ok
)
1377 if (ASCII_BYTE_P (inverse
))
1379 if (this_char_base
> 0)
1384 else if (CHAR_BYTE8_P (inverse
))
1385 /* Boyer-moore search can't handle a
1386 translation of an eight-bit
1389 else if (this_char_base
< 0)
1391 this_char_base
= inverse
& ~0x3F;
1393 char_base
= this_char_base
;
1394 else if (this_char_base
!= char_base
)
1397 else if ((inverse
& ~0x3F) != this_char_base
)
1401 TRANSLATE (inverse
, inverse_trt
, inverse
);
1406 /* Store this character into the translated pattern. */
1407 memcpy (pat
, str
, charlen
);
1409 base_pat
+= in_charlen
;
1410 len_byte
-= in_charlen
;
1413 /* If char_base is still negative we didn't find any translated
1414 non-ASCII characters. */
1420 /* Unibyte buffer. */
1424 int c
, translated
, inverse
;
1426 /* If we got here and the RE flag is set, it's because we're
1427 dealing with a regexp known to be trivial, so the backslash
1428 just quotes the next character. */
1429 if (RE
&& *base_pat
== '\\')
1436 TRANSLATE (translated
, trt
, c
);
1437 *pat
++ = translated
;
1438 /* Check that none of C's equivalents violates the
1439 assumptions of boyer_moore. */
1440 TRANSLATE (inverse
, inverse_trt
, c
);
1443 if (inverse
>= 0200)
1450 TRANSLATE (inverse
, inverse_trt
, inverse
);
1455 len_byte
= pat
- patbuf
;
1456 pat
= base_pat
= patbuf
;
1459 return boyer_moore (n
, pat
, len_byte
, trt
, inverse_trt
,
1463 return simple_search (n
, pat
, raw_pattern_size
, len_byte
, trt
,
1464 pos
, pos_byte
, lim
, lim_byte
);
1468 /* Do a simple string search N times for the string PAT,
1469 whose length is LEN/LEN_BYTE,
1470 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1471 TRT is the translation table.
1473 Return the character position where the match is found.
1474 Otherwise, if M matches remained to be found, return -M.
1476 This kind of search works regardless of what is in PAT and
1477 regardless of what is in TRT. It is used in cases where
1478 boyer_moore cannot work. */
1481 simple_search (EMACS_INT n
, unsigned char *pat
,
1482 ptrdiff_t len
, ptrdiff_t len_byte
, Lisp_Object trt
,
1483 ptrdiff_t pos
, ptrdiff_t pos_byte
,
1484 ptrdiff_t lim
, ptrdiff_t lim_byte
)
1486 bool multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
1487 bool forward
= n
> 0;
1488 /* Number of buffer bytes matched. Note that this may be different
1489 from len_byte in a multibyte buffer. */
1490 ptrdiff_t match_byte
= PTRDIFF_MIN
;
1492 if (lim
> pos
&& multibyte
)
1497 /* Try matching at position POS. */
1498 ptrdiff_t this_pos
= pos
;
1499 ptrdiff_t this_pos_byte
= pos_byte
;
1500 ptrdiff_t this_len
= len
;
1501 unsigned char *p
= pat
;
1502 if (pos
+ len
> lim
|| pos_byte
+ len_byte
> lim_byte
)
1505 while (this_len
> 0)
1507 int charlen
, buf_charlen
;
1510 pat_ch
= STRING_CHAR_AND_LENGTH (p
, charlen
);
1511 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1513 TRANSLATE (buf_ch
, trt
, buf_ch
);
1515 if (buf_ch
!= pat_ch
)
1521 this_pos_byte
+= buf_charlen
;
1527 match_byte
= this_pos_byte
- pos_byte
;
1529 pos_byte
+= match_byte
;
1533 INC_BOTH (pos
, pos_byte
);
1543 /* Try matching at position POS. */
1544 ptrdiff_t this_pos
= pos
;
1545 ptrdiff_t this_len
= len
;
1546 unsigned char *p
= pat
;
1548 if (pos
+ len
> lim
)
1551 while (this_len
> 0)
1554 int buf_ch
= FETCH_BYTE (this_pos
);
1555 TRANSLATE (buf_ch
, trt
, buf_ch
);
1557 if (buf_ch
!= pat_ch
)
1576 /* Backwards search. */
1577 else if (lim
< pos
&& multibyte
)
1582 /* Try matching at position POS. */
1583 ptrdiff_t this_pos
= pos
;
1584 ptrdiff_t this_pos_byte
= pos_byte
;
1585 ptrdiff_t this_len
= len
;
1586 const unsigned char *p
= pat
+ len_byte
;
1588 if (this_pos
- len
< lim
|| (pos_byte
- len_byte
) < lim_byte
)
1591 while (this_len
> 0)
1595 DEC_BOTH (this_pos
, this_pos_byte
);
1596 PREV_CHAR_BOUNDARY (p
, pat
);
1597 pat_ch
= STRING_CHAR (p
);
1598 buf_ch
= STRING_CHAR (BYTE_POS_ADDR (this_pos_byte
));
1599 TRANSLATE (buf_ch
, trt
, buf_ch
);
1601 if (buf_ch
!= pat_ch
)
1609 match_byte
= pos_byte
- this_pos_byte
;
1611 pos_byte
= this_pos_byte
;
1615 DEC_BOTH (pos
, pos_byte
);
1625 /* Try matching at position POS. */
1626 ptrdiff_t this_pos
= pos
- len
;
1627 ptrdiff_t this_len
= len
;
1628 unsigned char *p
= pat
;
1633 while (this_len
> 0)
1636 int buf_ch
= FETCH_BYTE (this_pos
);
1637 TRANSLATE (buf_ch
, trt
, buf_ch
);
1639 if (buf_ch
!= pat_ch
)
1661 eassert (match_byte
!= PTRDIFF_MIN
);
1663 set_search_regs ((multibyte
? pos_byte
: pos
) - match_byte
, match_byte
);
1665 set_search_regs (multibyte
? pos_byte
: pos
, match_byte
);
1675 /* Do Boyer-Moore search N times for the string BASE_PAT,
1676 whose length is LEN_BYTE,
1677 from buffer position POS_BYTE until LIM_BYTE.
1678 DIRECTION says which direction we search in.
1679 TRT and INVERSE_TRT are translation tables.
1680 Characters in PAT are already translated by TRT.
1682 This kind of search works if all the characters in BASE_PAT that
1683 have nontrivial translation are the same aside from the last byte.
1684 This makes it possible to translate just the last byte of a
1685 character, and do so after just a simple test of the context.
1686 CHAR_BASE is nonzero if there is such a non-ASCII character.
1688 If that criterion is not satisfied, do not call this function. */
1691 boyer_moore (EMACS_INT n
, unsigned char *base_pat
,
1693 Lisp_Object trt
, Lisp_Object inverse_trt
,
1694 ptrdiff_t pos_byte
, ptrdiff_t lim_byte
,
1697 int direction
= ((n
> 0) ? 1 : -1);
1698 register ptrdiff_t dirlen
;
1700 int stride_for_teases
= 0;
1702 register unsigned char *cursor
, *p_limit
;
1703 register ptrdiff_t i
;
1705 unsigned char *pat
, *pat_end
;
1706 bool multibyte
= ! NILP (BVAR (current_buffer
, enable_multibyte_characters
));
1708 unsigned char simple_translate
[0400];
1709 /* These are set to the preceding bytes of a byte to be translated
1710 if char_base is nonzero. As the maximum byte length of a
1711 multibyte character is 5, we have to check at most four previous
1713 int translate_prev_byte1
= 0;
1714 int translate_prev_byte2
= 0;
1715 int translate_prev_byte3
= 0;
1717 /* The general approach is that we are going to maintain that we know
1718 the first (closest to the present position, in whatever direction
1719 we're searching) character that could possibly be the last
1720 (furthest from present position) character of a valid match. We
1721 advance the state of our knowledge by looking at that character
1722 and seeing whether it indeed matches the last character of the
1723 pattern. If it does, we take a closer look. If it does not, we
1724 move our pointer (to putative last characters) as far as is
1725 logically possible. This amount of movement, which I call a
1726 stride, will be the length of the pattern if the actual character
1727 appears nowhere in the pattern, otherwise it will be the distance
1728 from the last occurrence of that character to the end of the
1729 pattern. If the amount is zero we have a possible match. */
1731 /* Here we make a "mickey mouse" BM table. The stride of the search
1732 is determined only by the last character of the putative match.
1733 If that character does not match, we will stride the proper
1734 distance to propose a match that superimposes it on the last
1735 instance of a character that matches it (per trt), or misses
1736 it entirely if there is none. */
1738 dirlen
= len_byte
* direction
;
1740 /* Record position after the end of the pattern. */
1741 pat_end
= base_pat
+ len_byte
;
1742 /* BASE_PAT points to a character that we start scanning from.
1743 It is the first character in a forward search,
1744 the last character in a backward search. */
1746 base_pat
= pat_end
- 1;
1748 /* A character that does not appear in the pattern induces a
1749 stride equal to the pattern length. */
1750 for (i
= 0; i
< 0400; i
++)
1753 /* We use this for translation, instead of TRT itself.
1754 We fill this in to handle the characters that actually
1755 occur in the pattern. Others don't matter anyway! */
1756 for (i
= 0; i
< 0400; i
++)
1757 simple_translate
[i
] = i
;
1761 /* Setup translate_prev_byte1/2/3/4 from CHAR_BASE. Only a
1762 byte following them are the target of translation. */
1763 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1764 int cblen
= CHAR_STRING (char_base
, str
);
1766 translate_prev_byte1
= str
[cblen
- 2];
1769 translate_prev_byte2
= str
[cblen
- 3];
1771 translate_prev_byte3
= str
[cblen
- 4];
1778 unsigned char *ptr
= base_pat
+ i
;
1782 /* If the byte currently looking at is the last of a
1783 character to check case-equivalents, set CH to that
1784 character. An ASCII character and a non-ASCII character
1785 matching with CHAR_BASE are to be checked. */
1788 if (ASCII_BYTE_P (*ptr
) || ! multibyte
)
1791 && ((pat_end
- ptr
) == 1 || CHAR_HEAD_P (ptr
[1])))
1793 unsigned char *charstart
= ptr
- 1;
1795 while (! (CHAR_HEAD_P (*charstart
)))
1797 ch
= STRING_CHAR (charstart
);
1798 if (char_base
!= (ch
& ~0x3F))
1802 if (ch
>= 0200 && multibyte
)
1803 j
= (ch
& 0x3F) | 0200;
1808 stride_for_teases
= BM_tab
[j
];
1810 BM_tab
[j
] = dirlen
- i
;
1811 /* A translation table is accompanied by its inverse -- see
1812 comment following downcase_table for details. */
1815 int starting_ch
= ch
;
1820 TRANSLATE (ch
, inverse_trt
, ch
);
1821 if (ch
>= 0200 && multibyte
)
1822 j
= (ch
& 0x3F) | 0200;
1826 /* For all the characters that map into CH,
1827 set up simple_translate to map the last byte
1829 simple_translate
[j
] = starting_j
;
1830 if (ch
== starting_ch
)
1832 BM_tab
[j
] = dirlen
- i
;
1841 stride_for_teases
= BM_tab
[j
];
1842 BM_tab
[j
] = dirlen
- i
;
1844 /* stride_for_teases tells how much to stride if we get a
1845 match on the far character but are subsequently
1846 disappointed, by recording what the stride would have been
1847 for that character if the last character had been
1850 pos_byte
+= dirlen
- ((direction
> 0) ? direction
: 0);
1851 /* loop invariant - POS_BYTE points at where last char (first
1852 char if reverse) of pattern would align in a possible match. */
1856 unsigned char *tail_end_ptr
;
1858 /* It's been reported that some (broken) compiler thinks that
1859 Boolean expressions in an arithmetic context are unsigned.
1860 Using an explicit ?1:0 prevents this. */
1861 if ((lim_byte
- pos_byte
- ((direction
> 0) ? 1 : 0)) * direction
1863 return (n
* (0 - direction
));
1864 /* First we do the part we can by pointers (maybe nothing) */
1867 limit
= pos_byte
- dirlen
+ direction
;
1870 limit
= BUFFER_CEILING_OF (limit
);
1871 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1872 can take on without hitting edge of buffer or the gap. */
1873 limit
= min (limit
, pos_byte
+ 20000);
1874 limit
= min (limit
, lim_byte
- 1);
1878 limit
= BUFFER_FLOOR_OF (limit
);
1879 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1880 can take on without hitting edge of buffer or the gap. */
1881 limit
= max (limit
, pos_byte
- 20000);
1882 limit
= max (limit
, lim_byte
);
1884 tail_end
= BUFFER_CEILING_OF (pos_byte
) + 1;
1885 tail_end_ptr
= BYTE_POS_ADDR (tail_end
);
1887 if ((limit
- pos_byte
) * direction
> 20)
1891 p_limit
= BYTE_POS_ADDR (limit
);
1892 p2
= (cursor
= BYTE_POS_ADDR (pos_byte
));
1893 /* In this loop, pos + cursor - p2 is the surrogate for pos. */
1894 while (1) /* use one cursor setting as long as i can */
1896 if (direction
> 0) /* worth duplicating */
1898 while (cursor
<= p_limit
)
1900 if (BM_tab
[*cursor
] == 0)
1902 cursor
+= BM_tab
[*cursor
];
1907 while (cursor
>= p_limit
)
1909 if (BM_tab
[*cursor
] == 0)
1911 cursor
+= BM_tab
[*cursor
];
1914 /* If you are here, cursor is beyond the end of the
1915 searched region. You fail to match within the
1916 permitted region and would otherwise try a character
1917 beyond that region. */
1921 i
= dirlen
- direction
;
1924 while ((i
-= direction
) + direction
!= 0)
1927 cursor
-= direction
;
1928 /* Translate only the last byte of a character. */
1930 || ((cursor
== tail_end_ptr
1931 || CHAR_HEAD_P (cursor
[1]))
1932 && (CHAR_HEAD_P (cursor
[0])
1933 /* Check if this is the last byte of
1934 a translatable character. */
1935 || (translate_prev_byte1
== cursor
[-1]
1936 && (CHAR_HEAD_P (translate_prev_byte1
)
1937 || (translate_prev_byte2
== cursor
[-2]
1938 && (CHAR_HEAD_P (translate_prev_byte2
)
1939 || (translate_prev_byte3
== cursor
[-3]))))))))
1940 ch
= simple_translate
[*cursor
];
1949 while ((i
-= direction
) + direction
!= 0)
1951 cursor
-= direction
;
1952 if (pat
[i
] != *cursor
)
1956 cursor
+= dirlen
- i
- direction
; /* fix cursor */
1957 if (i
+ direction
== 0)
1959 ptrdiff_t position
, start
, end
;
1961 cursor
-= direction
;
1963 position
= pos_byte
+ cursor
- p2
+ ((direction
> 0)
1964 ? 1 - len_byte
: 0);
1965 set_search_regs (position
, len_byte
);
1967 if (NILP (Vinhibit_changing_match_data
))
1969 start
= search_regs
.start
[0];
1970 end
= search_regs
.end
[0];
1973 /* If Vinhibit_changing_match_data is non-nil,
1974 search_regs will not be changed. So let's
1975 compute start and end here. */
1977 start
= BYTE_TO_CHAR (position
);
1978 end
= BYTE_TO_CHAR (position
+ len_byte
);
1981 if ((n
-= direction
) != 0)
1982 cursor
+= dirlen
; /* to resume search */
1984 return direction
> 0 ? end
: start
;
1987 cursor
+= stride_for_teases
; /* <sigh> we lose - */
1989 pos_byte
+= cursor
- p2
;
1992 /* Now we'll pick up a clump that has to be done the hard
1993 way because it covers a discontinuity. */
1995 limit
= ((direction
> 0)
1996 ? BUFFER_CEILING_OF (pos_byte
- dirlen
+ 1)
1997 : BUFFER_FLOOR_OF (pos_byte
- dirlen
- 1));
1998 limit
= ((direction
> 0)
1999 ? min (limit
+ len_byte
, lim_byte
- 1)
2000 : max (limit
- len_byte
, lim_byte
));
2001 /* LIMIT is now the last value POS_BYTE can have
2002 and still be valid for a possible match. */
2005 /* This loop can be coded for space rather than
2006 speed because it will usually run only once.
2007 (the reach is at most len + 21, and typically
2008 does not exceed len). */
2009 while ((limit
- pos_byte
) * direction
>= 0)
2011 int ch
= FETCH_BYTE (pos_byte
);
2012 if (BM_tab
[ch
] == 0)
2014 pos_byte
+= BM_tab
[ch
];
2016 break; /* ran off the end */
2019 /* Found what might be a match. */
2020 i
= dirlen
- direction
;
2021 while ((i
-= direction
) + direction
!= 0)
2025 pos_byte
-= direction
;
2026 ptr
= BYTE_POS_ADDR (pos_byte
);
2027 /* Translate only the last byte of a character. */
2029 || ((ptr
== tail_end_ptr
2030 || CHAR_HEAD_P (ptr
[1]))
2031 && (CHAR_HEAD_P (ptr
[0])
2032 /* Check if this is the last byte of a
2033 translatable character. */
2034 || (translate_prev_byte1
== ptr
[-1]
2035 && (CHAR_HEAD_P (translate_prev_byte1
)
2036 || (translate_prev_byte2
== ptr
[-2]
2037 && (CHAR_HEAD_P (translate_prev_byte2
)
2038 || translate_prev_byte3
== ptr
[-3])))))))
2039 ch
= simple_translate
[*ptr
];
2045 /* Above loop has moved POS_BYTE part or all the way
2046 back to the first pos (last pos if reverse).
2047 Set it once again at the last (first if reverse) char. */
2048 pos_byte
+= dirlen
- i
- direction
;
2049 if (i
+ direction
== 0)
2051 ptrdiff_t position
, start
, end
;
2052 pos_byte
-= direction
;
2054 position
= pos_byte
+ ((direction
> 0) ? 1 - len_byte
: 0);
2055 set_search_regs (position
, len_byte
);
2057 if (NILP (Vinhibit_changing_match_data
))
2059 start
= search_regs
.start
[0];
2060 end
= search_regs
.end
[0];
2063 /* If Vinhibit_changing_match_data is non-nil,
2064 search_regs will not be changed. So let's
2065 compute start and end here. */
2067 start
= BYTE_TO_CHAR (position
);
2068 end
= BYTE_TO_CHAR (position
+ len_byte
);
2071 if ((n
-= direction
) != 0)
2072 pos_byte
+= dirlen
; /* to resume search */
2074 return direction
> 0 ? end
: start
;
2077 pos_byte
+= stride_for_teases
;
2080 /* We have done one clump. Can we continue? */
2081 if ((lim_byte
- pos_byte
) * direction
< 0)
2082 return ((0 - n
) * direction
);
2084 return BYTE_TO_CHAR (pos_byte
);
2087 /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES
2088 for the overall match just found in the current buffer.
2089 Also clear out the match data for registers 1 and up. */
2092 set_search_regs (ptrdiff_t beg_byte
, ptrdiff_t nbytes
)
2096 if (!NILP (Vinhibit_changing_match_data
))
2099 /* Make sure we have registers in which to store
2100 the match position. */
2101 if (search_regs
.num_regs
== 0)
2103 search_regs
.start
= xmalloc (2 * sizeof (regoff_t
));
2104 search_regs
.end
= xmalloc (2 * sizeof (regoff_t
));
2105 search_regs
.num_regs
= 2;
2108 /* Clear out the other registers. */
2109 for (i
= 1; i
< search_regs
.num_regs
; i
++)
2111 search_regs
.start
[i
] = -1;
2112 search_regs
.end
[i
] = -1;
2115 search_regs
.start
[0] = BYTE_TO_CHAR (beg_byte
);
2116 search_regs
.end
[0] = BYTE_TO_CHAR (beg_byte
+ nbytes
);
2117 XSETBUFFER (last_thing_searched
, current_buffer
);
2120 DEFUN ("search-backward", Fsearch_backward
, Ssearch_backward
, 1, 4,
2121 "MSearch backward: ",
2122 doc
: /* Search backward from point for STRING.
2123 Set point to the beginning of the occurrence found, and return point.
2124 An optional second argument bounds the search; it is a buffer position.
2125 The match found must not extend before that position.
2126 Optional third argument, if t, means if fail just return nil (no error).
2127 If not nil and not t, position at limit of search and return nil.
2128 Optional fourth argument COUNT, if non-nil, means to search for COUNT
2129 successive occurrences. If COUNT is negative, search forward,
2130 instead of backward, for -COUNT occurrences.
2132 Search case-sensitivity is determined by the value of the variable
2133 `case-fold-search', which see.
2135 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2136 (Lisp_Object string
, Lisp_Object bound
, Lisp_Object noerror
, Lisp_Object count
)
2138 return search_command (string
, bound
, noerror
, count
, -1, 0, 0);
2141 DEFUN ("search-forward", Fsearch_forward
, Ssearch_forward
, 1, 4, "MSearch: ",
2142 doc
: /* Search forward from point for STRING.
2143 Set point to the end of the occurrence found, and return point.
2144 An optional second argument bounds the search; it is a buffer position.
2145 The match found must not extend after that position. A value of nil is
2146 equivalent to (point-max).
2147 Optional third argument, if t, means if fail just return nil (no error).
2148 If not nil and not t, move to limit of search and return nil.
2149 Optional fourth argument COUNT, if non-nil, means to search for COUNT
2150 successive occurrences. If COUNT is negative, search backward,
2151 instead of forward, for -COUNT occurrences.
2153 Search case-sensitivity is determined by the value of the variable
2154 `case-fold-search', which see.
2156 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2157 (Lisp_Object string
, Lisp_Object bound
, Lisp_Object noerror
, Lisp_Object count
)
2159 return search_command (string
, bound
, noerror
, count
, 1, 0, 0);
2162 DEFUN ("re-search-backward", Fre_search_backward
, Sre_search_backward
, 1, 4,
2163 "sRE search backward: ",
2164 doc
: /* Search backward from point for match for regular expression REGEXP.
2165 Set point to the beginning of the match, and return point.
2166 The match found is the one starting last in the buffer
2167 and yet ending before the origin of the search.
2168 An optional second argument bounds the search; it is a buffer position.
2169 The match found must start at or after that position.
2170 Optional third argument, if t, means if fail just return nil (no error).
2171 If not nil and not t, move to limit of search and return nil.
2172 Optional fourth argument is repeat count--search for successive occurrences.
2174 Search case-sensitivity is determined by the value of the variable
2175 `case-fold-search', which see.
2177 See also the functions `match-beginning', `match-end', `match-string',
2178 and `replace-match'. */)
2179 (Lisp_Object regexp
, Lisp_Object bound
, Lisp_Object noerror
, Lisp_Object count
)
2181 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 0);
2184 DEFUN ("re-search-forward", Fre_search_forward
, Sre_search_forward
, 1, 4,
2186 doc
: /* Search forward from point for regular expression REGEXP.
2187 Set point to the end of the occurrence found, and return point.
2188 An optional second argument bounds the search; it is a buffer position.
2189 The match found must not extend after that position.
2190 Optional third argument, if t, means if fail just return nil (no error).
2191 If not nil and not t, move to limit of search and return nil.
2192 Optional fourth argument is repeat count--search for successive occurrences.
2194 Search case-sensitivity is determined by the value of the variable
2195 `case-fold-search', which see.
2197 See also the functions `match-beginning', `match-end', `match-string',
2198 and `replace-match'. */)
2199 (Lisp_Object regexp
, Lisp_Object bound
, Lisp_Object noerror
, Lisp_Object count
)
2201 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 0);
2204 DEFUN ("posix-search-backward", Fposix_search_backward
, Sposix_search_backward
, 1, 4,
2205 "sPosix search backward: ",
2206 doc
: /* Search backward from point for match for regular expression REGEXP.
2207 Find the longest match in accord with Posix regular expression rules.
2208 Set point to the beginning of the match, and return point.
2209 The match found is the one starting last in the buffer
2210 and yet ending before the origin of the search.
2211 An optional second argument bounds the search; it is a buffer position.
2212 The match found must start at or after that position.
2213 Optional third argument, if t, means if fail just return nil (no error).
2214 If not nil and not t, move to limit of search and return nil.
2215 Optional fourth argument is repeat count--search for successive occurrences.
2217 Search case-sensitivity is determined by the value of the variable
2218 `case-fold-search', which see.
2220 See also the functions `match-beginning', `match-end', `match-string',
2221 and `replace-match'. */)
2222 (Lisp_Object regexp
, Lisp_Object bound
, Lisp_Object noerror
, Lisp_Object count
)
2224 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 1);
2227 DEFUN ("posix-search-forward", Fposix_search_forward
, Sposix_search_forward
, 1, 4,
2229 doc
: /* Search forward from point for regular expression REGEXP.
2230 Find the longest match in accord with Posix regular expression rules.
2231 Set point to the end of the occurrence found, and return point.
2232 An optional second argument bounds the search; it is a buffer position.
2233 The match found must not extend after that position.
2234 Optional third argument, if t, means if fail just return nil (no error).
2235 If not nil and not t, move to limit of search and return nil.
2236 Optional fourth argument is repeat count--search for successive occurrences.
2238 Search case-sensitivity is determined by the value of the variable
2239 `case-fold-search', which see.
2241 See also the functions `match-beginning', `match-end', `match-string',
2242 and `replace-match'. */)
2243 (Lisp_Object regexp
, Lisp_Object bound
, Lisp_Object noerror
, Lisp_Object count
)
2245 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 1);
2248 DEFUN ("replace-match", Freplace_match
, Sreplace_match
, 1, 5, 0,
2249 doc
: /* Replace text matched by last search with NEWTEXT.
2250 Leave point at the end of the replacement text.
2252 If optional second arg FIXEDCASE is non-nil, do not alter the case of
2253 the replacement text. Otherwise, maybe capitalize the whole text, or
2254 maybe just word initials, based on the replaced text. If the replaced
2255 text has only capital letters and has at least one multiletter word,
2256 convert NEWTEXT to all caps. Otherwise if all words are capitalized
2257 in the replaced text, capitalize each word in NEWTEXT.
2259 If optional third arg LITERAL is non-nil, insert NEWTEXT literally.
2260 Otherwise treat `\\' as special:
2261 `\\&' in NEWTEXT means substitute original matched text.
2262 `\\N' means substitute what matched the Nth `\\(...\\)'.
2263 If Nth parens didn't match, substitute nothing.
2264 `\\\\' means insert one `\\'.
2265 `\\?' is treated literally
2266 (for compatibility with `query-replace-regexp').
2267 Any other character following `\\' signals an error.
2268 Case conversion does not apply to these substitutions.
2270 If optional fourth argument STRING is non-nil, it should be a string
2271 to act on; this should be the string on which the previous match was
2272 done via `string-match'. In this case, `replace-match' creates and
2273 returns a new string, made by copying STRING and replacing the part of
2274 STRING that was matched (the original STRING itself is not altered).
2276 The optional fifth argument SUBEXP specifies a subexpression;
2277 it says to replace just that subexpression with NEWTEXT,
2278 rather than replacing the entire matched text.
2279 This is, in a vague sense, the inverse of using `\\N' in NEWTEXT;
2280 `\\N' copies subexp N into NEWTEXT, but using N as SUBEXP puts
2281 NEWTEXT in place of subexp N.
2282 This is useful only after a regular expression search or match,
2283 since only regular expressions have distinguished subexpressions. */)
2284 (Lisp_Object newtext
, Lisp_Object fixedcase
, Lisp_Object literal
, Lisp_Object string
, Lisp_Object subexp
)
2286 enum { nochange
, all_caps
, cap_initial
} case_action
;
2287 ptrdiff_t pos
, pos_byte
;
2288 bool some_multiletter_word
;
2289 bool some_lowercase
;
2290 bool some_uppercase
;
2291 bool some_nonuppercase_initial
;
2294 ptrdiff_t opoint
, newpoint
;
2296 CHECK_STRING (newtext
);
2298 if (! NILP (string
))
2299 CHECK_STRING (string
);
2301 case_action
= nochange
; /* We tried an initialization */
2302 /* but some C compilers blew it */
2304 if (search_regs
.num_regs
<= 0)
2305 error ("`replace-match' called before any match found");
2311 CHECK_NUMBER (subexp
);
2312 if (! (0 <= XINT (subexp
) && XINT (subexp
) < search_regs
.num_regs
))
2313 args_out_of_range (subexp
, make_number (search_regs
.num_regs
));
2314 sub
= XINT (subexp
);
2319 if (search_regs
.start
[sub
] < BEGV
2320 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2321 || search_regs
.end
[sub
] > ZV
)
2322 args_out_of_range (make_number (search_regs
.start
[sub
]),
2323 make_number (search_regs
.end
[sub
]));
2327 if (search_regs
.start
[sub
] < 0
2328 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2329 || search_regs
.end
[sub
] > SCHARS (string
))
2330 args_out_of_range (make_number (search_regs
.start
[sub
]),
2331 make_number (search_regs
.end
[sub
]));
2334 if (NILP (fixedcase
))
2336 /* Decide how to casify by examining the matched text. */
2339 pos
= search_regs
.start
[sub
];
2340 last
= search_regs
.end
[sub
];
2343 pos_byte
= CHAR_TO_BYTE (pos
);
2345 pos_byte
= string_char_to_byte (string
, pos
);
2348 case_action
= all_caps
;
2350 /* some_multiletter_word is set nonzero if any original word
2351 is more than one letter long. */
2352 some_multiletter_word
= 0;
2354 some_nonuppercase_initial
= 0;
2361 c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
);
2362 INC_BOTH (pos
, pos_byte
);
2365 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, pos
, pos_byte
);
2369 /* Cannot be all caps if any original char is lower case */
2372 if (SYNTAX (prevc
) != Sword
)
2373 some_nonuppercase_initial
= 1;
2375 some_multiletter_word
= 1;
2377 else if (uppercasep (c
))
2380 if (SYNTAX (prevc
) != Sword
)
2383 some_multiletter_word
= 1;
2387 /* If the initial is a caseless word constituent,
2388 treat that like a lowercase initial. */
2389 if (SYNTAX (prevc
) != Sword
)
2390 some_nonuppercase_initial
= 1;
2396 /* Convert to all caps if the old text is all caps
2397 and has at least one multiletter word. */
2398 if (! some_lowercase
&& some_multiletter_word
)
2399 case_action
= all_caps
;
2400 /* Capitalize each word, if the old text has all capitalized words. */
2401 else if (!some_nonuppercase_initial
&& some_multiletter_word
)
2402 case_action
= cap_initial
;
2403 else if (!some_nonuppercase_initial
&& some_uppercase
)
2404 /* Should x -> yz, operating on X, give Yz or YZ?
2405 We'll assume the latter. */
2406 case_action
= all_caps
;
2408 case_action
= nochange
;
2411 /* Do replacement in a string. */
2414 Lisp_Object before
, after
;
2416 before
= Fsubstring (string
, make_number (0),
2417 make_number (search_regs
.start
[sub
]));
2418 after
= Fsubstring (string
, make_number (search_regs
.end
[sub
]), Qnil
);
2420 /* Substitute parts of the match into NEWTEXT
2424 ptrdiff_t lastpos
= 0;
2425 ptrdiff_t lastpos_byte
= 0;
2426 /* We build up the substituted string in ACCUM. */
2429 ptrdiff_t length
= SBYTES (newtext
);
2433 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2435 ptrdiff_t substart
= -1;
2436 ptrdiff_t subend
= 0;
2437 bool delbackslash
= 0;
2439 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2443 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2447 substart
= search_regs
.start
[sub
];
2448 subend
= search_regs
.end
[sub
];
2450 else if (c
>= '1' && c
<= '9')
2452 if (c
- '0' < search_regs
.num_regs
2453 && 0 <= search_regs
.start
[c
- '0'])
2455 substart
= search_regs
.start
[c
- '0'];
2456 subend
= search_regs
.end
[c
- '0'];
2460 /* If that subexp did not match,
2461 replace \\N with nothing. */
2469 error ("Invalid use of `\\' in replacement text");
2473 if (pos
- 2 != lastpos
)
2474 middle
= substring_both (newtext
, lastpos
,
2476 pos
- 2, pos_byte
- 2);
2479 accum
= concat3 (accum
, middle
,
2481 make_number (substart
),
2482 make_number (subend
)));
2484 lastpos_byte
= pos_byte
;
2486 else if (delbackslash
)
2488 middle
= substring_both (newtext
, lastpos
,
2490 pos
- 1, pos_byte
- 1);
2492 accum
= concat2 (accum
, middle
);
2494 lastpos_byte
= pos_byte
;
2499 middle
= substring_both (newtext
, lastpos
,
2505 newtext
= concat2 (accum
, middle
);
2508 /* Do case substitution in NEWTEXT if desired. */
2509 if (case_action
== all_caps
)
2510 newtext
= Fupcase (newtext
);
2511 else if (case_action
== cap_initial
)
2512 newtext
= Fupcase_initials (newtext
);
2514 return concat3 (before
, newtext
, after
);
2517 /* Record point, then move (quietly) to the start of the match. */
2518 if (PT
>= search_regs
.end
[sub
])
2520 else if (PT
> search_regs
.start
[sub
])
2521 opoint
= search_regs
.end
[sub
] - ZV
;
2525 /* If we want non-literal replacement,
2526 perform substitution on the replacement string. */
2529 ptrdiff_t length
= SBYTES (newtext
);
2530 unsigned char *substed
;
2531 ptrdiff_t substed_alloc_size
, substed_len
;
2532 bool buf_multibyte
= !NILP (BVAR (current_buffer
, enable_multibyte_characters
));
2533 bool str_multibyte
= STRING_MULTIBYTE (newtext
);
2534 bool really_changed
= 0;
2536 substed_alloc_size
= ((STRING_BYTES_BOUND
- 100) / 2 < length
2537 ? STRING_BYTES_BOUND
2538 : length
* 2 + 100);
2539 substed
= xmalloc (substed_alloc_size
);
2542 /* Go thru NEWTEXT, producing the actual text to insert in
2543 SUBSTED while adjusting multibyteness to that of the current
2546 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2548 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2549 const unsigned char *add_stuff
= NULL
;
2550 ptrdiff_t add_len
= 0;
2555 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
, pos
, pos_byte
);
2557 c
= multibyte_char_to_unibyte (c
);
2561 /* Note that we don't have to increment POS. */
2562 c
= SREF (newtext
, pos_byte
++);
2564 MAKE_CHAR_MULTIBYTE (c
);
2567 /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
2568 or set IDX to a match index, which means put that part
2569 of the buffer text into SUBSTED. */
2577 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
,
2579 if (!buf_multibyte
&& !ASCII_CHAR_P (c
))
2580 c
= multibyte_char_to_unibyte (c
);
2584 c
= SREF (newtext
, pos_byte
++);
2586 MAKE_CHAR_MULTIBYTE (c
);
2591 else if (c
>= '1' && c
<= '9' && c
- '0' < search_regs
.num_regs
)
2593 if (search_regs
.start
[c
- '0'] >= 1)
2597 add_len
= 1, add_stuff
= (unsigned char *) "\\";
2601 error ("Invalid use of `\\' in replacement text");
2606 add_len
= CHAR_STRING (c
, str
);
2610 /* If we want to copy part of a previous match,
2611 set up ADD_STUFF and ADD_LEN to point to it. */
2614 ptrdiff_t begbyte
= CHAR_TO_BYTE (search_regs
.start
[idx
]);
2615 add_len
= CHAR_TO_BYTE (search_regs
.end
[idx
]) - begbyte
;
2616 if (search_regs
.start
[idx
] < GPT
&& GPT
< search_regs
.end
[idx
])
2617 move_gap_both (search_regs
.start
[idx
], begbyte
);
2618 add_stuff
= BYTE_POS_ADDR (begbyte
);
2621 /* Now the stuff we want to add to SUBSTED
2622 is invariably ADD_LEN bytes starting at ADD_STUFF. */
2624 /* Make sure SUBSTED is big enough. */
2625 if (substed_alloc_size
- substed_len
< add_len
)
2627 xpalloc (substed
, &substed_alloc_size
,
2628 add_len
- (substed_alloc_size
- substed_len
),
2629 STRING_BYTES_BOUND
, 1);
2631 /* Now add to the end of SUBSTED. */
2634 memcpy (substed
+ substed_len
, add_stuff
, add_len
);
2635 substed_len
+= add_len
;
2644 multibyte_chars_in_text (substed
, substed_len
);
2646 newtext
= make_multibyte_string ((char *) substed
, nchars
,
2650 newtext
= make_unibyte_string ((char *) substed
, substed_len
);
2655 /* Replace the old text with the new in the cleanest possible way. */
2656 replace_range (search_regs
.start
[sub
], search_regs
.end
[sub
],
2658 newpoint
= search_regs
.start
[sub
] + SCHARS (newtext
);
2660 if (case_action
== all_caps
)
2661 Fupcase_region (make_number (search_regs
.start
[sub
]),
2662 make_number (newpoint
));
2663 else if (case_action
== cap_initial
)
2664 Fupcase_initials_region (make_number (search_regs
.start
[sub
]),
2665 make_number (newpoint
));
2667 /* Adjust search data for this change. */
2669 ptrdiff_t oldend
= search_regs
.end
[sub
];
2670 ptrdiff_t oldstart
= search_regs
.start
[sub
];
2671 ptrdiff_t change
= newpoint
- search_regs
.end
[sub
];
2674 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2676 if (search_regs
.start
[i
] >= oldend
)
2677 search_regs
.start
[i
] += change
;
2678 else if (search_regs
.start
[i
] > oldstart
)
2679 search_regs
.start
[i
] = oldstart
;
2680 if (search_regs
.end
[i
] >= oldend
)
2681 search_regs
.end
[i
] += change
;
2682 else if (search_regs
.end
[i
] > oldstart
)
2683 search_regs
.end
[i
] = oldstart
;
2687 /* Put point back where it was in the text. */
2689 TEMP_SET_PT (opoint
+ ZV
);
2691 TEMP_SET_PT (opoint
);
2693 /* Now move point "officially" to the start of the inserted replacement. */
2694 move_if_not_intangible (newpoint
);
2700 match_limit (Lisp_Object num
, bool beginningp
)
2707 args_out_of_range (num
, make_number (0));
2708 if (search_regs
.num_regs
<= 0)
2709 error ("No match data, because no search succeeded");
2710 if (n
>= search_regs
.num_regs
2711 || search_regs
.start
[n
] < 0)
2713 return (make_number ((beginningp
) ? search_regs
.start
[n
]
2714 : search_regs
.end
[n
]));
2717 DEFUN ("match-beginning", Fmatch_beginning
, Smatch_beginning
, 1, 1, 0,
2718 doc
: /* Return position of start of text matched by last search.
2719 SUBEXP, a number, specifies which parenthesized expression in the last
2721 Value is nil if SUBEXPth pair didn't match, or there were less than
2723 Zero means the entire text matched by the whole regexp or whole string. */)
2724 (Lisp_Object subexp
)
2726 return match_limit (subexp
, 1);
2729 DEFUN ("match-end", Fmatch_end
, Smatch_end
, 1, 1, 0,
2730 doc
: /* Return position of end of text matched by last search.
2731 SUBEXP, a number, specifies which parenthesized expression in the last
2733 Value is nil if SUBEXPth pair didn't match, or there were less than
2735 Zero means the entire text matched by the whole regexp or whole string. */)
2736 (Lisp_Object subexp
)
2738 return match_limit (subexp
, 0);
2741 DEFUN ("match-data", Fmatch_data
, Smatch_data
, 0, 3, 0,
2742 doc
: /* Return a list containing all info on what the last search matched.
2743 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
2744 All the elements are markers or nil (nil if the Nth pair didn't match)
2745 if the last match was on a buffer; integers or nil if a string was matched.
2746 Use `set-match-data' to reinstate the data in this list.
2748 If INTEGERS (the optional first argument) is non-nil, always use
2749 integers \(rather than markers) to represent buffer positions. In
2750 this case, and if the last match was in a buffer, the buffer will get
2751 stored as one additional element at the end of the list.
2753 If REUSE is a list, reuse it as part of the value. If REUSE is long
2754 enough to hold all the values, and if INTEGERS is non-nil, no consing
2757 If optional third arg RESEAT is non-nil, any previous markers on the
2758 REUSE list will be modified to point to nowhere.
2760 Return value is undefined if the last search failed. */)
2761 (Lisp_Object integers
, Lisp_Object reuse
, Lisp_Object reseat
)
2763 Lisp_Object tail
, prev
;
2768 for (tail
= reuse
; CONSP (tail
); tail
= XCDR (tail
))
2769 if (MARKERP (XCAR (tail
)))
2771 unchain_marker (XMARKER (XCAR (tail
)));
2772 XSETCAR (tail
, Qnil
);
2775 if (NILP (last_thing_searched
))
2780 data
= alloca ((2 * search_regs
.num_regs
+ 1) * sizeof *data
);
2783 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2785 ptrdiff_t start
= search_regs
.start
[i
];
2788 if (EQ (last_thing_searched
, Qt
)
2789 || ! NILP (integers
))
2791 XSETFASTINT (data
[2 * i
], start
);
2792 XSETFASTINT (data
[2 * i
+ 1], search_regs
.end
[i
]);
2794 else if (BUFFERP (last_thing_searched
))
2796 data
[2 * i
] = Fmake_marker ();
2797 Fset_marker (data
[2 * i
],
2798 make_number (start
),
2799 last_thing_searched
);
2800 data
[2 * i
+ 1] = Fmake_marker ();
2801 Fset_marker (data
[2 * i
+ 1],
2802 make_number (search_regs
.end
[i
]),
2803 last_thing_searched
);
2806 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
2812 data
[2 * i
] = data
[2 * i
+ 1] = Qnil
;
2815 if (BUFFERP (last_thing_searched
) && !NILP (integers
))
2817 data
[len
] = last_thing_searched
;
2821 /* If REUSE is not usable, cons up the values and return them. */
2822 if (! CONSP (reuse
))
2823 return Flist (len
, data
);
2825 /* If REUSE is a list, store as many value elements as will fit
2826 into the elements of REUSE. */
2827 for (i
= 0, tail
= reuse
; CONSP (tail
);
2828 i
++, tail
= XCDR (tail
))
2831 XSETCAR (tail
, data
[i
]);
2833 XSETCAR (tail
, Qnil
);
2837 /* If we couldn't fit all value elements into REUSE,
2838 cons up the rest of them and add them to the end of REUSE. */
2840 XSETCDR (prev
, Flist (len
- i
, data
+ i
));
2845 /* We used to have an internal use variant of `reseat' described as:
2847 If RESEAT is `evaporate', put the markers back on the free list
2848 immediately. No other references to the markers must exist in this
2849 case, so it is used only internally on the unwind stack and
2850 save-match-data from Lisp.
2852 But it was ill-conceived: those supposedly-internal markers get exposed via
2853 the undo-list, so freeing them here is unsafe. */
2855 DEFUN ("set-match-data", Fset_match_data
, Sset_match_data
, 1, 2, 0,
2856 doc
: /* Set internal data on last search match from elements of LIST.
2857 LIST should have been created by calling `match-data' previously.
2859 If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */)
2860 (register Lisp_Object list
, Lisp_Object reseat
)
2863 register Lisp_Object marker
;
2865 if (running_asynch_code
)
2866 save_search_regs ();
2870 /* Unless we find a marker with a buffer or an explicit buffer
2871 in LIST, assume that this match data came from a string. */
2872 last_thing_searched
= Qt
;
2874 /* Allocate registers if they don't already exist. */
2876 EMACS_INT length
= XFASTINT (Flength (list
)) / 2;
2878 if (length
> search_regs
.num_regs
)
2880 ptrdiff_t num_regs
= search_regs
.num_regs
;
2881 if (PTRDIFF_MAX
< length
)
2882 memory_full (SIZE_MAX
);
2884 xpalloc (search_regs
.start
, &num_regs
, length
- num_regs
,
2885 min (PTRDIFF_MAX
, UINT_MAX
), sizeof (regoff_t
));
2887 xrealloc (search_regs
.end
, num_regs
* sizeof (regoff_t
));
2889 for (i
= search_regs
.num_regs
; i
< num_regs
; i
++)
2890 search_regs
.start
[i
] = -1;
2892 search_regs
.num_regs
= num_regs
;
2895 for (i
= 0; CONSP (list
); i
++)
2897 marker
= XCAR (list
);
2898 if (BUFFERP (marker
))
2900 last_thing_searched
= marker
;
2907 search_regs
.start
[i
] = -1;
2916 if (MARKERP (marker
))
2918 if (XMARKER (marker
)->buffer
== 0)
2919 XSETFASTINT (marker
, 0);
2921 XSETBUFFER (last_thing_searched
, XMARKER (marker
)->buffer
);
2924 CHECK_NUMBER_COERCE_MARKER (marker
);
2927 if (!NILP (reseat
) && MARKERP (m
))
2929 unchain_marker (XMARKER (m
));
2930 XSETCAR (list
, Qnil
);
2933 if ((list
= XCDR (list
), !CONSP (list
)))
2936 m
= marker
= XCAR (list
);
2938 if (MARKERP (marker
) && XMARKER (marker
)->buffer
== 0)
2939 XSETFASTINT (marker
, 0);
2941 CHECK_NUMBER_COERCE_MARKER (marker
);
2942 if ((XINT (from
) < 0
2943 ? TYPE_MINIMUM (regoff_t
) <= XINT (from
)
2944 : XINT (from
) <= TYPE_MAXIMUM (regoff_t
))
2945 && (XINT (marker
) < 0
2946 ? TYPE_MINIMUM (regoff_t
) <= XINT (marker
)
2947 : XINT (marker
) <= TYPE_MAXIMUM (regoff_t
)))
2949 search_regs
.start
[i
] = XINT (from
);
2950 search_regs
.end
[i
] = XINT (marker
);
2954 search_regs
.start
[i
] = -1;
2957 if (!NILP (reseat
) && MARKERP (m
))
2959 unchain_marker (XMARKER (m
));
2960 XSETCAR (list
, Qnil
);
2966 for (; i
< search_regs
.num_regs
; i
++)
2967 search_regs
.start
[i
] = -1;
2973 /* If true the match data have been saved in saved_search_regs
2974 during the execution of a sentinel or filter. */
2975 /* static bool search_regs_saved; */
2976 /* static struct re_registers saved_search_regs; */
2977 /* static Lisp_Object saved_last_thing_searched; */
2979 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
2980 if asynchronous code (filter or sentinel) is running. */
2982 save_search_regs (void)
2984 if (!search_regs_saved
)
2986 saved_search_regs
.num_regs
= search_regs
.num_regs
;
2987 saved_search_regs
.start
= search_regs
.start
;
2988 saved_search_regs
.end
= search_regs
.end
;
2989 saved_last_thing_searched
= last_thing_searched
;
2990 last_thing_searched
= Qnil
;
2991 search_regs
.num_regs
= 0;
2992 search_regs
.start
= 0;
2993 search_regs
.end
= 0;
2995 search_regs_saved
= 1;
2999 /* Called upon exit from filters and sentinels. */
3001 restore_search_regs (void)
3003 if (search_regs_saved
)
3005 if (search_regs
.num_regs
> 0)
3007 xfree (search_regs
.start
);
3008 xfree (search_regs
.end
);
3010 search_regs
.num_regs
= saved_search_regs
.num_regs
;
3011 search_regs
.start
= saved_search_regs
.start
;
3012 search_regs
.end
= saved_search_regs
.end
;
3013 last_thing_searched
= saved_last_thing_searched
;
3014 saved_last_thing_searched
= Qnil
;
3015 search_regs_saved
= 0;
3020 unwind_set_match_data (Lisp_Object list
)
3022 /* It is NOT ALWAYS safe to free (evaporate) the markers immediately. */
3023 return Fset_match_data (list
, Qt
);
3026 /* Called to unwind protect the match data. */
3028 record_unwind_save_match_data (void)
3030 record_unwind_protect (unwind_set_match_data
,
3031 Fmatch_data (Qnil
, Qnil
, Qnil
));
3034 /* Quote a string to deactivate reg-expr chars */
3036 DEFUN ("regexp-quote", Fregexp_quote
, Sregexp_quote
, 1, 1, 0,
3037 doc
: /* Return a regexp string which matches exactly STRING and nothing else. */)
3038 (Lisp_Object string
)
3040 char *in
, *out
, *end
;
3042 ptrdiff_t backslashes_added
= 0;
3044 CHECK_STRING (string
);
3046 temp
= alloca (SBYTES (string
) * 2);
3048 /* Now copy the data into the new string, inserting escapes. */
3050 in
= SSDATA (string
);
3051 end
= in
+ SBYTES (string
);
3054 for (; in
!= end
; in
++)
3057 || *in
== '*' || *in
== '.' || *in
== '\\'
3058 || *in
== '?' || *in
== '+'
3059 || *in
== '^' || *in
== '$')
3060 *out
++ = '\\', backslashes_added
++;
3064 return make_specified_string (temp
,
3065 SCHARS (string
) + backslashes_added
,
3067 STRING_MULTIBYTE (string
));
3071 syms_of_search (void)
3075 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
3077 searchbufs
[i
].buf
.allocated
= 100;
3078 searchbufs
[i
].buf
.buffer
= xmalloc (100);
3079 searchbufs
[i
].buf
.fastmap
= searchbufs
[i
].fastmap
;
3080 searchbufs
[i
].regexp
= Qnil
;
3081 searchbufs
[i
].f_whitespace_regexp
= Qnil
;
3082 searchbufs
[i
].syntax_table
= Qnil
;
3083 staticpro (&searchbufs
[i
].regexp
);
3084 staticpro (&searchbufs
[i
].f_whitespace_regexp
);
3085 staticpro (&searchbufs
[i
].syntax_table
);
3086 searchbufs
[i
].next
= (i
== REGEXP_CACHE_SIZE
-1 ? 0 : &searchbufs
[i
+1]);
3088 searchbuf_head
= &searchbufs
[0];
3090 DEFSYM (Qsearch_failed
, "search-failed");
3091 DEFSYM (Qinvalid_regexp
, "invalid-regexp");
3093 Fput (Qsearch_failed
, Qerror_conditions
,
3094 listn (CONSTYPE_PURE
, 2, Qsearch_failed
, Qerror
));
3095 Fput (Qsearch_failed
, Qerror_message
,
3096 build_pure_c_string ("Search failed"));
3098 Fput (Qinvalid_regexp
, Qerror_conditions
,
3099 listn (CONSTYPE_PURE
, 2, Qinvalid_regexp
, Qerror
));
3100 Fput (Qinvalid_regexp
, Qerror_message
,
3101 build_pure_c_string ("Invalid regexp"));
3103 last_thing_searched
= Qnil
;
3104 staticpro (&last_thing_searched
);
3106 saved_last_thing_searched
= Qnil
;
3107 staticpro (&saved_last_thing_searched
);
3109 DEFVAR_LISP ("search-spaces-regexp", Vsearch_spaces_regexp
,
3110 doc
: /* Regexp to substitute for bunches of spaces in regexp search.
3111 Some commands use this for user-specified regexps.
3112 Spaces that occur inside character classes or repetition operators
3113 or other such regexp constructs are not replaced with this.
3114 A value of nil (which is the normal value) means treat spaces literally. */);
3115 Vsearch_spaces_regexp
= Qnil
;
3117 DEFVAR_LISP ("inhibit-changing-match-data", Vinhibit_changing_match_data
,
3118 doc
: /* Internal use only.
3119 If non-nil, the primitive searching and matching functions
3120 such as `looking-at', `string-match', `re-search-forward', etc.,
3121 do not set the match data. The proper way to use this variable
3122 is to bind it with `let' around a small expression. */);
3123 Vinhibit_changing_match_data
= Qnil
;
3125 defsubr (&Slooking_at
);
3126 defsubr (&Sposix_looking_at
);
3127 defsubr (&Sstring_match
);
3128 defsubr (&Sposix_string_match
);
3129 defsubr (&Ssearch_forward
);
3130 defsubr (&Ssearch_backward
);
3131 defsubr (&Sre_search_forward
);
3132 defsubr (&Sre_search_backward
);
3133 defsubr (&Sposix_search_forward
);
3134 defsubr (&Sposix_search_backward
);
3135 defsubr (&Sreplace_match
);
3136 defsubr (&Smatch_beginning
);
3137 defsubr (&Smatch_end
);
3138 defsubr (&Smatch_data
);
3139 defsubr (&Sset_match_data
);
3140 defsubr (&Sregexp_quote
);