1 /* String search routines for GNU Emacs.
2 Copyright (C) 1985, 1986, 1987, 1993, 1994, 1997, 1998, 1999, 2001, 2002,
3 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
4 Free Software Foundation, Inc.
6 This file is part of GNU Emacs.
8 GNU Emacs is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 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/>. */
28 #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
, 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 /* Nonzero means regexp was compiled to do full POSIX backtracking. */
56 /* The instances of that struct. */
57 struct regexp_cache searchbufs
[REGEXP_CACHE_SIZE
];
59 /* The head of the linked list; points to the most recently used buffer. */
60 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 */
89 Lisp_Object Qinvalid_regexp
;
91 /* Error condition used for failing searches */
92 Lisp_Object Qsearch_failed
;
94 Lisp_Object Vsearch_spaces_regexp
;
96 /* If non-nil, the match data will not be changed during call to
97 searching or matching functions. This variable is for internal use
99 Lisp_Object Vinhibit_changing_match_data
;
101 static void set_search_regs (EMACS_INT
, EMACS_INT
);
102 static void save_search_regs (void);
103 static EMACS_INT
simple_search (int, unsigned char *, int, int,
104 Lisp_Object
, EMACS_INT
, EMACS_INT
,
105 EMACS_INT
, EMACS_INT
);
106 static EMACS_INT
boyer_moore (int, unsigned char *, int, int,
107 Lisp_Object
, Lisp_Object
,
108 EMACS_INT
, EMACS_INT
,
109 EMACS_INT
, EMACS_INT
, int);
110 static EMACS_INT
search_buffer (Lisp_Object
, EMACS_INT
, EMACS_INT
,
111 EMACS_INT
, EMACS_INT
, int, int,
112 Lisp_Object
, Lisp_Object
, int);
113 static void matcher_overflow (void) NO_RETURN
;
116 matcher_overflow (void)
118 error ("Stack overflow in regexp matcher");
121 /* Compile a regexp and signal a Lisp error if anything goes wrong.
122 PATTERN is the pattern to compile.
123 CP is the place to put the result.
124 TRANSLATE is a translation table for ignoring case, or nil for none.
125 REGP is the structure that says where to store the "register"
126 values that will result from matching this pattern.
127 If it is 0, we should compile the pattern not to record any
128 subexpression bounds.
129 POSIX is nonzero if we want full backtracking (POSIX style)
130 for this pattern. 0 means backtrack only enough to get a valid match.
132 The behavior also depends on Vsearch_spaces_regexp. */
135 compile_pattern_1 (struct regexp_cache
*cp
, Lisp_Object pattern
, Lisp_Object translate
, struct re_registers
*regp
, int posix
)
141 cp
->buf
.translate
= (! NILP (translate
) ? translate
: make_number (0));
143 cp
->buf
.multibyte
= STRING_MULTIBYTE (pattern
);
144 cp
->buf
.charset_unibyte
= charset_unibyte
;
145 if (STRINGP (Vsearch_spaces_regexp
))
146 cp
->whitespace_regexp
= Vsearch_spaces_regexp
;
148 cp
->whitespace_regexp
= Qnil
;
150 /* rms: I think BLOCK_INPUT is not needed here any more,
151 because regex.c defines malloc to call xmalloc.
152 Using BLOCK_INPUT here means the debugger won't run if an error occurs.
153 So let's turn it off. */
155 old
= re_set_syntax (RE_SYNTAX_EMACS
156 | (posix
? 0 : RE_NO_POSIX_BACKTRACKING
));
158 if (STRINGP (Vsearch_spaces_regexp
))
159 re_set_whitespace_regexp (SDATA (Vsearch_spaces_regexp
));
161 re_set_whitespace_regexp (NULL
);
163 val
= (char *) re_compile_pattern ((char *) SDATA (pattern
),
164 SBYTES (pattern
), &cp
->buf
);
166 /* If the compiled pattern hard codes some of the contents of the
167 syntax-table, it can only be reused with *this* syntax table. */
168 cp
->syntax_table
= cp
->buf
.used_syntax
? current_buffer
->syntax_table
: Qt
;
170 re_set_whitespace_regexp (NULL
);
175 xsignal1 (Qinvalid_regexp
, build_string (val
));
177 cp
->regexp
= Fcopy_sequence (pattern
);
180 /* Shrink each compiled regexp buffer in the cache
181 to the size actually used right now.
182 This is called from garbage collection. */
185 shrink_regexp_cache (void)
187 struct regexp_cache
*cp
;
189 for (cp
= searchbuf_head
; cp
!= 0; cp
= cp
->next
)
191 cp
->buf
.allocated
= cp
->buf
.used
;
193 = (unsigned char *) xrealloc (cp
->buf
.buffer
, cp
->buf
.used
);
197 /* Clear the regexp cache w.r.t. a particular syntax table,
198 because it was changed.
199 There is no danger of memory leak here because re_compile_pattern
200 automagically manages the memory in each re_pattern_buffer struct,
201 based on its `allocated' and `buffer' values. */
203 clear_regexp_cache (void)
207 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
208 /* It's tempting to compare with the syntax-table we've actually changed,
209 but it's not sufficient because char-table inheritance means that
210 modifying one syntax-table can change others at the same time. */
211 if (!EQ (searchbufs
[i
].syntax_table
, Qt
))
212 searchbufs
[i
].regexp
= Qnil
;
215 /* Compile a regexp if necessary, but first check to see if there's one in
217 PATTERN is the pattern to compile.
218 TRANSLATE is a translation table for ignoring case, or nil for none.
219 REGP is the structure that says where to store the "register"
220 values that will result from matching this pattern.
221 If it is 0, we should compile the pattern not to record any
222 subexpression bounds.
223 POSIX is nonzero if we want full backtracking (POSIX style)
224 for this pattern. 0 means backtrack only enough to get a valid match. */
226 struct re_pattern_buffer
*
227 compile_pattern (Lisp_Object pattern
, struct re_registers
*regp
, Lisp_Object translate
, int posix
, int multibyte
)
229 struct regexp_cache
*cp
, **cpp
;
231 for (cpp
= &searchbuf_head
; ; cpp
= &cp
->next
)
234 /* Entries are initialized to nil, and may be set to nil by
235 compile_pattern_1 if the pattern isn't valid. Don't apply
236 string accessors in those cases. However, compile_pattern_1
237 is only applied to the cache entry we pick here to reuse. So
238 nil should never appear before a non-nil entry. */
239 if (NILP (cp
->regexp
))
241 if (SCHARS (cp
->regexp
) == SCHARS (pattern
)
242 && STRING_MULTIBYTE (cp
->regexp
) == STRING_MULTIBYTE (pattern
)
243 && !NILP (Fstring_equal (cp
->regexp
, pattern
))
244 && EQ (cp
->buf
.translate
, (! NILP (translate
) ? translate
: make_number (0)))
245 && cp
->posix
== posix
246 && (EQ (cp
->syntax_table
, Qt
)
247 || EQ (cp
->syntax_table
, current_buffer
->syntax_table
))
248 && !NILP (Fequal (cp
->whitespace_regexp
, Vsearch_spaces_regexp
))
249 && cp
->buf
.charset_unibyte
== charset_unibyte
)
252 /* If we're at the end of the cache, compile into the nil cell
253 we found, or the last (least recently used) cell with a
258 compile_pattern_1 (cp
, pattern
, translate
, regp
, posix
);
263 /* When we get here, cp (aka *cpp) contains the compiled pattern,
264 either because we found it in the cache or because we just compiled it.
265 Move it to the front of the queue to mark it as most recently used. */
267 cp
->next
= searchbuf_head
;
270 /* Advise the searching functions about the space we have allocated
271 for register data. */
273 re_set_registers (&cp
->buf
, regp
, regp
->num_regs
, regp
->start
, regp
->end
);
275 /* The compiled pattern can be used both for multibyte and unibyte
276 target. But, we have to tell which the pattern is used for. */
277 cp
->buf
.target_multibyte
= multibyte
;
284 looking_at_1 (Lisp_Object string
, int posix
)
287 unsigned char *p1
, *p2
;
290 struct re_pattern_buffer
*bufp
;
292 if (running_asynch_code
)
295 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
296 XCHAR_TABLE (current_buffer
->case_canon_table
)->extras
[2]
297 = current_buffer
->case_eqv_table
;
299 CHECK_STRING (string
);
300 bufp
= compile_pattern (string
,
301 (NILP (Vinhibit_changing_match_data
)
302 ? &search_regs
: NULL
),
303 (!NILP (current_buffer
->case_fold_search
)
304 ? current_buffer
->case_canon_table
: Qnil
),
306 !NILP (current_buffer
->enable_multibyte_characters
));
309 QUIT
; /* Do a pending quit right away, to avoid paradoxical behavior */
311 /* Get pointers and sizes of the two strings
312 that make up the visible portion of the buffer. */
315 s1
= GPT_BYTE
- BEGV_BYTE
;
317 s2
= ZV_BYTE
- GPT_BYTE
;
321 s2
= ZV_BYTE
- BEGV_BYTE
;
326 s1
= ZV_BYTE
- BEGV_BYTE
;
330 re_match_object
= Qnil
;
332 i
= re_match_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
334 (NILP (Vinhibit_changing_match_data
)
335 ? &search_regs
: NULL
),
336 ZV_BYTE
- BEGV_BYTE
);
342 val
= (0 <= i
? Qt
: Qnil
);
343 if (NILP (Vinhibit_changing_match_data
) && i
>= 0)
344 for (i
= 0; i
< search_regs
.num_regs
; i
++)
345 if (search_regs
.start
[i
] >= 0)
348 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
350 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
353 /* Set last_thing_searched only when match data is changed. */
354 if (NILP (Vinhibit_changing_match_data
))
355 XSETBUFFER (last_thing_searched
, current_buffer
);
360 DEFUN ("looking-at", Flooking_at
, Slooking_at
, 1, 1, 0,
361 doc
: /* Return t if text after point matches regular expression REGEXP.
362 This function modifies the match data that `match-beginning',
363 `match-end' and `match-data' access; save and restore the match
364 data if you want to preserve them. */)
368 return looking_at_1 (regexp
, 0);
371 DEFUN ("posix-looking-at", Fposix_looking_at
, Sposix_looking_at
, 1, 1, 0,
372 doc
: /* Return t if text after point matches regular expression REGEXP.
373 Find the longest match, in accord with Posix regular expression rules.
374 This function modifies the match data that `match-beginning',
375 `match-end' and `match-data' access; save and restore the match
376 data if you want to preserve them. */)
380 return looking_at_1 (regexp
, 1);
384 string_match_1 (Lisp_Object regexp
, Lisp_Object string
, Lisp_Object start
, int posix
)
387 struct re_pattern_buffer
*bufp
;
388 EMACS_INT pos
, pos_byte
;
391 if (running_asynch_code
)
394 CHECK_STRING (regexp
);
395 CHECK_STRING (string
);
398 pos
= 0, pos_byte
= 0;
401 int len
= SCHARS (string
);
403 CHECK_NUMBER (start
);
405 if (pos
< 0 && -pos
<= len
)
407 else if (0 > pos
|| pos
> len
)
408 args_out_of_range (string
, start
);
409 pos_byte
= string_char_to_byte (string
, pos
);
412 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
413 XCHAR_TABLE (current_buffer
->case_canon_table
)->extras
[2]
414 = current_buffer
->case_eqv_table
;
416 bufp
= compile_pattern (regexp
,
417 (NILP (Vinhibit_changing_match_data
)
418 ? &search_regs
: NULL
),
419 (!NILP (current_buffer
->case_fold_search
)
420 ? current_buffer
->case_canon_table
: Qnil
),
422 STRING_MULTIBYTE (string
));
424 re_match_object
= string
;
426 val
= re_search (bufp
, (char *) SDATA (string
),
427 SBYTES (string
), pos_byte
,
428 SBYTES (string
) - pos_byte
,
429 (NILP (Vinhibit_changing_match_data
)
430 ? &search_regs
: NULL
));
433 /* Set last_thing_searched only when match data is changed. */
434 if (NILP (Vinhibit_changing_match_data
))
435 last_thing_searched
= Qt
;
439 if (val
< 0) return Qnil
;
441 if (NILP (Vinhibit_changing_match_data
))
442 for (i
= 0; i
< search_regs
.num_regs
; i
++)
443 if (search_regs
.start
[i
] >= 0)
446 = string_byte_to_char (string
, search_regs
.start
[i
]);
448 = string_byte_to_char (string
, search_regs
.end
[i
]);
451 return make_number (string_byte_to_char (string
, val
));
454 DEFUN ("string-match", Fstring_match
, Sstring_match
, 2, 3, 0,
455 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
456 Matching ignores case if `case-fold-search' is non-nil.
457 If third arg START is non-nil, start search at that index in STRING.
458 For index of first char beyond the match, do (match-end 0).
459 `match-end' and `match-beginning' also give indices of substrings
460 matched by parenthesis constructs in the pattern.
462 You can use the function `match-string' to extract the substrings
463 matched by the parenthesis constructions in REGEXP. */)
464 (regexp
, string
, start
)
465 Lisp_Object regexp
, string
, start
;
467 return string_match_1 (regexp
, string
, start
, 0);
470 DEFUN ("posix-string-match", Fposix_string_match
, Sposix_string_match
, 2, 3, 0,
471 doc
: /* Return index of start of first match for REGEXP in STRING, or nil.
472 Find the longest match, in accord with Posix regular expression rules.
473 Case is ignored if `case-fold-search' is non-nil in the current buffer.
474 If third arg START is non-nil, start search at that index in STRING.
475 For index of first char beyond the match, do (match-end 0).
476 `match-end' and `match-beginning' also give indices of substrings
477 matched by parenthesis constructs in the pattern. */)
478 (regexp
, string
, start
)
479 Lisp_Object regexp
, string
, start
;
481 return string_match_1 (regexp
, string
, start
, 1);
484 /* Match REGEXP against STRING, searching all of STRING,
485 and return the index of the match, or negative on failure.
486 This does not clobber the match data. */
489 fast_string_match (Lisp_Object regexp
, Lisp_Object string
)
492 struct re_pattern_buffer
*bufp
;
494 bufp
= compile_pattern (regexp
, 0, Qnil
,
495 0, STRING_MULTIBYTE (string
));
497 re_match_object
= string
;
499 val
= re_search (bufp
, (char *) SDATA (string
),
506 /* Match REGEXP against STRING, searching all of STRING ignoring case,
507 and return the index of the match, or negative on failure.
508 This does not clobber the match data.
509 We assume that STRING contains single-byte characters. */
511 extern Lisp_Object Vascii_downcase_table
;
514 fast_c_string_match_ignore_case (Lisp_Object regexp
, const char *string
)
517 struct re_pattern_buffer
*bufp
;
518 int len
= strlen (string
);
520 regexp
= string_make_unibyte (regexp
);
521 re_match_object
= Qt
;
522 bufp
= compile_pattern (regexp
, 0,
523 Vascii_canon_table
, 0,
526 val
= re_search (bufp
, string
, len
, 0, len
, 0);
531 /* Like fast_string_match but ignore case. */
534 fast_string_match_ignore_case (Lisp_Object regexp
, Lisp_Object string
)
537 struct re_pattern_buffer
*bufp
;
539 bufp
= compile_pattern (regexp
, 0, Vascii_canon_table
,
540 0, STRING_MULTIBYTE (string
));
542 re_match_object
= string
;
544 val
= re_search (bufp
, (char *) SDATA (string
),
551 /* Match REGEXP against the characters after POS to LIMIT, and return
552 the number of matched characters. If STRING is non-nil, match
553 against the characters in it. In that case, POS and LIMIT are
554 indices into the string. This function doesn't modify the match
558 fast_looking_at (Lisp_Object regexp
, EMACS_INT pos
, EMACS_INT pos_byte
, EMACS_INT limit
, EMACS_INT limit_byte
, Lisp_Object string
)
561 struct re_pattern_buffer
*buf
;
562 unsigned char *p1
, *p2
;
566 if (STRINGP (string
))
569 pos_byte
= string_char_to_byte (string
, pos
);
571 limit_byte
= string_char_to_byte (string
, limit
);
575 s2
= SBYTES (string
);
576 re_match_object
= string
;
577 multibyte
= STRING_MULTIBYTE (string
);
582 pos_byte
= CHAR_TO_BYTE (pos
);
584 limit_byte
= CHAR_TO_BYTE (limit
);
585 pos_byte
-= BEGV_BYTE
;
586 limit_byte
-= BEGV_BYTE
;
588 s1
= GPT_BYTE
- BEGV_BYTE
;
590 s2
= ZV_BYTE
- GPT_BYTE
;
594 s2
= ZV_BYTE
- BEGV_BYTE
;
599 s1
= ZV_BYTE
- BEGV_BYTE
;
602 re_match_object
= Qnil
;
603 multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
606 buf
= compile_pattern (regexp
, 0, Qnil
, 0, multibyte
);
608 len
= re_match_2 (buf
, (char *) p1
, s1
, (char *) p2
, s2
,
609 pos_byte
, NULL
, limit_byte
);
616 /* The newline cache: remembering which sections of text have no newlines. */
618 /* If the user has requested newline caching, make sure it's on.
619 Otherwise, make sure it's off.
620 This is our cheezy way of associating an action with the change of
621 state of a buffer-local variable. */
623 newline_cache_on_off (struct buffer
*buf
)
625 if (NILP (buf
->cache_long_line_scans
))
627 /* It should be off. */
628 if (buf
->newline_cache
)
630 free_region_cache (buf
->newline_cache
);
631 buf
->newline_cache
= 0;
636 /* It should be on. */
637 if (buf
->newline_cache
== 0)
638 buf
->newline_cache
= new_region_cache ();
643 /* Search for COUNT instances of the character TARGET between START and END.
645 If COUNT is positive, search forwards; END must be >= START.
646 If COUNT is negative, search backwards for the -COUNTth instance;
647 END must be <= START.
648 If COUNT is zero, do anything you please; run rogue, for all I care.
650 If END is zero, use BEGV or ZV instead, as appropriate for the
651 direction indicated by COUNT.
653 If we find COUNT instances, set *SHORTAGE to zero, and return the
654 position past the COUNTth match. Note that for reverse motion
655 this is not the same as the usual convention for Emacs motion commands.
657 If we don't find COUNT instances before reaching END, set *SHORTAGE
658 to the number of TARGETs left unfound, and return END.
660 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
661 except when inside redisplay. */
664 scan_buffer (register int target
, EMACS_INT start
, EMACS_INT end
, int count
, int *shortage
, int allow_quit
)
666 struct region_cache
*newline_cache
;
677 if (! end
) end
= BEGV
;
680 newline_cache_on_off (current_buffer
);
681 newline_cache
= current_buffer
->newline_cache
;
686 immediate_quit
= allow_quit
;
691 /* Our innermost scanning loop is very simple; it doesn't know
692 about gaps, buffer ends, or the newline cache. ceiling is
693 the position of the last character before the next such
694 obstacle --- the last character the dumb search loop should
696 EMACS_INT ceiling_byte
= CHAR_TO_BYTE (end
) - 1;
697 EMACS_INT start_byte
= CHAR_TO_BYTE (start
);
700 /* If we're looking for a newline, consult the newline cache
701 to see where we can avoid some scanning. */
702 if (target
== '\n' && newline_cache
)
706 while (region_cache_forward
707 (current_buffer
, newline_cache
, start_byte
, &next_change
))
708 start_byte
= next_change
;
709 immediate_quit
= allow_quit
;
711 /* START should never be after END. */
712 if (start_byte
> ceiling_byte
)
713 start_byte
= ceiling_byte
;
715 /* Now the text after start is an unknown region, and
716 next_change is the position of the next known region. */
717 ceiling_byte
= min (next_change
- 1, ceiling_byte
);
720 /* The dumb loop can only scan text stored in contiguous
721 bytes. BUFFER_CEILING_OF returns the last character
722 position that is contiguous, so the ceiling is the
723 position after that. */
724 tem
= BUFFER_CEILING_OF (start_byte
);
725 ceiling_byte
= min (tem
, ceiling_byte
);
728 /* The termination address of the dumb loop. */
729 register unsigned char *ceiling_addr
730 = BYTE_POS_ADDR (ceiling_byte
) + 1;
731 register unsigned char *cursor
732 = BYTE_POS_ADDR (start_byte
);
733 unsigned char *base
= cursor
;
735 while (cursor
< ceiling_addr
)
737 unsigned char *scan_start
= cursor
;
740 while (*cursor
!= target
&& ++cursor
< ceiling_addr
)
743 /* If we're looking for newlines, cache the fact that
744 the region from start to cursor is free of them. */
745 if (target
== '\n' && newline_cache
)
746 know_region_cache (current_buffer
, newline_cache
,
747 start_byte
+ scan_start
- base
,
748 start_byte
+ cursor
- base
);
750 /* Did we find the target character? */
751 if (cursor
< ceiling_addr
)
756 return BYTE_TO_CHAR (start_byte
+ cursor
- base
+ 1);
762 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
768 /* The last character to check before the next obstacle. */
769 EMACS_INT ceiling_byte
= CHAR_TO_BYTE (end
);
770 EMACS_INT start_byte
= CHAR_TO_BYTE (start
);
773 /* Consult the newline cache, if appropriate. */
774 if (target
== '\n' && newline_cache
)
778 while (region_cache_backward
779 (current_buffer
, newline_cache
, start_byte
, &next_change
))
780 start_byte
= next_change
;
781 immediate_quit
= allow_quit
;
783 /* Start should never be at or before end. */
784 if (start_byte
<= ceiling_byte
)
785 start_byte
= ceiling_byte
+ 1;
787 /* Now the text before start is an unknown region, and
788 next_change is the position of the next known region. */
789 ceiling_byte
= max (next_change
, ceiling_byte
);
792 /* Stop scanning before the gap. */
793 tem
= BUFFER_FLOOR_OF (start_byte
- 1);
794 ceiling_byte
= max (tem
, ceiling_byte
);
797 /* The termination address of the dumb loop. */
798 register unsigned char *ceiling_addr
= BYTE_POS_ADDR (ceiling_byte
);
799 register unsigned char *cursor
= BYTE_POS_ADDR (start_byte
- 1);
800 unsigned char *base
= cursor
;
802 while (cursor
>= ceiling_addr
)
804 unsigned char *scan_start
= cursor
;
806 while (*cursor
!= target
&& --cursor
>= ceiling_addr
)
809 /* If we're looking for newlines, cache the fact that
810 the region from after the cursor to start is free of them. */
811 if (target
== '\n' && newline_cache
)
812 know_region_cache (current_buffer
, newline_cache
,
813 start_byte
+ cursor
- base
,
814 start_byte
+ scan_start
- base
);
816 /* Did we find the target character? */
817 if (cursor
>= ceiling_addr
)
822 return BYTE_TO_CHAR (start_byte
+ cursor
- base
);
828 start
= BYTE_TO_CHAR (start_byte
+ cursor
- base
);
834 *shortage
= count
* direction
;
838 /* Search for COUNT instances of a line boundary, which means either a
839 newline or (if selective display enabled) a carriage return.
840 Start at START. If COUNT is negative, search backwards.
842 We report the resulting position by calling TEMP_SET_PT_BOTH.
844 If we find COUNT instances. we position after (always after,
845 even if scanning backwards) the COUNTth match, and return 0.
847 If we don't find COUNT instances before reaching the end of the
848 buffer (or the beginning, if scanning backwards), we return
849 the number of line boundaries left unfound, and position at
850 the limit we bumped up against.
852 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
853 except in special cases. */
856 scan_newline (EMACS_INT start
, EMACS_INT start_byte
, EMACS_INT limit
, EMACS_INT limit_byte
, register int count
, int allow_quit
)
858 int direction
= ((count
> 0) ? 1 : -1);
860 register unsigned char *cursor
;
864 register unsigned char *ceiling_addr
;
866 int old_immediate_quit
= immediate_quit
;
868 /* The code that follows is like scan_buffer
869 but checks for either newline or carriage return. */
874 start_byte
= CHAR_TO_BYTE (start
);
878 while (start_byte
< limit_byte
)
880 ceiling
= BUFFER_CEILING_OF (start_byte
);
881 ceiling
= min (limit_byte
- 1, ceiling
);
882 ceiling_addr
= BYTE_POS_ADDR (ceiling
) + 1;
883 base
= (cursor
= BYTE_POS_ADDR (start_byte
));
886 while (*cursor
!= '\n' && ++cursor
!= ceiling_addr
)
889 if (cursor
!= ceiling_addr
)
893 immediate_quit
= old_immediate_quit
;
894 start_byte
= start_byte
+ cursor
- base
+ 1;
895 start
= BYTE_TO_CHAR (start_byte
);
896 TEMP_SET_PT_BOTH (start
, start_byte
);
900 if (++cursor
== ceiling_addr
)
906 start_byte
+= cursor
- 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
) - 1;
916 base
= (cursor
= BYTE_POS_ADDR (start_byte
- 1) + 1);
919 while (--cursor
!= ceiling_addr
&& *cursor
!= '\n')
922 if (cursor
!= ceiling_addr
)
926 immediate_quit
= old_immediate_quit
;
927 /* Return the position AFTER the match we found. */
928 start_byte
= start_byte
+ cursor
- base
+ 1;
929 start
= BYTE_TO_CHAR (start_byte
);
930 TEMP_SET_PT_BOTH (start
, start_byte
);
937 /* Here we add 1 to compensate for the last decrement
938 of CURSOR, which took it past the valid range. */
939 start_byte
+= cursor
- base
+ 1;
943 TEMP_SET_PT_BOTH (limit
, limit_byte
);
944 immediate_quit
= old_immediate_quit
;
946 return count
* direction
;
950 find_next_newline_no_quit (EMACS_INT from
, int cnt
)
952 return scan_buffer ('\n', from
, 0, cnt
, (int *) 0, 0);
955 /* Like find_next_newline, but returns position before the newline,
956 not after, and only search up to TO. This isn't just
957 find_next_newline (...)-1, because you might hit TO. */
960 find_before_next_newline (EMACS_INT from
, EMACS_INT to
, int cnt
)
963 int pos
= scan_buffer ('\n', from
, to
, cnt
, &shortage
, 1);
971 /* Subroutines of Lisp buffer search functions. */
974 search_command (Lisp_Object string
, Lisp_Object bound
, Lisp_Object noerror
, Lisp_Object count
, int direction
, int RE
, int posix
)
982 CHECK_NUMBER (count
);
986 CHECK_STRING (string
);
990 lim
= ZV
, lim_byte
= ZV_BYTE
;
992 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
996 CHECK_NUMBER_COERCE_MARKER (bound
);
998 if (n
> 0 ? lim
< PT
: lim
> PT
)
999 error ("Invalid search bound (wrong side of point)");
1001 lim
= ZV
, lim_byte
= ZV_BYTE
;
1002 else if (lim
< BEGV
)
1003 lim
= BEGV
, lim_byte
= BEGV_BYTE
;
1005 lim_byte
= CHAR_TO_BYTE (lim
);
1008 /* This is so set_image_of_range_1 in regex.c can find the EQV table. */
1009 XCHAR_TABLE (current_buffer
->case_canon_table
)->extras
[2]
1010 = current_buffer
->case_eqv_table
;
1012 np
= search_buffer (string
, PT
, PT_BYTE
, lim
, lim_byte
, n
, RE
,
1013 (!NILP (current_buffer
->case_fold_search
)
1014 ? current_buffer
->case_canon_table
1016 (!NILP (current_buffer
->case_fold_search
)
1017 ? current_buffer
->case_eqv_table
1023 xsignal1 (Qsearch_failed
, string
);
1025 if (!EQ (noerror
, Qt
))
1027 if (lim
< BEGV
|| lim
> ZV
)
1029 SET_PT_BOTH (lim
, lim_byte
);
1031 #if 0 /* This would be clean, but maybe programs depend on
1032 a value of nil here. */
1040 if (np
< BEGV
|| np
> ZV
)
1045 return make_number (np
);
1048 /* Return 1 if REGEXP it matches just one constant string. */
1051 trivial_regexp_p (Lisp_Object regexp
)
1053 int len
= SBYTES (regexp
);
1054 unsigned char *s
= SDATA (regexp
);
1059 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
1066 case '|': case '(': case ')': case '`': case '\'': case 'b':
1067 case 'B': case '<': case '>': case 'w': case 'W': case 's':
1068 case 'S': case '=': case '{': case '}': case '_':
1069 case 'c': case 'C': /* for categoryspec and notcategoryspec */
1070 case '1': case '2': case '3': case '4': case '5':
1071 case '6': case '7': case '8': case '9':
1079 /* Search for the n'th occurrence of STRING in the current buffer,
1080 starting at position POS and stopping at position LIM,
1081 treating STRING as a literal string if RE is false or as
1082 a regular expression if RE is true.
1084 If N is positive, searching is forward and LIM must be greater than POS.
1085 If N is negative, searching is backward and LIM must be less than POS.
1087 Returns -x if x occurrences remain to be found (x > 0),
1088 or else the position at the beginning of the Nth occurrence
1089 (if searching backward) or the end (if searching forward).
1091 POSIX is nonzero if we want full backtracking (POSIX style)
1092 for this pattern. 0 means backtrack only enough to get a valid match. */
1094 #define TRANSLATE(out, trt, d) \
1100 temp = Faref (trt, make_number (d)); \
1101 if (INTEGERP (temp)) \
1102 out = XINT (temp); \
1111 /* Only used in search_buffer, to record the end position of the match
1112 when searching regexps and SEARCH_REGS should not be changed
1113 (i.e. Vinhibit_changing_match_data is non-nil). */
1114 static struct re_registers search_regs_1
;
1117 search_buffer (string
, pos
, pos_byte
, lim
, lim_byte
, n
,
1118 RE
, trt
, inverse_trt
, posix
)
1127 Lisp_Object inverse_trt
;
1130 int len
= SCHARS (string
);
1131 int len_byte
= SBYTES (string
);
1134 if (running_asynch_code
)
1135 save_search_regs ();
1137 /* Searching 0 times means don't move. */
1138 /* Null string is found at starting position. */
1139 if (len
== 0 || n
== 0)
1141 set_search_regs (pos_byte
, 0);
1145 if (RE
&& !(trivial_regexp_p (string
) && NILP (Vsearch_spaces_regexp
)))
1147 unsigned char *p1
, *p2
;
1149 struct re_pattern_buffer
*bufp
;
1151 bufp
= compile_pattern (string
,
1152 (NILP (Vinhibit_changing_match_data
)
1153 ? &search_regs
: &search_regs_1
),
1155 !NILP (current_buffer
->enable_multibyte_characters
));
1157 immediate_quit
= 1; /* Quit immediately if user types ^G,
1158 because letting this function finish
1159 can take too long. */
1160 QUIT
; /* Do a pending quit right away,
1161 to avoid paradoxical behavior */
1162 /* Get pointers and sizes of the two strings
1163 that make up the visible portion of the buffer. */
1166 s1
= GPT_BYTE
- BEGV_BYTE
;
1168 s2
= ZV_BYTE
- GPT_BYTE
;
1172 s2
= ZV_BYTE
- BEGV_BYTE
;
1177 s1
= ZV_BYTE
- BEGV_BYTE
;
1180 re_match_object
= Qnil
;
1185 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1186 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1187 (NILP (Vinhibit_changing_match_data
)
1188 ? &search_regs
: &search_regs_1
),
1189 /* Don't allow match past current point */
1190 pos_byte
- BEGV_BYTE
);
1193 matcher_overflow ();
1197 if (NILP (Vinhibit_changing_match_data
))
1199 pos_byte
= search_regs
.start
[0] + BEGV_BYTE
;
1200 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1201 if (search_regs
.start
[i
] >= 0)
1203 search_regs
.start
[i
]
1204 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1206 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1208 XSETBUFFER (last_thing_searched
, current_buffer
);
1209 /* Set pos to the new position. */
1210 pos
= search_regs
.start
[0];
1214 pos_byte
= search_regs_1
.start
[0] + BEGV_BYTE
;
1215 /* Set pos to the new position. */
1216 pos
= BYTE_TO_CHAR (search_regs_1
.start
[0] + BEGV_BYTE
);
1229 val
= re_search_2 (bufp
, (char *) p1
, s1
, (char *) p2
, s2
,
1230 pos_byte
- BEGV_BYTE
, lim_byte
- pos_byte
,
1231 (NILP (Vinhibit_changing_match_data
)
1232 ? &search_regs
: &search_regs_1
),
1233 lim_byte
- BEGV_BYTE
);
1236 matcher_overflow ();
1240 if (NILP (Vinhibit_changing_match_data
))
1242 pos_byte
= search_regs
.end
[0] + BEGV_BYTE
;
1243 for (i
= 0; i
< search_regs
.num_regs
; i
++)
1244 if (search_regs
.start
[i
] >= 0)
1246 search_regs
.start
[i
]
1247 = BYTE_TO_CHAR (search_regs
.start
[i
] + BEGV_BYTE
);
1249 = BYTE_TO_CHAR (search_regs
.end
[i
] + BEGV_BYTE
);
1251 XSETBUFFER (last_thing_searched
, current_buffer
);
1252 pos
= search_regs
.end
[0];
1256 pos_byte
= search_regs_1
.end
[0] + BEGV_BYTE
;
1257 pos
= BYTE_TO_CHAR (search_regs_1
.end
[0] + BEGV_BYTE
);
1270 else /* non-RE case */
1272 unsigned char *raw_pattern
, *pat
;
1273 int raw_pattern_size
;
1274 int raw_pattern_size_byte
;
1275 unsigned char *patbuf
;
1276 int multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
1277 unsigned char *base_pat
;
1278 /* Set to positive if we find a non-ASCII char that need
1279 translation. Otherwise set to zero later. */
1281 int boyer_moore_ok
= 1;
1283 /* MULTIBYTE says whether the text to be searched is multibyte.
1284 We must convert PATTERN to match that, or we will not really
1285 find things right. */
1287 if (multibyte
== STRING_MULTIBYTE (string
))
1289 raw_pattern
= (unsigned char *) SDATA (string
);
1290 raw_pattern_size
= SCHARS (string
);
1291 raw_pattern_size_byte
= SBYTES (string
);
1295 raw_pattern_size
= SCHARS (string
);
1296 raw_pattern_size_byte
1297 = count_size_as_multibyte (SDATA (string
),
1299 raw_pattern
= (unsigned char *) alloca (raw_pattern_size_byte
+ 1);
1300 copy_text (SDATA (string
), raw_pattern
,
1301 SCHARS (string
), 0, 1);
1305 /* Converting multibyte to single-byte.
1307 ??? Perhaps this conversion should be done in a special way
1308 by subtracting nonascii-insert-offset from each non-ASCII char,
1309 so that only the multibyte chars which really correspond to
1310 the chosen single-byte character set can possibly match. */
1311 raw_pattern_size
= SCHARS (string
);
1312 raw_pattern_size_byte
= SCHARS (string
);
1313 raw_pattern
= (unsigned char *) alloca (raw_pattern_size
+ 1);
1314 copy_text (SDATA (string
), raw_pattern
,
1315 SBYTES (string
), 1, 0);
1318 /* Copy and optionally translate the pattern. */
1319 len
= raw_pattern_size
;
1320 len_byte
= raw_pattern_size_byte
;
1321 patbuf
= (unsigned char *) alloca (len
* MAX_MULTIBYTE_LENGTH
);
1323 base_pat
= raw_pattern
;
1326 /* Fill patbuf by translated characters in STRING while
1327 checking if we can use boyer-moore search. If TRT is
1328 non-nil, we can use boyer-moore search only if TRT can be
1329 represented by the byte array of 256 elements. For that,
1330 all non-ASCII case-equivalents of all case-senstive
1331 characters in STRING must belong to the same charset and
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. */
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
;
1441 len_byte
= pat
- patbuf
;
1442 len
= raw_pattern_size
;
1443 pat
= base_pat
= patbuf
;
1446 return boyer_moore (n
, pat
, len
, len_byte
, trt
, inverse_trt
,
1447 pos
, pos_byte
, lim
, lim_byte
,
1450 return simple_search (n
, pat
, len
, len_byte
, trt
,
1451 pos
, pos_byte
, lim
, lim_byte
);
1455 /* Do a simple string search N times for the string PAT,
1456 whose length is LEN/LEN_BYTE,
1457 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1458 TRT is the translation table.
1460 Return the character position where the match is found.
1461 Otherwise, if M matches remained to be found, return -M.
1463 This kind of search works regardless of what is in PAT and
1464 regardless of what is in TRT. It is used in cases where
1465 boyer_moore cannot work. */
1468 simple_search (int n
, unsigned char *pat
, int len
, int len_byte
, Lisp_Object trt
, EMACS_INT pos
, EMACS_INT pos_byte
, EMACS_INT lim
, EMACS_INT lim_byte
)
1470 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1471 int forward
= n
> 0;
1472 /* Number of buffer bytes matched. Note that this may be different
1473 from len_byte in a multibyte buffer. */
1476 if (lim
> pos
&& multibyte
)
1481 /* Try matching at position POS. */
1482 EMACS_INT this_pos
= pos
;
1483 EMACS_INT this_pos_byte
= pos_byte
;
1485 unsigned char *p
= pat
;
1486 if (pos
+ len
> lim
|| pos_byte
+ len_byte
> lim_byte
)
1489 while (this_len
> 0)
1491 int charlen
, buf_charlen
;
1494 pat_ch
= STRING_CHAR_AND_LENGTH (p
, charlen
);
1495 buf_ch
= STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte
),
1497 TRANSLATE (buf_ch
, trt
, buf_ch
);
1499 if (buf_ch
!= pat_ch
)
1505 this_pos_byte
+= buf_charlen
;
1511 match_byte
= this_pos_byte
- pos_byte
;
1513 pos_byte
+= match_byte
;
1517 INC_BOTH (pos
, pos_byte
);
1527 /* Try matching at position POS. */
1528 EMACS_INT this_pos
= pos
;
1530 unsigned char *p
= pat
;
1532 if (pos
+ len
> lim
)
1535 while (this_len
> 0)
1538 int buf_ch
= FETCH_BYTE (this_pos
);
1539 TRANSLATE (buf_ch
, trt
, buf_ch
);
1541 if (buf_ch
!= pat_ch
)
1560 /* Backwards search. */
1561 else if (lim
< pos
&& multibyte
)
1566 /* Try matching at position POS. */
1567 EMACS_INT this_pos
= pos
;
1568 EMACS_INT this_pos_byte
= pos_byte
;
1570 const unsigned char *p
= pat
+ len_byte
;
1572 if (this_pos
- len
< lim
|| (pos_byte
- len_byte
) < lim_byte
)
1575 while (this_len
> 0)
1580 DEC_BOTH (this_pos
, this_pos_byte
);
1581 PREV_CHAR_BOUNDARY (p
, pat
);
1582 pat_ch
= STRING_CHAR (p
);
1583 buf_ch
= STRING_CHAR (BYTE_POS_ADDR (this_pos_byte
));
1584 TRANSLATE (buf_ch
, trt
, buf_ch
);
1586 if (buf_ch
!= pat_ch
)
1594 match_byte
= pos_byte
- this_pos_byte
;
1596 pos_byte
= this_pos_byte
;
1600 DEC_BOTH (pos
, pos_byte
);
1610 /* Try matching at position POS. */
1611 EMACS_INT this_pos
= pos
- len
;
1613 unsigned char *p
= pat
;
1618 while (this_len
> 0)
1621 int buf_ch
= FETCH_BYTE (this_pos
);
1622 TRANSLATE (buf_ch
, trt
, buf_ch
);
1624 if (buf_ch
!= pat_ch
)
1647 set_search_regs ((multibyte
? pos_byte
: pos
) - match_byte
, match_byte
);
1649 set_search_regs (multibyte
? pos_byte
: pos
, match_byte
);
1659 /* Do Boyer-Moore search N times for the string BASE_PAT,
1660 whose length is LEN/LEN_BYTE,
1661 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
1662 DIRECTION says which direction we search in.
1663 TRT and INVERSE_TRT are translation tables.
1664 Characters in PAT are already translated by TRT.
1666 This kind of search works if all the characters in BASE_PAT that
1667 have nontrivial translation are the same aside from the last byte.
1668 This makes it possible to translate just the last byte of a
1669 character, and do so after just a simple test of the context.
1670 CHAR_BASE is nonzero if there is such a non-ASCII character.
1672 If that criterion is not satisfied, do not call this function. */
1675 boyer_moore (n
, base_pat
, len
, len_byte
, trt
, inverse_trt
,
1676 pos
, pos_byte
, lim
, lim_byte
, char_base
)
1678 unsigned char *base_pat
;
1681 Lisp_Object inverse_trt
;
1682 EMACS_INT pos
, pos_byte
;
1683 EMACS_INT lim
, lim_byte
;
1686 int direction
= ((n
> 0) ? 1 : -1);
1687 register int dirlen
;
1689 int stride_for_teases
= 0;
1691 register unsigned char *cursor
, *p_limit
;
1693 unsigned char *pat
, *pat_end
;
1694 int multibyte
= ! NILP (current_buffer
->enable_multibyte_characters
);
1696 unsigned char simple_translate
[0400];
1697 /* These are set to the preceding bytes of a byte to be translated
1698 if char_base is nonzero. As the maximum byte length of a
1699 multibyte character is 5, we have to check at most four previous
1701 int translate_prev_byte1
= 0;
1702 int translate_prev_byte2
= 0;
1703 int translate_prev_byte3
= 0;
1704 int translate_prev_byte4
= 0;
1706 /* The general approach is that we are going to maintain that we know
1707 the first (closest to the present position, in whatever direction
1708 we're searching) character that could possibly be the last
1709 (furthest from present position) character of a valid match. We
1710 advance the state of our knowledge by looking at that character
1711 and seeing whether it indeed matches the last character of the
1712 pattern. If it does, we take a closer look. If it does not, we
1713 move our pointer (to putative last characters) as far as is
1714 logically possible. This amount of movement, which I call a
1715 stride, will be the length of the pattern if the actual character
1716 appears nowhere in the pattern, otherwise it will be the distance
1717 from the last occurrence of that character to the end of the
1718 pattern. If the amount is zero we have a possible match. */
1720 /* Here we make a "mickey mouse" BM table. The stride of the search
1721 is determined only by the last character of the putative match.
1722 If that character does not match, we will stride the proper
1723 distance to propose a match that superimposes it on the last
1724 instance of a character that matches it (per trt), or misses
1725 it entirely if there is none. */
1727 dirlen
= len_byte
* direction
;
1729 /* Record position after the end of the pattern. */
1730 pat_end
= base_pat
+ len_byte
;
1731 /* BASE_PAT points to a character that we start scanning from.
1732 It is the first character in a forward search,
1733 the last character in a backward search. */
1735 base_pat
= pat_end
- 1;
1737 /* A character that does not appear in the pattern induces a
1738 stride equal to the pattern length. */
1739 for (i
= 0; i
< 0400; i
++)
1742 /* We use this for translation, instead of TRT itself.
1743 We fill this in to handle the characters that actually
1744 occur in the pattern. Others don't matter anyway! */
1745 for (i
= 0; i
< 0400; i
++)
1746 simple_translate
[i
] = i
;
1750 /* Setup translate_prev_byte1/2/3/4 from CHAR_BASE. Only a
1751 byte following them are the target of translation. */
1752 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
1753 int len
= CHAR_STRING (char_base
, str
);
1755 translate_prev_byte1
= str
[len
- 2];
1758 translate_prev_byte2
= str
[len
- 3];
1761 translate_prev_byte3
= str
[len
- 4];
1763 translate_prev_byte4
= str
[len
- 5];
1771 unsigned char *ptr
= base_pat
+ i
;
1775 /* If the byte currently looking at is the last of a
1776 character to check case-equivalents, set CH to that
1777 character. An ASCII character and a non-ASCII character
1778 matching with CHAR_BASE are to be checked. */
1781 if (ASCII_BYTE_P (*ptr
) || ! multibyte
)
1784 && ((pat_end
- ptr
) == 1 || CHAR_HEAD_P (ptr
[1])))
1786 unsigned char *charstart
= ptr
- 1;
1788 while (! (CHAR_HEAD_P (*charstart
)))
1790 ch
= STRING_CHAR (charstart
);
1791 if (char_base
!= (ch
& ~0x3F))
1796 j
= (ch
& 0x3F) | 0200;
1801 stride_for_teases
= BM_tab
[j
];
1803 BM_tab
[j
] = dirlen
- i
;
1804 /* A translation table is accompanied by its inverse -- see */
1805 /* comment following downcase_table for details */
1808 int starting_ch
= ch
;
1813 TRANSLATE (ch
, inverse_trt
, ch
);
1815 j
= (ch
& 0x3F) | 0200;
1819 /* For all the characters that map into CH,
1820 set up simple_translate to map the last byte
1822 simple_translate
[j
] = starting_j
;
1823 if (ch
== starting_ch
)
1825 BM_tab
[j
] = dirlen
- i
;
1834 stride_for_teases
= BM_tab
[j
];
1835 BM_tab
[j
] = dirlen
- i
;
1837 /* stride_for_teases tells how much to stride if we get a
1838 match on the far character but are subsequently
1839 disappointed, by recording what the stride would have been
1840 for that character if the last character had been
1843 pos_byte
+= dirlen
- ((direction
> 0) ? direction
: 0);
1844 /* loop invariant - POS_BYTE points at where last char (first
1845 char if reverse) of pattern would align in a possible match. */
1849 unsigned char *tail_end_ptr
;
1851 /* It's been reported that some (broken) compiler thinks that
1852 Boolean expressions in an arithmetic context are unsigned.
1853 Using an explicit ?1:0 prevents this. */
1854 if ((lim_byte
- pos_byte
- ((direction
> 0) ? 1 : 0)) * direction
1856 return (n
* (0 - direction
));
1857 /* First we do the part we can by pointers (maybe nothing) */
1860 limit
= pos_byte
- dirlen
+ direction
;
1863 limit
= BUFFER_CEILING_OF (limit
);
1864 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1865 can take on without hitting edge of buffer or the gap. */
1866 limit
= min (limit
, pos_byte
+ 20000);
1867 limit
= min (limit
, lim_byte
- 1);
1871 limit
= BUFFER_FLOOR_OF (limit
);
1872 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
1873 can take on without hitting edge of buffer or the gap. */
1874 limit
= max (limit
, pos_byte
- 20000);
1875 limit
= max (limit
, lim_byte
);
1877 tail_end
= BUFFER_CEILING_OF (pos_byte
) + 1;
1878 tail_end_ptr
= BYTE_POS_ADDR (tail_end
);
1880 if ((limit
- pos_byte
) * direction
> 20)
1884 p_limit
= BYTE_POS_ADDR (limit
);
1885 p2
= (cursor
= BYTE_POS_ADDR (pos_byte
));
1886 /* In this loop, pos + cursor - p2 is the surrogate for pos. */
1887 while (1) /* use one cursor setting as long as i can */
1889 if (direction
> 0) /* worth duplicating */
1891 while (cursor
<= p_limit
)
1893 if (BM_tab
[*cursor
] == 0)
1895 cursor
+= BM_tab
[*cursor
];
1900 while (cursor
>= p_limit
)
1902 if (BM_tab
[*cursor
] == 0)
1904 cursor
+= BM_tab
[*cursor
];
1907 /* If you are here, cursor is beyond the end of the
1908 searched region. You fail to match within the
1909 permitted region and would otherwise try a character
1910 beyond that region. */
1914 i
= dirlen
- direction
;
1917 while ((i
-= direction
) + direction
!= 0)
1920 cursor
-= direction
;
1921 /* Translate only the last byte of a character. */
1923 || ((cursor
== tail_end_ptr
1924 || CHAR_HEAD_P (cursor
[1]))
1925 && (CHAR_HEAD_P (cursor
[0])
1926 /* Check if this is the last byte of
1927 a translable character. */
1928 || (translate_prev_byte1
== cursor
[-1]
1929 && (CHAR_HEAD_P (translate_prev_byte1
)
1930 || (translate_prev_byte2
== cursor
[-2]
1931 && (CHAR_HEAD_P (translate_prev_byte2
)
1932 || (translate_prev_byte3
== cursor
[-3]))))))))
1933 ch
= simple_translate
[*cursor
];
1942 while ((i
-= direction
) + direction
!= 0)
1944 cursor
-= direction
;
1945 if (pat
[i
] != *cursor
)
1949 cursor
+= dirlen
- i
- direction
; /* fix cursor */
1950 if (i
+ direction
== 0)
1952 EMACS_INT position
, start
, end
;
1954 cursor
-= direction
;
1956 position
= pos_byte
+ cursor
- p2
+ ((direction
> 0)
1957 ? 1 - len_byte
: 0);
1958 set_search_regs (position
, len_byte
);
1960 if (NILP (Vinhibit_changing_match_data
))
1962 start
= search_regs
.start
[0];
1963 end
= search_regs
.end
[0];
1966 /* If Vinhibit_changing_match_data is non-nil,
1967 search_regs will not be changed. So let's
1968 compute start and end here. */
1970 start
= BYTE_TO_CHAR (position
);
1971 end
= BYTE_TO_CHAR (position
+ len_byte
);
1974 if ((n
-= direction
) != 0)
1975 cursor
+= dirlen
; /* to resume search */
1977 return direction
> 0 ? end
: start
;
1980 cursor
+= stride_for_teases
; /* <sigh> we lose - */
1982 pos_byte
+= cursor
- p2
;
1985 /* Now we'll pick up a clump that has to be done the hard
1986 way because it covers a discontinuity. */
1988 limit
= ((direction
> 0)
1989 ? BUFFER_CEILING_OF (pos_byte
- dirlen
+ 1)
1990 : BUFFER_FLOOR_OF (pos_byte
- dirlen
- 1));
1991 limit
= ((direction
> 0)
1992 ? min (limit
+ len_byte
, lim_byte
- 1)
1993 : max (limit
- len_byte
, lim_byte
));
1994 /* LIMIT is now the last value POS_BYTE can have
1995 and still be valid for a possible match. */
1998 /* This loop can be coded for space rather than
1999 speed because it will usually run only once.
2000 (the reach is at most len + 21, and typically
2001 does not exceed len). */
2002 while ((limit
- pos_byte
) * direction
>= 0)
2004 int ch
= FETCH_BYTE (pos_byte
);
2005 if (BM_tab
[ch
] == 0)
2007 pos_byte
+= BM_tab
[ch
];
2009 break; /* ran off the end */
2012 /* Found what might be a match. */
2013 i
= dirlen
- direction
;
2014 while ((i
-= direction
) + direction
!= 0)
2018 pos_byte
-= direction
;
2019 ptr
= BYTE_POS_ADDR (pos_byte
);
2020 /* Translate only the last byte of a character. */
2022 || ((ptr
== tail_end_ptr
2023 || CHAR_HEAD_P (ptr
[1]))
2024 && (CHAR_HEAD_P (ptr
[0])
2025 /* Check if this is the last byte of a
2026 translable character. */
2027 || (translate_prev_byte1
== ptr
[-1]
2028 && (CHAR_HEAD_P (translate_prev_byte1
)
2029 || (translate_prev_byte2
== ptr
[-2]
2030 && (CHAR_HEAD_P (translate_prev_byte2
)
2031 || translate_prev_byte3
== ptr
[-3])))))))
2032 ch
= simple_translate
[*ptr
];
2038 /* Above loop has moved POS_BYTE part or all the way
2039 back to the first pos (last pos if reverse).
2040 Set it once again at the last (first if reverse) char. */
2041 pos_byte
+= dirlen
- i
- direction
;
2042 if (i
+ direction
== 0)
2044 EMACS_INT position
, start
, end
;
2045 pos_byte
-= direction
;
2047 position
= pos_byte
+ ((direction
> 0) ? 1 - len_byte
: 0);
2048 set_search_regs (position
, len_byte
);
2050 if (NILP (Vinhibit_changing_match_data
))
2052 start
= search_regs
.start
[0];
2053 end
= search_regs
.end
[0];
2056 /* If Vinhibit_changing_match_data is non-nil,
2057 search_regs will not be changed. So let's
2058 compute start and end here. */
2060 start
= BYTE_TO_CHAR (position
);
2061 end
= BYTE_TO_CHAR (position
+ len_byte
);
2064 if ((n
-= direction
) != 0)
2065 pos_byte
+= dirlen
; /* to resume search */
2067 return direction
> 0 ? end
: start
;
2070 pos_byte
+= stride_for_teases
;
2073 /* We have done one clump. Can we continue? */
2074 if ((lim_byte
- pos_byte
) * direction
< 0)
2075 return ((0 - n
) * direction
);
2077 return BYTE_TO_CHAR (pos_byte
);
2080 /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES
2081 for the overall match just found in the current buffer.
2082 Also clear out the match data for registers 1 and up. */
2085 set_search_regs (EMACS_INT beg_byte
, EMACS_INT nbytes
)
2089 if (!NILP (Vinhibit_changing_match_data
))
2092 /* Make sure we have registers in which to store
2093 the match position. */
2094 if (search_regs
.num_regs
== 0)
2096 search_regs
.start
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
2097 search_regs
.end
= (regoff_t
*) xmalloc (2 * sizeof (regoff_t
));
2098 search_regs
.num_regs
= 2;
2101 /* Clear out the other registers. */
2102 for (i
= 1; i
< search_regs
.num_regs
; i
++)
2104 search_regs
.start
[i
] = -1;
2105 search_regs
.end
[i
] = -1;
2108 search_regs
.start
[0] = BYTE_TO_CHAR (beg_byte
);
2109 search_regs
.end
[0] = BYTE_TO_CHAR (beg_byte
+ nbytes
);
2110 XSETBUFFER (last_thing_searched
, current_buffer
);
2113 /* Given STRING, a string of words separated by word delimiters,
2114 compute a regexp that matches those exact words separated by
2115 arbitrary punctuation. If LAX is nonzero, the end of the string
2116 need not match a word boundary unless it ends in whitespace. */
2119 wordify (Lisp_Object string
, int lax
)
2121 register unsigned char *p
, *o
;
2122 register int i
, i_byte
, len
, punct_count
= 0, word_count
= 0;
2125 int adjust
, whitespace_at_end
;
2127 CHECK_STRING (string
);
2129 len
= SCHARS (string
);
2131 for (i
= 0, i_byte
= 0; i
< len
; )
2135 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, i
, i_byte
);
2137 if (SYNTAX (c
) != Sword
)
2140 if (i
> 0 && SYNTAX (prev_c
) == Sword
)
2147 if (SYNTAX (prev_c
) == Sword
)
2150 whitespace_at_end
= 0;
2153 whitespace_at_end
= 1;
2156 return empty_unibyte_string
;
2158 adjust
= - punct_count
+ 5 * (word_count
- 1)
2159 + ((lax
&& !whitespace_at_end
) ? 2 : 4);
2160 if (STRING_MULTIBYTE (string
))
2161 val
= make_uninit_multibyte_string (len
+ adjust
,
2165 val
= make_uninit_string (len
+ adjust
);
2172 for (i
= 0, i_byte
= 0; i
< len
; )
2175 int i_byte_orig
= i_byte
;
2177 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, i
, i_byte
);
2179 if (SYNTAX (c
) == Sword
)
2181 memcpy (o
, SDATA (string
) + i_byte_orig
, i_byte
- i_byte_orig
);
2182 o
+= i_byte
- i_byte_orig
;
2184 else if (i
> 0 && SYNTAX (prev_c
) == Sword
&& --word_count
)
2196 if (!lax
|| whitespace_at_end
)
2205 DEFUN ("search-backward", Fsearch_backward
, Ssearch_backward
, 1, 4,
2206 "MSearch backward: ",
2207 doc
: /* Search backward from point for STRING.
2208 Set point to the beginning of the occurrence found, and return point.
2209 An optional second argument bounds the search; it is a buffer position.
2210 The match found must not extend before that position.
2211 Optional third argument, if t, means if fail just return nil (no error).
2212 If not nil and not t, position at limit of search and return nil.
2213 Optional fourth argument is repeat count--search for successive occurrences.
2215 Search case-sensitivity is determined by the value of the variable
2216 `case-fold-search', which see.
2218 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2219 (string
, bound
, noerror
, count
)
2220 Lisp_Object string
, bound
, noerror
, count
;
2222 return search_command (string
, bound
, noerror
, count
, -1, 0, 0);
2225 DEFUN ("search-forward", Fsearch_forward
, Ssearch_forward
, 1, 4, "MSearch: ",
2226 doc
: /* Search forward from point for STRING.
2227 Set point to the end of the occurrence found, and return point.
2228 An optional second argument bounds the search; it is a buffer position.
2229 The match found must not extend after that position. A value of nil is
2230 equivalent to (point-max).
2231 Optional third argument, if t, means if fail just return nil (no error).
2232 If not nil and not t, move to limit of search and return nil.
2233 Optional fourth argument is repeat count--search for successive occurrences.
2235 Search case-sensitivity is determined by the value of the variable
2236 `case-fold-search', which see.
2238 See also the functions `match-beginning', `match-end' and `replace-match'. */)
2239 (string
, bound
, noerror
, count
)
2240 Lisp_Object string
, bound
, noerror
, count
;
2242 return search_command (string
, bound
, noerror
, count
, 1, 0, 0);
2245 DEFUN ("word-search-backward", Fword_search_backward
, Sword_search_backward
, 1, 4,
2246 "sWord search backward: ",
2247 doc
: /* Search backward from point for STRING, ignoring differences in punctuation.
2248 Set point to the beginning of the occurrence found, and return point.
2249 An optional second argument bounds the search; it is a buffer position.
2250 The match found must not extend before that position.
2251 Optional third argument, if t, means if fail just return nil (no error).
2252 If not nil and not t, move to limit of search and return nil.
2253 Optional fourth argument is repeat count--search for successive occurrences. */)
2254 (string
, bound
, noerror
, count
)
2255 Lisp_Object string
, bound
, noerror
, count
;
2257 return search_command (wordify (string
, 0), bound
, noerror
, count
, -1, 1, 0);
2260 DEFUN ("word-search-forward", Fword_search_forward
, Sword_search_forward
, 1, 4,
2262 doc
: /* Search forward from point for STRING, ignoring differences in punctuation.
2263 Set point to the end of the occurrence found, and return point.
2264 An optional second argument bounds the search; it is a buffer position.
2265 The match found must not extend after that position.
2266 Optional third argument, if t, means if fail just return nil (no error).
2267 If not nil and not t, move to limit of search and return nil.
2268 Optional fourth argument is repeat count--search for successive occurrences. */)
2269 (string
, bound
, noerror
, count
)
2270 Lisp_Object string
, bound
, noerror
, count
;
2272 return search_command (wordify (string
, 0), bound
, noerror
, count
, 1, 1, 0);
2275 DEFUN ("word-search-backward-lax", Fword_search_backward_lax
, Sword_search_backward_lax
, 1, 4,
2276 "sWord search backward: ",
2277 doc
: /* Search backward from point for STRING, ignoring differences in punctuation.
2278 Set point to the beginning of the occurrence found, and return point.
2280 Unlike `word-search-backward', the end of STRING need not match a word
2281 boundary unless it ends in whitespace.
2283 An optional second argument bounds the search; it is a buffer position.
2284 The match found must not extend before that position.
2285 Optional third argument, if t, means if fail just return nil (no error).
2286 If not nil and not t, move to limit of search and return nil.
2287 Optional fourth argument is repeat count--search for successive occurrences. */)
2288 (string
, bound
, noerror
, count
)
2289 Lisp_Object string
, bound
, noerror
, count
;
2291 return search_command (wordify (string
, 1), bound
, noerror
, count
, -1, 1, 0);
2294 DEFUN ("word-search-forward-lax", Fword_search_forward_lax
, Sword_search_forward_lax
, 1, 4,
2296 doc
: /* Search forward from point for STRING, ignoring differences in punctuation.
2297 Set point to the end of the occurrence found, and return point.
2299 Unlike `word-search-forward', the end of STRING need not match a word
2300 boundary unless it ends in whitespace.
2302 An optional second argument bounds the search; it is a buffer position.
2303 The match found must not extend after that position.
2304 Optional third argument, if t, means if fail just return nil (no error).
2305 If not nil and not t, move to limit of search and return nil.
2306 Optional fourth argument is repeat count--search for successive occurrences. */)
2307 (string
, bound
, noerror
, count
)
2308 Lisp_Object string
, bound
, noerror
, count
;
2310 return search_command (wordify (string
, 1), bound
, noerror
, count
, 1, 1, 0);
2313 DEFUN ("re-search-backward", Fre_search_backward
, Sre_search_backward
, 1, 4,
2314 "sRE search backward: ",
2315 doc
: /* Search backward from point for match for regular expression REGEXP.
2316 Set point to the beginning of the match, and return point.
2317 The match found is the one starting last in the buffer
2318 and yet ending before the origin of the search.
2319 An optional second argument bounds the search; it is a buffer position.
2320 The match found must start at or after that position.
2321 Optional third argument, if t, means if fail just return nil (no error).
2322 If not nil and not t, move to limit of search and return nil.
2323 Optional fourth argument is repeat count--search for successive occurrences.
2324 See also the functions `match-beginning', `match-end', `match-string',
2325 and `replace-match'. */)
2326 (regexp
, bound
, noerror
, count
)
2327 Lisp_Object regexp
, bound
, noerror
, count
;
2329 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 0);
2332 DEFUN ("re-search-forward", Fre_search_forward
, Sre_search_forward
, 1, 4,
2334 doc
: /* Search forward from point for regular expression REGEXP.
2335 Set point to the end of the occurrence found, and return point.
2336 An optional second argument bounds the search; it is a buffer position.
2337 The match found must not extend after that position.
2338 Optional third argument, if t, means if fail just return nil (no error).
2339 If not nil and not t, move to limit of search and return nil.
2340 Optional fourth argument is repeat count--search for successive occurrences.
2341 See also the functions `match-beginning', `match-end', `match-string',
2342 and `replace-match'. */)
2343 (regexp
, bound
, noerror
, count
)
2344 Lisp_Object regexp
, bound
, noerror
, count
;
2346 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 0);
2349 DEFUN ("posix-search-backward", Fposix_search_backward
, Sposix_search_backward
, 1, 4,
2350 "sPosix search backward: ",
2351 doc
: /* Search backward from point for match for regular expression REGEXP.
2352 Find the longest match in accord with Posix regular expression rules.
2353 Set point to the beginning of the match, and return point.
2354 The match found is the one starting last in the buffer
2355 and yet ending before the origin of the search.
2356 An optional second argument bounds the search; it is a buffer position.
2357 The match found must start at or after that position.
2358 Optional third argument, if t, means if fail just return nil (no error).
2359 If not nil and not t, move to limit of search and return nil.
2360 Optional fourth argument is repeat count--search for successive occurrences.
2361 See also the functions `match-beginning', `match-end', `match-string',
2362 and `replace-match'. */)
2363 (regexp
, bound
, noerror
, count
)
2364 Lisp_Object regexp
, bound
, noerror
, count
;
2366 return search_command (regexp
, bound
, noerror
, count
, -1, 1, 1);
2369 DEFUN ("posix-search-forward", Fposix_search_forward
, Sposix_search_forward
, 1, 4,
2371 doc
: /* Search forward from point for regular expression REGEXP.
2372 Find the longest match in accord with Posix regular expression rules.
2373 Set point to the end of the occurrence found, and return point.
2374 An optional second argument bounds the search; it is a buffer position.
2375 The match found must not extend after that position.
2376 Optional third argument, if t, means if fail just return nil (no error).
2377 If not nil and not t, move to limit of search and return nil.
2378 Optional fourth argument is repeat count--search for successive occurrences.
2379 See also the functions `match-beginning', `match-end', `match-string',
2380 and `replace-match'. */)
2381 (regexp
, bound
, noerror
, count
)
2382 Lisp_Object regexp
, bound
, noerror
, count
;
2384 return search_command (regexp
, bound
, noerror
, count
, 1, 1, 1);
2387 DEFUN ("replace-match", Freplace_match
, Sreplace_match
, 1, 5, 0,
2388 doc
: /* Replace text matched by last search with NEWTEXT.
2389 Leave point at the end of the replacement text.
2391 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
2392 Otherwise maybe capitalize the whole text, or maybe just word initials,
2393 based on the replaced text.
2394 If the replaced text has only capital letters
2395 and has at least one multiletter word, convert NEWTEXT to all caps.
2396 Otherwise if all words are capitalized in the replaced text,
2397 capitalize each word in NEWTEXT.
2399 If third arg LITERAL is non-nil, insert NEWTEXT literally.
2400 Otherwise treat `\\' as special:
2401 `\\&' in NEWTEXT means substitute original matched text.
2402 `\\N' means substitute what matched the Nth `\\(...\\)'.
2403 If Nth parens didn't match, substitute nothing.
2404 `\\\\' means insert one `\\'.
2405 Case conversion does not apply to these substitutions.
2407 FIXEDCASE and LITERAL are optional arguments.
2409 The optional fourth argument STRING can be a string to modify.
2410 This is meaningful when the previous match was done against STRING,
2411 using `string-match'. When used this way, `replace-match'
2412 creates and returns a new string made by copying STRING and replacing
2413 the part of STRING that was matched.
2415 The optional fifth argument SUBEXP specifies a subexpression;
2416 it says to replace just that subexpression with NEWTEXT,
2417 rather than replacing the entire matched text.
2418 This is, in a vague sense, the inverse of using `\\N' in NEWTEXT;
2419 `\\N' copies subexp N into NEWTEXT, but using N as SUBEXP puts
2420 NEWTEXT in place of subexp N.
2421 This is useful only after a regular expression search or match,
2422 since only regular expressions have distinguished subexpressions. */)
2423 (newtext
, fixedcase
, literal
, string
, subexp
)
2424 Lisp_Object newtext
, fixedcase
, literal
, string
, subexp
;
2426 enum { nochange
, all_caps
, cap_initial
} case_action
;
2427 register int pos
, pos_byte
;
2428 int some_multiletter_word
;
2431 int some_nonuppercase_initial
;
2432 register int c
, prevc
;
2434 EMACS_INT opoint
, newpoint
;
2436 CHECK_STRING (newtext
);
2438 if (! NILP (string
))
2439 CHECK_STRING (string
);
2441 case_action
= nochange
; /* We tried an initialization */
2442 /* but some C compilers blew it */
2444 if (search_regs
.num_regs
<= 0)
2445 error ("`replace-match' called before any match found");
2451 CHECK_NUMBER (subexp
);
2452 sub
= XINT (subexp
);
2453 if (sub
< 0 || sub
>= search_regs
.num_regs
)
2454 args_out_of_range (subexp
, make_number (search_regs
.num_regs
));
2459 if (search_regs
.start
[sub
] < BEGV
2460 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2461 || search_regs
.end
[sub
] > ZV
)
2462 args_out_of_range (make_number (search_regs
.start
[sub
]),
2463 make_number (search_regs
.end
[sub
]));
2467 if (search_regs
.start
[sub
] < 0
2468 || search_regs
.start
[sub
] > search_regs
.end
[sub
]
2469 || search_regs
.end
[sub
] > SCHARS (string
))
2470 args_out_of_range (make_number (search_regs
.start
[sub
]),
2471 make_number (search_regs
.end
[sub
]));
2474 if (NILP (fixedcase
))
2476 /* Decide how to casify by examining the matched text. */
2479 pos
= search_regs
.start
[sub
];
2480 last
= search_regs
.end
[sub
];
2483 pos_byte
= CHAR_TO_BYTE (pos
);
2485 pos_byte
= string_char_to_byte (string
, pos
);
2488 case_action
= all_caps
;
2490 /* some_multiletter_word is set nonzero if any original word
2491 is more than one letter long. */
2492 some_multiletter_word
= 0;
2494 some_nonuppercase_initial
= 0;
2501 c
= FETCH_CHAR_AS_MULTIBYTE (pos_byte
);
2502 INC_BOTH (pos
, pos_byte
);
2505 FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c
, string
, pos
, pos_byte
);
2509 /* Cannot be all caps if any original char is lower case */
2512 if (SYNTAX (prevc
) != Sword
)
2513 some_nonuppercase_initial
= 1;
2515 some_multiletter_word
= 1;
2517 else if (UPPERCASEP (c
))
2520 if (SYNTAX (prevc
) != Sword
)
2523 some_multiletter_word
= 1;
2527 /* If the initial is a caseless word constituent,
2528 treat that like a lowercase initial. */
2529 if (SYNTAX (prevc
) != Sword
)
2530 some_nonuppercase_initial
= 1;
2536 /* Convert to all caps if the old text is all caps
2537 and has at least one multiletter word. */
2538 if (! some_lowercase
&& some_multiletter_word
)
2539 case_action
= all_caps
;
2540 /* Capitalize each word, if the old text has all capitalized words. */
2541 else if (!some_nonuppercase_initial
&& some_multiletter_word
)
2542 case_action
= cap_initial
;
2543 else if (!some_nonuppercase_initial
&& some_uppercase
)
2544 /* Should x -> yz, operating on X, give Yz or YZ?
2545 We'll assume the latter. */
2546 case_action
= all_caps
;
2548 case_action
= nochange
;
2551 /* Do replacement in a string. */
2554 Lisp_Object before
, after
;
2556 before
= Fsubstring (string
, make_number (0),
2557 make_number (search_regs
.start
[sub
]));
2558 after
= Fsubstring (string
, make_number (search_regs
.end
[sub
]), Qnil
);
2560 /* Substitute parts of the match into NEWTEXT
2564 EMACS_INT lastpos
= 0;
2565 EMACS_INT lastpos_byte
= 0;
2566 /* We build up the substituted string in ACCUM. */
2569 int length
= SBYTES (newtext
);
2573 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2577 int delbackslash
= 0;
2579 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2583 FETCH_STRING_CHAR_ADVANCE (c
, newtext
, pos
, pos_byte
);
2587 substart
= search_regs
.start
[sub
];
2588 subend
= search_regs
.end
[sub
];
2590 else if (c
>= '1' && c
<= '9')
2592 if (search_regs
.start
[c
- '0'] >= 0
2593 && c
<= search_regs
.num_regs
+ '0')
2595 substart
= search_regs
.start
[c
- '0'];
2596 subend
= search_regs
.end
[c
- '0'];
2600 /* If that subexp did not match,
2601 replace \\N with nothing. */
2609 error ("Invalid use of `\\' in replacement text");
2613 if (pos
- 2 != lastpos
)
2614 middle
= substring_both (newtext
, lastpos
,
2616 pos
- 2, pos_byte
- 2);
2619 accum
= concat3 (accum
, middle
,
2621 make_number (substart
),
2622 make_number (subend
)));
2624 lastpos_byte
= pos_byte
;
2626 else if (delbackslash
)
2628 middle
= substring_both (newtext
, lastpos
,
2630 pos
- 1, pos_byte
- 1);
2632 accum
= concat2 (accum
, middle
);
2634 lastpos_byte
= pos_byte
;
2639 middle
= substring_both (newtext
, lastpos
,
2645 newtext
= concat2 (accum
, middle
);
2648 /* Do case substitution in NEWTEXT if desired. */
2649 if (case_action
== all_caps
)
2650 newtext
= Fupcase (newtext
);
2651 else if (case_action
== cap_initial
)
2652 newtext
= Fupcase_initials (newtext
);
2654 return concat3 (before
, newtext
, after
);
2657 /* Record point, then move (quietly) to the start of the match. */
2658 if (PT
>= search_regs
.end
[sub
])
2660 else if (PT
> search_regs
.start
[sub
])
2661 opoint
= search_regs
.end
[sub
] - ZV
;
2665 /* If we want non-literal replacement,
2666 perform substitution on the replacement string. */
2669 int length
= SBYTES (newtext
);
2670 unsigned char *substed
;
2671 int substed_alloc_size
, substed_len
;
2672 int buf_multibyte
= !NILP (current_buffer
->enable_multibyte_characters
);
2673 int str_multibyte
= STRING_MULTIBYTE (newtext
);
2674 Lisp_Object rev_tbl
;
2675 int really_changed
= 0;
2679 substed_alloc_size
= length
* 2 + 100;
2680 substed
= (unsigned char *) xmalloc (substed_alloc_size
+ 1);
2683 /* Go thru NEWTEXT, producing the actual text to insert in
2684 SUBSTED while adjusting multibyteness to that of the current
2687 for (pos_byte
= 0, pos
= 0; pos_byte
< length
;)
2689 unsigned char str
[MAX_MULTIBYTE_LENGTH
];
2690 unsigned char *add_stuff
= NULL
;
2696 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
, pos
, pos_byte
);
2698 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2702 /* Note that we don't have to increment POS. */
2703 c
= SREF (newtext
, pos_byte
++);
2705 MAKE_CHAR_MULTIBYTE (c
);
2708 /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
2709 or set IDX to a match index, which means put that part
2710 of the buffer text into SUBSTED. */
2718 FETCH_STRING_CHAR_ADVANCE_NO_CHECK (c
, newtext
,
2720 if (!buf_multibyte
&& !ASCII_CHAR_P (c
))
2721 c
= multibyte_char_to_unibyte (c
, rev_tbl
);
2725 c
= SREF (newtext
, pos_byte
++);
2727 MAKE_CHAR_MULTIBYTE (c
);
2732 else if (c
>= '1' && c
<= '9' && c
<= search_regs
.num_regs
+ '0')
2734 if (search_regs
.start
[c
- '0'] >= 1)
2738 add_len
= 1, add_stuff
= "\\";
2742 error ("Invalid use of `\\' in replacement text");
2747 add_len
= CHAR_STRING (c
, str
);
2751 /* If we want to copy part of a previous match,
2752 set up ADD_STUFF and ADD_LEN to point to it. */
2755 EMACS_INT begbyte
= CHAR_TO_BYTE (search_regs
.start
[idx
]);
2756 add_len
= CHAR_TO_BYTE (search_regs
.end
[idx
]) - begbyte
;
2757 if (search_regs
.start
[idx
] < GPT
&& GPT
< search_regs
.end
[idx
])
2758 move_gap (search_regs
.start
[idx
]);
2759 add_stuff
= BYTE_POS_ADDR (begbyte
);
2762 /* Now the stuff we want to add to SUBSTED
2763 is invariably ADD_LEN bytes starting at ADD_STUFF. */
2765 /* Make sure SUBSTED is big enough. */
2766 if (substed_len
+ add_len
>= substed_alloc_size
)
2768 substed_alloc_size
= substed_len
+ add_len
+ 500;
2769 substed
= (unsigned char *) xrealloc (substed
,
2770 substed_alloc_size
+ 1);
2773 /* Now add to the end of SUBSTED. */
2776 memcpy (substed
+ substed_len
, add_stuff
, add_len
);
2777 substed_len
+= add_len
;
2785 int nchars
= multibyte_chars_in_text (substed
, substed_len
);
2787 newtext
= make_multibyte_string (substed
, nchars
, substed_len
);
2790 newtext
= make_unibyte_string (substed
, substed_len
);
2795 /* Replace the old text with the new in the cleanest possible way. */
2796 replace_range (search_regs
.start
[sub
], search_regs
.end
[sub
],
2798 newpoint
= search_regs
.start
[sub
] + SCHARS (newtext
);
2800 if (case_action
== all_caps
)
2801 Fupcase_region (make_number (search_regs
.start
[sub
]),
2802 make_number (newpoint
));
2803 else if (case_action
== cap_initial
)
2804 Fupcase_initials_region (make_number (search_regs
.start
[sub
]),
2805 make_number (newpoint
));
2807 /* Adjust search data for this change. */
2809 EMACS_INT oldend
= search_regs
.end
[sub
];
2810 EMACS_INT oldstart
= search_regs
.start
[sub
];
2811 EMACS_INT change
= newpoint
- search_regs
.end
[sub
];
2814 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2816 if (search_regs
.start
[i
] >= oldend
)
2817 search_regs
.start
[i
] += change
;
2818 else if (search_regs
.start
[i
] > oldstart
)
2819 search_regs
.start
[i
] = oldstart
;
2820 if (search_regs
.end
[i
] >= oldend
)
2821 search_regs
.end
[i
] += change
;
2822 else if (search_regs
.end
[i
] > oldstart
)
2823 search_regs
.end
[i
] = oldstart
;
2827 /* Put point back where it was in the text. */
2829 TEMP_SET_PT (opoint
+ ZV
);
2831 TEMP_SET_PT (opoint
);
2833 /* Now move point "officially" to the start of the inserted replacement. */
2834 move_if_not_intangible (newpoint
);
2840 match_limit (Lisp_Object num
, int beginningp
)
2847 args_out_of_range (num
, make_number (0));
2848 if (search_regs
.num_regs
<= 0)
2849 error ("No match data, because no search succeeded");
2850 if (n
>= search_regs
.num_regs
2851 || search_regs
.start
[n
] < 0)
2853 return (make_number ((beginningp
) ? search_regs
.start
[n
]
2854 : search_regs
.end
[n
]));
2857 DEFUN ("match-beginning", Fmatch_beginning
, Smatch_beginning
, 1, 1, 0,
2858 doc
: /* Return position of start of text matched by last search.
2859 SUBEXP, a number, specifies which parenthesized expression in the last
2861 Value is nil if SUBEXPth pair didn't match, or there were less than
2863 Zero means the entire text matched by the whole regexp or whole string. */)
2867 return match_limit (subexp
, 1);
2870 DEFUN ("match-end", Fmatch_end
, Smatch_end
, 1, 1, 0,
2871 doc
: /* Return position of end of text matched by last search.
2872 SUBEXP, a number, specifies which parenthesized expression in the last
2874 Value is nil if SUBEXPth pair didn't match, or there were less than
2876 Zero means the entire text matched by the whole regexp or whole string. */)
2880 return match_limit (subexp
, 0);
2883 DEFUN ("match-data", Fmatch_data
, Smatch_data
, 0, 3, 0,
2884 doc
: /* Return a list containing all info on what the last search matched.
2885 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
2886 All the elements are markers or nil (nil if the Nth pair didn't match)
2887 if the last match was on a buffer; integers or nil if a string was matched.
2888 Use `set-match-data' to reinstate the data in this list.
2890 If INTEGERS (the optional first argument) is non-nil, always use
2891 integers \(rather than markers) to represent buffer positions. In
2892 this case, and if the last match was in a buffer, the buffer will get
2893 stored as one additional element at the end of the list.
2895 If REUSE is a list, reuse it as part of the value. If REUSE is long
2896 enough to hold all the values, and if INTEGERS is non-nil, no consing
2899 If optional third arg RESEAT is non-nil, any previous markers on the
2900 REUSE list will be modified to point to nowhere.
2902 Return value is undefined if the last search failed. */)
2903 (integers
, reuse
, reseat
)
2904 Lisp_Object integers
, reuse
, reseat
;
2906 Lisp_Object tail
, prev
;
2911 for (tail
= reuse
; CONSP (tail
); tail
= XCDR (tail
))
2912 if (MARKERP (XCAR (tail
)))
2914 unchain_marker (XMARKER (XCAR (tail
)));
2915 XSETCAR (tail
, Qnil
);
2918 if (NILP (last_thing_searched
))
2923 data
= (Lisp_Object
*) alloca ((2 * search_regs
.num_regs
+ 1)
2924 * sizeof (Lisp_Object
));
2927 for (i
= 0; i
< search_regs
.num_regs
; i
++)
2929 int start
= search_regs
.start
[i
];
2932 if (EQ (last_thing_searched
, Qt
)
2933 || ! NILP (integers
))
2935 XSETFASTINT (data
[2 * i
], start
);
2936 XSETFASTINT (data
[2 * i
+ 1], search_regs
.end
[i
]);
2938 else if (BUFFERP (last_thing_searched
))
2940 data
[2 * i
] = Fmake_marker ();
2941 Fset_marker (data
[2 * i
],
2942 make_number (start
),
2943 last_thing_searched
);
2944 data
[2 * i
+ 1] = Fmake_marker ();
2945 Fset_marker (data
[2 * i
+ 1],
2946 make_number (search_regs
.end
[i
]),
2947 last_thing_searched
);
2950 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
2956 data
[2 * i
] = data
[2 * i
+ 1] = Qnil
;
2959 if (BUFFERP (last_thing_searched
) && !NILP (integers
))
2961 data
[len
] = last_thing_searched
;
2965 /* If REUSE is not usable, cons up the values and return them. */
2966 if (! CONSP (reuse
))
2967 return Flist (len
, data
);
2969 /* If REUSE is a list, store as many value elements as will fit
2970 into the elements of REUSE. */
2971 for (i
= 0, tail
= reuse
; CONSP (tail
);
2972 i
++, tail
= XCDR (tail
))
2975 XSETCAR (tail
, data
[i
]);
2977 XSETCAR (tail
, Qnil
);
2981 /* If we couldn't fit all value elements into REUSE,
2982 cons up the rest of them and add them to the end of REUSE. */
2984 XSETCDR (prev
, Flist (len
- i
, data
+ i
));
2989 /* We used to have an internal use variant of `reseat' described as:
2991 If RESEAT is `evaporate', put the markers back on the free list
2992 immediately. No other references to the markers must exist in this
2993 case, so it is used only internally on the unwind stack and
2994 save-match-data from Lisp.
2996 But it was ill-conceived: those supposedly-internal markers get exposed via
2997 the undo-list, so freeing them here is unsafe. */
2999 DEFUN ("set-match-data", Fset_match_data
, Sset_match_data
, 1, 2, 0,
3000 doc
: /* Set internal data on last search match from elements of LIST.
3001 LIST should have been created by calling `match-data' previously.
3003 If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */)
3005 register Lisp_Object list
, reseat
;
3008 register Lisp_Object marker
;
3010 if (running_asynch_code
)
3011 save_search_regs ();
3015 /* Unless we find a marker with a buffer or an explicit buffer
3016 in LIST, assume that this match data came from a string. */
3017 last_thing_searched
= Qt
;
3019 /* Allocate registers if they don't already exist. */
3021 int length
= XFASTINT (Flength (list
)) / 2;
3023 if (length
> search_regs
.num_regs
)
3025 if (search_regs
.num_regs
== 0)
3028 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
3030 = (regoff_t
*) xmalloc (length
* sizeof (regoff_t
));
3035 = (regoff_t
*) xrealloc (search_regs
.start
,
3036 length
* sizeof (regoff_t
));
3038 = (regoff_t
*) xrealloc (search_regs
.end
,
3039 length
* sizeof (regoff_t
));
3042 for (i
= search_regs
.num_regs
; i
< length
; i
++)
3043 search_regs
.start
[i
] = -1;
3045 search_regs
.num_regs
= length
;
3048 for (i
= 0; CONSP (list
); i
++)
3050 marker
= XCAR (list
);
3051 if (BUFFERP (marker
))
3053 last_thing_searched
= marker
;
3060 search_regs
.start
[i
] = -1;
3069 if (MARKERP (marker
))
3071 if (XMARKER (marker
)->buffer
== 0)
3072 XSETFASTINT (marker
, 0);
3074 XSETBUFFER (last_thing_searched
, XMARKER (marker
)->buffer
);
3077 CHECK_NUMBER_COERCE_MARKER (marker
);
3078 from
= XINT (marker
);
3080 if (!NILP (reseat
) && MARKERP (m
))
3082 unchain_marker (XMARKER (m
));
3083 XSETCAR (list
, Qnil
);
3086 if ((list
= XCDR (list
), !CONSP (list
)))
3089 m
= marker
= XCAR (list
);
3091 if (MARKERP (marker
) && XMARKER (marker
)->buffer
== 0)
3092 XSETFASTINT (marker
, 0);
3094 CHECK_NUMBER_COERCE_MARKER (marker
);
3095 search_regs
.start
[i
] = from
;
3096 search_regs
.end
[i
] = XINT (marker
);
3098 if (!NILP (reseat
) && MARKERP (m
))
3100 unchain_marker (XMARKER (m
));
3101 XSETCAR (list
, Qnil
);
3107 for (; i
< search_regs
.num_regs
; i
++)
3108 search_regs
.start
[i
] = -1;
3114 /* If non-zero the match data have been saved in saved_search_regs
3115 during the execution of a sentinel or filter. */
3116 static int search_regs_saved
;
3117 static struct re_registers saved_search_regs
;
3118 static Lisp_Object saved_last_thing_searched
;
3120 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
3121 if asynchronous code (filter or sentinel) is running. */
3123 save_search_regs (void)
3125 if (!search_regs_saved
)
3127 saved_search_regs
.num_regs
= search_regs
.num_regs
;
3128 saved_search_regs
.start
= search_regs
.start
;
3129 saved_search_regs
.end
= search_regs
.end
;
3130 saved_last_thing_searched
= last_thing_searched
;
3131 last_thing_searched
= Qnil
;
3132 search_regs
.num_regs
= 0;
3133 search_regs
.start
= 0;
3134 search_regs
.end
= 0;
3136 search_regs_saved
= 1;
3140 /* Called upon exit from filters and sentinels. */
3142 restore_search_regs (void)
3144 if (search_regs_saved
)
3146 if (search_regs
.num_regs
> 0)
3148 xfree (search_regs
.start
);
3149 xfree (search_regs
.end
);
3151 search_regs
.num_regs
= saved_search_regs
.num_regs
;
3152 search_regs
.start
= saved_search_regs
.start
;
3153 search_regs
.end
= saved_search_regs
.end
;
3154 last_thing_searched
= saved_last_thing_searched
;
3155 saved_last_thing_searched
= Qnil
;
3156 search_regs_saved
= 0;
3161 unwind_set_match_data (Lisp_Object list
)
3163 /* It is NOT ALWAYS safe to free (evaporate) the markers immediately. */
3164 return Fset_match_data (list
, Qt
);
3167 /* Called to unwind protect the match data. */
3169 record_unwind_save_match_data (void)
3171 record_unwind_protect (unwind_set_match_data
,
3172 Fmatch_data (Qnil
, Qnil
, Qnil
));
3175 /* Quote a string to inactivate reg-expr chars */
3177 DEFUN ("regexp-quote", Fregexp_quote
, Sregexp_quote
, 1, 1, 0,
3178 doc
: /* Return a regexp string which matches exactly STRING and nothing else. */)
3182 register unsigned char *in
, *out
, *end
;
3183 register unsigned char *temp
;
3184 int backslashes_added
= 0;
3186 CHECK_STRING (string
);
3188 temp
= (unsigned char *) alloca (SBYTES (string
) * 2);
3190 /* Now copy the data into the new string, inserting escapes. */
3192 in
= SDATA (string
);
3193 end
= in
+ SBYTES (string
);
3196 for (; in
!= end
; in
++)
3199 || *in
== '*' || *in
== '.' || *in
== '\\'
3200 || *in
== '?' || *in
== '+'
3201 || *in
== '^' || *in
== '$')
3202 *out
++ = '\\', backslashes_added
++;
3206 return make_specified_string (temp
,
3207 SCHARS (string
) + backslashes_added
,
3209 STRING_MULTIBYTE (string
));
3213 syms_of_search (void)
3217 for (i
= 0; i
< REGEXP_CACHE_SIZE
; ++i
)
3219 searchbufs
[i
].buf
.allocated
= 100;
3220 searchbufs
[i
].buf
.buffer
= (unsigned char *) xmalloc (100);
3221 searchbufs
[i
].buf
.fastmap
= searchbufs
[i
].fastmap
;
3222 searchbufs
[i
].regexp
= Qnil
;
3223 searchbufs
[i
].whitespace_regexp
= Qnil
;
3224 searchbufs
[i
].syntax_table
= Qnil
;
3225 staticpro (&searchbufs
[i
].regexp
);
3226 staticpro (&searchbufs
[i
].whitespace_regexp
);
3227 staticpro (&searchbufs
[i
].syntax_table
);
3228 searchbufs
[i
].next
= (i
== REGEXP_CACHE_SIZE
-1 ? 0 : &searchbufs
[i
+1]);
3230 searchbuf_head
= &searchbufs
[0];
3232 Qsearch_failed
= intern_c_string ("search-failed");
3233 staticpro (&Qsearch_failed
);
3234 Qinvalid_regexp
= intern_c_string ("invalid-regexp");
3235 staticpro (&Qinvalid_regexp
);
3237 Fput (Qsearch_failed
, Qerror_conditions
,
3238 pure_cons (Qsearch_failed
, pure_cons (Qerror
, Qnil
)));
3239 Fput (Qsearch_failed
, Qerror_message
,
3240 make_pure_c_string ("Search failed"));
3242 Fput (Qinvalid_regexp
, Qerror_conditions
,
3243 pure_cons (Qinvalid_regexp
, pure_cons (Qerror
, Qnil
)));
3244 Fput (Qinvalid_regexp
, Qerror_message
,
3245 make_pure_c_string ("Invalid regexp"));
3247 last_thing_searched
= Qnil
;
3248 staticpro (&last_thing_searched
);
3250 saved_last_thing_searched
= Qnil
;
3251 staticpro (&saved_last_thing_searched
);
3253 DEFVAR_LISP ("search-spaces-regexp", &Vsearch_spaces_regexp
,
3254 doc
: /* Regexp to substitute for bunches of spaces in regexp search.
3255 Some commands use this for user-specified regexps.
3256 Spaces that occur inside character classes or repetition operators
3257 or other such regexp constructs are not replaced with this.
3258 A value of nil (which is the normal value) means treat spaces literally. */);
3259 Vsearch_spaces_regexp
= Qnil
;
3261 DEFVAR_LISP ("inhibit-changing-match-data", &Vinhibit_changing_match_data
,
3262 doc
: /* Internal use only.
3263 If non-nil, the primitive searching and matching functions
3264 such as `looking-at', `string-match', `re-search-forward', etc.,
3265 do not set the match data. The proper way to use this variable
3266 is to bind it with `let' around a small expression. */);
3267 Vinhibit_changing_match_data
= Qnil
;
3269 defsubr (&Slooking_at
);
3270 defsubr (&Sposix_looking_at
);
3271 defsubr (&Sstring_match
);
3272 defsubr (&Sposix_string_match
);
3273 defsubr (&Ssearch_forward
);
3274 defsubr (&Ssearch_backward
);
3275 defsubr (&Sword_search_forward
);
3276 defsubr (&Sword_search_backward
);
3277 defsubr (&Sword_search_forward_lax
);
3278 defsubr (&Sword_search_backward_lax
);
3279 defsubr (&Sre_search_forward
);
3280 defsubr (&Sre_search_backward
);
3281 defsubr (&Sposix_search_forward
);
3282 defsubr (&Sposix_search_backward
);
3283 defsubr (&Sreplace_match
);
3284 defsubr (&Smatch_beginning
);
3285 defsubr (&Smatch_end
);
3286 defsubr (&Smatch_data
);
3287 defsubr (&Sset_match_data
);
3288 defsubr (&Sregexp_quote
);
3291 /* arch-tag: a6059d79-0552-4f14-a2cb-d379a4e3c78f
3292 (do not change this comment) */