1 /* String search routines for GNU Emacs.
2 Copyright (C) 1985, 86,87,93,94,97,98, 1999, 2004
3 Free Software Foundation, Inc.
5 This file is part of GNU Emacs.
7 GNU Emacs is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2, or (at your option)
12 GNU Emacs is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with GNU Emacs; see the file COPYING. If not, write to
19 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA. */
28 #include "character.h"
29 #include "region-cache.h"
31 #include "blockinput.h"
32 #include "intervals.h"
34 #include <sys/types.h>
37 #define REGEXP_CACHE_SIZE 20
39 /* If the regexp is non-nil, then the buffer contains the compiled form
40 of that regexp, suitable for searching. */
43 struct regexp_cache
*next
;
45 struct re_pattern_buffer buf
;
47 /* Nonzero means regexp was compiled to do full POSIX backtracking. */
51 /* The instances of that struct. */
52 struct regexp_cache searchbufs
[REGEXP_CACHE_SIZE
];
54 /* The head of the linked list; points to the most recently used buffer. */
55 struct regexp_cache
*searchbuf_head
;
58 /* Every call to re_match, etc., must pass &search_regs as the regs
59 argument unless you can show it is unnecessary (i.e., if re_match
60 is certainly going to be called again before region-around-match
63 Since the registers are now dynamically allocated, we need to make
64 sure not to refer to the Nth register before checking that it has
65 been allocated by checking search_regs.num_regs.
67 The regex code keeps track of whether it has allocated the search
68 buffer using bits in the re_pattern_buffer. This means that whenever
69 you compile a new pattern, it completely forgets whether it has
70 allocated any registers, and will allocate new registers the next
71 time you call a searching or matching function. Therefore, we need
72 to call re_set_registers after compiling a new pattern or after
73 setting the match registers, so that the regex functions will be
74 able to free or re-allocate it properly. */
75 static struct re_registers search_regs
;
77 /* The buffer in which the last search was performed, or
78 Qt if the last search was done in a string;
79 Qnil if no searching has been done yet. */
80 static Lisp_Object last_thing_searched
;
82 /* error condition signaled when regexp compile_pattern fails */
84 Lisp_Object Qinvalid_regexp
;
86 static void set_search_regs ();
87 static void save_search_regs ();
88 static int simple_search ();
89 static int boyer_moore ();
90 static int search_buffer ();
95 error ("Stack overflow in regexp matcher");
98 /* Compile a regexp and signal a Lisp error if anything goes wrong.
99 PATTERN is the pattern to compile.
100 CP is the place to put the result.
101 TRANSLATE is a translation table for ignoring case, or nil for none.
102 REGP is the structure that says where to store the "register"
103 values that will result from matching this pattern.
104 If it is 0, we should compile the pattern not to record any
105 subexpression bounds.
106 POSIX is nonzero if we want full backtracking (POSIX style)
107 for this pattern. 0 means backtrack only enough to get a valid match.
108 MULTIBYTE is nonzero iff a target of match is a multibyte buffer or
112 compile_pattern_1 (cp
, pattern
, translate
, regp
, posix
, multibyte
)
113 struct regexp_cache
*cp
;
115 Lisp_Object translate
;
116 struct re_registers
*regp
;
124 cp
->buf
.translate
= (! NILP (translate
) ? translate
: make_number (0));
126 cp
->buf
.multibyte
= STRING_MULTIBYTE (pattern
);
127 cp
->buf
.target_multibyte
= multibyte
;
129 old
= re_set_syntax (RE_SYNTAX_EMACS
130 | (posix
? 0 : RE_NO_POSIX_BACKTRACKING
));
131 val
= (char *) re_compile_pattern ((char *) SDATA (pattern
),
132 SBYTES (pattern
), &cp
->buf
);
136 Fsignal (Qinvalid_regexp
, Fcons (build_string (val
), Qnil
));
138 cp
->regexp
= Fcopy_sequence (pattern
);
141 /* Shrink each compiled regexp buffer in the cache
142 to the size actually used right now.
143 This is called from garbage collection. */
146 shrink_regexp_cache ()
148 struct regexp_cache
*cp
;
150 for (cp
= searchbuf_head
; cp
!= 0; cp
= cp
->next
)
152 cp
->buf
.allocated
= cp
->buf
.used
;
154 = (unsigned char *) xrealloc (cp
->buf
.buffer
, cp
->buf
.used
);
158 /* Compile a regexp if necessary, but first check to see if there's one in
160 PATTERN is the pattern to compile.
161 TRANSLATE is a translation table for ignoring case, or nil for none.
162 REGP is the structure that says where to store the "register"
163 values that will result from matching this pattern.
164 If it is 0, we should compile the pattern not to record any
165 subexpression bounds.
166 POSIX is nonzero if we want full backtracking (POSIX style)
167 for this pattern. 0 means backtrack only enough to get a valid match. */
169 struct re_pattern_buffer
*
170 compile_pattern (pattern
, regp
, translate
, posix
, multibyte
)
172 struct re_registers
*regp
;
173 Lisp_Object translate
;
174 int posix
, multibyte
;
176 struct regexp_cache
*cp
, **cpp
;
178 for (cpp
= &searchbuf_head
; ; cpp
= &cp
->next
)
181 /* Entries are initialized to nil, and may be set to nil by
182 compile_pattern_1 if the pattern isn't valid. Don't apply
183 string accessors in those cases. However, compile_pattern_1
184 is only applied to the cache entry we pick here to reuse. So
185 nil should never appear before a non-nil entry. */
186 if (NILP (cp
->regexp
))
188 if (SCHARS (cp
->regexp
) == SCHARS (pattern
)
189 && STRING_MULTIBYTE (cp
->regexp
) == STRING_MULTIBYTE (pattern
)
190 && !NILP (Fstring_equal (cp
->regexp
, pattern
))
191 && EQ (cp
->buf
.translate
, (! NILP (translate
) ? translate
: make_number (0)))
192 && cp
->posix
== posix
193 && cp
->buf
.target_multibyte
== multibyte
)
196 /* If we're at the end of the cache, compile into the nil cell
197 we found, or the last (least recently used) cell with a
202 compile_pattern_1 (cp
, pattern
, translate
, regp
, posix
, multibyte
);
207 /* When we get here, cp (aka *cpp) contains the compiled pattern,
208 either because we found it in the cache or because we just compiled it.
209 Move it to the front of the queue to mark it as most recently used. */
211 cp
->next
= searchbuf_head
;
214 /* Advise the searching functions about the space we have allocated
215 for register data. */
217 re_set_registers (&cp
->buf
, regp
, regp
->num_regs
, regp
->start
, regp
->end
);
222 /* Error condition used for failing searches */
223 Lisp_Object Qsearch_failed
;
229 Fsignal (Qsearch_failed
, Fcons (arg
, Qnil
));
234 looking_at_1 (string
, posix
)
239 unsigned char *p1
, *p2
;
242 struct re_pattern_buffer
*bufp
;
244 if (running_asynch_code
)
247 CHECK_STRING (string
);
248 bufp
= compile_pattern (string
, &search_regs
,
249 (!NILP (current_buffer
->case_fold_search
)
250 ? DOWNCASE_TABLE
: Qnil
),
252 !NILP (current_buffer
->enable_multibyte_characters
));
255 QUIT
; /* Do a pending quit right away, to avoid paradoxical behavior */
257 /* Get pointers and sizes of the two strings
258 that make up the visible portion of the buffer. */
261 s1
= GPT_BYTE
- BEGV_BYTE
;
263 s2
= ZV_BYTE
- GPT_BYTE
;
267 s2
= ZV_BYTE
- BEGV_BYTE
;
272 s1
= ZV_BYTE
- BEGV_BYTE
;
276 re_match_object
= Qnil
;
278 i
= re_match_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
279 PT_BYTE
- BEGV_BYTE
, &search_regs
,
280 ZV_BYTE
- BEGV_BYTE
);
286 val
= (0 <= i
? Qt
: Qnil
);
288 for (i
= 0; i
< search_regs
.num_regs
; i
++)
289 if (search_regs
.start
[i
] >= 0)
292 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
294 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
296 XSETBUFFER (last_thing_searched
, current_buffer
);
300 DEFUN ("looking-at", Flooking_at
, Slooking_at
, 1, 1, 0,
301 doc
: /* Return t if text after point matches regular expression REGEXP.
302 This function modifies the match data that `match-beginning',
303 `match-end' and `match-data' access; save and restore the match
304 data if you want to preserve them. */)
308 return looking_at_1 (regexp
, 0);
311 DEFUN ("posix-looking-at", Fposix_looking_at
, Sposix_looking_at
, 1, 1, 0,
312 doc
: /* Return t if text after point matches regular expression REGEXP.
313 Find the longest match, in accord with Posix regular expression rules.
314 This function modifies the match data that `match-beginning',
315 `match-end' and `match-data' access; save and restore the match
316 data if you want to preserve them. */)
320 return looking_at_1 (regexp
, 1);
324 string_match_1 (regexp
, string
, start
, posix
)
325 Lisp_Object regexp
, string
, start
;
329 struct re_pattern_buffer
*bufp
;
333 if (running_asynch_code
)
336 CHECK_STRING (regexp
);
337 CHECK_STRING (string
);
340 pos
= 0, pos_byte
= 0;
343 int len
= SCHARS (string
);
345 CHECK_NUMBER (start
);
347 if (pos
< 0 && -pos
<= len
)
349 else if (0 > pos
|| pos
> len
)
350 args_out_of_range (string
, start
);
351 pos_byte
= string_char_to_byte (string
, pos
);
354 bufp
= compile_pattern (regexp
, &search_regs
,
355 (!NILP (current_buffer
->case_fold_search
)
356 ? DOWNCASE_TABLE
: Qnil
),
358 STRING_MULTIBYTE (string
));
360 re_match_object
= string
;
362 val
= re_search (bufp
, (char *) SDATA (string
),
363 SBYTES (string
), pos_byte
,
364 SBYTES (string
) - pos_byte
,
367 last_thing_searched
= Qt
;
370 if (val
< 0) return Qnil
;
372 for (i
= 0; i
< search_regs
.num_regs
; i
++)
373 if (search_regs
.start
[i
] >= 0)
376 = string_byte_to_char (string
, search_regs
.start
[i
]);
378 = string_byte_to_char (string
, search_regs
.end
[i
]);
381 return make_number (string_byte_to_char (string
, val
));
384 DEFUN ("string-match", Fstring_match
, Sstring_match
, 2, 3, 0,
385 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
386 Case is ignored if `case-fold-search' is non-nil in the current buffer.
387 If third arg START is non-nil, start search at that index in STRING.
388 For index of first char beyond the match, do (match-end 0).
389 `match-end' and `match-beginning' also give indices of substrings
390 matched by parenthesis constructs in the pattern.
392 You can use the function `match-string' to extract the substrings
393 matched by the parenthesis constructions in REGEXP. */)
394 (regexp
, string
, start
)
395 Lisp_Object regexp
, string
, start
;
397 return string_match_1 (regexp
, string
, start
, 0);
400 DEFUN ("posix-string-match", Fposix_string_match
, Sposix_string_match
, 2, 3, 0,
401 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
402 Find the longest match, in accord with Posix regular expression rules.
403 Case is ignored if `case-fold-search' is non-nil in the current buffer.
404 If third arg START is non-nil, start search at that index in STRING.
405 For index of first char beyond the match, do (match-end 0).
406 `match-end' and `match-beginning' also give indices of substrings
407 matched by parenthesis constructs in the pattern. */)
408 (regexp
, string
, start
)
409 Lisp_Object regexp
, string
, start
;
411 return string_match_1 (regexp
, string
, start
, 1);
414 /* Match REGEXP against STRING, searching all of STRING,
415 and return the index of the match, or negative on failure.
416 This does not clobber the match data. */
419 fast_string_match (regexp
, string
)
420 Lisp_Object regexp
, string
;
423 struct re_pattern_buffer
*bufp
;
425 bufp
= compile_pattern (regexp
, 0, Qnil
,
426 0, STRING_MULTIBYTE (string
));
428 re_match_object
= string
;
430 val
= re_search (bufp
, (char *) SDATA (string
),
437 /* Match REGEXP against STRING, searching all of STRING ignoring case,
438 and return the index of the match, or negative on failure.
439 This does not clobber the match data.
440 We assume that STRING contains single-byte characters. */
442 extern Lisp_Object Vascii_downcase_table
;
445 fast_c_string_match_ignore_case (regexp
, string
)
450 struct re_pattern_buffer
*bufp
;
451 int len
= strlen (string
);
453 regexp
= string_make_unibyte (regexp
);
454 re_match_object
= Qt
;
455 bufp
= compile_pattern (regexp
, 0,
456 Vascii_downcase_table
, 0,
459 val
= re_search (bufp
, string
, len
, 0, len
, 0);
464 /* The newline cache: remembering which sections of text have no newlines. */
466 /* If the user has requested newline caching, make sure it's on.
467 Otherwise, make sure it's off.
468 This is our cheezy way of associating an action with the change of
469 state of a buffer-local variable. */
471 newline_cache_on_off (buf
)
474 if (NILP (buf
->cache_long_line_scans
))
476 /* It should be off. */
477 if (buf
->newline_cache
)
479 free_region_cache (buf
->newline_cache
);
480 buf
->newline_cache
= 0;
485 /* It should be on. */
486 if (buf
->newline_cache
== 0)
487 buf
->newline_cache
= new_region_cache ();
492 /* Search for COUNT instances of the character TARGET between START and END.
494 If COUNT is positive, search forwards; END must be >= START.
495 If COUNT is negative, search backwards for the -COUNTth instance;
496 END must be <= START.
497 If COUNT is zero, do anything you please; run rogue, for all I care.
499 If END is zero, use BEGV or ZV instead, as appropriate for the
500 direction indicated by COUNT.
502 If we find COUNT instances, set *SHORTAGE to zero, and return the
503 position after the COUNTth match. Note that for reverse motion
504 this is not the same as the usual convention for Emacs motion commands.
506 If we don't find COUNT instances before reaching END, set *SHORTAGE
507 to the number of TARGETs left unfound, and return END.
509 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
510 except when inside redisplay. */
513 scan_buffer (target
, start
, end
, count
, shortage
, allow_quit
)
520 struct region_cache
*newline_cache
;
531 if (! end
) end
= BEGV
;
534 newline_cache_on_off (current_buffer
);
535 newline_cache
= current_buffer
->newline_cache
;
540 immediate_quit
= allow_quit
;
545 /* Our innermost scanning loop is very simple; it doesn't know
546 about gaps, buffer ends, or the newline cache. ceiling is
547 the position of the last character before the next such
548 obstacle --- the last character the dumb search loop should
550 int ceiling_byte
= CHAR_TO_BYTE (end
) - 1;
551 int start_byte
= CHAR_TO_BYTE (start
);
554 /* If we're looking for a newline, consult the newline cache
555 to see where we can avoid some scanning. */
556 if (target
== '\n' && newline_cache
)
560 while (region_cache_forward
561 (current_buffer
, newline_cache
, start_byte
, &next_change
))
562 start_byte
= next_change
;
563 immediate_quit
= allow_quit
;
565 /* START should never be after END. */
566 if (start_byte
> ceiling_byte
)
567 start_byte
= ceiling_byte
;
569 /* Now the text after start is an unknown region, and
570 next_change is the position of the next known region. */
571 ceiling_byte
= min (next_change
- 1, ceiling_byte
);
574 /* The dumb loop can only scan text stored in contiguous
575 bytes. BUFFER_CEILING_OF returns the last character
576 position that is contiguous, so the ceiling is the
577 position after that. */
578 tem
= BUFFER_CEILING_OF (start_byte
);
579 ceiling_byte
= min (tem
, ceiling_byte
);
582 /* The termination address of the dumb loop. */
583 register unsigned char *ceiling_addr
584 = BYTE_POS_ADDR (ceiling_byte
) + 1;
585 register unsigned char *cursor
586 = BYTE_POS_ADDR (start_byte
);
587 unsigned char *base
= cursor
;
589 while (cursor
< ceiling_addr
)
591 unsigned char *scan_start
= cursor
;
594 while (*cursor
!= target
&& ++cursor
< ceiling_addr
)
597 /* If we're looking for newlines, cache the fact that
598 the region from start to cursor is free of them. */
599 if (target
== '\n' && newline_cache
)
600 know_region_cache (current_buffer
, newline_cache
,
601 start_byte
+ scan_start
- base
,
602 start_byte
+ cursor
- base
);
604 /* Did we find the target character? */
605 if (cursor
< ceiling_addr
)
610 return BYTE_TO_CHAR (start_byte
+ cursor
- base
+ 1);
616 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
622 /* The last character to check before the next obstacle. */
623 int ceiling_byte
= CHAR_TO_BYTE (end
);
624 int start_byte
= CHAR_TO_BYTE (start
);
627 /* Consult the newline cache, if appropriate. */
628 if (target
== '\n' && newline_cache
)
632 while (region_cache_backward
633 (current_buffer
, newline_cache
, start_byte
, &next_change
))
634 start_byte
= next_change
;
635 immediate_quit
= allow_quit
;
637 /* Start should never be at or before end. */
638 if (start_byte
<= ceiling_byte
)
639 start_byte
= ceiling_byte
+ 1;
641 /* Now the text before start is an unknown region, and
642 next_change is the position of the next known region. */
643 ceiling_byte
= max (next_change
, ceiling_byte
);
646 /* Stop scanning before the gap. */
647 tem
= BUFFER_FLOOR_OF (start_byte
- 1);
648 ceiling_byte
= max (tem
, ceiling_byte
);
651 /* The termination address of the dumb loop. */
652 register unsigned char *ceiling_addr
= BYTE_POS_ADDR (ceiling_byte
);
653 register unsigned char *cursor
= BYTE_POS_ADDR (start_byte
- 1);
654 unsigned char *base
= cursor
;
656 while (cursor
>= ceiling_addr
)
658 unsigned char *scan_start
= cursor
;
660 while (*cursor
!= target
&& --cursor
>= ceiling_addr
)
663 /* If we're looking for newlines, cache the fact that
664 the region from after the cursor to start is free of them. */
665 if (target
== '\n' && newline_cache
)
666 know_region_cache (current_buffer
, newline_cache
,
667 start_byte
+ cursor
- base
,
668 start_byte
+ scan_start
- base
);
670 /* Did we find the target character? */
671 if (cursor
>= ceiling_addr
)
676 return BYTE_TO_CHAR (start_byte
+ cursor
- base
);
682 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
688 *shortage
= count
* direction
;
692 /* Search for COUNT instances of a line boundary, which means either a
693 newline or (if selective display enabled) a carriage return.
694 Start at START. If COUNT is negative, search backwards.
696 We report the resulting position by calling TEMP_SET_PT_BOTH.
698 If we find COUNT instances. we position after (always after,
699 even if scanning backwards) the COUNTth match, and return 0.
701 If we don't find COUNT instances before reaching the end of the
702 buffer (or the beginning, if scanning backwards), we return
703 the number of line boundaries left unfound, and position at
704 the limit we bumped up against.
706 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
707 except in special cases. */
710 scan_newline (start
, start_byte
, limit
, limit_byte
, count
, allow_quit
)
711 int start
, start_byte
;
712 int limit
, limit_byte
;
716 int direction
= ((count
> 0) ? 1 : -1);
718 register unsigned char *cursor
;
721 register int ceiling
;
722 register unsigned char *ceiling_addr
;
724 int old_immediate_quit
= immediate_quit
;
726 /* The code that follows is like scan_buffer
727 but checks for either newline or carriage return. */
732 start_byte
= CHAR_TO_BYTE (start
);
736 while (start_byte
< limit_byte
)
738 ceiling
= BUFFER_CEILING_OF (start_byte
);
739 ceiling
= min (limit_byte
- 1, ceiling
);
740 ceiling_addr
= BYTE_POS_ADDR (ceiling
) + 1;
741 base
= (cursor
= BYTE_POS_ADDR (start_byte
));
744 while (*cursor
!= '\n' && ++cursor
!= ceiling_addr
)
747 if (cursor
!= ceiling_addr
)
751 immediate_quit
= old_immediate_quit
;
752 start_byte
= start_byte
+ cursor
- base
+ 1;
753 start
= BYTE_TO_CHAR (start_byte
);
754 TEMP_SET_PT_BOTH (start
, start_byte
);
758 if (++cursor
== ceiling_addr
)
764 start_byte
+= cursor
- base
;
769 while (start_byte
> limit_byte
)
771 ceiling
= BUFFER_FLOOR_OF (start_byte
- 1);
772 ceiling
= max (limit_byte
, ceiling
);
773 ceiling_addr
= BYTE_POS_ADDR (ceiling
) - 1;
774 base
= (cursor
= BYTE_POS_ADDR (start_byte
- 1) + 1);
777 while (--cursor
!= ceiling_addr
&& *cursor
!= '\n')
780 if (cursor
!= ceiling_addr
)
784 immediate_quit
= old_immediate_quit
;
785 /* Return the position AFTER the match we found. */
786 start_byte
= start_byte
+ cursor
- base
+ 1;
787 start
= BYTE_TO_CHAR (start_byte
);
788 TEMP_SET_PT_BOTH (start
, start_byte
);
795 /* Here we add 1 to compensate for the last decrement
796 of CURSOR, which took it past the valid range. */
797 start_byte
+= cursor
- base
+ 1;
801 TEMP_SET_PT_BOTH (limit
, limit_byte
);
802 immediate_quit
= old_immediate_quit
;
804 return count
* direction
;
808 find_next_newline_no_quit (from
, cnt
)
809 register int from
, cnt
;
811 return scan_buffer ('\n', from
, 0, cnt
, (int *) 0, 0);
814 /* Like find_next_newline, but returns position before the newline,
815 not after, and only search up to TO. This isn't just
816 find_next_newline (...)-1, because you might hit TO. */
819 find_before_next_newline (from
, to
, cnt
)
823 int pos
= scan_buffer ('\n', from
, to
, cnt
, &shortage
, 1);
831 /* Subroutines of Lisp buffer search functions. */
834 search_command (string
, bound
, noerror
, count
, direction
, RE
, posix
)
835 Lisp_Object string
, bound
, noerror
, count
;
846 CHECK_NUMBER (count
);
850 CHECK_STRING (string
);
854 lim
= ZV
, lim_byte
= ZV_BYTE
;
856 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
860 CHECK_NUMBER_COERCE_MARKER (bound
);
862 if (n
> 0 ? lim
< PT
: lim
> PT
)
863 error ("Invalid search bound (wrong side of point)");
865 lim
= ZV
, lim_byte
= ZV_BYTE
;
867 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
869 lim_byte
= CHAR_TO_BYTE (lim
);
872 np
= search_buffer (string
, PT
, PT_BYTE
, lim
, lim_byte
, n
, RE
,
873 (!NILP (current_buffer
->case_fold_search
)
874 ? current_buffer
->case_canon_table
876 (!NILP (current_buffer
->case_fold_search
)
877 ? current_buffer
->case_eqv_table
883 return signal_failure (string
);
884 if (!EQ (noerror
, Qt
))
886 if (lim
< BEGV
|| lim
> ZV
)
888 SET_PT_BOTH (lim
, lim_byte
);
890 #if 0 /* This would be clean, but maybe programs depend on
891 a value of nil here. */
899 if (np
< BEGV
|| np
> ZV
)
904 return make_number (np
);
907 /* Return 1 if REGEXP it matches just one constant string. */
910 trivial_regexp_p (regexp
)
913 int len
= SBYTES (regexp
);
914 unsigned char *s
= SDATA (regexp
);
919 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
926 case '|': case '(': case ')': case '`': case '\'': case 'b':
927 case 'B': case '<': case '>': case 'w': case 'W': case 's':
928 case 'S': case '=': case '{': case '}': case '_':
929 case 'c': case 'C': /* for categoryspec and notcategoryspec */
930 case '1': case '2': case '3': case '4': case '5':
931 case '6': case '7': case '8': case '9':
939 /* Search for the n'th occurrence of STRING in the current buffer,
940 starting at position POS and stopping at position LIM,
941 treating STRING as a literal string if RE is false or as
942 a regular expression if RE is true.
944 If N is positive, searching is forward and LIM must be greater than POS.
945 If N is negative, searching is backward and LIM must be less than POS.
947 Returns -x if x occurrences remain to be found (x > 0),
948 or else the position at the beginning of the Nth occurrence
949 (if searching backward) or the end (if searching forward).
951 POSIX is nonzero if we want full backtracking (POSIX style)
952 for this pattern. 0 means backtrack only enough to get a valid match. */
954 #define TRANSLATE(out, trt, d) \
960 temp = Faref (trt, make_number (d)); \
961 if (INTEGERP (temp)) \
972 search_buffer (string
, pos
, pos_byte
, lim
, lim_byte
, n
,
973 RE
, trt
, inverse_trt
, posix
)
982 Lisp_Object inverse_trt
;
985 int len
= SCHARS (string
);
986 int len_byte
= SBYTES (string
);
989 if (running_asynch_code
)
992 /* Searching 0 times means don't move. */
993 /* Null string is found at starting position. */
994 if (len
== 0 || n
== 0)
996 set_search_regs (pos_byte
, 0);
1000 if (RE
&& !trivial_regexp_p (string
))
1002 unsigned char *p1
, *p2
;
1004 struct re_pattern_buffer
*bufp
;
1006 bufp
= compile_pattern (string
, &search_regs
, trt
, posix
,
1007 !NILP (current_buffer
->enable_multibyte_characters
));
1009 immediate_quit
= 1; /* Quit immediately if user types ^G,
1010 because letting this function finish
1011 can take too long. */
1012 QUIT
; /* Do a pending quit right away,
1013 to avoid paradoxical behavior */
1014 /* Get pointers and sizes of the two strings
1015 that make up the visible portion of the buffer. */
1018 s1
= GPT_BYTE
- BEGV_BYTE
;
1020 s2
= ZV_BYTE
- GPT_BYTE
;
1024 s2
= ZV_BYTE
- BEGV_BYTE
;
1029 s1
= ZV_BYTE
- BEGV_BYTE
;
1032 re_match_object
= Qnil
;
1037 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1038 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1040 /* Don't allow match past current point */
1041 pos_byte
- BEGV_BYTE
);
1044 matcher_overflow ();
1048 pos_byte
= search_regs
.start
[0] + BEGV_BYTE
;
1049 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1050 if (search_regs
.start
[i
] >= 0)
1052 search_regs
.start
[i
]
1053 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1055 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1057 XSETBUFFER (last_thing_searched
, current_buffer
);
1058 /* Set pos to the new position. */
1059 pos
= search_regs
.start
[0];
1071 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1072 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1074 lim_byte
- BEGV_BYTE
);
1077 matcher_overflow ();
1081 pos_byte
= search_regs
.end
[0] + BEGV_BYTE
;
1082 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1083 if (search_regs
.start
[i
] >= 0)
1085 search_regs
.start
[i
]
1086 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1088 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1090 XSETBUFFER (last_thing_searched
, current_buffer
);
1091 pos
= search_regs
.end
[0];
1103 else /* non-RE case */
1105 unsigned char *raw_pattern
, *pat
;
1106 int raw_pattern_size
;
1107 int raw_pattern_size_byte
;
1108 unsigned char *patbuf
;
1109 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1110 unsigned char *base_pat
= SDATA (string
);
1111 /* High bits of char; 0 for ASCII characters, (CHAR & ~0x3F)
1112 otherwise. Characters of the same high bits have the same
1113 sequence of bytes but last. To do the BM search, all
1114 characters in STRING must have the same high bits (including
1115 their case translations). */
1116 int char_high_bits
= -1;
1117 int boyer_moore_ok
= 1;
1119 /* MULTIBYTE says whether the text to be searched is multibyte.
1120 We must convert PATTERN to match that, or we will not really
1121 find things right. */
1123 if (multibyte
== STRING_MULTIBYTE (string
))
1125 raw_pattern
= (unsigned char *) SDATA (string
);
1126 raw_pattern_size
= SCHARS (string
);
1127 raw_pattern_size_byte
= SBYTES (string
);
1131 raw_pattern_size
= SCHARS (string
);
1132 raw_pattern_size_byte
1133 = count_size_as_multibyte (SDATA (string
),
1135 raw_pattern
= (unsigned char *) alloca (raw_pattern_size_byte
+ 1);
1136 copy_text (SDATA (string
), raw_pattern
,
1137 SCHARS (string
), 0, 1);
1141 /* Converting multibyte to single-byte.
1143 ??? Perhaps this conversion should be done in a special way
1144 by subtracting nonascii-insert-offset from each non-ASCII char,
1145 so that only the multibyte chars which really correspond to
1146 the chosen single-byte character set can possibly match. */
1147 raw_pattern_size
= SCHARS (string
);
1148 raw_pattern_size_byte
= SCHARS (string
);
1149 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
1150 copy_text (SDATA (string
), raw_pattern
,
1151 SBYTES (string
), 1, 0);
1154 /* Copy and optionally translate the pattern. */
1155 len
= raw_pattern_size
;
1156 len_byte
= raw_pattern_size_byte
;
1157 patbuf
= (unsigned char *) alloca (len
* MAX_MULTIBYTE_LENGTH
);
1159 base_pat
= raw_pattern
;
1164 int c
, translated
, inverse
;
1167 /* If we got here and the RE flag is set, it's because we're
1168 dealing with a regexp known to be trivial, so the backslash
1169 just quotes the next character. */
1170 if (RE
&& *base_pat
== '\\')
1177 c
= STRING_CHAR_AND_LENGTH (base_pat
, len_byte
, in_charlen
);
1179 /* Translate the character, if requested. */
1180 TRANSLATE (translated
, trt
, c
);
1181 TRANSLATE (inverse
, inverse_trt
, c
);
1183 /* Did this char actually get translated?
1184 Would any other char get translated into it? */
1185 if (translated
!= c
|| inverse
!= c
)
1187 /* Keep track of which character set row
1188 contains the characters that need translation. */
1189 int this_high_bit
= ASCII_CHAR_P (c
) ? 0 : (c
& ~0x3F);
1190 int c1
= inverse
!= c
? inverse
: translated
;
1191 int trt_high_bit
= ASCII_CHAR_P (c1
) ? 0 : (c1
& ~0x3F);
1193 if (this_high_bit
!= trt_high_bit
)
1195 else if (char_high_bits
== -1)
1196 char_high_bits
= this_high_bit
;
1197 else if (char_high_bits
!= this_high_bit
)
1198 /* If two different rows appear, needing translation,
1199 then we cannot use boyer_moore search. */
1203 /* Store this character into the translated pattern. */
1204 CHAR_STRING_ADVANCE (translated
, pat
);
1205 base_pat
+= in_charlen
;
1206 len_byte
-= in_charlen
;
1211 /* Unibyte buffer. */
1217 /* If we got here and the RE flag is set, it's because we're
1218 dealing with a regexp known to be trivial, so the backslash
1219 just quotes the next character. */
1220 if (RE
&& *base_pat
== '\\')
1226 TRANSLATE (translated
, trt
, c
);
1227 *pat
++ = translated
;
1231 len_byte
= pat
- patbuf
;
1232 len
= raw_pattern_size
;
1233 pat
= base_pat
= patbuf
;
1236 return boyer_moore (n
, pat
, len
, len_byte
, trt
, inverse_trt
,
1237 pos
, pos_byte
, lim
, lim_byte
,
1240 return simple_search (n
, pat
, len
, len_byte
, trt
,
1241 pos
, pos_byte
, lim
, lim_byte
);
1245 /* Do a simple string search N times for the string PAT,
1246 whose length is LEN/LEN_BYTE,
1247 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1248 TRT is the translation table.
1250 Return the character position where the match is found.
1251 Otherwise, if M matches remained to be found, return -M.
1253 This kind of search works regardless of what is in PAT and
1254 regardless of what is in TRT. It is used in cases where
1255 boyer_moore cannot work. */
1258 simple_search (n
, pat
, len
, len_byte
, trt
, pos
, pos_byte
, lim
, lim_byte
)
1266 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1267 int forward
= n
> 0;
1269 if (lim
> pos
&& multibyte
)
1274 /* Try matching at position POS. */
1276 int this_pos_byte
= pos_byte
;
1278 int this_len_byte
= len_byte
;
1279 unsigned char *p
= pat
;
1280 if (pos
+ len
> lim
)
1283 while (this_len
> 0)
1285 int charlen
, buf_charlen
;
1288 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1289 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1290 ZV_BYTE
- this_pos_byte
,
1292 TRANSLATE (buf_ch
, trt
, buf_ch
);
1294 if (buf_ch
!= pat_ch
)
1297 this_len_byte
-= charlen
;
1301 this_pos_byte
+= buf_charlen
;
1308 pos_byte
+= len_byte
;
1312 INC_BOTH (pos
, pos_byte
);
1322 /* Try matching at position POS. */
1325 unsigned char *p
= pat
;
1327 if (pos
+ len
> lim
)
1330 while (this_len
> 0)
1333 int buf_ch
= FETCH_BYTE (this_pos
);
1334 TRANSLATE (buf_ch
, trt
, buf_ch
);
1336 if (buf_ch
!= pat_ch
)
1354 /* Backwards search. */
1355 else if (lim
< pos
&& multibyte
)
1360 /* Try matching at position POS. */
1361 int this_pos
= pos
- len
;
1364 int this_len_byte
= len_byte
;
1365 unsigned char *p
= pat
;
1367 if (pos
- len
< lim
)
1369 this_pos_byte
= CHAR_TO_BYTE (this_pos
);
1371 while (this_len
> 0)
1373 int charlen
, buf_charlen
;
1376 pat_ch
= STRING_CHAR_AND_LENGTH (p
, this_len_byte
, charlen
);
1377 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1378 ZV_BYTE
- this_pos_byte
,
1380 TRANSLATE (buf_ch
, trt
, buf_ch
);
1382 if (buf_ch
!= pat_ch
)
1385 this_len_byte
-= charlen
;
1388 this_pos_byte
+= buf_charlen
;
1395 pos_byte
-= len_byte
;
1399 DEC_BOTH (pos
, pos_byte
);
1409 /* Try matching at position POS. */
1410 int this_pos
= pos
- len
;
1412 unsigned char *p
= pat
;
1414 if (pos
- len
< lim
)
1417 while (this_len
> 0)
1420 int buf_ch
= FETCH_BYTE (this_pos
);
1421 TRANSLATE (buf_ch
, trt
, buf_ch
);
1423 if (buf_ch
!= pat_ch
)
1445 set_search_regs ((multibyte
? pos_byte
: pos
) - len_byte
, len_byte
);
1447 set_search_regs (multibyte
? pos_byte
: pos
, len_byte
);
1457 /* Do Boyer-Moore search N times for the string PAT,
1458 whose length is LEN/LEN_BYTE,
1459 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1460 DIRECTION says which direction we search in.
1461 TRT and INVERSE_TRT are translation tables.
1463 This kind of search works if all the characters in PAT that have
1464 nontrivial translation are the same aside from the last byte. This
1465 makes it possible to translate just the last byte of a character,
1466 and do so after just a simple test of the context.
1468 If that criterion is not satisfied, do not call this function. */
1471 boyer_moore (n
, base_pat
, len
, len_byte
, trt
, inverse_trt
,
1472 pos
, pos_byte
, lim
, lim_byte
, char_high_bits
)
1474 unsigned char *base_pat
;
1477 Lisp_Object inverse_trt
;
1482 int direction
= ((n
> 0) ? 1 : -1);
1483 register int dirlen
;
1484 int infinity
, limit
, stride_for_teases
= 0;
1485 register int *BM_tab
;
1487 register unsigned char *cursor
, *p_limit
;
1489 unsigned char *pat
, *pat_end
;
1490 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1492 unsigned char simple_translate
[0400];
1493 int translate_prev_byte
= 0;
1494 int translate_anteprev_byte
= 0;
1497 int BM_tab_space
[0400];
1498 BM_tab
= &BM_tab_space
[0];
1500 BM_tab
= (int *) alloca (0400 * sizeof (int));
1502 /* The general approach is that we are going to maintain that we know */
1503 /* the first (closest to the present position, in whatever direction */
1504 /* we're searching) character that could possibly be the last */
1505 /* (furthest from present position) character of a valid match. We */
1506 /* advance the state of our knowledge by looking at that character */
1507 /* and seeing whether it indeed matches the last character of the */
1508 /* pattern. If it does, we take a closer look. If it does not, we */
1509 /* move our pointer (to putative last characters) as far as is */
1510 /* logically possible. This amount of movement, which I call a */
1511 /* stride, will be the length of the pattern if the actual character */
1512 /* appears nowhere in the pattern, otherwise it will be the distance */
1513 /* from the last occurrence of that character to the end of the */
1515 /* As a coding trick, an enormous stride is coded into the table for */
1516 /* characters that match the last character. This allows use of only */
1517 /* a single test, a test for having gone past the end of the */
1518 /* permissible match region, to test for both possible matches (when */
1519 /* the stride goes past the end immediately) and failure to */
1520 /* match (where you get nudged past the end one stride at a time). */
1522 /* Here we make a "mickey mouse" BM table. The stride of the search */
1523 /* is determined only by the last character of the putative match. */
1524 /* If that character does not match, we will stride the proper */
1525 /* distance to propose a match that superimposes it on the last */
1526 /* instance of a character that matches it (per trt), or misses */
1527 /* it entirely if there is none. */
1529 dirlen
= len_byte
* direction
;
1530 infinity
= dirlen
- (lim_byte
+ pos_byte
+ len_byte
+ len_byte
) * direction
;
1532 /* Record position after the end of the pattern. */
1533 pat_end
= base_pat
+ len_byte
;
1534 /* BASE_PAT points to a character that we start scanning from.
1535 It is the first character in a forward search,
1536 the last character in a backward search. */
1538 base_pat
= pat_end
- 1;
1540 BM_tab_base
= BM_tab
;
1542 j
= dirlen
; /* to get it in a register */
1543 /* A character that does not appear in the pattern induces a */
1544 /* stride equal to the pattern length. */
1545 while (BM_tab_base
!= BM_tab
)
1553 /* We use this for translation, instead of TRT itself.
1554 We fill this in to handle the characters that actually
1555 occur in the pattern. Others don't matter anyway! */
1556 bzero (simple_translate
, sizeof simple_translate
);
1557 for (i
= 0; i
< 0400; i
++)
1558 simple_translate
[i
] = i
;
1561 while (i
!= infinity
)
1563 unsigned char *ptr
= base_pat
+ i
;
1571 int this_translated
= 1;
1574 /* Is *PTR the last byte of a character? */
1575 && (pat_end
- ptr
== 1 || CHAR_HEAD_P (ptr
[1])))
1577 unsigned char *charstart
= ptr
;
1578 while (! CHAR_HEAD_P (*charstart
))
1580 untranslated
= STRING_CHAR (charstart
, ptr
- charstart
+ 1);
1582 == (ASCII_CHAR_P (untranslated
) ? 0 : untranslated
& ~0x3F))
1584 TRANSLATE (ch
, trt
, untranslated
);
1585 if (! CHAR_HEAD_P (*ptr
))
1587 translate_prev_byte
= ptr
[-1];
1588 if (! CHAR_HEAD_P (translate_prev_byte
))
1589 translate_anteprev_byte
= ptr
[-2];
1594 this_translated
= 0;
1598 else if (!multibyte
)
1599 TRANSLATE (ch
, trt
, *ptr
);
1603 this_translated
= 0;
1608 j
= (ch
& 0x3F) | 0200;
1610 j
= (unsigned char) ch
;
1613 stride_for_teases
= BM_tab
[j
];
1615 BM_tab
[j
] = dirlen
- i
;
1616 /* A translation table is accompanied by its inverse -- see */
1617 /* comment following downcase_table for details */
1618 if (this_translated
)
1620 int starting_ch
= ch
;
1624 TRANSLATE (ch
, inverse_trt
, ch
);
1626 j
= (ch
& 0x3F) | 0200;
1628 j
= (unsigned char) ch
;
1630 /* For all the characters that map into CH,
1631 set up simple_translate to map the last byte
1633 simple_translate
[j
] = starting_j
;
1634 if (ch
== starting_ch
)
1636 BM_tab
[j
] = dirlen
- i
;
1645 stride_for_teases
= BM_tab
[j
];
1646 BM_tab
[j
] = dirlen
- i
;
1648 /* stride_for_teases tells how much to stride if we get a */
1649 /* match on the far character but are subsequently */
1650 /* disappointed, by recording what the stride would have been */
1651 /* for that character if the last character had been */
1654 infinity
= dirlen
- infinity
;
1655 pos_byte
+= dirlen
- ((direction
> 0) ? direction
: 0);
1656 /* loop invariant - POS_BYTE points at where last char (first
1657 char if reverse) of pattern would align in a possible match. */
1661 unsigned char *tail_end_ptr
;
1663 /* It's been reported that some (broken) compiler thinks that
1664 Boolean expressions in an arithmetic context are unsigned.
1665 Using an explicit ?1:0 prevents this. */
1666 if ((lim_byte
- pos_byte
- ((direction
> 0) ? 1 : 0)) * direction
1668 return (n
* (0 - direction
));
1669 /* First we do the part we can by pointers (maybe nothing) */
1672 limit
= pos_byte
- dirlen
+ direction
;
1675 limit
= BUFFER_CEILING_OF (limit
);
1676 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1677 can take on without hitting edge of buffer or the gap. */
1678 limit
= min (limit
, pos_byte
+ 20000);
1679 limit
= min (limit
, lim_byte
- 1);
1683 limit
= BUFFER_FLOOR_OF (limit
);
1684 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1685 can take on without hitting edge of buffer or the gap. */
1686 limit
= max (limit
, pos_byte
- 20000);
1687 limit
= max (limit
, lim_byte
);
1689 tail_end
= BUFFER_CEILING_OF (pos_byte
) + 1;
1690 tail_end_ptr
= BYTE_POS_ADDR (tail_end
);
1692 if ((limit
- pos_byte
) * direction
> 20)
1696 p_limit
= BYTE_POS_ADDR (limit
);
1697 p2
= (cursor
= BYTE_POS_ADDR (pos_byte
));
1698 /* In this loop, pos + cursor - p2 is the surrogate for pos */
1699 while (1) /* use one cursor setting as long as i can */
1701 if (direction
> 0) /* worth duplicating */
1703 /* Use signed comparison if appropriate
1704 to make cursor+infinity sure to be > p_limit.
1705 Assuming that the buffer lies in a range of addresses
1706 that are all "positive" (as ints) or all "negative",
1707 either kind of comparison will work as long
1708 as we don't step by infinity. So pick the kind
1709 that works when we do step by infinity. */
1710 if ((EMACS_INT
) (p_limit
+ infinity
) > (EMACS_INT
) p_limit
)
1711 while ((EMACS_INT
) cursor
<= (EMACS_INT
) p_limit
)
1712 cursor
+= BM_tab
[*cursor
];
1714 while ((EMACS_UINT
) cursor
<= (EMACS_UINT
) p_limit
)
1715 cursor
+= BM_tab
[*cursor
];
1719 if ((EMACS_INT
) (p_limit
+ infinity
) < (EMACS_INT
) p_limit
)
1720 while ((EMACS_INT
) cursor
>= (EMACS_INT
) p_limit
)
1721 cursor
+= BM_tab
[*cursor
];
1723 while ((EMACS_UINT
) cursor
>= (EMACS_UINT
) p_limit
)
1724 cursor
+= BM_tab
[*cursor
];
1726 /* If you are here, cursor is beyond the end of the searched region. */
1727 /* This can happen if you match on the far character of the pattern, */
1728 /* because the "stride" of that character is infinity, a number able */
1729 /* to throw you well beyond the end of the search. It can also */
1730 /* happen if you fail to match within the permitted region and would */
1731 /* otherwise try a character beyond that region */
1732 if ((cursor
- p_limit
) * direction
<= len_byte
)
1733 break; /* a small overrun is genuine */
1734 cursor
-= infinity
; /* large overrun = hit */
1735 i
= dirlen
- direction
;
1738 while ((i
-= direction
) + direction
!= 0)
1741 cursor
-= direction
;
1742 /* Translate only the last byte of a character. */
1744 || ((cursor
== tail_end_ptr
1745 || CHAR_HEAD_P (cursor
[1]))
1746 && (CHAR_HEAD_P (cursor
[0])
1747 || (translate_prev_byte
== cursor
[-1]
1748 && (CHAR_HEAD_P (translate_prev_byte
)
1749 || translate_anteprev_byte
== cursor
[-2])))))
1750 ch
= simple_translate
[*cursor
];
1759 while ((i
-= direction
) + direction
!= 0)
1761 cursor
-= direction
;
1762 if (pat
[i
] != *cursor
)
1766 cursor
+= dirlen
- i
- direction
; /* fix cursor */
1767 if (i
+ direction
== 0)
1771 cursor
-= direction
;
1773 position
= pos_byte
+ cursor
- p2
+ ((direction
> 0)
1774 ? 1 - len_byte
: 0);
1775 set_search_regs (position
, len_byte
);
1777 if ((n
-= direction
) != 0)
1778 cursor
+= dirlen
; /* to resume search */
1780 return ((direction
> 0)
1781 ? search_regs
.end
[0] : search_regs
.start
[0]);
1784 cursor
+= stride_for_teases
; /* <sigh> we lose - */
1786 pos_byte
+= cursor
- p2
;
1789 /* Now we'll pick up a clump that has to be done the hard */
1790 /* way because it covers a discontinuity */
1792 limit
= ((direction
> 0)
1793 ? BUFFER_CEILING_OF (pos_byte
- dirlen
+ 1)
1794 : BUFFER_FLOOR_OF (pos_byte
- dirlen
- 1));
1795 limit
= ((direction
> 0)
1796 ? min (limit
+ len_byte
, lim_byte
- 1)
1797 : max (limit
- len_byte
, lim_byte
));
1798 /* LIMIT is now the last value POS_BYTE can have
1799 and still be valid for a possible match. */
1802 /* This loop can be coded for space rather than */
1803 /* speed because it will usually run only once. */
1804 /* (the reach is at most len + 21, and typically */
1805 /* does not exceed len) */
1806 while ((limit
- pos_byte
) * direction
>= 0)
1807 pos_byte
+= BM_tab
[FETCH_BYTE (pos_byte
)];
1808 /* now run the same tests to distinguish going off the */
1809 /* end, a match or a phony match. */
1810 if ((pos_byte
- limit
) * direction
<= len_byte
)
1811 break; /* ran off the end */
1812 /* Found what might be a match.
1813 Set POS_BYTE back to last (first if reverse) pos. */
1814 pos_byte
-= infinity
;
1815 i
= dirlen
- direction
;
1816 while ((i
-= direction
) + direction
!= 0)
1820 pos_byte
-= direction
;
1821 ptr
= BYTE_POS_ADDR (pos_byte
);
1822 /* Translate only the last byte of a character. */
1824 || ((ptr
== tail_end_ptr
1825 || CHAR_HEAD_P (ptr
[1]))
1826 && (CHAR_HEAD_P (ptr
[0])
1827 || (translate_prev_byte
== ptr
[-1]
1828 && (CHAR_HEAD_P (translate_prev_byte
)
1829 || translate_anteprev_byte
== ptr
[-2])))))
1830 ch
= simple_translate
[*ptr
];
1836 /* Above loop has moved POS_BYTE part or all the way
1837 back to the first pos (last pos if reverse).
1838 Set it once again at the last (first if reverse) char. */
1839 pos_byte
+= dirlen
- i
- direction
;
1840 if (i
+ direction
== 0)
1843 pos_byte
-= direction
;
1845 position
= pos_byte
+ ((direction
> 0) ? 1 - len_byte
: 0);
1847 set_search_regs (position
, len_byte
);
1849 if ((n
-= direction
) != 0)
1850 pos_byte
+= dirlen
; /* to resume search */
1852 return ((direction
> 0)
1853 ? search_regs
.end
[0] : search_regs
.start
[0]);
1856 pos_byte
+= stride_for_teases
;
1859 /* We have done one clump. Can we continue? */
1860 if ((lim_byte
- pos_byte
) * direction
< 0)
1861 return ((0 - n
) * direction
);
1863 return BYTE_TO_CHAR (pos_byte
);
1866 /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES
1867 for the overall match just found in the current buffer.
1868 Also clear out the match data for registers 1 and up. */
1871 set_search_regs (beg_byte
, nbytes
)
1872 int beg_byte
, nbytes
;
1876 /* Make sure we have registers in which to store
1877 the match position. */
1878 if (search_regs
.num_regs
== 0)
1880 search_regs
.start
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
1881 search_regs
.end
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
1882 search_regs
.num_regs
= 2;
1885 /* Clear out the other registers. */
1886 for (i
= 1; i
< search_regs
.num_regs
; i
++)
1888 search_regs
.start
[i
] = -1;
1889 search_regs
.end
[i
] = -1;
1892 search_regs
.start
[0] = BYTE_TO_CHAR (beg_byte
);
1893 search_regs
.end
[0] = BYTE_TO_CHAR (beg_byte
+ nbytes
);
1894 XSETBUFFER (last_thing_searched
, current_buffer
);
1897 /* Given a string of words separated by word delimiters,
1898 compute a regexp that matches those exact words
1899 separated by arbitrary punctuation. */
1905 register unsigned char *p
, *o
;
1906 register int i
, i_byte
, len
, punct_count
= 0, word_count
= 0;
1911 CHECK_STRING (string
);
1913 len
= SCHARS (string
);
1915 for (i
= 0, i_byte
= 0; i
< len
; )
1919 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, i
, i_byte
);
1921 if (SYNTAX (c
) != Sword
)
1924 if (i
> 0 && SYNTAX (prev_c
) == Sword
)
1931 if (SYNTAX (prev_c
) == Sword
)
1934 return empty_string
;
1936 adjust
= - punct_count
+ 5 * (word_count
- 1) + 4;
1937 if (STRING_MULTIBYTE (string
))
1938 val
= make_uninit_multibyte_string (len
+ adjust
,
1942 val
= make_uninit_string (len
+ adjust
);
1949 for (i
= 0, i_byte
= 0; i
< len
; )
1952 int i_byte_orig
= i_byte
;
1954 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, i
, i_byte
);
1956 if (SYNTAX (c
) == Sword
)
1958 bcopy (SDATA (string
) + i_byte_orig
, o
,
1959 i_byte
- i_byte_orig
);
1960 o
+= i_byte
- i_byte_orig
;
1962 else if (i
> 0 && SYNTAX (prev_c
) == Sword
&& --word_count
)
1980 DEFUN ("search-backward", Fsearch_backward
, Ssearch_backward
, 1, 4,
1981 "MSearch backward: ",
1982 doc
: /* Search backward from point for STRING.
1983 Set point to the beginning of the occurrence found, and return point.
1984 An optional second argument bounds the search; it is a buffer position.
1985 The match found must not extend before that position.
1986 Optional third argument, if t, means if fail just return nil (no error).
1987 If not nil and not t, position at limit of search and return nil.
1988 Optional fourth argument is repeat count--search for successive occurrences.
1990 Search case-sensitivity is determined by the value of the variable
1991 `case-fold-search', which see.
1993 See also the functions `match-beginning', `match-end' and `replace-match'. */)
1994 (string
, bound
, noerror
, count
)
1995 Lisp_Object string
, bound
, noerror
, count
;
1997 return search_command (string
, bound
, noerror
, count
, -1, 0, 0);
2000 DEFUN ("search-forward", Fsearch_forward
, Ssearch_forward
, 1, 4, "MSearch: ",
2001 doc
: /* Search forward from point for STRING.
2002 Set point to the end of the occurrence found, and return point.
2003 An optional second argument bounds the search; it is a buffer position.
2004 The match found must not extend after that position. nil is equivalent
2006 Optional third argument, if t, means if fail just return nil (no error).
2007 If not nil and not t, move to limit of search and return nil.
2008 Optional fourth argument is repeat count--search for successive occurrences.
2010 Search case-sensitivity is determined by the value of the variable
2011 `case-fold-search', which see.
2013 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2014 (string
, bound
, noerror
, count
)
2015 Lisp_Object string
, bound
, noerror
, count
;
2017 return search_command (string
, bound
, noerror
, count
, 1, 0, 0);
2020 DEFUN ("word-search-backward", Fword_search_backward
, Sword_search_backward
, 1, 4,
2021 "sWord search backward: ",
2022 doc
: /* Search backward from point for STRING, ignoring differences in punctuation.
2023 Set point to the beginning of the occurrence found, and return point.
2024 An optional second argument bounds the search; it is a buffer position.
2025 The match found must not extend before that position.
2026 Optional third argument, if t, means if fail just return nil (no error).
2027 If not nil and not t, move to limit of search and return nil.
2028 Optional fourth argument is repeat count--search for successive occurrences. */)
2029 (string
, bound
, noerror
, count
)
2030 Lisp_Object string
, bound
, noerror
, count
;
2032 return search_command (wordify (string
), bound
, noerror
, count
, -1, 1, 0);
2035 DEFUN ("word-search-forward", Fword_search_forward
, Sword_search_forward
, 1, 4,
2037 doc
: /* Search forward from point for STRING, ignoring differences in punctuation.
2038 Set point to the end of the occurrence found, and return point.
2039 An optional second argument bounds the search; it is a buffer position.
2040 The match found must not extend after that position.
2041 Optional third argument, if t, means if fail just return nil (no error).
2042 If not nil and not t, move to limit of search and return nil.
2043 Optional fourth argument is repeat count--search for successive occurrences. */)
2044 (string
, bound
, noerror
, count
)
2045 Lisp_Object string
, bound
, noerror
, count
;
2047 return search_command (wordify (string
), bound
, noerror
, count
, 1, 1, 0);
2050 DEFUN ("re-search-backward", Fre_search_backward
, Sre_search_backward
, 1, 4,
2051 "sRE search backward: ",
2052 doc
: /* Search backward from point for match for regular expression REGEXP.
2053 Set point to the beginning of the match, and return point.
2054 The match found is the one starting last in the buffer
2055 and yet ending before the origin of the search.
2056 An optional second argument bounds the search; it is a buffer position.
2057 The match found must start at or after that position.
2058 Optional third argument, if t, means if fail just return nil (no error).
2059 If not nil and not t, move to limit of search and return nil.
2060 Optional fourth argument is repeat count--search for successive occurrences.
2061 See also the functions `match-beginning', `match-end', `match-string',
2062 and `replace-match'. */)
2063 (regexp
, bound
, noerror
, count
)
2064 Lisp_Object regexp
, bound
, noerror
, count
;
2066 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 0);
2069 DEFUN ("re-search-forward", Fre_search_forward
, Sre_search_forward
, 1, 4,
2071 doc
: /* Search forward from point for regular expression REGEXP.
2072 Set point to the end of the occurrence found, and return point.
2073 An optional second argument bounds the search; it is a buffer position.
2074 The match found must not extend after that position.
2075 Optional third argument, if t, means if fail just return nil (no error).
2076 If not nil and not t, move to limit of search and return nil.
2077 Optional fourth argument is repeat count--search for successive occurrences.
2078 See also the functions `match-beginning', `match-end', `match-string',
2079 and `replace-match'. */)
2080 (regexp
, bound
, noerror
, count
)
2081 Lisp_Object regexp
, bound
, noerror
, count
;
2083 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 0);
2086 DEFUN ("posix-search-backward", Fposix_search_backward
, Sposix_search_backward
, 1, 4,
2087 "sPosix search backward: ",
2088 doc
: /* Search backward from point for match for regular expression REGEXP.
2089 Find the longest match in accord with Posix regular expression rules.
2090 Set point to the beginning of the match, and return point.
2091 The match found is the one starting last in the buffer
2092 and yet ending before the origin of the search.
2093 An optional second argument bounds the search; it is a buffer position.
2094 The match found must start at or after that position.
2095 Optional third argument, if t, means if fail just return nil (no error).
2096 If not nil and not t, move to limit of search and return nil.
2097 Optional fourth argument is repeat count--search for successive occurrences.
2098 See also the functions `match-beginning', `match-end', `match-string',
2099 and `replace-match'. */)
2100 (regexp
, bound
, noerror
, count
)
2101 Lisp_Object regexp
, bound
, noerror
, count
;
2103 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 1);
2106 DEFUN ("posix-search-forward", Fposix_search_forward
, Sposix_search_forward
, 1, 4,
2108 doc
: /* Search forward from point for regular expression REGEXP.
2109 Find the longest match in accord with Posix regular expression rules.
2110 Set point to the end of the occurrence found, and return point.
2111 An optional second argument bounds the search; it is a buffer position.
2112 The match found must not extend after that position.
2113 Optional third argument, if t, means if fail just return nil (no error).
2114 If not nil and not t, move to limit of search and return nil.
2115 Optional fourth argument is repeat count--search for successive occurrences.
2116 See also the functions `match-beginning', `match-end', `match-string',
2117 and `replace-match'. */)
2118 (regexp
, bound
, noerror
, count
)
2119 Lisp_Object regexp
, bound
, noerror
, count
;
2121 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 1);
2124 DEFUN ("replace-match", Freplace_match
, Sreplace_match
, 1, 5, 0,
2125 doc
: /* Replace text matched by last search with NEWTEXT.
2126 Leave point at the end of the replacement text.
2128 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
2129 Otherwise maybe capitalize the whole text, or maybe just word initials,
2130 based on the replaced text.
2131 If the replaced text has only capital letters
2132 and has at least one multiletter word, convert NEWTEXT to all caps.
2133 Otherwise if all words are capitalized in the replaced text,
2134 capitalize each word in NEWTEXT.
2136 If third arg LITERAL is non-nil, insert NEWTEXT literally.
2137 Otherwise treat `\\' as special:
2138 `\\&' in NEWTEXT means substitute original matched text.
2139 `\\N' means substitute what matched the Nth `\\(...\\)'.
2140 If Nth parens didn't match, substitute nothing.
2141 `\\\\' means insert one `\\'.
2142 Case conversion does not apply to these substitutions.
2144 FIXEDCASE and LITERAL are optional arguments.
2146 The optional fourth argument STRING can be a string to modify.
2147 This is meaningful when the previous match was done against STRING,
2148 using `string-match'. When used this way, `replace-match'
2149 creates and returns a new string made by copying STRING and replacing
2150 the part of STRING that was matched.
2152 The optional fifth argument SUBEXP specifies a subexpression;
2153 it says to replace just that subexpression with NEWTEXT,
2154 rather than replacing the entire matched text.
2155 This is, in a vague sense, the inverse of using `\\N' in NEWTEXT;
2156 `\\N' copies subexp N into NEWTEXT, but using N as SUBEXP puts
2157 NEWTEXT in place of subexp N.
2158 This is useful only after a regular expression search or match,
2159 since only regular expressions have distinguished subexpressions. */)
2160 (newtext
, fixedcase
, literal
, string
, subexp
)
2161 Lisp_Object newtext
, fixedcase
, literal
, string
, subexp
;
2163 enum { nochange
, all_caps
, cap_initial
} case_action
;
2164 register int pos
, pos_byte
;
2165 int some_multiletter_word
;
2168 int some_nonuppercase_initial
;
2169 register int c
, prevc
;
2171 int opoint
, newpoint
;
2173 CHECK_STRING (newtext
);
2175 if (! NILP (string
))
2176 CHECK_STRING (string
);
2178 case_action
= nochange
; /* We tried an initialization */
2179 /* but some C compilers blew it */
2181 if (search_regs
.num_regs
<= 0)
2182 error ("replace-match called before any match found");
2188 CHECK_NUMBER (subexp
);
2189 sub
= XINT (subexp
);
2190 if (sub
< 0 || sub
>= search_regs
.num_regs
)
2191 args_out_of_range (subexp
, make_number (search_regs
.num_regs
));
2196 if (search_regs
.start
[sub
] < BEGV
2197 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2198 || search_regs
.end
[sub
] > ZV
)
2199 args_out_of_range (make_number (search_regs
.start
[sub
]),
2200 make_number (search_regs
.end
[sub
]));
2204 if (search_regs
.start
[sub
] < 0
2205 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2206 || search_regs
.end
[sub
] > SCHARS (string
))
2207 args_out_of_range (make_number (search_regs
.start
[sub
]),
2208 make_number (search_regs
.end
[sub
]));
2211 if (NILP (fixedcase
))
2213 /* Decide how to casify by examining the matched text. */
2216 pos
= search_regs
.start
[sub
];
2217 last
= search_regs
.end
[sub
];
2220 pos_byte
= CHAR_TO_BYTE (pos
);
2222 pos_byte
= string_char_to_byte (string
, pos
);
2225 case_action
= all_caps
;
2227 /* some_multiletter_word is set nonzero if any original word
2228 is more than one letter long. */
2229 some_multiletter_word
= 0;
2231 some_nonuppercase_initial
= 0;
2238 c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
);
2239 INC_BOTH (pos
, pos_byte
);
2242 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, pos
, pos_byte
);
2246 /* Cannot be all caps if any original char is lower case */
2249 if (SYNTAX (prevc
) != Sword
)
2250 some_nonuppercase_initial
= 1;
2252 some_multiletter_word
= 1;
2254 else if (!NOCASEP (c
))
2257 if (SYNTAX (prevc
) != Sword
)
2260 some_multiletter_word
= 1;
2264 /* If the initial is a caseless word constituent,
2265 treat that like a lowercase initial. */
2266 if (SYNTAX (prevc
) != Sword
)
2267 some_nonuppercase_initial
= 1;
2273 /* Convert to all caps if the old text is all caps
2274 and has at least one multiletter word. */
2275 if (! some_lowercase
&& some_multiletter_word
)
2276 case_action
= all_caps
;
2277 /* Capitalize each word, if the old text has all capitalized words. */
2278 else if (!some_nonuppercase_initial
&& some_multiletter_word
)
2279 case_action
= cap_initial
;
2280 else if (!some_nonuppercase_initial
&& some_uppercase
)
2281 /* Should x -> yz, operating on X, give Yz or YZ?
2282 We'll assume the latter. */
2283 case_action
= all_caps
;
2285 case_action
= nochange
;
2288 /* Do replacement in a string. */
2291 Lisp_Object before
, after
;
2293 before
= Fsubstring (string
, make_number (0),
2294 make_number (search_regs
.start
[sub
]));
2295 after
= Fsubstring (string
, make_number (search_regs
.end
[sub
]), Qnil
);
2297 /* Substitute parts of the match into NEWTEXT
2302 int lastpos_byte
= 0;
2303 /* We build up the substituted string in ACCUM. */
2306 int length
= SBYTES (newtext
);
2310 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2314 int delbackslash
= 0;
2316 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2320 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2324 substart
= search_regs
.start
[sub
];
2325 subend
= search_regs
.end
[sub
];
2327 else if (c
>= '1' && c
<= '9')
2329 if (search_regs
.start
[c
- '0'] >= 0
2330 && c
<= search_regs
.num_regs
+ '0')
2332 substart
= search_regs
.start
[c
- '0'];
2333 subend
= search_regs
.end
[c
- '0'];
2337 /* If that subexp did not match,
2338 replace \\N with nothing. */
2346 error ("Invalid use of `\\' in replacement text");
2350 if (pos
- 2 != lastpos
)
2351 middle
= substring_both (newtext
, lastpos
,
2353 pos
- 2, pos_byte
- 2);
2356 accum
= concat3 (accum
, middle
,
2358 make_number (substart
),
2359 make_number (subend
)));
2361 lastpos_byte
= pos_byte
;
2363 else if (delbackslash
)
2365 middle
= substring_both (newtext
, lastpos
,
2367 pos
- 1, pos_byte
- 1);
2369 accum
= concat2 (accum
, middle
);
2371 lastpos_byte
= pos_byte
;
2376 middle
= substring_both (newtext
, lastpos
,
2382 newtext
= concat2 (accum
, middle
);
2385 /* Do case substitution in NEWTEXT if desired. */
2386 if (case_action
== all_caps
)
2387 newtext
= Fupcase (newtext
);
2388 else if (case_action
== cap_initial
)
2389 newtext
= Fupcase_initials (newtext
);
2391 return concat3 (before
, newtext
, after
);
2394 /* Record point, then move (quietly) to the start of the match. */
2395 if (PT
>= search_regs
.end
[sub
])
2397 else if (PT
> search_regs
.start
[sub
])
2398 opoint
= search_regs
.end
[sub
] - ZV
;
2402 /* If we want non-literal replacement,
2403 perform substitution on the replacement string. */
2406 int length
= SBYTES (newtext
);
2407 unsigned char *substed
;
2408 int substed_alloc_size
, substed_len
;
2409 int buf_multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
2410 int str_multibyte
= STRING_MULTIBYTE (newtext
);
2411 Lisp_Object rev_tbl
;
2412 int really_changed
= 0;
2416 substed_alloc_size
= length
* 2 + 100;
2417 substed
= (unsigned char *) xmalloc (substed_alloc_size
+ 1);
2420 /* Go thru NEWTEXT, producing the actual text to insert in
2421 SUBSTED while adjusting multibyteness to that of the current
2424 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2426 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2427 unsigned char *add_stuff
= NULL
;
2433 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
, pos
, pos_byte
);
2435 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2439 /* Note that we don't have to increment POS. */
2440 c
= SREF (newtext
, pos_byte
++);
2442 c
= unibyte_char_to_multibyte (c
);
2445 /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
2446 or set IDX to a match index, which means put that part
2447 of the buffer text into SUBSTED. */
2455 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
,
2457 if (!buf_multibyte
&& !ASCII_CHAR_P (c
))
2458 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2462 c
= SREF (newtext
, pos_byte
++);
2464 c
= unibyte_char_to_multibyte (c
);
2469 else if (c
>= '1' && c
<= '9' && c
<= search_regs
.num_regs
+ '0')
2471 if (search_regs
.start
[c
- '0'] >= 1)
2475 add_len
= 1, add_stuff
= "\\";
2479 error ("Invalid use of `\\' in replacement text");
2484 add_len
= CHAR_STRING (c
, str
);
2488 /* If we want to copy part of a previous match,
2489 set up ADD_STUFF and ADD_LEN to point to it. */
2492 int begbyte
= CHAR_TO_BYTE (search_regs
.start
[idx
]);
2493 add_len
= CHAR_TO_BYTE (search_regs
.end
[idx
]) - begbyte
;
2494 if (search_regs
.start
[idx
] < GPT
&& GPT
< search_regs
.end
[idx
])
2495 move_gap (search_regs
.start
[idx
]);
2496 add_stuff
= BYTE_POS_ADDR (begbyte
);
2499 /* Now the stuff we want to add to SUBSTED
2500 is invariably ADD_LEN bytes starting at ADD_STUFF. */
2502 /* Make sure SUBSTED is big enough. */
2503 if (substed_len
+ add_len
>= substed_alloc_size
)
2505 substed_alloc_size
= substed_len
+ add_len
+ 500;
2506 substed
= (unsigned char *) xrealloc (substed
,
2507 substed_alloc_size
+ 1);
2510 /* Now add to the end of SUBSTED. */
2513 bcopy (add_stuff
, substed
+ substed_len
, add_len
);
2514 substed_len
+= add_len
;
2522 int nchars
= multibyte_chars_in_text (substed
, substed_len
);
2524 newtext
= make_multibyte_string (substed
, nchars
, substed_len
);
2527 newtext
= make_unibyte_string (substed
, substed_len
);
2532 /* Replace the old text with the new in the cleanest possible way. */
2533 replace_range (search_regs
.start
[sub
], search_regs
.end
[sub
],
2535 newpoint
= search_regs
.start
[sub
] + SCHARS (newtext
);
2537 if (case_action
== all_caps
)
2538 Fupcase_region (make_number (search_regs
.start
[sub
]),
2539 make_number (newpoint
));
2540 else if (case_action
== cap_initial
)
2541 Fupcase_initials_region (make_number (search_regs
.start
[sub
]),
2542 make_number (newpoint
));
2544 /* Adjust search data for this change. */
2546 int oldend
= search_regs
.end
[sub
];
2547 int oldstart
= search_regs
.start
[sub
];
2548 int change
= newpoint
- search_regs
.end
[sub
];
2551 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2553 if (search_regs
.start
[i
] >= oldend
)
2554 search_regs
.start
[i
] += change
;
2555 else if (search_regs
.start
[i
] > oldstart
)
2556 search_regs
.start
[i
] = oldstart
;
2557 if (search_regs
.end
[i
] >= oldend
)
2558 search_regs
.end
[i
] += change
;
2559 else if (search_regs
.end
[i
] > oldstart
)
2560 search_regs
.end
[i
] = oldstart
;
2564 /* Put point back where it was in the text. */
2566 TEMP_SET_PT (opoint
+ ZV
);
2568 TEMP_SET_PT (opoint
);
2570 /* Now move point "officially" to the start of the inserted replacement. */
2571 move_if_not_intangible (newpoint
);
2577 match_limit (num
, beginningp
)
2586 args_out_of_range (num
, make_number (0));
2587 if (search_regs
.num_regs
<= 0)
2588 error ("No match data, because no search succeeded");
2589 if (n
>= search_regs
.num_regs
2590 || search_regs
.start
[n
] < 0)
2592 return (make_number ((beginningp
) ? search_regs
.start
[n
]
2593 : search_regs
.end
[n
]));
2596 DEFUN ("match-beginning", Fmatch_beginning
, Smatch_beginning
, 1, 1, 0,
2597 doc
: /* Return position of start of text matched by last search.
2598 SUBEXP, a number, specifies which parenthesized expression in the last
2600 Value is nil if SUBEXPth pair didn't match, or there were less than
2602 Zero means the entire text matched by the whole regexp or whole string. */)
2606 return match_limit (subexp
, 1);
2609 DEFUN ("match-end", Fmatch_end
, Smatch_end
, 1, 1, 0,
2610 doc
: /* Return position of end of text matched by last search.
2611 SUBEXP, a number, specifies which parenthesized expression in the last
2613 Value is nil if SUBEXPth pair didn't match, or there were less than
2615 Zero means the entire text matched by the whole regexp or whole string. */)
2619 return match_limit (subexp
, 0);
2622 DEFUN ("match-data", Fmatch_data
, Smatch_data
, 0, 2, 0,
2623 doc
: /* Return a list containing all info on what the last search matched.
2624 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
2625 All the elements are markers or nil (nil if the Nth pair didn't match)
2626 if the last match was on a buffer; integers or nil if a string was matched.
2627 Use `store-match-data' to reinstate the data in this list.
2629 If INTEGERS (the optional first argument) is non-nil, always use
2630 integers \(rather than markers) to represent buffer positions. In
2631 this case, and if the last match was in a buffer, the buffer will get
2632 stored as one additional element at the end of the list.
2634 If REUSE is a list, reuse it as part of the value. If REUSE is long enough
2635 to hold all the values, and if INTEGERS is non-nil, no consing is done.
2637 Return value is undefined if the last search failed. */)
2639 Lisp_Object integers
, reuse
;
2641 Lisp_Object tail
, prev
;
2645 if (NILP (last_thing_searched
))
2650 data
= (Lisp_Object
*) alloca ((2 * search_regs
.num_regs
+ 1)
2651 * sizeof (Lisp_Object
));
2654 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2656 int start
= search_regs
.start
[i
];
2659 if (EQ (last_thing_searched
, Qt
)
2660 || ! NILP (integers
))
2662 XSETFASTINT (data
[2 * i
], start
);
2663 XSETFASTINT (data
[2 * i
+ 1], search_regs
.end
[i
]);
2665 else if (BUFFERP (last_thing_searched
))
2667 data
[2 * i
] = Fmake_marker ();
2668 Fset_marker (data
[2 * i
],
2669 make_number (start
),
2670 last_thing_searched
);
2671 data
[2 * i
+ 1] = Fmake_marker ();
2672 Fset_marker (data
[2 * i
+ 1],
2673 make_number (search_regs
.end
[i
]),
2674 last_thing_searched
);
2677 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
2683 data
[2 * i
] = data
[2 * i
+ 1] = Qnil
;
2686 if (BUFFERP (last_thing_searched
) && !NILP (integers
))
2688 data
[len
] = last_thing_searched
;
2692 /* If REUSE is not usable, cons up the values and return them. */
2693 if (! CONSP (reuse
))
2694 return Flist (len
, data
);
2696 /* If REUSE is a list, store as many value elements as will fit
2697 into the elements of REUSE. */
2698 for (i
= 0, tail
= reuse
; CONSP (tail
);
2699 i
++, tail
= XCDR (tail
))
2702 XSETCAR (tail
, data
[i
]);
2704 XSETCAR (tail
, Qnil
);
2708 /* If we couldn't fit all value elements into REUSE,
2709 cons up the rest of them and add them to the end of REUSE. */
2711 XSETCDR (prev
, Flist (len
- i
, data
+ i
));
2717 DEFUN ("set-match-data", Fset_match_data
, Sset_match_data
, 1, 1, 0,
2718 doc
: /* Set internal data on last search match from elements of LIST.
2719 LIST should have been created by calling `match-data' previously. */)
2721 register Lisp_Object list
;
2724 register Lisp_Object marker
;
2726 if (running_asynch_code
)
2727 save_search_regs ();
2729 if (!CONSP (list
) && !NILP (list
))
2730 list
= wrong_type_argument (Qconsp
, list
);
2732 /* Unless we find a marker with a buffer or an explicit buffer
2733 in LIST, assume that this match data came from a string. */
2734 last_thing_searched
= Qt
;
2736 /* Allocate registers if they don't already exist. */
2738 int length
= XFASTINT (Flength (list
)) / 2;
2740 if (length
> search_regs
.num_regs
)
2742 if (search_regs
.num_regs
== 0)
2745 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2747 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
2752 = (regoff_t
*) xrealloc (search_regs
.start
,
2753 length
* sizeof (regoff_t
));
2755 = (regoff_t
*) xrealloc (search_regs
.end
,
2756 length
* sizeof (regoff_t
));
2759 for (i
= search_regs
.num_regs
; i
< length
; i
++)
2760 search_regs
.start
[i
] = -1;
2762 search_regs
.num_regs
= length
;
2767 marker
= Fcar (list
);
2768 if (BUFFERP (marker
))
2770 last_thing_searched
= marker
;
2777 search_regs
.start
[i
] = -1;
2784 if (MARKERP (marker
))
2786 if (XMARKER (marker
)->buffer
== 0)
2787 XSETFASTINT (marker
, 0);
2789 XSETBUFFER (last_thing_searched
, XMARKER (marker
)->buffer
);
2792 CHECK_NUMBER_COERCE_MARKER (marker
);
2793 from
= XINT (marker
);
2796 marker
= Fcar (list
);
2797 if (MARKERP (marker
) && XMARKER (marker
)->buffer
== 0)
2798 XSETFASTINT (marker
, 0);
2800 CHECK_NUMBER_COERCE_MARKER (marker
);
2801 search_regs
.start
[i
] = from
;
2802 search_regs
.end
[i
] = XINT (marker
);
2807 for (; i
< search_regs
.num_regs
; i
++)
2808 search_regs
.start
[i
] = -1;
2814 /* If non-zero the match data have been saved in saved_search_regs
2815 during the execution of a sentinel or filter. */
2816 static int search_regs_saved
;
2817 static struct re_registers saved_search_regs
;
2818 static Lisp_Object saved_last_thing_searched
;
2820 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
2821 if asynchronous code (filter or sentinel) is running. */
2825 if (!search_regs_saved
)
2827 saved_search_regs
.num_regs
= search_regs
.num_regs
;
2828 saved_search_regs
.start
= search_regs
.start
;
2829 saved_search_regs
.end
= search_regs
.end
;
2830 saved_last_thing_searched
= last_thing_searched
;
2831 last_thing_searched
= Qnil
;
2832 search_regs
.num_regs
= 0;
2833 search_regs
.start
= 0;
2834 search_regs
.end
= 0;
2836 search_regs_saved
= 1;
2840 /* Called upon exit from filters and sentinels. */
2842 restore_match_data ()
2844 if (search_regs_saved
)
2846 if (search_regs
.num_regs
> 0)
2848 xfree (search_regs
.start
);
2849 xfree (search_regs
.end
);
2851 search_regs
.num_regs
= saved_search_regs
.num_regs
;
2852 search_regs
.start
= saved_search_regs
.start
;
2853 search_regs
.end
= saved_search_regs
.end
;
2854 last_thing_searched
= saved_last_thing_searched
;
2855 saved_last_thing_searched
= Qnil
;
2856 search_regs_saved
= 0;
2860 /* Quote a string to inactivate reg-expr chars */
2862 DEFUN ("regexp-quote", Fregexp_quote
, Sregexp_quote
, 1, 1, 0,
2863 doc
: /* Return a regexp string which matches exactly STRING and nothing else. */)
2867 register unsigned char *in
, *out
, *end
;
2868 register unsigned char *temp
;
2869 int backslashes_added
= 0;
2871 CHECK_STRING (string
);
2873 temp
= (unsigned char *) alloca (SBYTES (string
) * 2);
2875 /* Now copy the data into the new string, inserting escapes. */
2877 in
= SDATA (string
);
2878 end
= in
+ SBYTES (string
);
2881 for (; in
!= end
; in
++)
2883 if (*in
== '[' || *in
== ']'
2884 || *in
== '*' || *in
== '.' || *in
== '\\'
2885 || *in
== '?' || *in
== '+'
2886 || *in
== '^' || *in
== '$')
2887 *out
++ = '\\', backslashes_added
++;
2891 return make_specified_string (temp
,
2892 SCHARS (string
) + backslashes_added
,
2894 STRING_MULTIBYTE (string
));
2902 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
2904 searchbufs
[i
].buf
.allocated
= 100;
2905 searchbufs
[i
].buf
.buffer
= (unsigned char *) xmalloc (100);
2906 searchbufs
[i
].buf
.fastmap
= searchbufs
[i
].fastmap
;
2907 searchbufs
[i
].regexp
= Qnil
;
2908 staticpro (&searchbufs
[i
].regexp
);
2909 searchbufs
[i
].next
= (i
== REGEXP_CACHE_SIZE
-1 ? 0 : &searchbufs
[i
+1]);
2911 searchbuf_head
= &searchbufs
[0];
2913 Qsearch_failed
= intern ("search-failed");
2914 staticpro (&Qsearch_failed
);
2915 Qinvalid_regexp
= intern ("invalid-regexp");
2916 staticpro (&Qinvalid_regexp
);
2918 Fput (Qsearch_failed
, Qerror_conditions
,
2919 Fcons (Qsearch_failed
, Fcons (Qerror
, Qnil
)));
2920 Fput (Qsearch_failed
, Qerror_message
,
2921 build_string ("Search failed"));
2923 Fput (Qinvalid_regexp
, Qerror_conditions
,
2924 Fcons (Qinvalid_regexp
, Fcons (Qerror
, Qnil
)));
2925 Fput (Qinvalid_regexp
, Qerror_message
,
2926 build_string ("Invalid regexp"));
2928 last_thing_searched
= Qnil
;
2929 staticpro (&last_thing_searched
);
2931 saved_last_thing_searched
= Qnil
;
2932 staticpro (&saved_last_thing_searched
);
2934 defsubr (&Slooking_at
);
2935 defsubr (&Sposix_looking_at
);
2936 defsubr (&Sstring_match
);
2937 defsubr (&Sposix_string_match
);
2938 defsubr (&Ssearch_forward
);
2939 defsubr (&Ssearch_backward
);
2940 defsubr (&Sword_search_forward
);
2941 defsubr (&Sword_search_backward
);
2942 defsubr (&Sre_search_forward
);
2943 defsubr (&Sre_search_backward
);
2944 defsubr (&Sposix_search_forward
);
2945 defsubr (&Sposix_search_backward
);
2946 defsubr (&Sreplace_match
);
2947 defsubr (&Smatch_beginning
);
2948 defsubr (&Smatch_end
);
2949 defsubr (&Smatch_data
);
2950 defsubr (&Sset_match_data
);
2951 defsubr (&Sregexp_quote
);
2954 /* arch-tag: a6059d79-0552-4f14-a2cb-d379a4e3c78f
2955 (do not change this comment) */