1 /* String search routines for XEmacs.
2 Copyright (C) 1985, 1986, 1987, 1992-1995 Free Software Foundation, Inc.
3 Copyright (C) 1995 Sun Microsystems, Inc.
5 This file is part of XEmacs.
7 XEmacs is free software; you can redistribute it and/or modify it
8 under the terms of the GNU General Public License as published by the
9 Free Software Foundation; either version 2, or (at your option) any
12 XEmacs is distributed in the hope that it will be useful, but WITHOUT
13 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with XEmacs; 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. */
22 /* Synched up with: FSF 19.29, except for region-cache stuff. */
24 /* Hacked on for Mule by Ben Wing, December 1994 and August 1995. */
26 /* This file has been Mule-ized except for the TRT stuff. */
34 #ifdef REGION_CACHE_NEEDS_WORK
35 #include "region-cache.h"
39 #include <sys/types.h>
43 #define REGEXP_CACHE_SIZE 20
45 /* If the regexp is non-nil, then the buffer contains the compiled form
46 of that regexp, suitable for searching. */
48 struct regexp_cache *next;
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. */
81 /* Note: things get trickier under Mule because the values returned from
82 the regexp routines are in Bytinds but we need them to be in Bufpos's.
83 We take the easy way out for the moment and just convert them immediately.
84 We could be more clever by not converting them until necessary, but
85 that gets real ugly real fast since the buffer might have changed and
86 the positions might be out of sync or out of range.
88 static struct re_registers search_regs;
90 /* The buffer in which the last search was performed, or
91 Qt if the last search was done in a string;
92 Qnil if no searching has been done yet. */
93 static Lisp_Object last_thing_searched;
95 /* error condition signalled when regexp compile_pattern fails */
97 Lisp_Object Qinvalid_regexp;
99 /* Regular expressions used in forward/backward-word */
100 Lisp_Object Vforward_word_regexp, Vbackward_word_regexp;
102 /* range table for use with skip_chars. Only needed for Mule. */
103 Lisp_Object Vskip_chars_range_table;
105 static void set_search_regs (struct buffer *buf, Bufpos beg, Charcount len);
106 static void save_search_regs (void);
107 static Bufpos search_buffer (struct buffer *buf, Lisp_Object str,
108 Bufpos bufpos, Bufpos buflim, EMACS_INT n, int RE,
109 unsigned char *trt, unsigned char *inverse_trt,
113 matcher_overflow (void)
115 error ("Stack overflow in regexp matcher");
118 /* Compile a regexp and signal a Lisp error if anything goes wrong.
119 PATTERN is the pattern to compile.
120 CP is the place to put the result.
121 TRANSLATE is a translation table for ignoring case, or NULL for none.
122 REGP is the structure that says where to store the "register"
123 values that will result from matching this pattern.
124 If it is 0, we should compile the pattern not to record any
125 subexpression bounds.
126 POSIX is nonzero if we want full backtracking (POSIX style)
127 for this pattern. 0 means backtrack only enough to get a valid match. */
130 compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
131 char *translate, struct re_registers *regp, int posix,
138 cp->buf.translate = translate;
140 old = re_set_syntax (RE_SYNTAX_EMACS
141 | (posix ? 0 : RE_NO_POSIX_BACKTRACKING));
143 re_compile_pattern ((char *) XSTRING_DATA (pattern),
144 XSTRING_LENGTH (pattern), &cp->buf);
148 maybe_signal_error (Qinvalid_regexp, list1 (build_string (val)),
153 cp->regexp = Fcopy_sequence (pattern);
157 /* Compile a regexp if necessary, but first check to see if there's one in
159 PATTERN is the pattern to compile.
160 TRANSLATE is a translation table for ignoring case, or NULL for none.
161 REGP is the structure that says where to store the "register"
162 values that will result from matching this pattern.
163 If it is 0, we should compile the pattern not to record any
164 subexpression bounds.
165 POSIX is nonzero if we want full backtracking (POSIX style)
166 for this pattern. 0 means backtrack only enough to get a valid match. */
168 struct re_pattern_buffer *
169 compile_pattern (Lisp_Object pattern, struct re_registers *regp,
170 char *translate, int posix, Error_behavior errb)
172 struct regexp_cache *cp, **cpp;
174 for (cpp = &searchbuf_head; ; cpp = &cp->next)
177 if (!NILP (Fstring_equal (cp->regexp, pattern))
178 && cp->buf.translate == translate
179 && cp->posix == posix)
182 /* If we're at the end of the cache, compile into the last cell. */
185 if (!compile_pattern_1 (cp, pattern, translate, regp, posix,
192 /* When we get here, cp (aka *cpp) contains the compiled pattern,
193 either because we found it in the cache or because we just compiled it.
194 Move it to the front of the queue to mark it as most recently used. */
196 cp->next = searchbuf_head;
199 /* Advise the searching functions about the space we have allocated
200 for register data. */
202 re_set_registers (&cp->buf, regp, regp->num_regs, regp->start, regp->end);
207 /* Error condition used for failing searches */
208 Lisp_Object Qsearch_failed;
211 signal_failure (Lisp_Object arg)
213 Fsignal (Qsearch_failed, list1 (arg));
217 /* Convert the search registers from Bytinds to Bufpos's. Needs to be
218 done after each regexp match that uses the search regs.
220 We could get a potential speedup by not converting the search registers
221 until it's really necessary, e.g. when match-data or replace-match is
222 called. However, this complexifies the code a lot (e.g. the buffer
223 could have changed and the Bytinds stored might be invalid) and is
224 probably not a great time-saver. */
227 fixup_search_regs_for_buffer (struct buffer *buf)
230 int num_regs = search_regs.num_regs;
232 for (i = 0; i < num_regs; i++)
234 if (search_regs.start[i] >= 0)
235 search_regs.start[i] = bytind_to_bufpos (buf, search_regs.start[i]);
236 if (search_regs.end[i] >= 0)
237 search_regs.end[i] = bytind_to_bufpos (buf, search_regs.end[i]);
241 /* Similar but for strings. */
243 fixup_search_regs_for_string (Lisp_Object string)
246 int num_regs = search_regs.num_regs;
248 /* #### bytecount_to_charcount() is not that efficient. This function
249 could be faster if it did its own conversion (using INC_CHARPTR()
250 and such), because the register ends are likely to be somewhat ordered.
251 (Even if not, you could sort them.)
253 Think about this if this function is a time hog, which it's probably
255 for (i = 0; i < num_regs; i++)
257 if (search_regs.start[i] > 0)
259 search_regs.start[i] =
260 bytecount_to_charcount (XSTRING_DATA (string),
261 search_regs.start[i]);
263 if (search_regs.end[i] > 0)
266 bytecount_to_charcount (XSTRING_DATA (string),
274 looking_at_1 (Lisp_Object string, struct buffer *buf, int posix)
276 /* This function has been Mule-ized, except for the trt table handling. */
281 struct re_pattern_buffer *bufp;
283 if (running_asynch_code)
286 CHECK_STRING (string);
287 bufp = compile_pattern (string, &search_regs,
288 (!NILP (buf->case_fold_search)
289 ? (char *) MIRROR_DOWNCASE_TABLE_AS_STRING (buf)
295 /* Get pointers and sizes of the two strings
296 that make up the visible portion of the buffer. */
298 p1 = BI_BUF_BEGV (buf);
299 p2 = BI_BUF_CEILING_OF (buf, p1);
301 s2 = BI_BUF_ZV (buf) - p2;
303 regex_emacs_buffer = buf;
304 i = re_match_2 (bufp, (char *) BI_BUF_BYTE_ADDRESS (buf, p1),
305 s1, (char *) BI_BUF_BYTE_ADDRESS (buf, p2), s2,
306 BI_BUF_PT (buf) - BI_BUF_BEGV (buf), &search_regs,
307 BI_BUF_ZV (buf) - BI_BUF_BEGV (buf));
312 val = (0 <= i ? Qt : Qnil);
316 int num_regs = search_regs.num_regs;
317 for (i = 0; i < num_regs; i++)
318 if (search_regs.start[i] >= 0)
320 search_regs.start[i] += BI_BUF_BEGV (buf);
321 search_regs.end[i] += BI_BUF_BEGV (buf);
324 XSETBUFFER (last_thing_searched, buf);
325 fixup_search_regs_for_buffer (buf);
329 DEFUN ("looking-at", Flooking_at, 1, 2, 0, /*
330 Return t if text after point matches regular expression REGEXP.
331 This function modifies the match data that `match-beginning',
332 `match-end' and `match-data' access; save and restore the match
333 data if you want to preserve them.
335 Optional argument BUFFER defaults to the current buffer.
339 return looking_at_1 (regexp, decode_buffer (buffer, 0), 0);
342 DEFUN ("posix-looking-at", Fposix_looking_at, 1, 2, 0, /*
343 Return t if text after point matches regular expression REGEXP.
344 Find the longest match, in accord with Posix regular expression rules.
345 This function modifies the match data that `match-beginning',
346 `match-end' and `match-data' access; save and restore the match
347 data if you want to preserve them.
349 Optional argument BUFFER defaults to the current buffer.
353 return looking_at_1 (regexp, decode_buffer (buffer, 0), 1);
357 string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start,
358 struct buffer *buf, int posix)
360 /* This function has been Mule-ized, except for the trt table handling. */
363 struct re_pattern_buffer *bufp;
365 if (running_asynch_code)
368 CHECK_STRING (regexp);
369 CHECK_STRING (string);
375 Charcount len = XSTRING_CHAR_LENGTH (string);
379 if (s < 0 && -s <= len)
381 else if (0 > s || s > len)
382 args_out_of_range (string, start);
386 bufp = compile_pattern (regexp, &search_regs,
387 (!NILP (buf->case_fold_search)
388 ? (char *) MIRROR_DOWNCASE_TABLE_AS_STRING (buf)
392 Bytecount bis = charcount_to_bytecount (XSTRING_DATA (string), s);
393 regex_emacs_buffer = buf;
394 val = re_search (bufp, (char *) XSTRING_DATA (string),
395 XSTRING_LENGTH (string), bis,
396 XSTRING_LENGTH (string) - bis,
401 if (val < 0) return Qnil;
402 last_thing_searched = Qt;
403 fixup_search_regs_for_string (string);
404 return make_int (bytecount_to_charcount (XSTRING_DATA (string), val));
407 DEFUN ("string-match", Fstring_match, 2, 4, 0, /*
408 Return index of start of first match for REGEXP in STRING, or nil.
409 If third arg START is non-nil, start search at that index in STRING.
410 For index of first char beyond the match, do (match-end 0).
411 `match-end' and `match-beginning' also give indices of substrings
412 matched by parenthesis constructs in the pattern.
414 Optional arg BUFFER controls how case folding is done (according to
415 the value of `case-fold-search' in that buffer and that buffer's case
416 tables) and defaults to the current buffer.
418 (regexp, string, start, buffer))
420 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 0);
423 DEFUN ("posix-string-match", Fposix_string_match, 2, 4, 0, /*
424 Return index of start of first match for REGEXP in STRING, or nil.
425 Find the longest match, in accord with Posix regular expression rules.
426 If third arg START is non-nil, start search at that index in STRING.
427 For index of first char beyond the match, do (match-end 0).
428 `match-end' and `match-beginning' also give indices of substrings
429 matched by parenthesis constructs in the pattern.
431 Optional arg BUFFER controls how case folding is done (according to
432 the value of `case-fold-search' in that buffer and that buffer's case
433 tables) and defaults to the current buffer.
435 (regexp, string, start, buffer))
437 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 1);
440 /* Match REGEXP against STRING, searching all of STRING,
441 and return the index of the match, or negative on failure.
442 This does not clobber the match data. */
445 fast_string_match (Lisp_Object regexp, CONST Bufbyte *nonreloc,
446 Lisp_Object reloc, Bytecount offset,
447 Bytecount length, int case_fold_search,
448 Error_behavior errb, int no_quit)
450 /* This function has been Mule-ized, except for the trt table handling. */
452 Bufbyte *newnonreloc = (Bufbyte *) nonreloc;
453 struct re_pattern_buffer *bufp;
455 bufp = compile_pattern (regexp, 0,
458 /* #### evil current-buffer dependency */
459 MIRROR_DOWNCASE_TABLE_AS_STRING (current_buffer)
463 return -1; /* will only do this when errb != ERROR_ME */
467 no_quit_in_re_search = 1;
469 fixup_internal_substring (nonreloc, reloc, offset, &length);
474 newnonreloc = XSTRING_DATA (reloc);
477 /* QUIT could relocate RELOC. Therefore we must alloca()
478 and copy. No way around this except some serious
479 rewriting of re_search(). */
480 newnonreloc = (Bufbyte *) alloca (length);
481 memcpy (newnonreloc, XSTRING_DATA (reloc), length);
485 /* #### evil current-buffer dependency */
486 regex_emacs_buffer = current_buffer;
487 val = re_search (bufp, (char *) newnonreloc + offset, length, 0,
490 no_quit_in_re_search = 0;
495 fast_lisp_string_match (Lisp_Object regex, Lisp_Object string)
497 return fast_string_match (regex, 0, string, 0, -1, 0, ERROR_ME, 0);
501 #ifdef REGION_CACHE_NEEDS_WORK
502 /* The newline cache: remembering which sections of text have no newlines. */
504 /* If the user has requested newline caching, make sure it's on.
505 Otherwise, make sure it's off.
506 This is our cheezy way of associating an action with the change of
507 state of a buffer-local variable. */
509 newline_cache_on_off (struct buffer *buf)
511 if (NILP (buf->cache_long_line_scans))
513 /* It should be off. */
514 if (buf->newline_cache)
516 free_region_cache (buf->newline_cache);
517 buf->newline_cache = 0;
522 /* It should be on. */
523 if (buf->newline_cache == 0)
524 buf->newline_cache = new_region_cache ();
529 /* Search in BUF for COUNT instances of the character TARGET between
532 If COUNT is positive, search forwards; END must be >= START.
533 If COUNT is negative, search backwards for the -COUNTth instance;
534 END must be <= START.
535 If COUNT is zero, do anything you please; run rogue, for all I care.
537 If END is zero, use BEGV or ZV instead, as appropriate for the
538 direction indicated by COUNT.
540 If we find COUNT instances, set *SHORTAGE to zero, and return the
541 position after the COUNTth match. Note that for reverse motion
542 this is not the same as the usual convention for Emacs motion commands.
544 If we don't find COUNT instances before reaching END, set *SHORTAGE
545 to the number of TARGETs left unfound, and return END.
547 If ALLOW_QUIT is non-zero, call QUIT periodically. */
550 bi_scan_buffer (struct buffer *buf, Emchar target, Bytind st, Bytind en,
551 EMACS_INT count, EMACS_INT *shortage, int allow_quit)
553 /* This function has been Mule-ized. */
554 Bytind lim = en > 0 ? en :
555 ((count > 0) ? BI_BUF_ZV (buf) : BI_BUF_BEGV (buf));
557 /* #### newline cache stuff in this function not yet ported */
567 /* Due to the Mule representation of characters in a buffer,
568 we can simply search for characters in the range 0 - 127
569 directly. For other characters, we do it the "hard" way.
570 Note that this way works for all characters but the other
574 while (st < lim && count > 0)
576 if (BI_BUF_FETCH_CHAR (buf, st) == target)
578 INC_BYTIND (buf, st);
584 while (st < lim && count > 0)
589 ceil = BI_BUF_CEILING_OF (buf, st);
590 ceil = min (lim, ceil);
591 bufptr = (Bufbyte *) memchr (BI_BUF_BYTE_ADDRESS (buf, st),
592 (int) target, ceil - st);
596 st = BI_BUF_PTR_BYTE_POS (buf, bufptr) + 1;
614 while (st > lim && count < 0)
616 DEC_BYTIND (buf, st);
617 if (BI_BUF_FETCH_CHAR (buf, st) == target)
624 while (st > lim && count < 0)
630 floor = BI_BUF_FLOOR_OF (buf, st);
631 floor = max (lim, floor);
632 /* No memrchr() ... */
633 bufptr = BI_BUF_BYTE_ADDRESS_BEFORE (buf, st);
634 floorptr = BI_BUF_BYTE_ADDRESS (buf, floor);
635 while (bufptr >= floorptr)
638 /* At this point, both ST and BUFPTR refer to the same
639 character. When the loop terminates, ST will
640 always point to the last character we tried. */
641 if (* (unsigned char *) bufptr == (unsigned char) target)
659 /* We found the character we were looking for; we have to return
660 the position *after* it due to the strange way that the return
662 INC_BYTIND (buf, st);
669 scan_buffer (struct buffer *buf, Emchar target, Bufpos start, Bufpos end,
670 EMACS_INT count, EMACS_INT *shortage, int allow_quit)
673 Bytind bi_start, bi_end;
675 bi_start = bufpos_to_bytind (buf, start);
677 bi_end = bufpos_to_bytind (buf, end);
680 bi_retval = bi_scan_buffer (buf, target, bi_start, bi_end, count,
681 shortage, allow_quit);
682 return bytind_to_bufpos (buf, bi_retval);
686 bi_find_next_newline_no_quit (struct buffer *buf, Bytind from, int count)
688 return bi_scan_buffer (buf, '\n', from, 0, count, 0, 0);
692 find_next_newline_no_quit (struct buffer *buf, Bufpos from, int count)
694 return scan_buffer (buf, '\n', from, 0, count, 0, 0);
698 find_next_newline (struct buffer *buf, Bufpos from, int count)
700 return scan_buffer (buf, '\n', from, 0, count, 0, 1);
704 bi_find_next_emchar_in_string (struct Lisp_String* str, Emchar target, Bytind st,
707 /* This function has been Mule-ized. */
708 Bytind lim = string_length (str) -1;
709 Bufbyte* s = string_data (str);
714 /* Due to the Mule representation of characters in a buffer,
715 we can simply search for characters in the range 0 - 127
716 directly. For other characters, we do it the "hard" way.
717 Note that this way works for all characters but the other
721 while (st < lim && count > 0)
723 if (string_char (str, st) == target)
725 INC_CHARBYTIND (s, st);
731 while (st < lim && count > 0)
733 Bufbyte *bufptr = (Bufbyte *) memchr (charptr_n_addr (s, st),
734 (int) target, lim - st);
738 st = (Bytind)(bufptr - s) + 1;
747 /* Like find_next_newline, but returns position before the newline,
748 not after, and only search up to TO. This isn't just
749 find_next_newline (...)-1, because you might hit TO. */
751 find_before_next_newline (struct buffer *buf, Bufpos from, Bufpos to, int count)
754 Bufpos pos = scan_buffer (buf, '\n', from, to, count, &shortage, 1);
763 skip_chars (struct buffer *buf, int forwardp, int syntaxp,
764 Lisp_Object string, Lisp_Object lim)
766 /* This function has been Mule-ized. */
767 REGISTER Bufbyte *p, *pend;
769 /* We store the first 256 chars in an array here and the rest in
771 unsigned char fastmap[0400];
774 struct Lisp_Char_Table *syntax_table =
775 XCHAR_TABLE (buf->mirror_syntax_table);
779 limit = forwardp ? BUF_ZV (buf) : BUF_BEGV (buf);
782 CHECK_INT_COERCE_MARKER (lim);
785 /* In any case, don't allow scan outside bounds of buffer. */
786 if (limit > BUF_ZV (buf)) limit = BUF_ZV (buf);
787 if (limit < BUF_BEGV (buf)) limit = BUF_BEGV (buf);
790 CHECK_STRING (string);
791 p = XSTRING_DATA (string);
792 pend = p + XSTRING_LENGTH (string);
793 memset (fastmap, 0, sizeof (fastmap));
795 Fclear_range_table (Vskip_chars_range_table);
797 if (p != pend && *p == '^')
803 /* Find the characters specified and set their elements of fastmap.
804 If syntaxp, each character counts as itself.
805 Otherwise, handle backslashes and ranges specially */
809 c = charptr_emchar (p);
813 if (c < 0400 && syntax_spec_code[c] < (unsigned char) Smax)
816 signal_simple_error ("Invalid syntax designator",
823 if (p == pend) break;
824 c = charptr_emchar (p);
827 if (p != pend && *p == '-')
832 if (p == pend) break;
833 cend = charptr_emchar (p);
834 while (c <= cend && c < 0400)
840 Fput_range_table (make_int (c), make_int (cend), Qt,
841 Vskip_chars_range_table);
849 Fput_range_table (make_int (c), make_int (c), Qt,
850 Vskip_chars_range_table);
855 if (syntaxp && fastmap['-'] != 0)
858 /* If ^ was the first character, complement the fastmap.
859 We don't complement the range table, however; we just use negate
860 in the comparisons below. */
863 for (i = 0; i < (int) (sizeof fastmap); i++)
867 Bufpos start_point = BUF_PT (buf);
871 /* All syntax designators are normal chars so nothing strange
875 while (BUF_PT (buf) < limit
876 && fastmap[(unsigned char)
878 [(int) SYNTAX (syntax_table,
880 (buf, BUF_PT (buf)))]])
881 BUF_SET_PT (buf, BUF_PT (buf) + 1);
885 while (BUF_PT (buf) > limit
886 && fastmap[(unsigned char)
888 [(int) SYNTAX (syntax_table,
890 (buf, BUF_PT (buf) - 1))]])
891 BUF_SET_PT (buf, BUF_PT (buf) - 1);
898 while (BUF_PT (buf) < limit)
900 Emchar ch = BUF_FETCH_CHAR (buf, BUF_PT (buf));
901 if ((ch < 0400) ? fastmap[ch] :
902 (NILP (Fget_range_table (make_int (ch),
903 Vskip_chars_range_table,
906 BUF_SET_PT (buf, BUF_PT (buf) + 1);
913 while (BUF_PT (buf) > limit)
915 Emchar ch = BUF_FETCH_CHAR (buf, BUF_PT (buf) - 1);
916 if ((ch < 0400) ? fastmap[ch] :
917 (NILP (Fget_range_table (make_int (ch),
918 Vskip_chars_range_table,
921 BUF_SET_PT (buf, BUF_PT (buf) - 1);
928 return make_int (BUF_PT (buf) - start_point);
932 DEFUN ("skip-chars-forward", Fskip_chars_forward, 1, 3, 0, /*
933 Move point forward, stopping before a char not in STRING, or at pos LIM.
934 STRING is like the inside of a `[...]' in a regular expression
935 except that `]' is never special and `\\' quotes `^', `-' or `\\'.
936 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
937 With arg "^a-zA-Z", skips nonletters stopping before first letter.
938 Returns the distance traveled, either zero or positive.
940 Optional argument BUFFER defaults to the current buffer.
942 (string, lim, buffer))
944 return skip_chars (decode_buffer (buffer, 0), 1, 0, string, lim);
947 DEFUN ("skip-chars-backward", Fskip_chars_backward, 1, 3, 0, /*
948 Move point backward, stopping after a char not in STRING, or at pos LIM.
949 See `skip-chars-forward' for details.
950 Returns the distance traveled, either zero or negative.
952 Optional argument BUFFER defaults to the current buffer.
954 (string, lim, buffer))
956 return skip_chars (decode_buffer (buffer, 0), 0, 0, string, lim);
960 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, 1, 3, 0, /*
961 Move point forward across chars in specified syntax classes.
962 SYNTAX is a string of syntax code characters.
963 Stop before a char whose syntax is not in SYNTAX, or at position LIM.
964 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
965 This function returns the distance traveled, either zero or positive.
967 Optional argument BUFFER defaults to the current buffer.
969 (syntax, lim, buffer))
971 return skip_chars (decode_buffer (buffer, 0), 1, 1, syntax, lim);
974 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, 1, 3, 0, /*
975 Move point backward across chars in specified syntax classes.
976 SYNTAX is a string of syntax code characters.
977 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.
978 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
979 This function returns the distance traveled, either zero or negative.
981 Optional argument BUFFER defaults to the current buffer.
983 (syntax, lim, buffer))
985 return skip_chars (decode_buffer (buffer, 0), 0, 1, syntax, lim);
989 /* Subroutines of Lisp buffer search functions. */
992 search_command (Lisp_Object string, Lisp_Object bound, Lisp_Object no_error,
993 Lisp_Object count, Lisp_Object buffer, int direction,
996 /* This function has been Mule-ized, except for the trt table handling. */
999 EMACS_INT n = direction;
1008 buf = decode_buffer (buffer, 0);
1009 CHECK_STRING (string);
1011 lim = n > 0 ? BUF_ZV (buf) : BUF_BEGV (buf);
1014 CHECK_INT_COERCE_MARKER (bound);
1016 if (n > 0 ? lim < BUF_PT (buf) : lim > BUF_PT (buf))
1017 error ("Invalid search bound (wrong side of point)");
1018 if (lim > BUF_ZV (buf))
1020 if (lim < BUF_BEGV (buf))
1021 lim = BUF_BEGV (buf);
1024 np = search_buffer (buf, string, BUF_PT (buf), lim, n, RE,
1025 (!NILP (buf->case_fold_search)
1026 ? MIRROR_CANON_TABLE_AS_STRING (buf)
1028 (!NILP (buf->case_fold_search)
1029 ? MIRROR_EQV_TABLE_AS_STRING (buf)
1034 if (NILP (no_error))
1035 return signal_failure (string);
1036 if (!EQ (no_error, Qt))
1038 if (lim < BUF_BEGV (buf) || lim > BUF_ZV (buf))
1040 BUF_SET_PT (buf, lim);
1042 #if 0 /* This would be clean, but maybe programs depend on
1043 a value of nil here. */
1051 if (np < BUF_BEGV (buf) || np > BUF_ZV (buf))
1054 BUF_SET_PT (buf, np);
1056 return make_int (np);
1060 trivial_regexp_p (Lisp_Object regexp)
1062 /* This function has been Mule-ized. */
1063 Bytecount len = XSTRING_LENGTH (regexp);
1064 Bufbyte *s = XSTRING_DATA (regexp);
1069 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
1076 case '|': case '(': case ')': case '`': case '\'': case 'b':
1077 case 'B': case '<': case '>': case 'w': case 'W': case 's':
1080 /* 97/2/25 jhod Added for category matches */
1083 case '1': case '2': case '3': case '4': case '5':
1084 case '6': case '7': case '8': case '9':
1092 /* Search for the n'th occurrence of STRING in BUF,
1093 starting at position BUFPOS and stopping at position BUFLIM,
1094 treating PAT as a literal string if RE is false or as
1095 a regular expression if RE is true.
1097 If N is positive, searching is forward and BUFLIM must be greater
1099 If N is negative, searching is backward and BUFLIM must be less
1102 Returns -x if only N-x occurrences found (x > 0),
1103 or else the position at the beginning of the Nth occurrence
1104 (if searching backward) or the end (if searching forward).
1106 POSIX is nonzero if we want full backtracking (POSIX style)
1107 for this pattern. 0 means backtrack only enough to get a valid match. */
1110 search_buffer (struct buffer *buf, Lisp_Object string, Bufpos bufpos,
1111 Bufpos buflim, EMACS_INT n, int RE, unsigned char *trt,
1112 unsigned char *inverse_trt, int posix)
1114 /* This function has been Mule-ized, except for the trt table handling. */
1115 Bytecount len = XSTRING_LENGTH (string);
1116 Bufbyte *base_pat = XSTRING_DATA (string);
1117 REGISTER EMACS_INT *BM_tab;
1118 EMACS_INT *BM_tab_base;
1119 REGISTER int direction = ((n > 0) ? 1 : -1);
1120 REGISTER Bytecount dirlen;
1124 Bytecount stride_for_teases = 0;
1125 REGISTER Bufbyte *pat = 0;
1126 REGISTER Bufbyte *cursor, *p_limit, *ptr2;
1127 REGISTER EMACS_INT i, j;
1132 if (running_asynch_code)
1133 save_search_regs ();
1135 /* Null string is found at starting position. */
1138 set_search_regs (buf, bufpos, 0);
1142 /* Searching 0 times means don't move. */
1146 pos = bufpos_to_bytind (buf, bufpos);
1147 lim = bufpos_to_bytind (buf, buflim);
1148 if (RE && !trivial_regexp_p (string))
1150 struct re_pattern_buffer *bufp;
1152 bufp = compile_pattern (string, &search_regs, (char *) trt, posix,
1155 /* Get pointers and sizes of the two strings
1156 that make up the visible portion of the buffer. */
1158 p1 = BI_BUF_BEGV (buf);
1159 p2 = BI_BUF_CEILING_OF (buf, p1);
1161 s2 = BI_BUF_ZV (buf) - p2;
1167 regex_emacs_buffer = buf;
1168 val = re_search_2 (bufp,
1169 (char *) BI_BUF_BYTE_ADDRESS (buf, p1), s1,
1170 (char *) BI_BUF_BYTE_ADDRESS (buf, p2), s2,
1171 pos - BI_BUF_BEGV (buf), lim - pos, &search_regs,
1172 pos - BI_BUF_BEGV (buf));
1176 matcher_overflow ();
1180 int num_regs = search_regs.num_regs;
1181 j = BI_BUF_BEGV (buf);
1182 for (i = 0; i < num_regs; i++)
1183 if (search_regs.start[i] >= 0)
1185 search_regs.start[i] += j;
1186 search_regs.end[i] += j;
1188 XSETBUFFER (last_thing_searched, buf);
1189 /* Set pos to the new position. */
1190 pos = search_regs.start[0];
1191 fixup_search_regs_for_buffer (buf);
1192 /* And bufpos too. */
1193 bufpos = search_regs.start[0];
1205 regex_emacs_buffer = buf;
1206 val = re_search_2 (bufp,
1207 (char *) BI_BUF_BYTE_ADDRESS (buf, p1), s1,
1208 (char *) BI_BUF_BYTE_ADDRESS (buf, p2), s2,
1209 pos - BI_BUF_BEGV (buf), lim - pos, &search_regs,
1210 lim - BI_BUF_BEGV (buf));
1213 matcher_overflow ();
1217 int num_regs = search_regs.num_regs;
1218 j = BI_BUF_BEGV (buf);
1219 for (i = 0; i < num_regs; i++)
1220 if (search_regs.start[i] >= 0)
1222 search_regs.start[i] += j;
1223 search_regs.end[i] += j;
1225 XSETBUFFER (last_thing_searched, buf);
1226 /* Set pos to the new position. */
1227 pos = search_regs.end[0];
1228 fixup_search_regs_for_buffer (buf);
1229 /* And bufpos too. */
1230 bufpos = search_regs.end[0];
1240 else /* non-RE case */
1241 /* #### Someone really really really needs to comment the workings
1242 of this junk somewhat better.
1244 BTW "BM" stands for Boyer-Moore, which is one of the standard
1245 string-searching algorithms. It's the best string-searching
1246 algorithm out there provided
1248 a) You're not fazed by algorithm complexity. (Rabin-Karp, which
1249 uses hashing, is much much easier to code but not as fast.)
1250 b) You can freely move backwards in the string that you're
1253 As the comment below tries to explain (but garbles in typical
1254 programmer-ese), the idea is that you don't have to do a
1255 string match at every successive position in the text. For
1256 example, let's say the pattern is "a very long string". We
1257 compare the last character in the string (`g') with the
1258 corresponding character in the text. If it mismatches, and
1259 it is, say, `z', then we can skip forward by the entire
1260 length of the pattern because `z' does not occur anywhere
1261 in the pattern. If the mismatching character does occur
1262 in the pattern, we can usually still skip forward by more
1263 than one: e.g. if it is `l', then we can skip forward
1264 by the length of the substring "ong string" -- i.e. the
1265 largest end section of the pattern that does not contain
1266 the mismatched character. So what we do is compute, for
1267 each possible character, the distance we can skip forward
1268 (the "stride") and use it in the string matching. This
1269 is what the BM_tab holds. */
1272 EMACS_INT BM_tab_space[0400];
1273 BM_tab = &BM_tab_space[0];
1275 BM_tab = alloca_array (EMACS_INT, 256);
1278 Bufbyte *patbuf = alloca_array (Bufbyte, len);
1282 /* If we got here and the RE flag is set, it's because we're
1283 dealing with a regexp known to be trivial, so the backslash
1284 just quotes the next character. */
1285 if (RE && *base_pat == '\\')
1290 *pat++ = (trt ? trt[*base_pat++] : *base_pat++);
1293 pat = base_pat = patbuf;
1295 /* The general approach is that we are going to maintain that we know */
1296 /* the first (closest to the present position, in whatever direction */
1297 /* we're searching) character that could possibly be the last */
1298 /* (furthest from present position) character of a valid match. We */
1299 /* advance the state of our knowledge by looking at that character */
1300 /* and seeing whether it indeed matches the last character of the */
1301 /* pattern. If it does, we take a closer look. If it does not, we */
1302 /* move our pointer (to putative last characters) as far as is */
1303 /* logically possible. This amount of movement, which I call a */
1304 /* stride, will be the length of the pattern if the actual character */
1305 /* appears nowhere in the pattern, otherwise it will be the distance */
1306 /* from the last occurrence of that character to the end of the */
1308 /* As a coding trick, an enormous stride is coded into the table for */
1309 /* characters that match the last character. This allows use of only */
1310 /* a single test, a test for having gone past the end of the */
1311 /* permissible match region, to test for both possible matches (when */
1312 /* the stride goes past the end immediately) and failure to */
1313 /* match (where you get nudged past the end one stride at a time). */
1315 /* Here we make a "mickey mouse" BM table. The stride of the search */
1316 /* is determined only by the last character of the putative match. */
1317 /* If that character does not match, we will stride the proper */
1318 /* distance to propose a match that superimposes it on the last */
1319 /* instance of a character that matches it (per trt), or misses */
1320 /* it entirely if there is none. */
1322 dirlen = len * direction;
1323 infinity = dirlen - (lim + pos + len + len) * direction;
1325 pat = (base_pat += len - 1);
1326 BM_tab_base = BM_tab;
1328 j = dirlen; /* to get it in a register */
1329 /* A character that does not appear in the pattern induces a */
1330 /* stride equal to the pattern length. */
1331 while (BM_tab_base != BM_tab)
1339 while (i != infinity)
1341 j = pat[i]; i += direction;
1342 if (i == dirlen) i = infinity;
1347 stride_for_teases = BM_tab[j];
1348 BM_tab[j] = dirlen - i;
1349 /* A translation table is accompanied by its inverse -- see */
1350 /* comment following downcase_table for details */
1352 while ((j = inverse_trt[j]) != k)
1353 BM_tab[j] = dirlen - i;
1358 stride_for_teases = BM_tab[j];
1359 BM_tab[j] = dirlen - i;
1361 /* stride_for_teases tells how much to stride if we get a */
1362 /* match on the far character but are subsequently */
1363 /* disappointed, by recording what the stride would have been */
1364 /* for that character if the last character had been */
1367 infinity = dirlen - infinity;
1368 pos += dirlen - ((direction > 0) ? direction : 0);
1369 /* loop invariant - pos points at where last char (first char if reverse)
1370 of pattern would align in a possible match. */
1373 /* It's been reported that some (broken) compiler thinks that
1374 Boolean expressions in an arithmetic context are unsigned.
1375 Using an explicit ?1:0 prevents this. */
1376 if ((lim - pos - ((direction > 0) ? 1 : 0)) * direction < 0)
1377 return n * (0 - direction);
1378 /* First we do the part we can by pointers (maybe nothing) */
1381 limit = pos - dirlen + direction;
1382 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF
1383 have changed. See buffer.h. */
1384 limit = ((direction > 0)
1385 ? BI_BUF_CEILING_OF (buf, limit) - 1
1386 : BI_BUF_FLOOR_OF (buf, limit + 1));
1387 /* LIMIT is now the last (not beyond-last!) value
1388 POS can take on without hitting edge of buffer or the gap. */
1389 limit = ((direction > 0)
1390 ? min (lim - 1, min (limit, pos + 20000))
1391 : max (lim, max (limit, pos - 20000)));
1392 if ((limit - pos) * direction > 20)
1394 p_limit = BI_BUF_BYTE_ADDRESS (buf, limit);
1395 ptr2 = (cursor = BI_BUF_BYTE_ADDRESS (buf, pos));
1396 /* In this loop, pos + cursor - ptr2 is the surrogate for pos */
1397 while (1) /* use one cursor setting as long as i can */
1399 if (direction > 0) /* worth duplicating */
1401 /* Use signed comparison if appropriate
1402 to make cursor+infinity sure to be > p_limit.
1403 Assuming that the buffer lies in a range of addresses
1404 that are all "positive" (as ints) or all "negative",
1405 either kind of comparison will work as long
1406 as we don't step by infinity. So pick the kind
1407 that works when we do step by infinity. */
1408 if ((EMACS_INT) (p_limit + infinity) >
1409 (EMACS_INT) p_limit)
1410 while ((EMACS_INT) cursor <=
1411 (EMACS_INT) p_limit)
1412 cursor += BM_tab[*cursor];
1414 while ((EMACS_UINT) cursor <=
1415 (EMACS_UINT) p_limit)
1416 cursor += BM_tab[*cursor];
1420 if ((EMACS_INT) (p_limit + infinity) <
1421 (EMACS_INT) p_limit)
1422 while ((EMACS_INT) cursor >=
1423 (EMACS_INT) p_limit)
1424 cursor += BM_tab[*cursor];
1426 while ((EMACS_UINT) cursor >=
1427 (EMACS_UINT) p_limit)
1428 cursor += BM_tab[*cursor];
1430 /* If you are here, cursor is beyond the end of the searched region. */
1431 /* This can happen if you match on the far character of the pattern, */
1432 /* because the "stride" of that character is infinity, a number able */
1433 /* to throw you well beyond the end of the search. It can also */
1434 /* happen if you fail to match within the permitted region and would */
1435 /* otherwise try a character beyond that region */
1436 if ((cursor - p_limit) * direction <= len)
1437 break; /* a small overrun is genuine */
1438 cursor -= infinity; /* large overrun = hit */
1439 i = dirlen - direction;
1442 while ((i -= direction) + direction != 0)
1443 if (pat[i] != trt[*(cursor -= direction)])
1448 while ((i -= direction) + direction != 0)
1449 if (pat[i] != *(cursor -= direction))
1452 cursor += dirlen - i - direction; /* fix cursor */
1453 if (i + direction == 0)
1455 cursor -= direction;
1458 Bytind bytstart = (pos + cursor - ptr2 +
1461 Bufpos bufstart = bytind_to_bufpos (buf, bytstart);
1462 Bufpos bufend = bytind_to_bufpos (buf, bytstart + len);
1464 set_search_regs (buf, bufstart, bufend - bufstart);
1467 if ((n -= direction) != 0)
1468 cursor += dirlen; /* to resume search */
1470 return ((direction > 0)
1471 ? search_regs.end[0] : search_regs.start[0]);
1474 cursor += stride_for_teases; /* <sigh> we lose - */
1476 pos += cursor - ptr2;
1479 /* Now we'll pick up a clump that has to be done the hard */
1480 /* way because it covers a discontinuity */
1482 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF
1483 have changed. See buffer.h. */
1484 limit = ((direction > 0)
1485 ? BI_BUF_CEILING_OF (buf, pos - dirlen + 1) - 1
1486 : BI_BUF_FLOOR_OF (buf, pos - dirlen));
1487 limit = ((direction > 0)
1488 ? min (limit + len, lim - 1)
1489 : max (limit - len, lim));
1490 /* LIMIT is now the last value POS can have
1491 and still be valid for a possible match. */
1494 /* This loop can be coded for space rather than */
1495 /* speed because it will usually run only once. */
1496 /* (the reach is at most len + 21, and typically */
1497 /* does not exceed len) */
1498 while ((limit - pos) * direction >= 0)
1499 /* *not* BI_BUF_FETCH_CHAR. We are working here
1500 with bytes, not characters. */
1501 pos += BM_tab[*BI_BUF_BYTE_ADDRESS (buf, pos)];
1502 /* now run the same tests to distinguish going off the */
1503 /* end, a match or a phony match. */
1504 if ((pos - limit) * direction <= len)
1505 break; /* ran off the end */
1506 /* Found what might be a match.
1507 Set POS back to last (first if reverse) char pos. */
1509 i = dirlen - direction;
1510 while ((i -= direction) + direction != 0)
1513 if (pat[i] != (((Bufbyte *) trt)
1514 /* #### Does not handle TRT right */
1515 ? trt[*BI_BUF_BYTE_ADDRESS (buf, pos)]
1516 : *BI_BUF_BYTE_ADDRESS (buf, pos)))
1519 /* Above loop has moved POS part or all the way
1520 back to the first char pos (last char pos if reverse).
1521 Set it once again at the last (first if reverse) char. */
1522 pos += dirlen - i- direction;
1523 if (i + direction == 0)
1528 Bytind bytstart = (pos +
1531 Bufpos bufstart = bytind_to_bufpos (buf, bytstart);
1532 Bufpos bufend = bytind_to_bufpos (buf, bytstart + len);
1534 set_search_regs (buf, bufstart, bufend - bufstart);
1537 if ((n -= direction) != 0)
1538 pos += dirlen; /* to resume search */
1540 return ((direction > 0)
1541 ? search_regs.end[0] : search_regs.start[0]);
1544 pos += stride_for_teases;
1547 /* We have done one clump. Can we continue? */
1548 if ((lim - pos) * direction < 0)
1549 return (0 - n) * direction;
1551 return bytind_to_bufpos (buf, pos);
1555 /* Record beginning BEG and end BEG + LEN
1556 for a match just found in the current buffer. */
1559 set_search_regs (struct buffer *buf, Bufpos beg, Charcount len)
1561 /* This function has been Mule-ized. */
1562 /* Make sure we have registers in which to store
1563 the match position. */
1564 if (search_regs.num_regs == 0)
1566 search_regs.start = xnew (regoff_t);
1567 search_regs.end = xnew (regoff_t);
1568 search_regs.num_regs = 1;
1571 search_regs.start[0] = beg;
1572 search_regs.end[0] = beg + len;
1573 XSETBUFFER (last_thing_searched, buf);
1577 /* Given a string of words separated by word delimiters,
1578 compute a regexp that matches those exact words
1579 separated by arbitrary punctuation. */
1582 wordify (Lisp_Object buffer, Lisp_Object string)
1585 EMACS_INT punct_count = 0, word_count = 0;
1586 struct buffer *buf = decode_buffer (buffer, 0);
1587 struct Lisp_Char_Table *syntax_table =
1588 XCHAR_TABLE (buf->mirror_syntax_table);
1590 CHECK_STRING (string);
1591 len = XSTRING_CHAR_LENGTH (string);
1593 for (i = 0; i < len; i++)
1594 if (!WORD_SYNTAX_P (syntax_table, string_char (XSTRING (string), i)))
1597 if (i > 0 && WORD_SYNTAX_P (syntax_table,
1598 string_char (XSTRING (string), i - 1)))
1601 if (WORD_SYNTAX_P (syntax_table, string_char (XSTRING (string), len - 1)))
1603 if (!word_count) return build_string ("");
1606 /* The following value is an upper bound on the amount of storage we
1607 need. In non-Mule, it is exact. */
1609 (Bufbyte *) alloca (XSTRING_LENGTH (string) - punct_count +
1610 5 * (word_count - 1) + 4);
1611 Bufbyte *o = storage;
1616 for (i = 0; i < len; i++)
1618 Emchar ch = string_char (XSTRING (string), i);
1620 if (WORD_SYNTAX_P (syntax_table, ch))
1621 o += set_charptr_emchar (o, ch);
1623 && WORD_SYNTAX_P (syntax_table,
1624 string_char (XSTRING (string), i - 1))
1638 return make_string (storage, o - storage);
1642 DEFUN ("search-backward", Fsearch_backward, 1, 5, "sSearch backward: ", /*
1643 Search backward from point for STRING.
1644 Set point to the beginning of the occurrence found, and return point.
1645 An optional second argument bounds the search; it is a buffer position.
1646 The match found must not extend before that position.
1647 Optional third argument, if t, means if fail just return nil (no error).
1648 If not nil and not t, position at limit of search and return nil.
1649 Optional fourth argument is repeat count--search for successive occurrences.
1650 Optional fifth argument BUFFER specifies the buffer to search in and
1651 defaults to the current buffer.
1652 See also the functions `match-beginning', `match-end' and `replace-match'.
1654 (string, bound, no_error, count, buffer))
1656 return search_command (string, bound, no_error, count, buffer, -1, 0, 0);
1659 DEFUN ("search-forward", Fsearch_forward, 1, 5, "sSearch: ", /*
1660 Search forward from point for STRING.
1661 Set point to the end of the occurrence found, and return point.
1662 An optional second argument bounds the search; it is a buffer position.
1663 The match found must not extend after that position. nil is equivalent
1665 Optional third argument, if t, means if fail just return nil (no error).
1666 If not nil and not t, move to limit of search and return nil.
1667 Optional fourth argument is repeat count--search for successive occurrences.
1668 Optional fifth argument BUFFER specifies the buffer to search in and
1669 defaults to the current buffer.
1670 See also the functions `match-beginning', `match-end' and `replace-match'.
1672 (string, bound, no_error, count, buffer))
1674 return search_command (string, bound, no_error, count, buffer, 1, 0, 0);
1677 DEFUN ("word-search-backward", Fword_search_backward, 1, 5,
1678 "sWord search backward: ", /*
1679 Search backward from point for STRING, ignoring differences in punctuation.
1680 Set point to the beginning of the occurrence found, and return point.
1681 An optional second argument bounds the search; it is a buffer position.
1682 The match found must not extend before that position.
1683 Optional third argument, if t, means if fail just return nil (no error).
1684 If not nil and not t, move to limit of search and return nil.
1685 Optional fourth argument is repeat count--search for successive occurrences.
1686 Optional fifth argument BUFFER specifies the buffer to search in and
1687 defaults to the current buffer.
1689 (string, bound, no_error, count, buffer))
1691 return search_command (wordify (buffer, string), bound, no_error, count,
1695 DEFUN ("word-search-forward", Fword_search_forward, 1, 5, "sWord search: ", /*
1696 Search forward from point for STRING, ignoring differences in punctuation.
1697 Set point to the end of the occurrence found, and return point.
1698 An optional second argument bounds the search; it is a buffer position.
1699 The match found must not extend after that position.
1700 Optional third argument, if t, means if fail just return nil (no error).
1701 If not nil and not t, move to limit of search and return nil.
1702 Optional fourth argument is repeat count--search for successive occurrences.
1703 Optional fifth argument BUFFER specifies the buffer to search in and
1704 defaults to the current buffer.
1706 (string, bound, no_error, count, buffer))
1708 return search_command (wordify (buffer, string), bound, no_error, count,
1712 DEFUN ("re-search-backward", Fre_search_backward, 1, 5,
1713 "sRE search backward: ", /*
1714 Search backward from point for match for regular expression REGEXP.
1715 Set point to the beginning of the match, and return point.
1716 The match found is the one starting last in the buffer
1717 and yet ending before the origin of the search.
1718 An optional second argument bounds the search; it is a buffer position.
1719 The match found must start at or after that position.
1720 Optional third argument, if t, means if fail just return nil (no error).
1721 If not nil and not t, move to limit of search and return nil.
1722 Optional fourth argument is repeat count--search for successive occurrences.
1723 Optional fifth argument BUFFER specifies the buffer to search in and
1724 defaults to the current buffer.
1725 See also the functions `match-beginning', `match-end' and `replace-match'.
1727 (regexp, bound, no_error, count, buffer))
1729 return search_command (regexp, bound, no_error, count, buffer, -1, 1, 0);
1732 DEFUN ("re-search-forward", Fre_search_forward, 1, 5, "sRE search: ", /*
1733 Search forward from point for regular expression REGEXP.
1734 Set point to the end of the occurrence found, and return point.
1735 An optional second argument bounds the search; it is a buffer position.
1736 The match found must not extend after that position.
1737 Optional third argument, if t, means if fail just return nil (no error).
1738 If not nil and not t, move to limit of search and return nil.
1739 Optional fourth argument is repeat count--search for successive occurrences.
1740 Optional fifth argument BUFFER specifies the buffer to search in and
1741 defaults to the current buffer.
1742 See also the functions `match-beginning', `match-end' and `replace-match'.
1744 (regexp, bound, no_error, count, buffer))
1746 return search_command (regexp, bound, no_error, count, buffer, 1, 1, 0);
1749 DEFUN ("posix-search-backward", Fposix_search_backward, 1, 5,
1750 "sPosix search backward: ", /*
1751 Search backward from point for match for regular expression REGEXP.
1752 Find the longest match in accord with Posix regular expression rules.
1753 Set point to the beginning of the match, and return point.
1754 The match found is the one starting last in the buffer
1755 and yet ending before the origin of the search.
1756 An optional second argument bounds the search; it is a buffer position.
1757 The match found must start at or after that position.
1758 Optional third argument, if t, means if fail just return nil (no error).
1759 If not nil and not t, move to limit of search and return nil.
1760 Optional fourth argument is repeat count--search for successive occurrences.
1761 Optional fifth argument BUFFER specifies the buffer to search in and
1762 defaults to the current buffer.
1763 See also the functions `match-beginning', `match-end' and `replace-match'.
1765 (regexp, bound, no_error, count, buffer))
1767 return search_command (regexp, bound, no_error, count, buffer, -1, 1, 1);
1770 DEFUN ("posix-search-forward", Fposix_search_forward, 1, 5, "sPosix search: ", /*
1771 Search forward from point for regular expression REGEXP.
1772 Find the longest match in accord with Posix regular expression rules.
1773 Set point to the end of the occurrence found, and return point.
1774 An optional second argument bounds the search; it is a buffer position.
1775 The match found must not extend after that position.
1776 Optional third argument, if t, means if fail just return nil (no error).
1777 If not nil and not t, move to limit of search and return nil.
1778 Optional fourth argument is repeat count--search for successive occurrences.
1779 Optional fifth argument BUFFER specifies the buffer to search in and
1780 defaults to the current buffer.
1781 See also the functions `match-beginning', `match-end' and `replace-match'.
1783 (regexp, bound, no_error, count, buffer))
1785 return search_command (regexp, bound, no_error, count, buffer, 1, 1, 1);
1790 free_created_dynarrs (Lisp_Object cons)
1792 Dynarr_free (get_opaque_ptr (XCAR (cons)));
1793 Dynarr_free (get_opaque_ptr (XCDR (cons)));
1794 free_opaque_ptr (XCAR (cons));
1795 free_opaque_ptr (XCDR (cons));
1796 free_cons (XCONS (cons));
1800 DEFUN ("replace-match", Freplace_match, 1, 5, 0, /*
1801 Replace text matched by last search with NEWTEXT.
1802 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
1803 Otherwise maybe capitalize the whole text, or maybe just word initials,
1804 based on the replaced text.
1805 If the replaced text has only capital letters
1806 and has at least one multiletter word, convert NEWTEXT to all caps.
1807 If the replaced text has at least one word starting with a capital letter,
1808 then capitalize each word in NEWTEXT.
1810 If third arg LITERAL is non-nil, insert NEWTEXT literally.
1811 Otherwise treat `\\' as special:
1812 `\\&' in NEWTEXT means substitute original matched text.
1813 `\\N' means substitute what matched the Nth `\\(...\\)'.
1814 If Nth parens didn't match, substitute nothing.
1815 `\\\\' means insert one `\\'.
1816 `\\u' means upcase the next character.
1817 `\\l' means downcase the next character.
1818 `\\U' means begin upcasing all following characters.
1819 `\\L' means begin downcasing all following characters.
1820 `\\E' means terminate the effect of any `\\U' or `\\L'.
1821 Case changes made with `\\u', `\\l', `\\U', and `\\L' override
1822 all other case changes that may be made in the replaced text.
1823 FIXEDCASE and LITERAL are optional arguments.
1824 Leaves point at end of replacement text.
1826 The optional fourth argument STRING can be a string to modify.
1827 In that case, this function creates and returns a new string
1828 which is made by replacing the part of STRING that was matched.
1829 When fourth argument is a string, fifth argument STRBUFFER specifies
1830 the buffer to be used for syntax-table and case-table lookup and
1831 defaults to the current buffer. (When fourth argument is not a string,
1832 the buffer that the match occurred in has automatically been remembered
1833 and you do not need to specify it.)
1835 (newtext, fixedcase, literal, string, strbuffer))
1837 /* This function has been Mule-ized. */
1838 /* This function can GC */
1839 enum { nochange, all_caps, cap_initial } case_action;
1841 int some_multiletter_word;
1844 int some_nonuppercase_initial;
1848 struct Lisp_Char_Table *syntax_table;
1851 int_dynarr *ul_action_dynarr = 0;
1852 int_dynarr *ul_pos_dynarr = 0;
1855 CHECK_STRING (newtext);
1857 if (! NILP (string))
1859 CHECK_STRING (string);
1860 if (!EQ (last_thing_searched, Qt))
1861 error ("last thing matched was not a string");
1862 /* If the match data
1863 were abstracted into a special "match data" type instead
1864 of the typical half-assed "let the implementation be
1865 visible" form it's in, we could extend it to include
1866 the last string matched and the buffer used for that
1867 matching. But of course we can't change it as it is. */
1868 buf = decode_buffer (strbuffer, 0);
1869 XSETBUFFER (buffer, buf);
1873 if (!BUFFERP (last_thing_searched))
1874 error ("last thing matched was not a buffer");
1875 buffer = last_thing_searched;
1876 buf = XBUFFER (buffer);
1879 syntax_table = XCHAR_TABLE (buf->mirror_syntax_table);
1881 case_action = nochange; /* We tried an initialization */
1882 /* but some C compilers blew it */
1884 if (search_regs.num_regs == 0)
1885 error ("replace-match called before any match found");
1889 if (search_regs.start[0] < BUF_BEGV (buf)
1890 || search_regs.start[0] > search_regs.end[0]
1891 || search_regs.end[0] > BUF_ZV (buf))
1892 args_out_of_range (make_int (search_regs.start[0]),
1893 make_int (search_regs.end[0]));
1897 if (search_regs.start[0] < 0
1898 || search_regs.start[0] > search_regs.end[0]
1899 || search_regs.end[0] > XSTRING_CHAR_LENGTH (string))
1900 args_out_of_range (make_int (search_regs.start[0]),
1901 make_int (search_regs.end[0]));
1904 if (NILP (fixedcase))
1906 /* Decide how to casify by examining the matched text. */
1908 last = search_regs.end[0];
1910 case_action = all_caps;
1912 /* some_multiletter_word is set nonzero if any original word
1913 is more than one letter long. */
1914 some_multiletter_word = 0;
1916 some_nonuppercase_initial = 0;
1919 for (pos = search_regs.start[0]; pos < last; pos++)
1922 c = BUF_FETCH_CHAR (buf, pos);
1924 c = string_char (XSTRING (string), pos);
1926 if (LOWERCASEP (buf, c))
1928 /* Cannot be all caps if any original char is lower case */
1931 if (!WORD_SYNTAX_P (syntax_table, prevc))
1932 some_nonuppercase_initial = 1;
1934 some_multiletter_word = 1;
1936 else if (!NOCASEP (buf, c))
1939 if (!WORD_SYNTAX_P (syntax_table, prevc))
1942 some_multiletter_word = 1;
1946 /* If the initial is a caseless word constituent,
1947 treat that like a lowercase initial. */
1948 if (!WORD_SYNTAX_P (syntax_table, prevc))
1949 some_nonuppercase_initial = 1;
1955 /* Convert to all caps if the old text is all caps
1956 and has at least one multiletter word. */
1957 if (! some_lowercase && some_multiletter_word)
1958 case_action = all_caps;
1959 /* Capitalize each word, if the old text has all capitalized words. */
1960 else if (!some_nonuppercase_initial && some_multiletter_word)
1961 case_action = cap_initial;
1962 else if (!some_nonuppercase_initial && some_uppercase)
1963 /* Should x -> yz, operating on X, give Yz or YZ?
1964 We'll assume the latter. */
1965 case_action = all_caps;
1967 case_action = nochange;
1970 /* Do replacement in a string. */
1973 Lisp_Object before, after;
1975 speccount = specpdl_depth ();
1976 before = Fsubstring (string, Qzero, make_int (search_regs.start[0]));
1977 after = Fsubstring (string, make_int (search_regs.end[0]), Qnil);
1979 /* Do case substitution into NEWTEXT if desired. */
1982 Charcount stlen = XSTRING_CHAR_LENGTH (newtext);
1984 /* XEmacs change: rewrote this loop somewhat to make it
1985 cleaner. Also added \U, \E, etc. */
1986 Charcount literal_start = 0;
1987 /* We build up the substituted string in ACCUM. */
1992 /* OK, the basic idea here is that we scan through the
1993 replacement string until we find a backslash, which
1994 represents a substring of the original string to be
1995 substituted. We then append onto ACCUM the literal
1996 text before the backslash (LASTPOS marks the
1997 beginning of this) followed by the substring of the
1998 original string that needs to be inserted. */
1999 for (strpos = 0; strpos < stlen; strpos++)
2001 /* If LITERAL_END is set, we've encountered a backslash
2002 (the end of literal text to be inserted). */
2003 Charcount literal_end = -1;
2004 /* If SUBSTART is set, we need to also insert the
2005 text from SUBSTART to SUBEND in the original string. */
2006 Charcount substart = -1;
2007 Charcount subend = -1;
2009 c = string_char (XSTRING (newtext), strpos);
2010 if (c == '\\' && strpos < stlen - 1)
2012 c = string_char (XSTRING (newtext), ++strpos);
2015 literal_end = strpos - 1;
2016 substart = search_regs.start[0];
2017 subend = search_regs.end[0];
2019 else if (c >= '1' && c <= '9' &&
2020 c <= search_regs.num_regs + '0')
2022 if (search_regs.start[c - '0'] >= 0)
2024 literal_end = strpos - 1;
2025 substart = search_regs.start[c - '0'];
2026 subend = search_regs.end[c - '0'];
2029 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' ||
2032 /* Keep track of all case changes requested, but don't
2033 make them now. Do them later so we override
2037 ul_pos_dynarr = Dynarr_new (int);
2038 ul_action_dynarr = Dynarr_new (int);
2039 record_unwind_protect
2040 (free_created_dynarrs,
2042 (make_opaque_ptr (ul_pos_dynarr),
2043 make_opaque_ptr (ul_action_dynarr)));
2045 literal_end = strpos - 1;
2046 Dynarr_add (ul_pos_dynarr,
2048 ? XSTRING_CHAR_LENGTH (accum)
2049 : 0) + (literal_end - literal_start));
2050 Dynarr_add (ul_action_dynarr, c);
2053 /* So we get just one backslash. */
2054 literal_end = strpos;
2056 if (literal_end >= 0)
2058 Lisp_Object literal_text = Qnil;
2059 Lisp_Object substring = Qnil;
2060 if (literal_end != literal_start)
2061 literal_text = Fsubstring (newtext,
2062 make_int (literal_start),
2063 make_int (literal_end));
2064 if (substart >= 0 && subend != substart)
2065 substring = Fsubstring (string,
2066 make_int (substart),
2068 if (!NILP (literal_text) || !NILP (substring))
2069 accum = concat3 (accum, literal_text, substring);
2070 literal_start = strpos + 1;
2074 if (strpos != literal_start)
2075 /* some literal text at end to be inserted */
2076 newtext = concat2 (accum, Fsubstring (newtext,
2077 make_int (literal_start),
2078 make_int (strpos)));
2083 if (case_action == all_caps)
2084 newtext = Fupcase (newtext, buffer);
2085 else if (case_action == cap_initial)
2086 newtext = Fupcase_initials (newtext, buffer);
2088 /* Now finally, we need to process the \U's, \E's, etc. */
2092 int cur_action = 'E';
2093 Charcount stlen = XSTRING_CHAR_LENGTH (newtext);
2096 for (strpos = 0; strpos < stlen; strpos++)
2098 Emchar curchar = string_char (XSTRING (newtext), strpos);
2099 Emchar newchar = -1;
2100 if (i < Dynarr_length (ul_pos_dynarr) &&
2101 strpos == Dynarr_at (ul_pos_dynarr, i))
2103 int new_action = Dynarr_at (ul_action_dynarr, i);
2105 if (new_action == 'u')
2106 newchar = UPCASE (buf, curchar);
2107 else if (new_action == 'l')
2108 newchar = DOWNCASE (buf, curchar);
2110 cur_action = new_action;
2114 if (cur_action == 'U')
2115 newchar = UPCASE (buf, curchar);
2116 else if (cur_action == 'L')
2117 newchar = DOWNCASE (buf, curchar);
2121 if (newchar != curchar)
2122 set_string_char (XSTRING (newtext), strpos, newchar);
2126 /* frees the Dynarrs if necessary. */
2127 unbind_to (speccount, Qnil);
2128 return concat3 (before, newtext, after);
2131 mc_count = begin_multiple_change (buf, search_regs.start[0],
2132 search_regs.end[0]);
2134 /* begin_multiple_change() records an unwind-protect, so we need to
2135 record this value now. */
2136 speccount = specpdl_depth ();
2138 /* We insert the replacement text before the old text, and then
2139 delete the original text. This means that markers at the
2140 beginning or end of the original will float to the corresponding
2141 position in the replacement. */
2142 BUF_SET_PT (buf, search_regs.start[0]);
2143 if (!NILP (literal))
2144 Finsert (1, &newtext);
2147 Charcount stlen = XSTRING_CHAR_LENGTH (newtext);
2149 struct gcpro gcpro1;
2151 for (strpos = 0; strpos < stlen; strpos++)
2153 Charcount offset = BUF_PT (buf) - search_regs.start[0];
2155 c = string_char (XSTRING (newtext), strpos);
2156 if (c == '\\' && strpos < stlen - 1)
2158 c = string_char (XSTRING (newtext), ++strpos);
2160 Finsert_buffer_substring
2162 make_int (search_regs.start[0] + offset),
2163 make_int (search_regs.end[0] + offset));
2164 else if (c >= '1' && c <= '9' &&
2165 c <= search_regs.num_regs + '0')
2167 if (search_regs.start[c - '0'] >= 1)
2168 Finsert_buffer_substring
2170 make_int (search_regs.start[c - '0'] + offset),
2171 make_int (search_regs.end[c - '0'] + offset));
2173 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' ||
2176 /* Keep track of all case changes requested, but don't
2177 make them now. Do them later so we override
2181 ul_pos_dynarr = Dynarr_new (int);
2182 ul_action_dynarr = Dynarr_new (int);
2183 record_unwind_protect
2184 (free_created_dynarrs,
2185 Fcons (make_opaque_ptr (ul_pos_dynarr),
2186 make_opaque_ptr (ul_action_dynarr)));
2188 Dynarr_add (ul_pos_dynarr, BUF_PT (buf));
2189 Dynarr_add (ul_action_dynarr, c);
2192 buffer_insert_emacs_char (buf, c);
2195 buffer_insert_emacs_char (buf, c);
2200 inslen = BUF_PT (buf) - (search_regs.start[0]);
2201 buffer_delete_range (buf, search_regs.start[0] + inslen, search_regs.end[0] +
2204 if (case_action == all_caps)
2205 Fupcase_region (make_int (BUF_PT (buf) - inslen),
2206 make_int (BUF_PT (buf)), buffer);
2207 else if (case_action == cap_initial)
2208 Fupcase_initials_region (make_int (BUF_PT (buf) - inslen),
2209 make_int (BUF_PT (buf)), buffer);
2211 /* Now go through and make all the case changes that were requested
2212 in the replacement string. */
2215 Bufpos eend = BUF_PT (buf);
2217 int cur_action = 'E';
2219 for (pos = BUF_PT (buf) - inslen; pos < eend; pos++)
2221 Emchar curchar = BUF_FETCH_CHAR (buf, pos);
2222 Emchar newchar = -1;
2223 if (i < Dynarr_length (ul_pos_dynarr) &&
2224 pos == Dynarr_at (ul_pos_dynarr, i))
2226 int new_action = Dynarr_at (ul_action_dynarr, i);
2228 if (new_action == 'u')
2229 newchar = UPCASE (buf, curchar);
2230 else if (new_action == 'l')
2231 newchar = DOWNCASE (buf, curchar);
2233 cur_action = new_action;
2237 if (cur_action == 'U')
2238 newchar = UPCASE (buf, curchar);
2239 else if (cur_action == 'L')
2240 newchar = DOWNCASE (buf, curchar);
2244 if (newchar != curchar)
2245 buffer_replace_char (buf, pos, newchar, 0, 0);
2249 /* frees the Dynarrs if necessary. */
2250 unbind_to (speccount, Qnil);
2251 end_multiple_change (buf, mc_count);
2257 match_limit (Lisp_Object num, int beginningp)
2259 /* This function has been Mule-ized. */
2264 if (n < 0 || n >= search_regs.num_regs)
2265 args_out_of_range (num, make_int (search_regs.num_regs));
2266 if (search_regs.num_regs == 0 ||
2267 search_regs.start[n] < 0)
2269 return make_int (beginningp ? search_regs.start[n] : search_regs.end[n]);
2272 DEFUN ("match-beginning", Fmatch_beginning, 1, 1, 0, /*
2273 Return position of start of text matched by last regexp search.
2274 NUM, specifies which parenthesized expression in the last regexp.
2275 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
2276 Zero means the entire text matched by the whole regexp or whole string.
2280 return match_limit (num, 1);
2283 DEFUN ("match-end", Fmatch_end, 1, 1, 0, /*
2284 Return position of end of text matched by last regexp search.
2285 NUM specifies which parenthesized expression in the last regexp.
2286 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
2287 Zero means the entire text matched by the whole regexp or whole string.
2291 return match_limit (num, 0);
2294 DEFUN ("match-data", Fmatch_data, 0, 2, 0, /*
2295 Return a list containing all info on what the last regexp search matched.
2296 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
2297 All the elements are markers or nil (nil if the Nth pair didn't match)
2298 if the last match was on a buffer; integers or nil if a string was matched.
2299 Use `store-match-data' to reinstate the data in this list.
2301 If INTEGERS (the optional first argument) is non-nil, always use integers
2302 \(rather than markers) to represent buffer positions.
2303 If REUSE is a list, reuse it as part of the value. If REUSE is long enough
2304 to hold all the values, and if INTEGERS is non-nil, no consing is done.
2308 /* This function has been Mule-ized. */
2309 Lisp_Object tail, prev;
2314 if (NILP (last_thing_searched))
2315 /*error ("match-data called before any match found");*/
2318 data = alloca_array (Lisp_Object, 2 * search_regs.num_regs);
2321 for (i = 0; i < search_regs.num_regs; i++)
2323 Bufpos start = search_regs.start[i];
2326 if (EQ (last_thing_searched, Qt)
2327 || !NILP (integers))
2329 data[2 * i] = make_int (start);
2330 data[2 * i + 1] = make_int (search_regs.end[i]);
2332 else if (BUFFERP (last_thing_searched))
2334 data[2 * i] = Fmake_marker ();
2335 Fset_marker (data[2 * i],
2337 last_thing_searched);
2338 data[2 * i + 1] = Fmake_marker ();
2339 Fset_marker (data[2 * i + 1],
2340 make_int (search_regs.end[i]),
2341 last_thing_searched);
2344 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
2350 data[2 * i] = data [2 * i + 1] = Qnil;
2353 return Flist (2 * len + 2, data);
2355 /* If REUSE is a list, store as many value elements as will fit
2356 into the elements of REUSE. */
2357 for (prev = Qnil, i = 0, tail = reuse; CONSP (tail); i++, tail = XCDR (tail))
2359 if (i < 2 * len + 2)
2360 XCAR (tail) = data[i];
2366 /* If we couldn't fit all value elements into REUSE,
2367 cons up the rest of them and add them to the end of REUSE. */
2368 if (i < 2 * len + 2)
2369 XCDR (prev) = Flist (2 * len + 2 - i, data + i);
2375 DEFUN ("store-match-data", Fstore_match_data, 1, 1, 0, /*
2376 Set internal data on last search match from elements of LIST.
2377 LIST should have been created by calling `match-data' previously.
2381 /* This function has been Mule-ized. */
2383 REGISTER Lisp_Object marker;
2387 if (running_asynch_code)
2388 save_search_regs ();
2390 CONCHECK_LIST (list);
2392 /* Unless we find a marker with a buffer in LIST, assume that this
2393 match data came from a string. */
2394 last_thing_searched = Qt;
2396 /* Allocate registers if they don't already exist. */
2397 length = XINT (Flength (list)) / 2;
2398 num_regs = search_regs.num_regs;
2400 if (length > num_regs)
2402 if (search_regs.num_regs == 0)
2404 search_regs.start = xnew_array (regoff_t, length);
2405 search_regs.end = xnew_array (regoff_t, length);
2409 XREALLOC_ARRAY (search_regs.start, regoff_t, length);
2410 XREALLOC_ARRAY (search_regs.end, regoff_t, length);
2413 search_regs.num_regs = length;
2416 for (i = 0; i < num_regs; i++)
2418 marker = Fcar (list);
2421 search_regs.start[i] = -1;
2426 if (MARKERP (marker))
2428 if (XMARKER (marker)->buffer == 0)
2431 XSETBUFFER (last_thing_searched, XMARKER (marker)->buffer);
2434 CHECK_INT_COERCE_MARKER (marker);
2435 search_regs.start[i] = XINT (marker);
2438 marker = Fcar (list);
2439 if (MARKERP (marker) && XMARKER (marker)->buffer == 0)
2442 CHECK_INT_COERCE_MARKER (marker);
2443 search_regs.end[i] = XINT (marker);
2451 /* If non-zero the match data have been saved in saved_search_regs
2452 during the execution of a sentinel or filter. */
2453 static int search_regs_saved;
2454 static struct re_registers saved_search_regs;
2456 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
2457 if asynchronous code (filter or sentinel) is running. */
2459 save_search_regs (void)
2461 if (!search_regs_saved)
2463 saved_search_regs.num_regs = search_regs.num_regs;
2464 saved_search_regs.start = search_regs.start;
2465 saved_search_regs.end = search_regs.end;
2466 search_regs.num_regs = 0;
2467 search_regs.start = 0;
2468 search_regs.end = 0;
2470 search_regs_saved = 1;
2474 /* Called upon exit from filters and sentinels. */
2476 restore_match_data (void)
2478 if (search_regs_saved)
2480 if (search_regs.num_regs > 0)
2482 xfree (search_regs.start);
2483 xfree (search_regs.end);
2485 search_regs.num_regs = saved_search_regs.num_regs;
2486 search_regs.start = saved_search_regs.start;
2487 search_regs.end = saved_search_regs.end;
2489 search_regs_saved = 0;
2493 /* Quote a string to inactivate reg-expr chars */
2495 DEFUN ("regexp-quote", Fregexp_quote, 1, 1, 0, /*
2496 Return a regexp string which matches exactly STRING and nothing else.
2500 REGISTER Bufbyte *in, *out, *end;
2501 REGISTER Bufbyte *temp;
2505 temp = (Bufbyte *) alloca (XSTRING_LENGTH (str) * 2);
2507 /* Now copy the data into the new string, inserting escapes. */
2509 in = XSTRING_DATA (str);
2510 end = in + XSTRING_LENGTH (str);
2515 Emchar c = charptr_emchar (in);
2517 if (c == '[' || c == ']'
2518 || c == '*' || c == '.' || c == '\\'
2519 || c == '?' || c == '+'
2520 || c == '^' || c == '$')
2522 out += set_charptr_emchar (out, c);
2526 return make_string (temp, out - temp);
2529 DEFUN ("set-word-regexp", Fset_word_regexp, 1, 1, 0, /*
2530 Set the regexp to be used to match a word in regular-expression searching.
2531 #### Not yet implemented. Currently does nothing.
2532 #### Do not use this yet. Its calling interface is likely to change.
2540 /************************************************************************/
2541 /* initialization */
2542 /************************************************************************/
2545 syms_of_search (void)
2548 deferror (&Qsearch_failed, "search-failed", "Search failed", Qerror);
2549 deferror (&Qinvalid_regexp, "invalid-regexp", "Invalid regexp", Qerror);
2551 DEFSUBR (Flooking_at);
2552 DEFSUBR (Fposix_looking_at);
2553 DEFSUBR (Fstring_match);
2554 DEFSUBR (Fposix_string_match);
2555 DEFSUBR (Fskip_chars_forward);
2556 DEFSUBR (Fskip_chars_backward);
2557 DEFSUBR (Fskip_syntax_forward);
2558 DEFSUBR (Fskip_syntax_backward);
2559 DEFSUBR (Fsearch_forward);
2560 DEFSUBR (Fsearch_backward);
2561 DEFSUBR (Fword_search_forward);
2562 DEFSUBR (Fword_search_backward);
2563 DEFSUBR (Fre_search_forward);
2564 DEFSUBR (Fre_search_backward);
2565 DEFSUBR (Fposix_search_forward);
2566 DEFSUBR (Fposix_search_backward);
2567 DEFSUBR (Freplace_match);
2568 DEFSUBR (Fmatch_beginning);
2569 DEFSUBR (Fmatch_end);
2570 DEFSUBR (Fmatch_data);
2571 DEFSUBR (Fstore_match_data);
2572 DEFSUBR (Fregexp_quote);
2573 DEFSUBR (Fset_word_regexp);
2577 vars_of_search (void)
2581 for (i = 0; i < REGEXP_CACHE_SIZE; ++i)
2583 searchbufs[i].buf.allocated = 100;
2584 searchbufs[i].buf.buffer = (unsigned char *) xmalloc (100);
2585 searchbufs[i].buf.fastmap = searchbufs[i].fastmap;
2586 searchbufs[i].regexp = Qnil;
2587 staticpro (&searchbufs[i].regexp);
2588 searchbufs[i].next = (i == REGEXP_CACHE_SIZE-1 ? 0 : &searchbufs[i+1]);
2590 searchbuf_head = &searchbufs[0];
2592 last_thing_searched = Qnil;
2593 staticpro (&last_thing_searched);
2595 DEFVAR_LISP ("forward-word-regexp", &Vforward_word_regexp /*
2596 *Regular expression to be used in `forward-word'.
2597 #### Not yet implemented.
2599 Vforward_word_regexp = Qnil;
2601 DEFVAR_LISP ("backward-word-regexp", &Vbackward_word_regexp /*
2602 *Regular expression to be used in `backward-word'.
2603 #### Not yet implemented.
2605 Vbackward_word_regexp = Qnil;
2609 complex_vars_of_search (void)
2611 Vskip_chars_range_table = Fmake_range_table ();
2612 staticpro (&Vskip_chars_range_table);