428
|
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.
|
793
|
4 Copyright (C) 2001, 2002 Ben Wing.
|
428
|
5
|
|
6 This file is part of XEmacs.
|
|
7
|
|
8 XEmacs is free software; you can redistribute it and/or modify it
|
|
9 under the terms of the GNU General Public License as published by the
|
|
10 Free Software Foundation; either version 2, or (at your option) any
|
|
11 later version.
|
|
12
|
|
13 XEmacs is distributed in the hope that it will be useful, but WITHOUT
|
|
14 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
|
|
15 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
|
|
16 for more details.
|
|
17
|
|
18 You should have received a copy of the GNU General Public License
|
|
19 along with XEmacs; see the file COPYING. If not, write to
|
|
20 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
21 Boston, MA 02111-1307, USA. */
|
|
22
|
|
23 /* Synched up with: FSF 19.29, except for region-cache stuff. */
|
|
24
|
|
25 /* Hacked on for Mule by Ben Wing, December 1994 and August 1995. */
|
|
26
|
826
|
27 /* This file has been Mule-ized. */
|
428
|
28
|
|
29 #include <config.h>
|
|
30 #include "lisp.h"
|
|
31
|
|
32 #include "buffer.h"
|
|
33 #include "insdel.h"
|
|
34 #include "opaque.h"
|
|
35 #ifdef REGION_CACHE_NEEDS_WORK
|
|
36 #include "region-cache.h"
|
|
37 #endif
|
|
38 #include "syntax.h"
|
|
39
|
|
40 #include <sys/types.h>
|
|
41 #include "regex.h"
|
446
|
42 #include "casetab.h"
|
|
43 #include "chartab.h"
|
|
44
|
|
45 #define TRANSLATE(table, pos) \
|
867
|
46 (!NILP (table) ? TRT_TABLE_OF (table, (Ichar) pos) : pos)
|
428
|
47
|
|
48 #define REGEXP_CACHE_SIZE 20
|
|
49
|
|
50 /* If the regexp is non-nil, then the buffer contains the compiled form
|
|
51 of that regexp, suitable for searching. */
|
446
|
52 struct regexp_cache
|
|
53 {
|
428
|
54 struct regexp_cache *next;
|
|
55 Lisp_Object regexp;
|
|
56 struct re_pattern_buffer buf;
|
|
57 char fastmap[0400];
|
|
58 /* Nonzero means regexp was compiled to do full POSIX backtracking. */
|
|
59 char posix;
|
|
60 };
|
|
61
|
|
62 /* The instances of that struct. */
|
|
63 static struct regexp_cache searchbufs[REGEXP_CACHE_SIZE];
|
|
64
|
|
65 /* The head of the linked list; points to the most recently used buffer. */
|
|
66 static struct regexp_cache *searchbuf_head;
|
|
67
|
|
68
|
|
69 /* Every call to re_match, etc., must pass &search_regs as the regs
|
|
70 argument unless you can show it is unnecessary (i.e., if re_match
|
|
71 is certainly going to be called again before region-around-match
|
|
72 can be called).
|
|
73
|
|
74 Since the registers are now dynamically allocated, we need to make
|
|
75 sure not to refer to the Nth register before checking that it has
|
|
76 been allocated by checking search_regs.num_regs.
|
|
77
|
|
78 The regex code keeps track of whether it has allocated the search
|
|
79 buffer using bits in the re_pattern_buffer. This means that whenever
|
|
80 you compile a new pattern, it completely forgets whether it has
|
|
81 allocated any registers, and will allocate new registers the next
|
|
82 time you call a searching or matching function. Therefore, we need
|
|
83 to call re_set_registers after compiling a new pattern or after
|
|
84 setting the match registers, so that the regex functions will be
|
|
85 able to free or re-allocate it properly. */
|
|
86
|
|
87 /* Note: things get trickier under Mule because the values returned from
|
826
|
88 the regexp routines are in Bytebpos's but we need them to be in Charbpos's.
|
428
|
89 We take the easy way out for the moment and just convert them immediately.
|
|
90 We could be more clever by not converting them until necessary, but
|
|
91 that gets real ugly real fast since the buffer might have changed and
|
|
92 the positions might be out of sync or out of range.
|
|
93 */
|
|
94 static struct re_registers search_regs;
|
|
95
|
1468
|
96 /* Every function that sets the match data _must_ clear unused search
|
|
97 registers on success. An unsuccessful search or match _must_ preserve
|
|
98 the search registers. The traditional documentation implied that
|
|
99 any match operation might trash the registers, but in fact failures
|
|
100 have always preserved the match data (in GNU Emacs as well). Some
|
|
101 plausible code depends on this behavior (cf. `w3-configuration-data'
|
|
102 in library "w3-cfg").
|
|
103
|
|
104 Ordinary string searchs use set_search_regs to set the whole-string
|
|
105 match. That function takes care of clearing the unused subexpression
|
1425
|
106 registers.
|
|
107 */
|
|
108 static void set_search_regs (struct buffer *buf, Charbpos beg, Charcount len);
|
1468
|
109 static void clear_search_regs (void);
|
1425
|
110
|
428
|
111 /* The buffer in which the last search was performed, or
|
|
112 Qt if the last search was done in a string;
|
|
113 Qnil if no searching has been done yet. */
|
|
114 static Lisp_Object last_thing_searched;
|
|
115
|
|
116 /* error condition signalled when regexp compile_pattern fails */
|
|
117
|
|
118 Lisp_Object Qinvalid_regexp;
|
|
119
|
|
120 /* Regular expressions used in forward/backward-word */
|
|
121 Lisp_Object Vforward_word_regexp, Vbackward_word_regexp;
|
|
122
|
507
|
123 Fixnum warn_about_possibly_incompatible_back_references;
|
502
|
124
|
428
|
125 /* range table for use with skip_chars. Only needed for Mule. */
|
|
126 Lisp_Object Vskip_chars_range_table;
|
|
127
|
867
|
128 static Charbpos simple_search (struct buffer *buf, Ibyte *base_pat,
|
826
|
129 Bytecount len, Bytebpos pos, Bytebpos lim,
|
|
130 EMACS_INT n, Lisp_Object trt);
|
867
|
131 static Charbpos boyer_moore (struct buffer *buf, Ibyte *base_pat,
|
826
|
132 Bytecount len, Bytebpos pos, Bytebpos lim,
|
|
133 EMACS_INT n, Lisp_Object trt,
|
|
134 Lisp_Object inverse_trt, int charset_base);
|
665
|
135 static Charbpos search_buffer (struct buffer *buf, Lisp_Object str,
|
826
|
136 Charbpos charbpos, Charbpos buflim, EMACS_INT n,
|
|
137 int RE, Lisp_Object trt,
|
|
138 Lisp_Object inverse_trt, int posix);
|
771
|
139
|
428
|
140 static void
|
|
141 matcher_overflow (void)
|
|
142 {
|
563
|
143 stack_overflow ("Stack overflow in regexp matcher", Qunbound);
|
428
|
144 }
|
|
145
|
|
146 /* Compile a regexp and signal a Lisp error if anything goes wrong.
|
|
147 PATTERN is the pattern to compile.
|
|
148 CP is the place to put the result.
|
826
|
149 TRANSLATE is a translation table for ignoring case, or Qnil for none.
|
428
|
150 REGP is the structure that says where to store the "register"
|
|
151 values that will result from matching this pattern.
|
|
152 If it is 0, we should compile the pattern not to record any
|
|
153 subexpression bounds.
|
|
154 POSIX is nonzero if we want full backtracking (POSIX style)
|
|
155 for this pattern. 0 means backtrack only enough to get a valid match. */
|
|
156
|
|
157 static int
|
|
158 compile_pattern_1 (struct regexp_cache *cp, Lisp_Object pattern,
|
826
|
159 struct re_registers *regp, Lisp_Object translate,
|
|
160 int posix, Error_Behavior errb)
|
428
|
161 {
|
442
|
162 const char *val;
|
428
|
163 reg_syntax_t old;
|
|
164
|
|
165 cp->regexp = Qnil;
|
|
166 cp->buf.translate = translate;
|
|
167 cp->posix = posix;
|
|
168 old = re_set_syntax (RE_SYNTAX_EMACS
|
|
169 | (posix ? 0 : RE_NO_POSIX_BACKTRACKING));
|
442
|
170 val = (const char *)
|
428
|
171 re_compile_pattern ((char *) XSTRING_DATA (pattern),
|
|
172 XSTRING_LENGTH (pattern), &cp->buf);
|
|
173 re_set_syntax (old);
|
|
174 if (val)
|
|
175 {
|
563
|
176 maybe_signal_error (Qinvalid_regexp, 0, build_string (val),
|
428
|
177 Qsearch, errb);
|
|
178 return 0;
|
|
179 }
|
|
180
|
|
181 cp->regexp = Fcopy_sequence (pattern);
|
|
182 return 1;
|
|
183 }
|
|
184
|
|
185 /* Compile a regexp if necessary, but first check to see if there's one in
|
|
186 the cache.
|
|
187 PATTERN is the pattern to compile.
|
826
|
188 TRANSLATE is a translation table for ignoring case, or Qnil for none.
|
428
|
189 REGP is the structure that says where to store the "register"
|
|
190 values that will result from matching this pattern.
|
|
191 If it is 0, we should compile the pattern not to record any
|
|
192 subexpression bounds.
|
|
193 POSIX is nonzero if we want full backtracking (POSIX style)
|
|
194 for this pattern. 0 means backtrack only enough to get a valid match. */
|
|
195
|
|
196 struct re_pattern_buffer *
|
|
197 compile_pattern (Lisp_Object pattern, struct re_registers *regp,
|
826
|
198 Lisp_Object translate, Lisp_Object searchobj,
|
|
199 struct buffer *searchbuf, int posix, Error_Behavior errb)
|
428
|
200 {
|
|
201 struct regexp_cache *cp, **cpp;
|
|
202
|
|
203 for (cpp = &searchbuf_head; ; cpp = &cp->next)
|
|
204 {
|
|
205 cp = *cpp;
|
826
|
206 /* &&#### once we fix up the fastmap code in regex.c for 8-bit-fixed,
|
|
207 we need to record and compare the buffer and format, since the
|
|
208 fastmap will reflect the state of the buffer -- and things get
|
|
209 more complicated if the buffer has changed formats or (esp.) has
|
|
210 kept the format but changed its interpretation! may need to have
|
|
211 the code that changes the interpretation go through and invalidate
|
|
212 cache entries for that buffer. */
|
428
|
213 if (!NILP (Fstring_equal (cp->regexp, pattern))
|
446
|
214 && EQ (cp->buf.translate, translate)
|
428
|
215 && cp->posix == posix)
|
|
216 break;
|
|
217
|
|
218 /* If we're at the end of the cache, compile into the last cell. */
|
|
219 if (cp->next == 0)
|
|
220 {
|
826
|
221 if (!compile_pattern_1 (cp, pattern, regp, translate,
|
|
222 posix, errb))
|
428
|
223 return 0;
|
|
224 break;
|
|
225 }
|
|
226 }
|
|
227
|
|
228 /* When we get here, cp (aka *cpp) contains the compiled pattern,
|
|
229 either because we found it in the cache or because we just compiled it.
|
|
230 Move it to the front of the queue to mark it as most recently used. */
|
|
231 *cpp = cp->next;
|
|
232 cp->next = searchbuf_head;
|
|
233 searchbuf_head = cp;
|
|
234
|
|
235 /* Advise the searching functions about the space we have allocated
|
|
236 for register data. */
|
|
237 if (regp)
|
|
238 re_set_registers (&cp->buf, regp, regp->num_regs, regp->start, regp->end);
|
|
239
|
|
240 return &cp->buf;
|
|
241 }
|
|
242
|
|
243 /* Error condition used for failing searches */
|
|
244 Lisp_Object Qsearch_failed;
|
|
245
|
|
246 static Lisp_Object
|
|
247 signal_failure (Lisp_Object arg)
|
|
248 {
|
446
|
249 for (;;)
|
|
250 Fsignal (Qsearch_failed, list1 (arg));
|
|
251 return Qnil; /* Not reached. */
|
428
|
252 }
|
|
253
|
826
|
254 /* Convert the search registers from Bytebpos's to Charbpos's. Needs to be
|
428
|
255 done after each regexp match that uses the search regs.
|
|
256
|
|
257 We could get a potential speedup by not converting the search registers
|
|
258 until it's really necessary, e.g. when match-data or replace-match is
|
|
259 called. However, this complexifies the code a lot (e.g. the buffer
|
826
|
260 could have changed and the Bytebpos's stored might be invalid) and is
|
428
|
261 probably not a great time-saver. */
|
|
262
|
|
263 static void
|
|
264 fixup_search_regs_for_buffer (struct buffer *buf)
|
|
265 {
|
|
266 int i;
|
|
267 int num_regs = search_regs.num_regs;
|
|
268
|
|
269 for (i = 0; i < num_regs; i++)
|
|
270 {
|
|
271 if (search_regs.start[i] >= 0)
|
826
|
272 search_regs.start[i] = bytebpos_to_charbpos (buf,
|
|
273 search_regs.start[i]);
|
428
|
274 if (search_regs.end[i] >= 0)
|
665
|
275 search_regs.end[i] = bytebpos_to_charbpos (buf, search_regs.end[i]);
|
428
|
276 }
|
|
277 }
|
|
278
|
|
279 /* Similar but for strings. */
|
|
280 static void
|
|
281 fixup_search_regs_for_string (Lisp_Object string)
|
|
282 {
|
|
283 int i;
|
|
284 int num_regs = search_regs.num_regs;
|
|
285
|
|
286 /* #### bytecount_to_charcount() is not that efficient. This function
|
867
|
287 could be faster if it did its own conversion (using INC_IBYTEPTR()
|
428
|
288 and such), because the register ends are likely to be somewhat ordered.
|
|
289 (Even if not, you could sort them.)
|
|
290
|
|
291 Think about this if this function is a time hog, which it's probably
|
|
292 not. */
|
|
293 for (i = 0; i < num_regs; i++)
|
|
294 {
|
|
295 if (search_regs.start[i] > 0)
|
|
296 {
|
|
297 search_regs.start[i] =
|
793
|
298 string_index_byte_to_char (string, search_regs.start[i]);
|
428
|
299 }
|
|
300 if (search_regs.end[i] > 0)
|
|
301 {
|
|
302 search_regs.end[i] =
|
793
|
303 string_index_byte_to_char (string, search_regs.end[i]);
|
428
|
304 }
|
|
305 }
|
|
306 }
|
|
307
|
|
308
|
|
309 static Lisp_Object
|
|
310 looking_at_1 (Lisp_Object string, struct buffer *buf, int posix)
|
|
311 {
|
|
312 Lisp_Object val;
|
665
|
313 Bytebpos p1, p2;
|
428
|
314 Bytecount s1, s2;
|
|
315 REGISTER int i;
|
|
316 struct re_pattern_buffer *bufp;
|
826
|
317 struct syntax_cache scache_struct;
|
|
318 struct syntax_cache *scache = &scache_struct;
|
|
319
|
428
|
320 CHECK_STRING (string);
|
|
321 bufp = compile_pattern (string, &search_regs,
|
|
322 (!NILP (buf->case_fold_search)
|
446
|
323 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil),
|
826
|
324 wrap_buffer (buf), buf, posix, ERROR_ME);
|
428
|
325
|
|
326 QUIT;
|
|
327
|
|
328 /* Get pointers and sizes of the two strings
|
|
329 that make up the visible portion of the buffer. */
|
|
330
|
826
|
331 p1 = BYTE_BUF_BEGV (buf);
|
|
332 p2 = BYTE_BUF_CEILING_OF (buf, p1);
|
428
|
333 s1 = p2 - p1;
|
826
|
334 s2 = BYTE_BUF_ZV (buf) - p2;
|
|
335
|
|
336 /* By making the regex object, regex buffer, and syntax cache arguments
|
|
337 to re_{search,match}{,_2}, we've removed the need to do nasty things
|
|
338 to deal with regex reentrancy. (See stack trace in signal.c for proof
|
|
339 that this can happen.)
|
|
340
|
|
341 #### there is still a potential problem with the regex cache --
|
|
342 the compiled regex could be overwritten. we'd need 20-fold
|
|
343 reentrancy, though. Fix this. */
|
|
344
|
|
345 i = re_match_2 (bufp, (char *) BYTE_BUF_BYTE_ADDRESS (buf, p1),
|
|
346 s1, (char *) BYTE_BUF_BYTE_ADDRESS (buf, p2), s2,
|
|
347 BYTE_BUF_PT (buf) - BYTE_BUF_BEGV (buf), &search_regs,
|
|
348 BYTE_BUF_ZV (buf) - BYTE_BUF_BEGV (buf), wrap_buffer (buf),
|
|
349 buf, scache);
|
428
|
350
|
|
351 if (i == -2)
|
|
352 matcher_overflow ();
|
|
353
|
|
354 val = (0 <= i ? Qt : Qnil);
|
|
355 if (NILP (val))
|
826
|
356 return Qnil;
|
428
|
357 {
|
|
358 int num_regs = search_regs.num_regs;
|
|
359 for (i = 0; i < num_regs; i++)
|
|
360 if (search_regs.start[i] >= 0)
|
|
361 {
|
826
|
362 search_regs.start[i] += BYTE_BUF_BEGV (buf);
|
|
363 search_regs.end[i] += BYTE_BUF_BEGV (buf);
|
428
|
364 }
|
|
365 }
|
793
|
366 last_thing_searched = wrap_buffer (buf);
|
428
|
367 fixup_search_regs_for_buffer (buf);
|
826
|
368 return val;
|
428
|
369 }
|
|
370
|
|
371 DEFUN ("looking-at", Flooking_at, 1, 2, 0, /*
|
|
372 Return t if text after point matches regular expression REGEXP.
|
1468
|
373 When the match is successful, this function modifies the match data
|
|
374 that `match-beginning', `match-end' and `match-data' access; save the
|
|
375 match data with `match-data' and restore it with `store-match-data' if
|
|
376 you want to preserve them. If the match fails, the match data from the
|
|
377 previous success match is preserved.
|
428
|
378
|
|
379 Optional argument BUFFER defaults to the current buffer.
|
|
380 */
|
|
381 (regexp, buffer))
|
|
382 {
|
|
383 return looking_at_1 (regexp, decode_buffer (buffer, 0), 0);
|
|
384 }
|
|
385
|
|
386 DEFUN ("posix-looking-at", Fposix_looking_at, 1, 2, 0, /*
|
|
387 Return t if text after point matches regular expression REGEXP.
|
|
388 Find the longest match, in accord with Posix regular expression rules.
|
1468
|
389 When the match is successful, this function modifies the match data
|
|
390 that `match-beginning', `match-end' and `match-data' access; save the
|
|
391 match data with `match-data' and restore it with `store-match-data' if
|
|
392 you want to preserve them. If the match fails, the match data from the
|
|
393 previous success match is preserved.
|
428
|
394
|
|
395 Optional argument BUFFER defaults to the current buffer.
|
|
396 */
|
|
397 (regexp, buffer))
|
|
398 {
|
826
|
399 return looking_at_1 (regexp, decode_buffer (buffer, 0), 1);
|
428
|
400 }
|
|
401
|
|
402 static Lisp_Object
|
|
403 string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start,
|
|
404 struct buffer *buf, int posix)
|
|
405 {
|
|
406 Bytecount val;
|
|
407 Charcount s;
|
|
408 struct re_pattern_buffer *bufp;
|
|
409
|
853
|
410 /* Some FSF junk with running_asynch_code, to preserve the match
|
|
411 data. Not necessary because we don't call process filters
|
|
412 asynchronously (i.e. from within QUIT). */
|
428
|
413
|
|
414 CHECK_STRING (regexp);
|
|
415 CHECK_STRING (string);
|
|
416
|
|
417 if (NILP (start))
|
|
418 s = 0;
|
|
419 else
|
|
420 {
|
826
|
421 Charcount len = string_char_length (string);
|
428
|
422
|
|
423 CHECK_INT (start);
|
|
424 s = XINT (start);
|
|
425 if (s < 0 && -s <= len)
|
|
426 s = len + s;
|
|
427 else if (0 > s || s > len)
|
|
428 args_out_of_range (string, start);
|
|
429 }
|
|
430
|
|
431
|
|
432 bufp = compile_pattern (regexp, &search_regs,
|
|
433 (!NILP (buf->case_fold_search)
|
446
|
434 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil),
|
826
|
435 string, buf, 0, ERROR_ME);
|
428
|
436 QUIT;
|
|
437 {
|
793
|
438 Bytecount bis = string_index_char_to_byte (string, s);
|
826
|
439 struct syntax_cache scache_struct;
|
|
440 struct syntax_cache *scache = &scache_struct;
|
|
441
|
|
442 /* By making the regex object, regex buffer, and syntax cache arguments
|
|
443 to re_{search,match}{,_2}, we've removed the need to do nasty things
|
|
444 to deal with regex reentrancy. (See stack trace in signal.c for proof
|
|
445 that this can happen.)
|
|
446
|
|
447 #### there is still a potential problem with the regex cache --
|
|
448 the compiled regex could be overwritten. we'd need 20-fold
|
|
449 reentrancy, though. Fix this. */
|
|
450
|
428
|
451 val = re_search (bufp, (char *) XSTRING_DATA (string),
|
|
452 XSTRING_LENGTH (string), bis,
|
|
453 XSTRING_LENGTH (string) - bis,
|
826
|
454 &search_regs, string, buf, scache);
|
428
|
455 }
|
|
456 if (val == -2)
|
|
457 matcher_overflow ();
|
826
|
458 if (val < 0) return Qnil;
|
428
|
459 last_thing_searched = Qt;
|
|
460 fixup_search_regs_for_string (string);
|
826
|
461 return make_int (string_index_byte_to_char (string, val));
|
428
|
462 }
|
|
463
|
|
464 DEFUN ("string-match", Fstring_match, 2, 4, 0, /*
|
|
465 Return index of start of first match for REGEXP in STRING, or nil.
|
|
466 If third arg START is non-nil, start search at that index in STRING.
|
|
467 For index of first char beyond the match, do (match-end 0).
|
|
468 `match-end' and `match-beginning' also give indices of substrings
|
|
469 matched by parenthesis constructs in the pattern.
|
|
470
|
826
|
471 Optional arg BUFFER controls how case folding and syntax and category
|
|
472 lookup is done (according to the value of `case-fold-search' in that buffer
|
|
473 and that buffer's case tables, syntax tables, and category table). If nil
|
|
474 or unspecified, it defaults *NOT* to the current buffer but instead:
|
|
475
|
|
476 -- the value of `case-fold-search' in the current buffer is still respected
|
|
477 because of idioms like
|
|
478
|
|
479 (let ((case-fold-search nil))
|
|
480 (string-match "^foo.*bar" string))
|
|
481
|
|
482 but the case, syntax, and category tables come from the standard tables,
|
1468
|
483 which are accessed through functions `default-{case,syntax,category}-table'
|
|
484 and serve as the parents of the tables in particular buffer.
|
|
485
|
|
486 When the match is successful, this function modifies the match data
|
|
487 that `match-beginning', `match-end' and `match-data' access; save the
|
|
488 match data with `match-data' and restore it with `store-match-data' if
|
|
489 you want to preserve them. If the match fails, the match data from the
|
|
490 previous success match is preserved.
|
428
|
491 */
|
|
492 (regexp, string, start, buffer))
|
|
493 {
|
826
|
494 /* &&#### implement new interp for buffer arg; check code to see if it
|
|
495 makes more sense than prev */
|
428
|
496 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 0);
|
|
497 }
|
|
498
|
|
499 DEFUN ("posix-string-match", Fposix_string_match, 2, 4, 0, /*
|
|
500 Return index of start of first match for REGEXP in STRING, or nil.
|
|
501 Find the longest match, in accord with Posix regular expression rules.
|
|
502 If third arg START is non-nil, start search at that index in STRING.
|
|
503 For index of first char beyond the match, do (match-end 0).
|
|
504 `match-end' and `match-beginning' also give indices of substrings
|
|
505 matched by parenthesis constructs in the pattern.
|
|
506
|
|
507 Optional arg BUFFER controls how case folding is done (according to
|
|
508 the value of `case-fold-search' in that buffer and that buffer's case
|
|
509 tables) and defaults to the current buffer.
|
1468
|
510
|
|
511 When the match is successful, this function modifies the match data
|
|
512 that `match-beginning', `match-end' and `match-data' access; save the
|
|
513 match data with `match-data' and restore it with `store-match-data' if
|
|
514 you want to preserve them. If the match fails, the match data from the
|
|
515 previous success match is preserved.
|
428
|
516 */
|
|
517 (regexp, string, start, buffer))
|
|
518 {
|
|
519 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 1);
|
|
520 }
|
|
521
|
|
522 /* Match REGEXP against STRING, searching all of STRING,
|
|
523 and return the index of the match, or negative on failure.
|
|
524 This does not clobber the match data. */
|
|
525
|
|
526 Bytecount
|
1347
|
527 fast_string_match (Lisp_Object regexp, const Ibyte *nonreloc,
|
428
|
528 Lisp_Object reloc, Bytecount offset,
|
|
529 Bytecount length, int case_fold_search,
|
578
|
530 Error_Behavior errb, int no_quit)
|
428
|
531 {
|
|
532 Bytecount val;
|
867
|
533 Ibyte *newnonreloc = (Ibyte *) nonreloc;
|
428
|
534 struct re_pattern_buffer *bufp;
|
826
|
535 struct syntax_cache scache_struct;
|
|
536 struct syntax_cache *scache = &scache_struct;
|
428
|
537
|
|
538 bufp = compile_pattern (regexp, 0,
|
|
539 (case_fold_search
|
771
|
540 ? XCASE_TABLE_DOWNCASE (Vstandard_case_table)
|
446
|
541 : Qnil),
|
826
|
542 reloc, 0, 0, errb);
|
428
|
543 if (!bufp)
|
|
544 return -1; /* will only do this when errb != ERROR_ME */
|
|
545 if (!no_quit)
|
|
546 QUIT;
|
|
547 else
|
|
548 no_quit_in_re_search = 1;
|
|
549
|
|
550 fixup_internal_substring (nonreloc, reloc, offset, &length);
|
|
551
|
771
|
552 /* Don't need to protect against GC inside of re_search() due to QUIT;
|
|
553 QUIT is GC-inhibited. */
|
428
|
554 if (!NILP (reloc))
|
771
|
555 newnonreloc = XSTRING_DATA (reloc);
|
|
556
|
826
|
557 /* By making the regex object, regex buffer, and syntax cache arguments
|
|
558 to re_{search,match}{,_2}, we've removed the need to do nasty things
|
|
559 to deal with regex reentrancy. (See stack trace in signal.c for proof
|
|
560 that this can happen.)
|
|
561
|
|
562 #### there is still a potential problem with the regex cache --
|
|
563 the compiled regex could be overwritten. we'd need 20-fold
|
|
564 reentrancy, though. Fix this. */
|
|
565
|
428
|
566 val = re_search (bufp, (char *) newnonreloc + offset, length, 0,
|
826
|
567 length, 0, reloc, 0, scache);
|
428
|
568
|
|
569 no_quit_in_re_search = 0;
|
|
570 return val;
|
|
571 }
|
|
572
|
|
573 Bytecount
|
|
574 fast_lisp_string_match (Lisp_Object regex, Lisp_Object string)
|
|
575 {
|
|
576 return fast_string_match (regex, 0, string, 0, -1, 0, ERROR_ME, 0);
|
|
577 }
|
|
578
|
|
579
|
|
580 #ifdef REGION_CACHE_NEEDS_WORK
|
|
581 /* The newline cache: remembering which sections of text have no newlines. */
|
|
582
|
|
583 /* If the user has requested newline caching, make sure it's on.
|
|
584 Otherwise, make sure it's off.
|
|
585 This is our cheezy way of associating an action with the change of
|
|
586 state of a buffer-local variable. */
|
|
587 static void
|
|
588 newline_cache_on_off (struct buffer *buf)
|
|
589 {
|
|
590 if (NILP (buf->cache_long_line_scans))
|
|
591 {
|
|
592 /* It should be off. */
|
|
593 if (buf->newline_cache)
|
|
594 {
|
|
595 free_region_cache (buf->newline_cache);
|
|
596 buf->newline_cache = 0;
|
|
597 }
|
|
598 }
|
|
599 else
|
|
600 {
|
|
601 /* It should be on. */
|
|
602 if (buf->newline_cache == 0)
|
|
603 buf->newline_cache = new_region_cache ();
|
|
604 }
|
|
605 }
|
|
606 #endif
|
|
607
|
|
608 /* Search in BUF for COUNT instances of the character TARGET between
|
|
609 START and END.
|
|
610
|
|
611 If COUNT is positive, search forwards; END must be >= START.
|
|
612 If COUNT is negative, search backwards for the -COUNTth instance;
|
|
613 END must be <= START.
|
|
614 If COUNT is zero, do anything you please; run rogue, for all I care.
|
|
615
|
|
616 If END is zero, use BEGV or ZV instead, as appropriate for the
|
|
617 direction indicated by COUNT.
|
|
618
|
|
619 If we find COUNT instances, set *SHORTAGE to zero, and return the
|
|
620 position after the COUNTth match. Note that for reverse motion
|
|
621 this is not the same as the usual convention for Emacs motion commands.
|
|
622
|
|
623 If we don't find COUNT instances before reaching END, set *SHORTAGE
|
|
624 to the number of TARGETs left unfound, and return END.
|
|
625
|
|
626 If ALLOW_QUIT is non-zero, call QUIT periodically. */
|
|
627
|
665
|
628 static Bytebpos
|
867
|
629 byte_scan_buffer (struct buffer *buf, Ichar target, Bytebpos st, Bytebpos en,
|
872
|
630 EMACS_INT count, EMACS_INT *shortage, int allow_quit)
|
428
|
631 {
|
665
|
632 Bytebpos lim = en > 0 ? en :
|
826
|
633 ((count > 0) ? BYTE_BUF_ZV (buf) : BYTE_BUF_BEGV (buf));
|
428
|
634
|
|
635 /* #### newline cache stuff in this function not yet ported */
|
|
636 assert (count != 0);
|
|
637
|
|
638 if (shortage)
|
|
639 *shortage = 0;
|
|
640
|
|
641 if (count > 0)
|
|
642 {
|
|
643 #ifdef MULE
|
826
|
644 Internal_Format fmt = buf->text->format;
|
|
645 /* Check for char that's unrepresentable in the buffer -- it
|
|
646 certainly can't be there. */
|
867
|
647 if (!ichar_fits_in_format (target, fmt, wrap_buffer (buf)))
|
428
|
648 {
|
826
|
649 *shortage = count;
|
|
650 return lim;
|
|
651 }
|
|
652 /* Due to the Mule representation of characters in a buffer, we can
|
|
653 simply search for characters in the range 0 - 127 directly; for
|
|
654 8-bit-fixed, we can do this for all characters. In other cases,
|
|
655 we do it the "hard" way. Note that this way works for all
|
|
656 characters and all formats, but the other way is faster. */
|
|
657 else if (! (fmt == FORMAT_8_BIT_FIXED ||
|
867
|
658 (fmt == FORMAT_DEFAULT && ichar_ascii_p (target))))
|
826
|
659 {
|
867
|
660 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf));
|
428
|
661 while (st < lim && count > 0)
|
|
662 {
|
826
|
663 if (BYTE_BUF_FETCH_CHAR_RAW (buf, st) == raw)
|
428
|
664 count--;
|
665
|
665 INC_BYTEBPOS (buf, st);
|
428
|
666 }
|
|
667 }
|
|
668 else
|
|
669 #endif
|
|
670 {
|
867
|
671 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf));
|
428
|
672 while (st < lim && count > 0)
|
|
673 {
|
665
|
674 Bytebpos ceil;
|
867
|
675 Ibyte *bufptr;
|
428
|
676
|
826
|
677 ceil = BYTE_BUF_CEILING_OF (buf, st);
|
428
|
678 ceil = min (lim, ceil);
|
867
|
679 bufptr = (Ibyte *) memchr (BYTE_BUF_BYTE_ADDRESS (buf, st),
|
826
|
680 raw, ceil - st);
|
428
|
681 if (bufptr)
|
|
682 {
|
|
683 count--;
|
826
|
684 st = BYTE_BUF_PTR_BYTE_POS (buf, bufptr) + 1;
|
428
|
685 }
|
|
686 else
|
|
687 st = ceil;
|
|
688 }
|
|
689 }
|
|
690
|
|
691 if (shortage)
|
|
692 *shortage = count;
|
|
693 if (allow_quit)
|
|
694 QUIT;
|
|
695 return st;
|
|
696 }
|
|
697 else
|
|
698 {
|
|
699 #ifdef MULE
|
826
|
700 Internal_Format fmt = buf->text->format;
|
|
701 /* Check for char that's unrepresentable in the buffer -- it
|
|
702 certainly can't be there. */
|
867
|
703 if (!ichar_fits_in_format (target, fmt, wrap_buffer (buf)))
|
428
|
704 {
|
826
|
705 *shortage = -count;
|
|
706 return lim;
|
|
707 }
|
|
708 else if (! (fmt == FORMAT_8_BIT_FIXED ||
|
867
|
709 (fmt == FORMAT_DEFAULT && ichar_ascii_p (target))))
|
826
|
710 {
|
867
|
711 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf));
|
428
|
712 while (st > lim && count < 0)
|
|
713 {
|
665
|
714 DEC_BYTEBPOS (buf, st);
|
826
|
715 if (BYTE_BUF_FETCH_CHAR_RAW (buf, st) == raw)
|
428
|
716 count++;
|
|
717 }
|
|
718 }
|
|
719 else
|
|
720 #endif
|
|
721 {
|
867
|
722 Raw_Ichar raw = ichar_to_raw (target, fmt, wrap_buffer (buf));
|
428
|
723 while (st > lim && count < 0)
|
|
724 {
|
665
|
725 Bytebpos floor;
|
867
|
726 Ibyte *bufptr;
|
|
727 Ibyte *floorptr;
|
428
|
728
|
826
|
729 floor = BYTE_BUF_FLOOR_OF (buf, st);
|
428
|
730 floor = max (lim, floor);
|
|
731 /* No memrchr() ... */
|
826
|
732 bufptr = BYTE_BUF_BYTE_ADDRESS_BEFORE (buf, st);
|
|
733 floorptr = BYTE_BUF_BYTE_ADDRESS (buf, floor);
|
428
|
734 while (bufptr >= floorptr)
|
|
735 {
|
|
736 st--;
|
|
737 /* At this point, both ST and BUFPTR refer to the same
|
|
738 character. When the loop terminates, ST will
|
|
739 always point to the last character we tried. */
|
867
|
740 if (*bufptr == (Ibyte) raw)
|
428
|
741 {
|
|
742 count++;
|
|
743 break;
|
|
744 }
|
|
745 bufptr--;
|
|
746 }
|
|
747 }
|
|
748 }
|
|
749
|
|
750 if (shortage)
|
|
751 *shortage = -count;
|
|
752 if (allow_quit)
|
|
753 QUIT;
|
|
754 if (count)
|
|
755 return st;
|
|
756 else
|
|
757 {
|
|
758 /* We found the character we were looking for; we have to return
|
|
759 the position *after* it due to the strange way that the return
|
|
760 value is defined. */
|
665
|
761 INC_BYTEBPOS (buf, st);
|
428
|
762 return st;
|
|
763 }
|
|
764 }
|
|
765 }
|
|
766
|
665
|
767 Charbpos
|
867
|
768 scan_buffer (struct buffer *buf, Ichar target, Charbpos start, Charbpos end,
|
428
|
769 EMACS_INT count, EMACS_INT *shortage, int allow_quit)
|
|
770 {
|
826
|
771 Bytebpos byte_retval;
|
|
772 Bytebpos byte_start, byte_end;
|
|
773
|
|
774 byte_start = charbpos_to_bytebpos (buf, start);
|
428
|
775 if (end)
|
826
|
776 byte_end = charbpos_to_bytebpos (buf, end);
|
428
|
777 else
|
826
|
778 byte_end = 0;
|
|
779 byte_retval = byte_scan_buffer (buf, target, byte_start, byte_end, count,
|
428
|
780 shortage, allow_quit);
|
826
|
781 return bytebpos_to_charbpos (buf, byte_retval);
|
428
|
782 }
|
|
783
|
665
|
784 Bytebpos
|
826
|
785 byte_find_next_newline_no_quit (struct buffer *buf, Bytebpos from, int count)
|
428
|
786 {
|
826
|
787 return byte_scan_buffer (buf, '\n', from, 0, count, 0, 0);
|
428
|
788 }
|
|
789
|
665
|
790 Charbpos
|
|
791 find_next_newline_no_quit (struct buffer *buf, Charbpos from, int count)
|
428
|
792 {
|
|
793 return scan_buffer (buf, '\n', from, 0, count, 0, 0);
|
|
794 }
|
|
795
|
665
|
796 Charbpos
|
|
797 find_next_newline (struct buffer *buf, Charbpos from, int count)
|
428
|
798 {
|
|
799 return scan_buffer (buf, '\n', from, 0, count, 0, 1);
|
|
800 }
|
|
801
|
826
|
802 Bytecount
|
867
|
803 byte_find_next_ichar_in_string (Lisp_Object str, Ichar target, Bytecount st,
|
428
|
804 EMACS_INT count)
|
|
805 {
|
793
|
806 Bytebpos lim = XSTRING_LENGTH (str) -1;
|
867
|
807 Ibyte *s = XSTRING_DATA (str);
|
428
|
808
|
|
809 assert (count >= 0);
|
|
810
|
|
811 #ifdef MULE
|
|
812 /* Due to the Mule representation of characters in a buffer,
|
|
813 we can simply search for characters in the range 0 - 127
|
|
814 directly. For other characters, we do it the "hard" way.
|
|
815 Note that this way works for all characters but the other
|
|
816 way is faster. */
|
|
817 if (target >= 0200)
|
|
818 {
|
|
819 while (st < lim && count > 0)
|
|
820 {
|
867
|
821 if (string_ichar (str, st) == target)
|
428
|
822 count--;
|
826
|
823 INC_BYTECOUNT (s, st);
|
428
|
824 }
|
|
825 }
|
|
826 else
|
|
827 #endif
|
|
828 {
|
|
829 while (st < lim && count > 0)
|
|
830 {
|
867
|
831 Ibyte *bufptr = (Ibyte *) memchr (itext_n_addr (s, st),
|
428
|
832 (int) target, lim - st);
|
|
833 if (bufptr)
|
|
834 {
|
|
835 count--;
|
826
|
836 st = (Bytebpos) (bufptr - s) + 1;
|
428
|
837 }
|
|
838 else
|
|
839 st = lim;
|
|
840 }
|
|
841 }
|
|
842 return st;
|
|
843 }
|
|
844
|
|
845 /* Like find_next_newline, but returns position before the newline,
|
|
846 not after, and only search up to TO. This isn't just
|
|
847 find_next_newline (...)-1, because you might hit TO. */
|
665
|
848 Charbpos
|
826
|
849 find_before_next_newline (struct buffer *buf, Charbpos from, Charbpos to,
|
|
850 int count)
|
428
|
851 {
|
|
852 EMACS_INT shortage;
|
665
|
853 Charbpos pos = scan_buffer (buf, '\n', from, to, count, &shortage, 1);
|
428
|
854
|
|
855 if (shortage == 0)
|
|
856 pos--;
|
|
857
|
|
858 return pos;
|
|
859 }
|
|
860
|
872
|
861 /* This function synched with FSF 21.1 */
|
428
|
862 static Lisp_Object
|
|
863 skip_chars (struct buffer *buf, int forwardp, int syntaxp,
|
|
864 Lisp_Object string, Lisp_Object lim)
|
|
865 {
|
867
|
866 REGISTER Ibyte *p, *pend;
|
|
867 REGISTER Ichar c;
|
428
|
868 /* We store the first 256 chars in an array here and the rest in
|
|
869 a range table. */
|
|
870 unsigned char fastmap[0400];
|
|
871 int negate = 0;
|
|
872 REGISTER int i;
|
665
|
873 Charbpos limit;
|
826
|
874 struct syntax_cache *scache;
|
|
875
|
428
|
876 if (NILP (lim))
|
|
877 limit = forwardp ? BUF_ZV (buf) : BUF_BEGV (buf);
|
|
878 else
|
|
879 {
|
|
880 CHECK_INT_COERCE_MARKER (lim);
|
|
881 limit = XINT (lim);
|
|
882
|
|
883 /* In any case, don't allow scan outside bounds of buffer. */
|
|
884 if (limit > BUF_ZV (buf)) limit = BUF_ZV (buf);
|
|
885 if (limit < BUF_BEGV (buf)) limit = BUF_BEGV (buf);
|
|
886 }
|
|
887
|
|
888 CHECK_STRING (string);
|
|
889 p = XSTRING_DATA (string);
|
|
890 pend = p + XSTRING_LENGTH (string);
|
|
891 memset (fastmap, 0, sizeof (fastmap));
|
|
892
|
|
893 Fclear_range_table (Vskip_chars_range_table);
|
|
894
|
|
895 if (p != pend && *p == '^')
|
|
896 {
|
|
897 negate = 1;
|
|
898 p++;
|
|
899 }
|
|
900
|
|
901 /* Find the characters specified and set their elements of fastmap.
|
|
902 If syntaxp, each character counts as itself.
|
|
903 Otherwise, handle backslashes and ranges specially */
|
|
904
|
|
905 while (p != pend)
|
|
906 {
|
867
|
907 c = itext_ichar (p);
|
|
908 INC_IBYTEPTR (p);
|
428
|
909 if (syntaxp)
|
|
910 {
|
|
911 if (c < 0400 && syntax_spec_code[c] < (unsigned char) Smax)
|
|
912 fastmap[c] = 1;
|
|
913 else
|
831
|
914 invalid_argument ("Invalid syntax designator", make_char (c));
|
428
|
915 }
|
|
916 else
|
|
917 {
|
|
918 if (c == '\\')
|
|
919 {
|
|
920 if (p == pend) break;
|
867
|
921 c = itext_ichar (p);
|
|
922 INC_IBYTEPTR (p);
|
428
|
923 }
|
|
924 if (p != pend && *p == '-')
|
|
925 {
|
867
|
926 Ichar cend;
|
428
|
927
|
872
|
928 /* Skip over the dash. */
|
428
|
929 p++;
|
|
930 if (p == pend) break;
|
867
|
931 cend = itext_ichar (p);
|
428
|
932 while (c <= cend && c < 0400)
|
|
933 {
|
|
934 fastmap[c] = 1;
|
|
935 c++;
|
|
936 }
|
|
937 if (c <= cend)
|
|
938 Fput_range_table (make_int (c), make_int (cend), Qt,
|
|
939 Vskip_chars_range_table);
|
867
|
940 INC_IBYTEPTR (p);
|
428
|
941 }
|
|
942 else
|
|
943 {
|
|
944 if (c < 0400)
|
|
945 fastmap[c] = 1;
|
|
946 else
|
|
947 Fput_range_table (make_int (c), make_int (c), Qt,
|
|
948 Vskip_chars_range_table);
|
|
949 }
|
|
950 }
|
|
951 }
|
|
952
|
872
|
953 /* #### Not in FSF 21.1 */
|
428
|
954 if (syntaxp && fastmap['-'] != 0)
|
|
955 fastmap[' '] = 1;
|
|
956
|
|
957 /* If ^ was the first character, complement the fastmap.
|
|
958 We don't complement the range table, however; we just use negate
|
|
959 in the comparisons below. */
|
|
960
|
|
961 if (negate)
|
647
|
962 for (i = 0; i < (int) (sizeof (fastmap)); i++)
|
428
|
963 fastmap[i] ^= 1;
|
|
964
|
|
965 {
|
665
|
966 Charbpos start_point = BUF_PT (buf);
|
872
|
967 Charbpos pos = start_point;
|
|
968 Charbpos pos_byte = BYTE_BUF_PT (buf);
|
428
|
969
|
|
970 if (syntaxp)
|
|
971 {
|
872
|
972 scache = setup_buffer_syntax_cache (buf, pos, forwardp ? 1 : -1);
|
428
|
973 /* All syntax designators are normal chars so nothing strange
|
|
974 to worry about */
|
|
975 if (forwardp)
|
|
976 {
|
872
|
977 if (pos < limit)
|
|
978 while (fastmap[(unsigned char)
|
|
979 syntax_code_spec
|
|
980 [(int) SYNTAX_FROM_CACHE
|
|
981 (scache, BYTE_BUF_FETCH_CHAR (buf, pos_byte))]])
|
|
982 {
|
|
983 pos++;
|
|
984 INC_BYTEBPOS (buf, pos_byte);
|
879
|
985 if (pos >= limit)
|
872
|
986 break;
|
|
987 UPDATE_SYNTAX_CACHE_FORWARD (scache, pos);
|
|
988 }
|
428
|
989 }
|
|
990 else
|
|
991 {
|
872
|
992 while (pos > limit)
|
460
|
993 {
|
872
|
994 Charbpos savepos = pos_byte;
|
|
995 pos--;
|
|
996 DEC_BYTEBPOS (buf, pos_byte);
|
|
997 UPDATE_SYNTAX_CACHE_BACKWARD (scache, pos);
|
|
998 if (!fastmap[(unsigned char)
|
|
999 syntax_code_spec
|
|
1000 [(int) SYNTAX_FROM_CACHE
|
|
1001 (scache, BYTE_BUF_FETCH_CHAR (buf, pos_byte))]])
|
|
1002 {
|
|
1003 pos++;
|
|
1004 pos_byte = savepos;
|
|
1005 break;
|
|
1006 }
|
460
|
1007 }
|
428
|
1008 }
|
|
1009 }
|
|
1010 else
|
|
1011 {
|
|
1012 if (forwardp)
|
|
1013 {
|
872
|
1014 while (pos < limit)
|
428
|
1015 {
|
872
|
1016 Ichar ch = BYTE_BUF_FETCH_CHAR (buf, pos_byte);
|
428
|
1017 if ((ch < 0400) ? fastmap[ch] :
|
|
1018 (NILP (Fget_range_table (make_int (ch),
|
|
1019 Vskip_chars_range_table,
|
|
1020 Qnil))
|
|
1021 == negate))
|
872
|
1022 {
|
|
1023 pos++;
|
|
1024 INC_BYTEBPOS (buf, pos_byte);
|
|
1025 }
|
428
|
1026 else
|
|
1027 break;
|
|
1028 }
|
|
1029 }
|
|
1030 else
|
|
1031 {
|
872
|
1032 while (pos > limit)
|
428
|
1033 {
|
872
|
1034 Charbpos prev_pos_byte = pos_byte;
|
|
1035 Ichar ch;
|
|
1036
|
|
1037 DEC_BYTEBPOS (buf, prev_pos_byte);
|
|
1038 ch = BYTE_BUF_FETCH_CHAR (buf, prev_pos_byte);
|
428
|
1039 if ((ch < 0400) ? fastmap[ch] :
|
|
1040 (NILP (Fget_range_table (make_int (ch),
|
|
1041 Vskip_chars_range_table,
|
|
1042 Qnil))
|
|
1043 == negate))
|
872
|
1044 {
|
|
1045 pos--;
|
|
1046 pos_byte = prev_pos_byte;
|
|
1047 }
|
428
|
1048 else
|
|
1049 break;
|
|
1050 }
|
|
1051 }
|
|
1052 }
|
|
1053 QUIT;
|
872
|
1054 BOTH_BUF_SET_PT (buf, pos, pos_byte);
|
428
|
1055 return make_int (BUF_PT (buf) - start_point);
|
|
1056 }
|
|
1057 }
|
|
1058
|
|
1059 DEFUN ("skip-chars-forward", Fskip_chars_forward, 1, 3, 0, /*
|
444
|
1060 Move point forward, stopping before a char not in STRING, or at pos LIMIT.
|
428
|
1061 STRING is like the inside of a `[...]' in a regular expression
|
|
1062 except that `]' is never special and `\\' quotes `^', `-' or `\\'.
|
|
1063 Thus, with arg "a-zA-Z", this skips letters stopping before first nonletter.
|
|
1064 With arg "^a-zA-Z", skips nonletters stopping before first letter.
|
|
1065 Returns the distance traveled, either zero or positive.
|
|
1066
|
|
1067 Optional argument BUFFER defaults to the current buffer.
|
|
1068 */
|
444
|
1069 (string, limit, buffer))
|
428
|
1070 {
|
444
|
1071 return skip_chars (decode_buffer (buffer, 0), 1, 0, string, limit);
|
428
|
1072 }
|
|
1073
|
|
1074 DEFUN ("skip-chars-backward", Fskip_chars_backward, 1, 3, 0, /*
|
444
|
1075 Move point backward, stopping after a char not in STRING, or at pos LIMIT.
|
428
|
1076 See `skip-chars-forward' for details.
|
|
1077 Returns the distance traveled, either zero or negative.
|
|
1078
|
|
1079 Optional argument BUFFER defaults to the current buffer.
|
|
1080 */
|
444
|
1081 (string, limit, buffer))
|
428
|
1082 {
|
444
|
1083 return skip_chars (decode_buffer (buffer, 0), 0, 0, string, limit);
|
428
|
1084 }
|
|
1085
|
|
1086
|
|
1087 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, 1, 3, 0, /*
|
|
1088 Move point forward across chars in specified syntax classes.
|
|
1089 SYNTAX is a string of syntax code characters.
|
444
|
1090 Stop before a char whose syntax is not in SYNTAX, or at position LIMIT.
|
428
|
1091 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
|
|
1092 This function returns the distance traveled, either zero or positive.
|
|
1093
|
|
1094 Optional argument BUFFER defaults to the current buffer.
|
|
1095 */
|
444
|
1096 (syntax, limit, buffer))
|
428
|
1097 {
|
444
|
1098 return skip_chars (decode_buffer (buffer, 0), 1, 1, syntax, limit);
|
428
|
1099 }
|
|
1100
|
|
1101 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, 1, 3, 0, /*
|
|
1102 Move point backward across chars in specified syntax classes.
|
|
1103 SYNTAX is a string of syntax code characters.
|
444
|
1104 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIMIT.
|
428
|
1105 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.
|
|
1106 This function returns the distance traveled, either zero or negative.
|
|
1107
|
|
1108 Optional argument BUFFER defaults to the current buffer.
|
|
1109 */
|
444
|
1110 (syntax, limit, buffer))
|
428
|
1111 {
|
444
|
1112 return skip_chars (decode_buffer (buffer, 0), 0, 1, syntax, limit);
|
428
|
1113 }
|
|
1114
|
|
1115
|
|
1116 /* Subroutines of Lisp buffer search functions. */
|
|
1117
|
|
1118 static Lisp_Object
|
444
|
1119 search_command (Lisp_Object string, Lisp_Object limit, Lisp_Object noerror,
|
428
|
1120 Lisp_Object count, Lisp_Object buffer, int direction,
|
|
1121 int RE, int posix)
|
|
1122 {
|
665
|
1123 REGISTER Charbpos np;
|
|
1124 Charbpos lim;
|
428
|
1125 EMACS_INT n = direction;
|
|
1126 struct buffer *buf;
|
|
1127
|
|
1128 if (!NILP (count))
|
|
1129 {
|
|
1130 CHECK_INT (count);
|
|
1131 n *= XINT (count);
|
|
1132 }
|
|
1133
|
|
1134 buf = decode_buffer (buffer, 0);
|
|
1135 CHECK_STRING (string);
|
444
|
1136 if (NILP (limit))
|
428
|
1137 lim = n > 0 ? BUF_ZV (buf) : BUF_BEGV (buf);
|
|
1138 else
|
|
1139 {
|
444
|
1140 CHECK_INT_COERCE_MARKER (limit);
|
|
1141 lim = XINT (limit);
|
428
|
1142 if (n > 0 ? lim < BUF_PT (buf) : lim > BUF_PT (buf))
|
563
|
1143 invalid_argument ("Invalid search limit (wrong side of point)",
|
|
1144 Qunbound);
|
428
|
1145 if (lim > BUF_ZV (buf))
|
|
1146 lim = BUF_ZV (buf);
|
|
1147 if (lim < BUF_BEGV (buf))
|
|
1148 lim = BUF_BEGV (buf);
|
|
1149 }
|
|
1150
|
|
1151 np = search_buffer (buf, string, BUF_PT (buf), lim, n, RE,
|
|
1152 (!NILP (buf->case_fold_search)
|
446
|
1153 ? XCASE_TABLE_CANON (buf->case_table)
|
|
1154 : Qnil),
|
428
|
1155 (!NILP (buf->case_fold_search)
|
446
|
1156 ? XCASE_TABLE_EQV (buf->case_table)
|
|
1157 : Qnil), posix);
|
428
|
1158
|
|
1159 if (np <= 0)
|
|
1160 {
|
444
|
1161 if (NILP (noerror))
|
428
|
1162 return signal_failure (string);
|
444
|
1163 if (!EQ (noerror, Qt))
|
428
|
1164 {
|
|
1165 if (lim < BUF_BEGV (buf) || lim > BUF_ZV (buf))
|
|
1166 abort ();
|
|
1167 BUF_SET_PT (buf, lim);
|
|
1168 return Qnil;
|
|
1169 #if 0 /* This would be clean, but maybe programs depend on
|
|
1170 a value of nil here. */
|
|
1171 np = lim;
|
|
1172 #endif
|
|
1173 }
|
|
1174 else
|
|
1175 return Qnil;
|
|
1176 }
|
|
1177
|
|
1178 if (np < BUF_BEGV (buf) || np > BUF_ZV (buf))
|
|
1179 abort ();
|
|
1180
|
|
1181 BUF_SET_PT (buf, np);
|
|
1182
|
|
1183 return make_int (np);
|
|
1184 }
|
|
1185
|
|
1186 static int
|
|
1187 trivial_regexp_p (Lisp_Object regexp)
|
|
1188 {
|
|
1189 Bytecount len = XSTRING_LENGTH (regexp);
|
867
|
1190 Ibyte *s = XSTRING_DATA (regexp);
|
428
|
1191 while (--len >= 0)
|
|
1192 {
|
|
1193 switch (*s++)
|
|
1194 {
|
1724
|
1195 /* #### howcum ']' doesn't appear here, but ... */
|
428
|
1196 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
|
|
1197 return 0;
|
|
1198 case '\\':
|
|
1199 if (--len < 0)
|
|
1200 return 0;
|
|
1201 switch (*s++)
|
|
1202 {
|
1724
|
1203 /* ... ')' does appear here? ('<' and '>' can appear singly.) */
|
|
1204 /* #### are there other constructs to check? */
|
428
|
1205 case '|': case '(': case ')': case '`': case '\'': case 'b':
|
|
1206 case 'B': case '<': case '>': case 'w': case 'W': case 's':
|
1724
|
1207 case 'S': case '=': case '{': case '}':
|
428
|
1208 #ifdef MULE
|
|
1209 /* 97/2/25 jhod Added for category matches */
|
|
1210 case 'c': case 'C':
|
|
1211 #endif /* MULE */
|
|
1212 case '1': case '2': case '3': case '4': case '5':
|
|
1213 case '6': case '7': case '8': case '9':
|
|
1214 return 0;
|
|
1215 }
|
|
1216 }
|
|
1217 }
|
|
1218 return 1;
|
|
1219 }
|
|
1220
|
|
1221 /* Search for the n'th occurrence of STRING in BUF,
|
665
|
1222 starting at position CHARBPOS and stopping at position BUFLIM,
|
428
|
1223 treating PAT as a literal string if RE is false or as
|
|
1224 a regular expression if RE is true.
|
|
1225
|
|
1226 If N is positive, searching is forward and BUFLIM must be greater
|
665
|
1227 than CHARBPOS.
|
428
|
1228 If N is negative, searching is backward and BUFLIM must be less
|
665
|
1229 than CHARBPOS.
|
428
|
1230
|
|
1231 Returns -x if only N-x occurrences found (x > 0),
|
|
1232 or else the position at the beginning of the Nth occurrence
|
|
1233 (if searching backward) or the end (if searching forward).
|
|
1234
|
|
1235 POSIX is nonzero if we want full backtracking (POSIX style)
|
|
1236 for this pattern. 0 means backtrack only enough to get a valid match. */
|
665
|
1237 static Charbpos
|
|
1238 search_buffer (struct buffer *buf, Lisp_Object string, Charbpos charbpos,
|
|
1239 Charbpos buflim, EMACS_INT n, int RE, Lisp_Object trt,
|
446
|
1240 Lisp_Object inverse_trt, int posix)
|
428
|
1241 {
|
|
1242 Bytecount len = XSTRING_LENGTH (string);
|
867
|
1243 Ibyte *base_pat = XSTRING_DATA (string);
|
428
|
1244 REGISTER EMACS_INT i, j;
|
665
|
1245 Bytebpos p1, p2;
|
428
|
1246 Bytecount s1, s2;
|
665
|
1247 Bytebpos pos, lim;
|
428
|
1248
|
853
|
1249 /* Some FSF junk with running_asynch_code, to preserve the match
|
|
1250 data. Not necessary because we don't call process filters
|
|
1251 asynchronously (i.e. from within QUIT). */
|
428
|
1252
|
1425
|
1253 /* Searching 0 times means noop---don't move, don't touch registers. */
|
|
1254 if (n == 0)
|
|
1255 return charbpos;
|
|
1256
|
428
|
1257 /* Null string is found at starting position. */
|
|
1258 if (len == 0)
|
|
1259 {
|
665
|
1260 set_search_regs (buf, charbpos, 0);
|
|
1261 return charbpos;
|
428
|
1262 }
|
|
1263
|
665
|
1264 pos = charbpos_to_bytebpos (buf, charbpos);
|
|
1265 lim = charbpos_to_bytebpos (buf, buflim);
|
428
|
1266 if (RE && !trivial_regexp_p (string))
|
|
1267 {
|
|
1268 struct re_pattern_buffer *bufp;
|
826
|
1269
|
|
1270 bufp = compile_pattern (string, &search_regs, trt,
|
|
1271 wrap_buffer (buf), buf, posix, ERROR_ME);
|
428
|
1272
|
|
1273 /* Get pointers and sizes of the two strings
|
|
1274 that make up the visible portion of the buffer. */
|
|
1275
|
826
|
1276 p1 = BYTE_BUF_BEGV (buf);
|
|
1277 p2 = BYTE_BUF_CEILING_OF (buf, p1);
|
428
|
1278 s1 = p2 - p1;
|
826
|
1279 s2 = BYTE_BUF_ZV (buf) - p2;
|
|
1280
|
|
1281 while (n != 0)
|
428
|
1282 {
|
|
1283 Bytecount val;
|
826
|
1284 struct syntax_cache scache_struct;
|
|
1285 struct syntax_cache *scache = &scache_struct;
|
|
1286
|
428
|
1287 QUIT;
|
826
|
1288 /* By making the regex object, regex buffer, and syntax cache
|
|
1289 arguments to re_{search,match}{,_2}, we've removed the need to
|
|
1290 do nasty things to deal with regex reentrancy. (See stack
|
|
1291 trace in signal.c for proof that this can happen.)
|
|
1292
|
|
1293 #### there is still a potential problem with the regex cache --
|
|
1294 the compiled regex could be overwritten. we'd need 20-fold
|
|
1295 reentrancy, though. Fix this. */
|
|
1296
|
428
|
1297 val = re_search_2 (bufp,
|
826
|
1298 (char *) BYTE_BUF_BYTE_ADDRESS (buf, p1), s1,
|
|
1299 (char *) BYTE_BUF_BYTE_ADDRESS (buf, p2), s2,
|
|
1300 pos - BYTE_BUF_BEGV (buf), lim - pos, &search_regs,
|
|
1301 n > 0 ? lim - BYTE_BUF_BEGV (buf) :
|
|
1302 pos - BYTE_BUF_BEGV (buf), wrap_buffer (buf),
|
|
1303 buf, scache);
|
428
|
1304
|
|
1305 if (val == -2)
|
|
1306 {
|
|
1307 matcher_overflow ();
|
|
1308 }
|
|
1309 if (val >= 0)
|
|
1310 {
|
|
1311 int num_regs = search_regs.num_regs;
|
826
|
1312 j = BYTE_BUF_BEGV (buf);
|
428
|
1313 for (i = 0; i < num_regs; i++)
|
|
1314 if (search_regs.start[i] >= 0)
|
|
1315 {
|
|
1316 search_regs.start[i] += j;
|
|
1317 search_regs.end[i] += j;
|
|
1318 }
|
793
|
1319 last_thing_searched = wrap_buffer (buf);
|
428
|
1320 /* Set pos to the new position. */
|
826
|
1321 pos = n > 0 ? search_regs.end[0] : search_regs.start[0];
|
428
|
1322 fixup_search_regs_for_buffer (buf);
|
665
|
1323 /* And charbpos too. */
|
826
|
1324 charbpos = n > 0 ? search_regs.end[0] : search_regs.start[0];
|
428
|
1325 }
|
|
1326 else
|
826
|
1327 return (n > 0 ? 0 - n : n);
|
|
1328 if (n > 0) n--; else n++;
|
428
|
1329 }
|
665
|
1330 return charbpos;
|
428
|
1331 }
|
|
1332 else /* non-RE case */
|
|
1333 {
|
446
|
1334 int charset_base = -1;
|
|
1335 int boyer_moore_ok = 1;
|
867
|
1336 Ibyte *pat = 0;
|
|
1337 Ibyte *patbuf = alloca_array (Ibyte, len * MAX_ICHAR_LEN);
|
446
|
1338 pat = patbuf;
|
|
1339 #ifdef MULE
|
826
|
1340 /* &&#### needs some 8-bit work here */
|
446
|
1341 while (len > 0)
|
|
1342 {
|
867
|
1343 Ibyte tmp_str[MAX_ICHAR_LEN];
|
|
1344 Ichar c, translated, inverse;
|
446
|
1345 Bytecount orig_bytelen, new_bytelen, inv_bytelen;
|
|
1346
|
|
1347 /* If we got here and the RE flag is set, it's because
|
|
1348 we're dealing with a regexp known to be trivial, so the
|
|
1349 backslash just quotes the next character. */
|
|
1350 if (RE && *base_pat == '\\')
|
|
1351 {
|
|
1352 len--;
|
|
1353 base_pat++;
|
|
1354 }
|
867
|
1355 c = itext_ichar (base_pat);
|
446
|
1356 translated = TRANSLATE (trt, c);
|
|
1357 inverse = TRANSLATE (inverse_trt, c);
|
|
1358
|
867
|
1359 orig_bytelen = itext_ichar_len (base_pat);
|
|
1360 inv_bytelen = set_itext_ichar (tmp_str, inverse);
|
|
1361 new_bytelen = set_itext_ichar (tmp_str, translated);
|
446
|
1362
|
|
1363 if (new_bytelen != orig_bytelen || inv_bytelen != orig_bytelen)
|
|
1364 boyer_moore_ok = 0;
|
|
1365 if (translated != c || inverse != c)
|
|
1366 {
|
|
1367 /* Keep track of which character set row
|
|
1368 contains the characters that need translation. */
|
867
|
1369 int charset_base_code = c & ~ICHAR_FIELD3_MASK;
|
446
|
1370 if (charset_base == -1)
|
|
1371 charset_base = charset_base_code;
|
|
1372 else if (charset_base != charset_base_code)
|
|
1373 /* If two different rows appear, needing translation,
|
|
1374 then we cannot use boyer_moore search. */
|
|
1375 boyer_moore_ok = 0;
|
|
1376 }
|
|
1377 memcpy (pat, tmp_str, new_bytelen);
|
|
1378 pat += new_bytelen;
|
|
1379 base_pat += orig_bytelen;
|
|
1380 len -= orig_bytelen;
|
|
1381 }
|
|
1382 #else /* not MULE */
|
|
1383 while (--len >= 0)
|
|
1384 {
|
|
1385 /* If we got here and the RE flag is set, it's because
|
|
1386 we're dealing with a regexp known to be trivial, so the
|
|
1387 backslash just quotes the next character. */
|
|
1388 if (RE && *base_pat == '\\')
|
|
1389 {
|
|
1390 len--;
|
|
1391 base_pat++;
|
|
1392 }
|
|
1393 *pat++ = TRANSLATE (trt, *base_pat++);
|
|
1394 }
|
|
1395 #endif /* MULE */
|
|
1396 len = pat - patbuf;
|
|
1397 pat = base_pat = patbuf;
|
|
1398 if (boyer_moore_ok)
|
|
1399 return boyer_moore (buf, base_pat, len, pos, lim, n,
|
|
1400 trt, inverse_trt, charset_base);
|
|
1401 else
|
|
1402 return simple_search (buf, base_pat, len, pos, lim, n, trt);
|
|
1403 }
|
|
1404 }
|
|
1405
|
826
|
1406 /* Do a simple string search N times for the string PAT, whose length is
|
|
1407 LEN/LEN_BYTE, from buffer position POS until LIM. TRT is the
|
|
1408 translation table.
|
446
|
1409
|
|
1410 Return the character position where the match is found.
|
|
1411 Otherwise, if M matches remained to be found, return -M.
|
|
1412
|
|
1413 This kind of search works regardless of what is in PAT and
|
|
1414 regardless of what is in TRT. It is used in cases where
|
|
1415 boyer_moore cannot work. */
|
|
1416
|
665
|
1417 static Charbpos
|
867
|
1418 simple_search (struct buffer *buf, Ibyte *base_pat, Bytecount len,
|
826
|
1419 Bytebpos pos, Bytebpos lim, EMACS_INT n, Lisp_Object trt)
|
446
|
1420 {
|
|
1421 int forward = n > 0;
|
|
1422 Bytecount buf_len = 0; /* Shut up compiler. */
|
|
1423
|
826
|
1424 if (lim > pos)
|
446
|
1425 while (n > 0)
|
428
|
1426 {
|
446
|
1427 while (1)
|
428
|
1428 {
|
826
|
1429 Bytecount this_len = len;
|
|
1430 Bytebpos this_pos = pos;
|
867
|
1431 Ibyte *p = base_pat;
|
826
|
1432 if (pos >= lim)
|
446
|
1433 goto stop;
|
|
1434
|
|
1435 while (this_len > 0)
|
|
1436 {
|
867
|
1437 Ichar pat_ch, buf_ch;
|
446
|
1438 Bytecount pat_len;
|
|
1439
|
867
|
1440 pat_ch = itext_ichar (p);
|
826
|
1441 buf_ch = BYTE_BUF_FETCH_CHAR (buf, this_pos);
|
446
|
1442
|
|
1443 buf_ch = TRANSLATE (trt, buf_ch);
|
|
1444
|
|
1445 if (buf_ch != pat_ch)
|
|
1446 break;
|
|
1447
|
867
|
1448 pat_len = itext_ichar_len (p);
|
446
|
1449 p += pat_len;
|
|
1450 this_len -= pat_len;
|
826
|
1451 INC_BYTEBPOS (buf, this_pos);
|
446
|
1452 }
|
|
1453 if (this_len == 0)
|
428
|
1454 {
|
826
|
1455 buf_len = this_pos - pos;
|
|
1456 pos = this_pos;
|
446
|
1457 break;
|
428
|
1458 }
|
826
|
1459 INC_BYTEBPOS (buf, pos);
|
428
|
1460 }
|
446
|
1461 n--;
|
|
1462 }
|
|
1463 else
|
|
1464 while (n < 0)
|
|
1465 {
|
|
1466 while (1)
|
|
1467 {
|
826
|
1468 Bytecount this_len = len;
|
|
1469 Bytebpos this_pos = pos;
|
867
|
1470 Ibyte *p;
|
826
|
1471 if (pos <= lim)
|
446
|
1472 goto stop;
|
826
|
1473 p = base_pat + len;
|
446
|
1474
|
|
1475 while (this_len > 0)
|
|
1476 {
|
867
|
1477 Ichar pat_ch, buf_ch;
|
|
1478
|
|
1479 DEC_IBYTEPTR (p);
|
826
|
1480 DEC_BYTEBPOS (buf, this_pos);
|
867
|
1481 pat_ch = itext_ichar (p);
|
826
|
1482 buf_ch = BYTE_BUF_FETCH_CHAR (buf, this_pos);
|
446
|
1483
|
|
1484 buf_ch = TRANSLATE (trt, buf_ch);
|
|
1485
|
|
1486 if (buf_ch != pat_ch)
|
|
1487 break;
|
|
1488
|
867
|
1489 this_len -= itext_ichar_len (p);
|
446
|
1490 }
|
|
1491 if (this_len == 0)
|
|
1492 {
|
826
|
1493 buf_len = pos - this_pos;
|
|
1494 pos = this_pos;
|
446
|
1495 break;
|
|
1496 }
|
826
|
1497 DEC_BYTEBPOS (buf, pos);
|
446
|
1498 }
|
|
1499 n++;
|
428
|
1500 }
|
446
|
1501 stop:
|
|
1502 if (n == 0)
|
|
1503 {
|
665
|
1504 Charbpos beg, end, retval;
|
446
|
1505 if (forward)
|
|
1506 {
|
826
|
1507 beg = bytebpos_to_charbpos (buf, pos - buf_len);
|
|
1508 retval = end = bytebpos_to_charbpos (buf, pos);
|
446
|
1509 }
|
|
1510 else
|
428
|
1511 {
|
826
|
1512 retval = beg = bytebpos_to_charbpos (buf, pos);
|
|
1513 end = bytebpos_to_charbpos (buf, pos + buf_len);
|
428
|
1514 }
|
446
|
1515 set_search_regs (buf, beg, end - beg);
|
|
1516
|
|
1517 return retval;
|
|
1518 }
|
|
1519 else if (n > 0)
|
|
1520 return -n;
|
|
1521 else
|
|
1522 return n;
|
|
1523 }
|
|
1524
|
|
1525 /* Do Boyer-Moore search N times for the string PAT,
|
|
1526 whose length is LEN/LEN_BYTE,
|
|
1527 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
|
|
1528 DIRECTION says which direction we search in.
|
|
1529 TRT and INVERSE_TRT are translation tables.
|
|
1530
|
|
1531 This kind of search works if all the characters in PAT that have
|
|
1532 nontrivial translation are the same aside from the last byte. This
|
|
1533 makes it possible to translate just the last byte of a character,
|
|
1534 and do so after just a simple test of the context.
|
|
1535
|
|
1536 If that criterion is not satisfied, do not call this function. */
|
|
1537
|
665
|
1538 static Charbpos
|
867
|
1539 boyer_moore (struct buffer *buf, Ibyte *base_pat, Bytecount len,
|
665
|
1540 Bytebpos pos, Bytebpos lim, EMACS_INT n, Lisp_Object trt,
|
446
|
1541 Lisp_Object inverse_trt, int charset_base)
|
|
1542 {
|
826
|
1543 /* &&#### needs some 8-bit work here */
|
446
|
1544 /* #### Someone really really really needs to comment the workings
|
|
1545 of this junk somewhat better.
|
|
1546
|
|
1547 BTW "BM" stands for Boyer-Moore, which is one of the standard
|
|
1548 string-searching algorithms. It's the best string-searching
|
|
1549 algorithm out there, provided that:
|
|
1550
|
|
1551 a) You're not fazed by algorithm complexity. (Rabin-Karp, which
|
|
1552 uses hashing, is much much easier to code but not as fast.)
|
|
1553 b) You can freely move backwards in the string that you're
|
|
1554 searching through.
|
|
1555
|
|
1556 As the comment below tries to explain (but garbles in typical
|
|
1557 programmer-ese), the idea is that you don't have to do a
|
|
1558 string match at every successive position in the text. For
|
|
1559 example, let's say the pattern is "a very long string". We
|
|
1560 compare the last character in the string (`g') with the
|
|
1561 corresponding character in the text. If it mismatches, and
|
|
1562 it is, say, `z', then we can skip forward by the entire
|
|
1563 length of the pattern because `z' does not occur anywhere
|
|
1564 in the pattern. If the mismatching character does occur
|
|
1565 in the pattern, we can usually still skip forward by more
|
|
1566 than one: e.g. if it is `l', then we can skip forward
|
|
1567 by the length of the substring "ong string" -- i.e. the
|
|
1568 largest end section of the pattern that does not contain
|
|
1569 the mismatched character. So what we do is compute, for
|
|
1570 each possible character, the distance we can skip forward
|
|
1571 (the "stride") and use it in the string matching. This
|
|
1572 is what the BM_tab holds. */
|
|
1573 REGISTER EMACS_INT *BM_tab;
|
|
1574 EMACS_INT *BM_tab_base;
|
|
1575 REGISTER Bytecount dirlen;
|
|
1576 EMACS_INT infinity;
|
665
|
1577 Bytebpos limit;
|
446
|
1578 Bytecount stride_for_teases = 0;
|
|
1579 REGISTER EMACS_INT i, j;
|
867
|
1580 Ibyte *pat, *pat_end;
|
|
1581 REGISTER Ibyte *cursor, *p_limit, *ptr2;
|
|
1582 Ibyte simple_translate[0400];
|
446
|
1583 REGISTER int direction = ((n > 0) ? 1 : -1);
|
|
1584 #ifdef MULE
|
867
|
1585 Ibyte translate_prev_byte = 0;
|
|
1586 Ibyte translate_anteprev_byte = 0;
|
446
|
1587 #endif
|
|
1588 #ifdef C_ALLOCA
|
|
1589 EMACS_INT BM_tab_space[0400];
|
|
1590 BM_tab = &BM_tab_space[0];
|
|
1591 #else
|
|
1592 BM_tab = alloca_array (EMACS_INT, 256);
|
|
1593 #endif
|
|
1594
|
|
1595 /* The general approach is that we are going to maintain that we
|
|
1596 know the first (closest to the present position, in whatever
|
|
1597 direction we're searching) character that could possibly be
|
|
1598 the last (furthest from present position) character of a
|
|
1599 valid match. We advance the state of our knowledge by
|
|
1600 looking at that character and seeing whether it indeed
|
|
1601 matches the last character of the pattern. If it does, we
|
|
1602 take a closer look. If it does not, we move our pointer (to
|
|
1603 putative last characters) as far as is logically possible.
|
|
1604 This amount of movement, which I call a stride, will be the
|
|
1605 length of the pattern if the actual character appears nowhere
|
|
1606 in the pattern, otherwise it will be the distance from the
|
|
1607 last occurrence of that character to the end of the pattern.
|
|
1608 As a coding trick, an enormous stride is coded into the table
|
|
1609 for characters that match the last character. This allows
|
|
1610 use of only a single test, a test for having gone past the
|
|
1611 end of the permissible match region, to test for both
|
|
1612 possible matches (when the stride goes past the end
|
|
1613 immediately) and failure to match (where you get nudged past
|
|
1614 the end one stride at a time).
|
|
1615
|
|
1616 Here we make a "mickey mouse" BM table. The stride of the
|
|
1617 search is determined only by the last character of the
|
|
1618 putative match. If that character does not match, we will
|
|
1619 stride the proper distance to propose a match that
|
|
1620 superimposes it on the last instance of a character that
|
|
1621 matches it (per trt), or misses it entirely if there is
|
|
1622 none. */
|
|
1623
|
|
1624 dirlen = len * direction;
|
|
1625 infinity = dirlen - (lim + pos + len + len) * direction;
|
|
1626 /* Record position after the end of the pattern. */
|
|
1627 pat_end = base_pat + len;
|
|
1628 if (direction < 0)
|
|
1629 base_pat = pat_end - 1;
|
|
1630 BM_tab_base = BM_tab;
|
|
1631 BM_tab += 0400;
|
|
1632 j = dirlen; /* to get it in a register */
|
|
1633 /* A character that does not appear in the pattern induces a
|
|
1634 stride equal to the pattern length. */
|
|
1635 while (BM_tab_base != BM_tab)
|
|
1636 {
|
|
1637 *--BM_tab = j;
|
|
1638 *--BM_tab = j;
|
|
1639 *--BM_tab = j;
|
|
1640 *--BM_tab = j;
|
|
1641 }
|
|
1642 /* We use this for translation, instead of TRT itself. We
|
|
1643 fill this in to handle the characters that actually occur
|
|
1644 in the pattern. Others don't matter anyway! */
|
|
1645 xzero (simple_translate);
|
|
1646 for (i = 0; i < 0400; i++)
|
867
|
1647 simple_translate[i] = (Ibyte) i;
|
446
|
1648 i = 0;
|
1425
|
1649
|
446
|
1650 while (i != infinity)
|
|
1651 {
|
867
|
1652 Ibyte *ptr = base_pat + i;
|
446
|
1653 i += direction;
|
|
1654 if (i == dirlen)
|
|
1655 i = infinity;
|
|
1656 if (!NILP (trt))
|
428
|
1657 {
|
446
|
1658 #ifdef MULE
|
867
|
1659 Ichar ch, untranslated;
|
446
|
1660 int this_translated = 1;
|
|
1661
|
|
1662 /* Is *PTR the last byte of a character? */
|
867
|
1663 if (pat_end - ptr == 1 || ibyte_first_byte_p (ptr[1]))
|
428
|
1664 {
|
867
|
1665 Ibyte *charstart = ptr;
|
|
1666 while (!ibyte_first_byte_p (*charstart))
|
446
|
1667 charstart--;
|
867
|
1668 untranslated = itext_ichar (charstart);
|
|
1669 if (charset_base == (untranslated & ~ICHAR_FIELD3_MASK))
|
446
|
1670 {
|
|
1671 ch = TRANSLATE (trt, untranslated);
|
867
|
1672 if (!ibyte_first_byte_p (*ptr))
|
446
|
1673 {
|
|
1674 translate_prev_byte = ptr[-1];
|
867
|
1675 if (!ibyte_first_byte_p (translate_prev_byte))
|
446
|
1676 translate_anteprev_byte = ptr[-2];
|
|
1677 }
|
|
1678 }
|
|
1679 else
|
|
1680 {
|
|
1681 this_translated = 0;
|
|
1682 ch = *ptr;
|
|
1683 }
|
428
|
1684 }
|
|
1685 else
|
|
1686 {
|
446
|
1687 ch = *ptr;
|
|
1688 this_translated = 0;
|
|
1689 }
|
|
1690 if (ch > 0400)
|
|
1691 j = ((unsigned char) ch | 0200);
|
|
1692 else
|
|
1693 j = (unsigned char) ch;
|
|
1694
|
|
1695 if (i == infinity)
|
|
1696 stride_for_teases = BM_tab[j];
|
|
1697 BM_tab[j] = dirlen - i;
|
|
1698 /* A translation table is accompanied by its inverse --
|
826
|
1699 see comment in casetab.c. */
|
446
|
1700 if (this_translated)
|
|
1701 {
|
867
|
1702 Ichar starting_ch = ch;
|
446
|
1703 EMACS_INT starting_j = j;
|
|
1704 while (1)
|
|
1705 {
|
|
1706 ch = TRANSLATE (inverse_trt, ch);
|
|
1707 if (ch > 0400)
|
|
1708 j = ((unsigned char) ch | 0200);
|
|
1709 else
|
|
1710 j = (unsigned char) ch;
|
|
1711
|
|
1712 /* For all the characters that map into CH,
|
|
1713 set up simple_translate to map the last byte
|
|
1714 into STARTING_J. */
|
867
|
1715 simple_translate[j] = (Ibyte) starting_j;
|
446
|
1716 if (ch == starting_ch)
|
|
1717 break;
|
|
1718 BM_tab[j] = dirlen - i;
|
|
1719 }
|
|
1720 }
|
|
1721 #else
|
|
1722 EMACS_INT k;
|
|
1723 j = *ptr;
|
|
1724 k = (j = TRANSLATE (trt, j));
|
|
1725 if (i == infinity)
|
|
1726 stride_for_teases = BM_tab[j];
|
|
1727 BM_tab[j] = dirlen - i;
|
|
1728 /* A translation table is accompanied by its inverse --
|
826
|
1729 see comment in casetab.c. */
|
446
|
1730 while ((j = TRANSLATE (inverse_trt, j)) != k)
|
|
1731 {
|
867
|
1732 simple_translate[j] = (Ibyte) k;
|
428
|
1733 BM_tab[j] = dirlen - i;
|
|
1734 }
|
446
|
1735 #endif
|
|
1736 }
|
|
1737 else
|
|
1738 {
|
|
1739 j = *ptr;
|
|
1740
|
|
1741 if (i == infinity)
|
|
1742 stride_for_teases = BM_tab[j];
|
|
1743 BM_tab[j] = dirlen - i;
|
428
|
1744 }
|
446
|
1745 /* stride_for_teases tells how much to stride if we get a
|
|
1746 match on the far character but are subsequently
|
|
1747 disappointed, by recording what the stride would have been
|
|
1748 for that character if the last character had been
|
|
1749 different. */
|
|
1750 }
|
|
1751 infinity = dirlen - infinity;
|
|
1752 pos += dirlen - ((direction > 0) ? direction : 0);
|
|
1753 /* loop invariant - pos points at where last char (first char if
|
|
1754 reverse) of pattern would align in a possible match. */
|
|
1755 while (n != 0)
|
|
1756 {
|
665
|
1757 Bytebpos tail_end;
|
867
|
1758 Ibyte *tail_end_ptr;
|
446
|
1759 /* It's been reported that some (broken) compiler thinks
|
|
1760 that Boolean expressions in an arithmetic context are
|
|
1761 unsigned. Using an explicit ?1:0 prevents this. */
|
|
1762 if ((lim - pos - ((direction > 0) ? 1 : 0)) * direction < 0)
|
|
1763 return n * (0 - direction);
|
|
1764 /* First we do the part we can by pointers (maybe
|
|
1765 nothing) */
|
|
1766 QUIT;
|
|
1767 pat = base_pat;
|
|
1768 limit = pos - dirlen + direction;
|
|
1769 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF
|
|
1770 have changed. See buffer.h. */
|
|
1771 limit = ((direction > 0)
|
826
|
1772 ? BYTE_BUF_CEILING_OF (buf, limit) - 1
|
|
1773 : BYTE_BUF_FLOOR_OF (buf, limit + 1));
|
446
|
1774 /* LIMIT is now the last (not beyond-last!) value POS can
|
|
1775 take on without hitting edge of buffer or the gap. */
|
|
1776 limit = ((direction > 0)
|
|
1777 ? min (lim - 1, min (limit, pos + 20000))
|
|
1778 : max (lim, max (limit, pos - 20000)));
|
826
|
1779 tail_end = BYTE_BUF_CEILING_OF (buf, pos);
|
|
1780 tail_end_ptr = BYTE_BUF_BYTE_ADDRESS (buf, tail_end);
|
446
|
1781
|
|
1782 if ((limit - pos) * direction > 20)
|
428
|
1783 {
|
826
|
1784 /* We have to be careful because the code can generate addresses
|
|
1785 that don't point to the beginning of characters. */
|
|
1786 p_limit = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, limit);
|
|
1787 ptr2 = (cursor = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos));
|
446
|
1788 /* In this loop, pos + cursor - ptr2 is the surrogate
|
|
1789 for pos */
|
|
1790 while (1) /* use one cursor setting as long as i can */
|
|
1791 {
|
|
1792 if (direction > 0) /* worth duplicating */
|
|
1793 {
|
|
1794 /* Use signed comparison if appropriate to make
|
|
1795 cursor+infinity sure to be > p_limit.
|
|
1796 Assuming that the buffer lies in a range of
|
|
1797 addresses that are all "positive" (as ints)
|
|
1798 or all "negative", either kind of comparison
|
|
1799 will work as long as we don't step by
|
|
1800 infinity. So pick the kind that works when
|
|
1801 we do step by infinity. */
|
|
1802 if ((EMACS_INT) (p_limit + infinity) >
|
|
1803 (EMACS_INT) p_limit)
|
|
1804 while ((EMACS_INT) cursor <=
|
|
1805 (EMACS_INT) p_limit)
|
|
1806 cursor += BM_tab[*cursor];
|
|
1807 else
|
|
1808 while ((EMACS_UINT) cursor <=
|
|
1809 (EMACS_UINT) p_limit)
|
|
1810 cursor += BM_tab[*cursor];
|
|
1811 }
|
|
1812 else
|
|
1813 {
|
|
1814 if ((EMACS_INT) (p_limit + infinity) <
|
|
1815 (EMACS_INT) p_limit)
|
|
1816 while ((EMACS_INT) cursor >=
|
|
1817 (EMACS_INT) p_limit)
|
|
1818 cursor += BM_tab[*cursor];
|
|
1819 else
|
|
1820 while ((EMACS_UINT) cursor >=
|
|
1821 (EMACS_UINT) p_limit)
|
|
1822 cursor += BM_tab[*cursor];
|
|
1823 }
|
|
1824 /* If you are here, cursor is beyond the end of the
|
|
1825 searched region. This can happen if you match on
|
|
1826 the far character of the pattern, because the
|
|
1827 "stride" of that character is infinity, a number
|
|
1828 able to throw you well beyond the end of the
|
|
1829 search. It can also happen if you fail to match
|
|
1830 within the permitted region and would otherwise
|
|
1831 try a character beyond that region */
|
|
1832 if ((cursor - p_limit) * direction <= len)
|
|
1833 break; /* a small overrun is genuine */
|
|
1834 cursor -= infinity; /* large overrun = hit */
|
|
1835 i = dirlen - direction;
|
|
1836 if (!NILP (trt))
|
|
1837 {
|
|
1838 while ((i -= direction) + direction != 0)
|
|
1839 {
|
|
1840 #ifdef MULE
|
867
|
1841 Ichar ch;
|
446
|
1842 cursor -= direction;
|
|
1843 /* Translate only the last byte of a character. */
|
|
1844 if ((cursor == tail_end_ptr
|
867
|
1845 || ibyte_first_byte_p (cursor[1]))
|
|
1846 && (ibyte_first_byte_p (cursor[0])
|
446
|
1847 || (translate_prev_byte == cursor[-1]
|
867
|
1848 && (ibyte_first_byte_p (translate_prev_byte)
|
446
|
1849 || translate_anteprev_byte == cursor[-2]))))
|
|
1850 ch = simple_translate[*cursor];
|
|
1851 else
|
|
1852 ch = *cursor;
|
|
1853 if (pat[i] != ch)
|
|
1854 break;
|
|
1855 #else
|
|
1856 if (pat[i] != TRANSLATE (trt, *(cursor -= direction)))
|
|
1857 break;
|
|
1858 #endif
|
|
1859 }
|
|
1860 }
|
|
1861 else
|
|
1862 {
|
|
1863 while ((i -= direction) + direction != 0)
|
|
1864 if (pat[i] != *(cursor -= direction))
|
|
1865 break;
|
|
1866 }
|
|
1867 cursor += dirlen - i - direction; /* fix cursor */
|
|
1868 if (i + direction == 0)
|
|
1869 {
|
|
1870 cursor -= direction;
|
|
1871
|
|
1872 {
|
665
|
1873 Bytebpos bytstart = (pos + cursor - ptr2 +
|
446
|
1874 ((direction > 0)
|
|
1875 ? 1 - len : 0));
|
665
|
1876 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart);
|
|
1877 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len);
|
446
|
1878
|
|
1879 set_search_regs (buf, bufstart, bufend - bufstart);
|
|
1880 }
|
|
1881
|
|
1882 if ((n -= direction) != 0)
|
|
1883 cursor += dirlen; /* to resume search */
|
|
1884 else
|
|
1885 return ((direction > 0)
|
|
1886 ? search_regs.end[0] : search_regs.start[0]);
|
|
1887 }
|
|
1888 else
|
|
1889 cursor += stride_for_teases; /* <sigh> we lose - */
|
|
1890 }
|
|
1891 pos += cursor - ptr2;
|
|
1892 }
|
|
1893 else
|
|
1894 /* Now we'll pick up a clump that has to be done the hard
|
|
1895 way because it covers a discontinuity */
|
|
1896 {
|
428
|
1897 /* XEmacs change: definitions of CEILING_OF and FLOOR_OF
|
|
1898 have changed. See buffer.h. */
|
|
1899 limit = ((direction > 0)
|
826
|
1900 ? BYTE_BUF_CEILING_OF (buf, pos - dirlen + 1) - 1
|
|
1901 : BYTE_BUF_FLOOR_OF (buf, pos - dirlen));
|
428
|
1902 limit = ((direction > 0)
|
446
|
1903 ? min (limit + len, lim - 1)
|
|
1904 : max (limit - len, lim));
|
|
1905 /* LIMIT is now the last value POS can have
|
|
1906 and still be valid for a possible match. */
|
|
1907 while (1)
|
428
|
1908 {
|
446
|
1909 /* This loop can be coded for space rather than
|
|
1910 speed because it will usually run only once.
|
|
1911 (the reach is at most len + 21, and typically
|
|
1912 does not exceed len) */
|
|
1913 while ((limit - pos) * direction >= 0)
|
826
|
1914 /* *not* BYTE_BUF_FETCH_CHAR. We are working here
|
446
|
1915 with bytes, not characters. */
|
826
|
1916 pos += BM_tab[*BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos)];
|
446
|
1917 /* now run the same tests to distinguish going off
|
|
1918 the end, a match or a phony match. */
|
|
1919 if ((pos - limit) * direction <= len)
|
|
1920 break; /* ran off the end */
|
|
1921 /* Found what might be a match.
|
|
1922 Set POS back to last (first if reverse) char pos. */
|
|
1923 pos -= infinity;
|
|
1924 i = dirlen - direction;
|
|
1925 while ((i -= direction) + direction != 0)
|
428
|
1926 {
|
446
|
1927 #ifdef MULE
|
867
|
1928 Ichar ch;
|
|
1929 Ibyte *ptr;
|
446
|
1930 #endif
|
|
1931 pos -= direction;
|
|
1932 #ifdef MULE
|
826
|
1933 ptr = BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos);
|
446
|
1934 if ((ptr == tail_end_ptr
|
867
|
1935 || ibyte_first_byte_p (ptr[1]))
|
|
1936 && (ibyte_first_byte_p (ptr[0])
|
446
|
1937 || (translate_prev_byte == ptr[-1]
|
867
|
1938 && (ibyte_first_byte_p (translate_prev_byte)
|
446
|
1939 || translate_anteprev_byte == ptr[-2]))))
|
|
1940 ch = simple_translate[*ptr];
|
428
|
1941 else
|
446
|
1942 ch = *ptr;
|
|
1943 if (pat[i] != ch)
|
|
1944 break;
|
|
1945
|
|
1946 #else
|
826
|
1947 if (pat[i] !=
|
|
1948 TRANSLATE (trt,
|
|
1949 *BYTE_BUF_BYTE_ADDRESS_NO_VERIFY (buf, pos)))
|
446
|
1950 break;
|
|
1951 #endif
|
428
|
1952 }
|
446
|
1953 /* Above loop has moved POS part or all the way back
|
|
1954 to the first char pos (last char pos if reverse).
|
|
1955 Set it once again at the last (first if reverse)
|
|
1956 char. */
|
|
1957 pos += dirlen - i- direction;
|
|
1958 if (i + direction == 0)
|
428
|
1959 {
|
446
|
1960 pos -= direction;
|
|
1961
|
|
1962 {
|
665
|
1963 Bytebpos bytstart = (pos +
|
446
|
1964 ((direction > 0)
|
|
1965 ? 1 - len : 0));
|
665
|
1966 Charbpos bufstart = bytebpos_to_charbpos (buf, bytstart);
|
|
1967 Charbpos bufend = bytebpos_to_charbpos (buf, bytstart + len);
|
446
|
1968
|
|
1969 set_search_regs (buf, bufstart, bufend - bufstart);
|
|
1970 }
|
|
1971
|
|
1972 if ((n -= direction) != 0)
|
|
1973 pos += dirlen; /* to resume search */
|
428
|
1974 else
|
446
|
1975 return ((direction > 0)
|
|
1976 ? search_regs.end[0] : search_regs.start[0]);
|
428
|
1977 }
|
446
|
1978 else
|
|
1979 pos += stride_for_teases;
|
|
1980 }
|
428
|
1981 }
|
446
|
1982 /* We have done one clump. Can we continue? */
|
|
1983 if ((lim - pos) * direction < 0)
|
|
1984 return (0 - n) * direction;
|
428
|
1985 }
|
665
|
1986 return bytebpos_to_charbpos (buf, pos);
|
428
|
1987 }
|
|
1988
|
1024
|
1989 /* Record the whole-match data (beginning BEG and end BEG + LEN) and the
|
|
1990 buffer for a match just found. */
|
428
|
1991
|
|
1992 static void
|
665
|
1993 set_search_regs (struct buffer *buf, Charbpos beg, Charcount len)
|
428
|
1994 {
|
|
1995 /* Make sure we have registers in which to store
|
|
1996 the match position. */
|
|
1997 if (search_regs.num_regs == 0)
|
|
1998 {
|
|
1999 search_regs.start = xnew (regoff_t);
|
|
2000 search_regs.end = xnew (regoff_t);
|
|
2001 search_regs.num_regs = 1;
|
|
2002 }
|
|
2003
|
1468
|
2004 clear_search_regs ();
|
428
|
2005 search_regs.start[0] = beg;
|
|
2006 search_regs.end[0] = beg + len;
|
793
|
2007 last_thing_searched = wrap_buffer (buf);
|
428
|
2008 }
|
|
2009
|
1468
|
2010 /* Clear search registers so match data will be null. */
|
1024
|
2011
|
|
2012 static void
|
1468
|
2013 clear_search_regs (void)
|
1024
|
2014 {
|
|
2015 /* This function has been Mule-ized. */
|
|
2016 int i;
|
|
2017
|
1468
|
2018 for (i = 0; i < search_regs.num_regs; i++)
|
|
2019 search_regs.start[i] = search_regs.end[i] = -1;
|
1024
|
2020 }
|
|
2021
|
428
|
2022
|
|
2023 /* Given a string of words separated by word delimiters,
|
442
|
2024 compute a regexp that matches those exact words
|
|
2025 separated by arbitrary punctuation. */
|
428
|
2026
|
|
2027 static Lisp_Object
|
|
2028 wordify (Lisp_Object buffer, Lisp_Object string)
|
|
2029 {
|
|
2030 Charcount i, len;
|
|
2031 EMACS_INT punct_count = 0, word_count = 0;
|
|
2032 struct buffer *buf = decode_buffer (buffer, 0);
|
826
|
2033 Lisp_Object syntax_table = buf->mirror_syntax_table;
|
428
|
2034
|
|
2035 CHECK_STRING (string);
|
826
|
2036 len = string_char_length (string);
|
428
|
2037
|
|
2038 for (i = 0; i < len; i++)
|
867
|
2039 if (!WORD_SYNTAX_P (syntax_table, string_ichar (string, i)))
|
428
|
2040 {
|
|
2041 punct_count++;
|
|
2042 if (i > 0 && WORD_SYNTAX_P (syntax_table,
|
867
|
2043 string_ichar (string, i - 1)))
|
428
|
2044 word_count++;
|
|
2045 }
|
867
|
2046 if (WORD_SYNTAX_P (syntax_table, string_ichar (string, len - 1)))
|
428
|
2047 word_count++;
|
|
2048 if (!word_count) return build_string ("");
|
|
2049
|
|
2050 {
|
|
2051 /* The following value is an upper bound on the amount of storage we
|
|
2052 need. In non-Mule, it is exact. */
|
867
|
2053 Ibyte *storage =
|
|
2054 (Ibyte *) ALLOCA (XSTRING_LENGTH (string) - punct_count +
|
428
|
2055 5 * (word_count - 1) + 4);
|
867
|
2056 Ibyte *o = storage;
|
428
|
2057
|
|
2058 *o++ = '\\';
|
|
2059 *o++ = 'b';
|
|
2060
|
|
2061 for (i = 0; i < len; i++)
|
|
2062 {
|
867
|
2063 Ichar ch = string_ichar (string, i);
|
428
|
2064
|
|
2065 if (WORD_SYNTAX_P (syntax_table, ch))
|
867
|
2066 o += set_itext_ichar (o, ch);
|
428
|
2067 else if (i > 0
|
|
2068 && WORD_SYNTAX_P (syntax_table,
|
867
|
2069 string_ichar (string, i - 1))
|
428
|
2070 && --word_count)
|
|
2071 {
|
|
2072 *o++ = '\\';
|
|
2073 *o++ = 'W';
|
|
2074 *o++ = '\\';
|
|
2075 *o++ = 'W';
|
|
2076 *o++ = '*';
|
|
2077 }
|
|
2078 }
|
|
2079
|
|
2080 *o++ = '\\';
|
|
2081 *o++ = 'b';
|
|
2082
|
|
2083 return make_string (storage, o - storage);
|
|
2084 }
|
|
2085 }
|
|
2086
|
|
2087 DEFUN ("search-backward", Fsearch_backward, 1, 5, "sSearch backward: ", /*
|
|
2088 Search backward from point for STRING.
|
|
2089 Set point to the beginning of the occurrence found, and return point.
|
444
|
2090
|
|
2091 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2092 position. The match found must not extend before that position.
|
|
2093 The value nil is equivalent to (point-min).
|
|
2094
|
|
2095 Optional third argument NOERROR, if t, means just return nil (no
|
|
2096 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2097 and return nil.
|
|
2098
|
|
2099 Optional fourth argument COUNT is a repeat count--search for
|
|
2100 successive occurrences.
|
|
2101
|
428
|
2102 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2103 defaults to the current buffer.
|
|
2104
|
1468
|
2105 When the match is successful, this function modifies the match data
|
|
2106 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2107 match data with `match-data' and restore it with `store-match-data' if
|
|
2108 you want to preserve them. If the match fails, the match data from the
|
|
2109 previous success match is preserved.
|
|
2110
|
|
2111 See also the function `replace-match'.
|
428
|
2112 */
|
444
|
2113 (string, limit, noerror, count, buffer))
|
428
|
2114 {
|
444
|
2115 return search_command (string, limit, noerror, count, buffer, -1, 0, 0);
|
428
|
2116 }
|
|
2117
|
|
2118 DEFUN ("search-forward", Fsearch_forward, 1, 5, "sSearch: ", /*
|
|
2119 Search forward from point for STRING.
|
|
2120 Set point to the end of the occurrence found, and return point.
|
444
|
2121
|
|
2122 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2123 position. The match found must not extend after that position. The
|
|
2124 value nil is equivalent to (point-max).
|
|
2125
|
|
2126 Optional third argument NOERROR, if t, means just return nil (no
|
|
2127 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2128 and return nil.
|
|
2129
|
|
2130 Optional fourth argument COUNT is a repeat count--search for
|
|
2131 successive occurrences.
|
|
2132
|
428
|
2133 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2134 defaults to the current buffer.
|
|
2135
|
1468
|
2136 When the match is successful, this function modifies the match data
|
|
2137 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2138 match data with `match-data' and restore it with `store-match-data' if
|
|
2139 you want to preserve them. If the match fails, the match data from the
|
|
2140 previous success match is preserved.
|
|
2141
|
|
2142 See also the function `replace-match'.
|
428
|
2143 */
|
444
|
2144 (string, limit, noerror, count, buffer))
|
428
|
2145 {
|
444
|
2146 return search_command (string, limit, noerror, count, buffer, 1, 0, 0);
|
428
|
2147 }
|
|
2148
|
|
2149 DEFUN ("word-search-backward", Fword_search_backward, 1, 5,
|
|
2150 "sWord search backward: ", /*
|
|
2151 Search backward from point for STRING, ignoring differences in punctuation.
|
|
2152 Set point to the beginning of the occurrence found, and return point.
|
444
|
2153
|
|
2154 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2155 position. The match found must not extend before that position.
|
|
2156 The value nil is equivalent to (point-min).
|
|
2157
|
|
2158 Optional third argument NOERROR, if t, means just return nil (no
|
|
2159 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2160 and return nil.
|
|
2161
|
|
2162 Optional fourth argument COUNT is a repeat count--search for
|
|
2163 successive occurrences.
|
|
2164
|
428
|
2165 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2166 defaults to the current buffer.
|
|
2167
|
1468
|
2168 When the match is successful, this function modifies the match data
|
|
2169 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2170 match data with `match-data' and restore it with `store-match-data' if
|
|
2171 you want to preserve them. If the match fails, the match data from the
|
|
2172 previous success match is preserved.
|
|
2173
|
|
2174 See also the function `replace-match'.
|
428
|
2175 */
|
444
|
2176 (string, limit, noerror, count, buffer))
|
428
|
2177 {
|
444
|
2178 return search_command (wordify (buffer, string), limit, noerror, count,
|
428
|
2179 buffer, -1, 1, 0);
|
|
2180 }
|
|
2181
|
|
2182 DEFUN ("word-search-forward", Fword_search_forward, 1, 5, "sWord search: ", /*
|
|
2183 Search forward from point for STRING, ignoring differences in punctuation.
|
|
2184 Set point to the end of the occurrence found, and return point.
|
444
|
2185
|
|
2186 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2187 position. The match found must not extend after that position. The
|
|
2188 value nil is equivalent to (point-max).
|
|
2189
|
|
2190 Optional third argument NOERROR, if t, means just return nil (no
|
|
2191 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2192 and return nil.
|
|
2193
|
|
2194 Optional fourth argument COUNT is a repeat count--search for
|
|
2195 successive occurrences.
|
|
2196
|
428
|
2197 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2198 defaults to the current buffer.
|
|
2199
|
1468
|
2200 When the match is successful, this function modifies the match data
|
|
2201 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2202 match data with `match-data' and restore it with `store-match-data' if
|
|
2203 you want to preserve them. If the match fails, the match data from the
|
|
2204 previous success match is preserved.
|
|
2205
|
|
2206 See also the function `replace-match'.
|
428
|
2207 */
|
444
|
2208 (string, limit, noerror, count, buffer))
|
428
|
2209 {
|
444
|
2210 return search_command (wordify (buffer, string), limit, noerror, count,
|
428
|
2211 buffer, 1, 1, 0);
|
|
2212 }
|
|
2213
|
|
2214 DEFUN ("re-search-backward", Fre_search_backward, 1, 5,
|
|
2215 "sRE search backward: ", /*
|
|
2216 Search backward from point for match for regular expression REGEXP.
|
|
2217 Set point to the beginning of the match, and return point.
|
|
2218 The match found is the one starting last in the buffer
|
|
2219 and yet ending before the origin of the search.
|
444
|
2220
|
|
2221 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2222 position. The match found must not extend before that position.
|
|
2223 The value nil is equivalent to (point-min).
|
|
2224
|
|
2225 Optional third argument NOERROR, if t, means just return nil (no
|
|
2226 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2227 and return nil.
|
|
2228
|
|
2229 Optional fourth argument COUNT is a repeat count--search for
|
|
2230 successive occurrences.
|
|
2231
|
428
|
2232 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2233 defaults to the current buffer.
|
|
2234
|
1468
|
2235 When the match is successful, this function modifies the match data
|
|
2236 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2237 match data with `match-data' and restore it with `store-match-data' if
|
|
2238 you want to preserve them. If the match fails, the match data from the
|
|
2239 previous success match is preserved.
|
|
2240
|
|
2241 See also the function `replace-match'.
|
428
|
2242 */
|
444
|
2243 (regexp, limit, noerror, count, buffer))
|
428
|
2244 {
|
444
|
2245 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 0);
|
428
|
2246 }
|
|
2247
|
|
2248 DEFUN ("re-search-forward", Fre_search_forward, 1, 5, "sRE search: ", /*
|
|
2249 Search forward from point for regular expression REGEXP.
|
|
2250 Set point to the end of the occurrence found, and return point.
|
444
|
2251
|
|
2252 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2253 position. The match found must not extend after that position. The
|
|
2254 value nil is equivalent to (point-max).
|
|
2255
|
|
2256 Optional third argument NOERROR, if t, means just return nil (no
|
|
2257 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2258 and return nil.
|
|
2259
|
|
2260 Optional fourth argument COUNT is a repeat count--search for
|
|
2261 successive occurrences.
|
|
2262
|
428
|
2263 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2264 defaults to the current buffer.
|
|
2265
|
1468
|
2266 When the match is successful, this function modifies the match data
|
|
2267 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2268 match data with `match-data' and restore it with `store-match-data' if
|
|
2269 you want to preserve them. If the match fails, the match data from the
|
|
2270 previous success match is preserved.
|
|
2271
|
|
2272 See also the function `replace-match'.
|
428
|
2273 */
|
444
|
2274 (regexp, limit, noerror, count, buffer))
|
428
|
2275 {
|
444
|
2276 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 0);
|
428
|
2277 }
|
|
2278
|
|
2279 DEFUN ("posix-search-backward", Fposix_search_backward, 1, 5,
|
|
2280 "sPosix search backward: ", /*
|
|
2281 Search backward from point for match for regular expression REGEXP.
|
|
2282 Find the longest match in accord with Posix regular expression rules.
|
|
2283 Set point to the beginning of the match, and return point.
|
|
2284 The match found is the one starting last in the buffer
|
|
2285 and yet ending before the origin of the search.
|
444
|
2286
|
|
2287 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2288 position. The match found must not extend before that position.
|
|
2289 The value nil is equivalent to (point-min).
|
|
2290
|
|
2291 Optional third argument NOERROR, if t, means just return nil (no
|
|
2292 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2293 and return nil.
|
|
2294
|
|
2295 Optional fourth argument COUNT is a repeat count--search for
|
|
2296 successive occurrences.
|
|
2297
|
428
|
2298 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2299 defaults to the current buffer.
|
|
2300
|
1468
|
2301 When the match is successful, this function modifies the match data
|
|
2302 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2303 match data with `match-data' and restore it with `store-match-data' if
|
|
2304 you want to preserve them. If the match fails, the match data from the
|
|
2305 previous success match is preserved.
|
|
2306
|
|
2307 See also the function `replace-match'.
|
428
|
2308 */
|
444
|
2309 (regexp, limit, noerror, count, buffer))
|
428
|
2310 {
|
444
|
2311 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 1);
|
428
|
2312 }
|
|
2313
|
|
2314 DEFUN ("posix-search-forward", Fposix_search_forward, 1, 5, "sPosix search: ", /*
|
|
2315 Search forward from point for regular expression REGEXP.
|
|
2316 Find the longest match in accord with Posix regular expression rules.
|
|
2317 Set point to the end of the occurrence found, and return point.
|
444
|
2318
|
|
2319 Optional second argument LIMIT bounds the search; it is a buffer
|
|
2320 position. The match found must not extend after that position. The
|
|
2321 value nil is equivalent to (point-max).
|
|
2322
|
|
2323 Optional third argument NOERROR, if t, means just return nil (no
|
|
2324 error) if the search fails. If neither nil nor t, set point to LIMIT
|
|
2325 and return nil.
|
|
2326
|
|
2327 Optional fourth argument COUNT is a repeat count--search for
|
|
2328 successive occurrences.
|
|
2329
|
428
|
2330 Optional fifth argument BUFFER specifies the buffer to search in and
|
444
|
2331 defaults to the current buffer.
|
|
2332
|
1468
|
2333 When the match is successful, this function modifies the match data
|
|
2334 that `match-beginning', `match-end' and `match-data' access; save the
|
|
2335 match data with `match-data' and restore it with `store-match-data' if
|
|
2336 you want to preserve them. If the match fails, the match data from the
|
|
2337 previous success match is preserved.
|
|
2338
|
|
2339 See also the function `replace-match'.
|
428
|
2340 */
|
444
|
2341 (regexp, limit, noerror, count, buffer))
|
428
|
2342 {
|
444
|
2343 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 1);
|
428
|
2344 }
|
|
2345
|
|
2346
|
|
2347 static Lisp_Object
|
|
2348 free_created_dynarrs (Lisp_Object cons)
|
|
2349 {
|
|
2350 Dynarr_free (get_opaque_ptr (XCAR (cons)));
|
|
2351 Dynarr_free (get_opaque_ptr (XCDR (cons)));
|
|
2352 free_opaque_ptr (XCAR (cons));
|
|
2353 free_opaque_ptr (XCDR (cons));
|
853
|
2354 free_cons (cons);
|
428
|
2355 return Qnil;
|
|
2356 }
|
|
2357
|
|
2358 DEFUN ("replace-match", Freplace_match, 1, 5, 0, /*
|
444
|
2359 Replace text matched by last search with REPLACEMENT.
|
428
|
2360 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.
|
|
2361 Otherwise maybe capitalize the whole text, or maybe just word initials,
|
|
2362 based on the replaced text.
|
|
2363 If the replaced text has only capital letters
|
444
|
2364 and has at least one multiletter word, convert REPLACEMENT to all caps.
|
428
|
2365 If the replaced text has at least one word starting with a capital letter,
|
444
|
2366 then capitalize each word in REPLACEMENT.
|
428
|
2367
|
444
|
2368 If third arg LITERAL is non-nil, insert REPLACEMENT literally.
|
428
|
2369 Otherwise treat `\\' as special:
|
444
|
2370 `\\&' in REPLACEMENT means substitute original matched text.
|
428
|
2371 `\\N' means substitute what matched the Nth `\\(...\\)'.
|
|
2372 If Nth parens didn't match, substitute nothing.
|
|
2373 `\\\\' means insert one `\\'.
|
|
2374 `\\u' means upcase the next character.
|
|
2375 `\\l' means downcase the next character.
|
|
2376 `\\U' means begin upcasing all following characters.
|
|
2377 `\\L' means begin downcasing all following characters.
|
|
2378 `\\E' means terminate the effect of any `\\U' or `\\L'.
|
|
2379 Case changes made with `\\u', `\\l', `\\U', and `\\L' override
|
|
2380 all other case changes that may be made in the replaced text.
|
|
2381 FIXEDCASE and LITERAL are optional arguments.
|
|
2382 Leaves point at end of replacement text.
|
|
2383
|
|
2384 The optional fourth argument STRING can be a string to modify.
|
|
2385 In that case, this function creates and returns a new string
|
|
2386 which is made by replacing the part of STRING that was matched.
|
|
2387 When fourth argument is a string, fifth argument STRBUFFER specifies
|
|
2388 the buffer to be used for syntax-table and case-table lookup and
|
444
|
2389 defaults to the current buffer. When fourth argument is not a string,
|
428
|
2390 the buffer that the match occurred in has automatically been remembered
|
444
|
2391 and you do not need to specify it.
|
469
|
2392
|
|
2393 When fourth argument is nil, STRBUFFER specifies a subexpression of
|
|
2394 the match. It says to replace just that subexpression instead of the
|
|
2395 whole match. This is useful only after a regular expression search or
|
|
2396 match since only regular expressions have distinguished subexpressions.
|
1425
|
2397
|
1468
|
2398 If no match (including searches) has been conducted or the requested
|
|
2399 subexpression was not matched, an `args-out-of-range' error will be
|
|
2400 signaled. (If no match has ever been conducted in this instance of
|
|
2401 XEmacs, an `invalid-operation' error will be signaled. This is very
|
|
2402 rare.)
|
428
|
2403 */
|
444
|
2404 (replacement, fixedcase, literal, string, strbuffer))
|
428
|
2405 {
|
|
2406 /* This function can GC */
|
|
2407 enum { nochange, all_caps, cap_initial } case_action;
|
665
|
2408 Charbpos pos, last;
|
428
|
2409 int some_multiletter_word;
|
|
2410 int some_lowercase;
|
|
2411 int some_uppercase;
|
|
2412 int some_nonuppercase_initial;
|
867
|
2413 Ichar c, prevc;
|
428
|
2414 Charcount inslen;
|
|
2415 struct buffer *buf;
|
826
|
2416 Lisp_Object syntax_table;
|
428
|
2417 int mc_count;
|
|
2418 Lisp_Object buffer;
|
|
2419 int_dynarr *ul_action_dynarr = 0;
|
|
2420 int_dynarr *ul_pos_dynarr = 0;
|
502
|
2421 int sub = 0;
|
428
|
2422 int speccount;
|
|
2423
|
444
|
2424 CHECK_STRING (replacement);
|
428
|
2425
|
|
2426 if (! NILP (string))
|
|
2427 {
|
|
2428 CHECK_STRING (string);
|
|
2429 if (!EQ (last_thing_searched, Qt))
|
563
|
2430 invalid_argument ("last thing matched was not a string", Qunbound);
|
428
|
2431 /* If the match data
|
|
2432 were abstracted into a special "match data" type instead
|
|
2433 of the typical half-assed "let the implementation be
|
|
2434 visible" form it's in, we could extend it to include
|
|
2435 the last string matched and the buffer used for that
|
|
2436 matching. But of course we can't change it as it is. */
|
|
2437 buf = decode_buffer (strbuffer, 0);
|
793
|
2438 buffer = wrap_buffer (buf);
|
428
|
2439 }
|
|
2440 else
|
|
2441 {
|
707
|
2442 if (!NILP (strbuffer))
|
469
|
2443 {
|
|
2444 CHECK_INT (strbuffer);
|
|
2445 sub = XINT (strbuffer);
|
|
2446 if (sub < 0 || sub >= (int) search_regs.num_regs)
|
|
2447 args_out_of_range (strbuffer, make_int (search_regs.num_regs));
|
|
2448 }
|
428
|
2449 if (!BUFFERP (last_thing_searched))
|
563
|
2450 invalid_argument ("last thing matched was not a buffer", Qunbound);
|
428
|
2451 buffer = last_thing_searched;
|
|
2452 buf = XBUFFER (buffer);
|
|
2453 }
|
|
2454
|
826
|
2455 syntax_table = buf->mirror_syntax_table;
|
428
|
2456
|
|
2457 case_action = nochange; /* We tried an initialization */
|
|
2458 /* but some C compilers blew it */
|
|
2459
|
|
2460 if (search_regs.num_regs == 0)
|
826
|
2461 signal_error (Qinvalid_operation,
|
|
2462 "replace-match called before any match found", Qunbound);
|
428
|
2463
|
|
2464 if (NILP (string))
|
|
2465 {
|
469
|
2466 if (search_regs.start[sub] < BUF_BEGV (buf)
|
|
2467 || search_regs.start[sub] > search_regs.end[sub]
|
|
2468 || search_regs.end[sub] > BUF_ZV (buf))
|
|
2469 args_out_of_range (make_int (search_regs.start[sub]),
|
|
2470 make_int (search_regs.end[sub]));
|
428
|
2471 }
|
|
2472 else
|
|
2473 {
|
|
2474 if (search_regs.start[0] < 0
|
|
2475 || search_regs.start[0] > search_regs.end[0]
|
826
|
2476 || search_regs.end[0] > string_char_length (string))
|
428
|
2477 args_out_of_range (make_int (search_regs.start[0]),
|
|
2478 make_int (search_regs.end[0]));
|
|
2479 }
|
|
2480
|
|
2481 if (NILP (fixedcase))
|
|
2482 {
|
|
2483 /* Decide how to casify by examining the matched text. */
|
|
2484
|
707
|
2485 last = search_regs.end[sub];
|
428
|
2486 prevc = '\n';
|
|
2487 case_action = all_caps;
|
|
2488
|
|
2489 /* some_multiletter_word is set nonzero if any original word
|
|
2490 is more than one letter long. */
|
|
2491 some_multiletter_word = 0;
|
|
2492 some_lowercase = 0;
|
|
2493 some_nonuppercase_initial = 0;
|
|
2494 some_uppercase = 0;
|
|
2495
|
707
|
2496 for (pos = search_regs.start[sub]; pos < last; pos++)
|
428
|
2497 {
|
|
2498 if (NILP (string))
|
|
2499 c = BUF_FETCH_CHAR (buf, pos);
|
|
2500 else
|
867
|
2501 c = string_ichar (string, pos);
|
428
|
2502
|
|
2503 if (LOWERCASEP (buf, c))
|
|
2504 {
|
|
2505 /* Cannot be all caps if any original char is lower case */
|
|
2506
|
|
2507 some_lowercase = 1;
|
|
2508 if (!WORD_SYNTAX_P (syntax_table, prevc))
|
|
2509 some_nonuppercase_initial = 1;
|
|
2510 else
|
|
2511 some_multiletter_word = 1;
|
|
2512 }
|
|
2513 else if (!NOCASEP (buf, c))
|
|
2514 {
|
|
2515 some_uppercase = 1;
|
|
2516 if (!WORD_SYNTAX_P (syntax_table, prevc))
|
|
2517 ;
|
|
2518 else
|
|
2519 some_multiletter_word = 1;
|
|
2520 }
|
|
2521 else
|
|
2522 {
|
|
2523 /* If the initial is a caseless word constituent,
|
|
2524 treat that like a lowercase initial. */
|
|
2525 if (!WORD_SYNTAX_P (syntax_table, prevc))
|
|
2526 some_nonuppercase_initial = 1;
|
|
2527 }
|
|
2528
|
|
2529 prevc = c;
|
|
2530 }
|
|
2531
|
|
2532 /* Convert to all caps if the old text is all caps
|
|
2533 and has at least one multiletter word. */
|
|
2534 if (! some_lowercase && some_multiletter_word)
|
|
2535 case_action = all_caps;
|
|
2536 /* Capitalize each word, if the old text has all capitalized words. */
|
|
2537 else if (!some_nonuppercase_initial && some_multiletter_word)
|
|
2538 case_action = cap_initial;
|
|
2539 else if (!some_nonuppercase_initial && some_uppercase)
|
|
2540 /* Should x -> yz, operating on X, give Yz or YZ?
|
|
2541 We'll assume the latter. */
|
|
2542 case_action = all_caps;
|
|
2543 else
|
|
2544 case_action = nochange;
|
|
2545 }
|
|
2546
|
|
2547 /* Do replacement in a string. */
|
|
2548 if (!NILP (string))
|
|
2549 {
|
|
2550 Lisp_Object before, after;
|
|
2551
|
|
2552 speccount = specpdl_depth ();
|
|
2553 before = Fsubstring (string, Qzero, make_int (search_regs.start[0]));
|
|
2554 after = Fsubstring (string, make_int (search_regs.end[0]), Qnil);
|
|
2555
|
444
|
2556 /* Do case substitution into REPLACEMENT if desired. */
|
428
|
2557 if (NILP (literal))
|
|
2558 {
|
826
|
2559 Charcount stlen = string_char_length (replacement);
|
428
|
2560 Charcount strpos;
|
|
2561 /* XEmacs change: rewrote this loop somewhat to make it
|
|
2562 cleaner. Also added \U, \E, etc. */
|
|
2563 Charcount literal_start = 0;
|
|
2564 /* We build up the substituted string in ACCUM. */
|
|
2565 Lisp_Object accum;
|
|
2566
|
|
2567 accum = Qnil;
|
|
2568
|
|
2569 /* OK, the basic idea here is that we scan through the
|
|
2570 replacement string until we find a backslash, which
|
|
2571 represents a substring of the original string to be
|
|
2572 substituted. We then append onto ACCUM the literal
|
|
2573 text before the backslash (LASTPOS marks the
|
|
2574 beginning of this) followed by the substring of the
|
|
2575 original string that needs to be inserted. */
|
|
2576 for (strpos = 0; strpos < stlen; strpos++)
|
|
2577 {
|
|
2578 /* If LITERAL_END is set, we've encountered a backslash
|
|
2579 (the end of literal text to be inserted). */
|
|
2580 Charcount literal_end = -1;
|
|
2581 /* If SUBSTART is set, we need to also insert the
|
|
2582 text from SUBSTART to SUBEND in the original string. */
|
|
2583 Charcount substart = -1;
|
|
2584 Charcount subend = -1;
|
|
2585
|
867
|
2586 c = string_ichar (replacement, strpos);
|
428
|
2587 if (c == '\\' && strpos < stlen - 1)
|
|
2588 {
|
867
|
2589 c = string_ichar (replacement, ++strpos);
|
428
|
2590 if (c == '&')
|
|
2591 {
|
|
2592 literal_end = strpos - 1;
|
|
2593 substart = search_regs.start[0];
|
|
2594 subend = search_regs.end[0];
|
|
2595 }
|
|
2596 else if (c >= '1' && c <= '9' &&
|
|
2597 c <= search_regs.num_regs + '0')
|
|
2598 {
|
|
2599 if (search_regs.start[c - '0'] >= 0)
|
|
2600 {
|
|
2601 literal_end = strpos - 1;
|
|
2602 substart = search_regs.start[c - '0'];
|
|
2603 subend = search_regs.end[c - '0'];
|
|
2604 }
|
|
2605 }
|
|
2606 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' ||
|
|
2607 c == 'E')
|
|
2608 {
|
|
2609 /* Keep track of all case changes requested, but don't
|
|
2610 make them now. Do them later so we override
|
|
2611 everything else. */
|
|
2612 if (!ul_pos_dynarr)
|
|
2613 {
|
|
2614 ul_pos_dynarr = Dynarr_new (int);
|
|
2615 ul_action_dynarr = Dynarr_new (int);
|
|
2616 record_unwind_protect
|
|
2617 (free_created_dynarrs,
|
|
2618 noseeum_cons
|
|
2619 (make_opaque_ptr (ul_pos_dynarr),
|
|
2620 make_opaque_ptr (ul_action_dynarr)));
|
|
2621 }
|
|
2622 literal_end = strpos - 1;
|
|
2623 Dynarr_add (ul_pos_dynarr,
|
|
2624 (!NILP (accum)
|
826
|
2625 ? string_char_length (accum)
|
428
|
2626 : 0) + (literal_end - literal_start));
|
|
2627 Dynarr_add (ul_action_dynarr, c);
|
|
2628 }
|
|
2629 else if (c == '\\')
|
|
2630 /* So we get just one backslash. */
|
|
2631 literal_end = strpos;
|
|
2632 }
|
|
2633 if (literal_end >= 0)
|
|
2634 {
|
|
2635 Lisp_Object literal_text = Qnil;
|
|
2636 Lisp_Object substring = Qnil;
|
|
2637 if (literal_end != literal_start)
|
444
|
2638 literal_text = Fsubstring (replacement,
|
428
|
2639 make_int (literal_start),
|
|
2640 make_int (literal_end));
|
|
2641 if (substart >= 0 && subend != substart)
|
|
2642 substring = Fsubstring (string,
|
|
2643 make_int (substart),
|
|
2644 make_int (subend));
|
|
2645 if (!NILP (literal_text) || !NILP (substring))
|
|
2646 accum = concat3 (accum, literal_text, substring);
|
|
2647 literal_start = strpos + 1;
|
|
2648 }
|
|
2649 }
|
|
2650
|
|
2651 if (strpos != literal_start)
|
|
2652 /* some literal text at end to be inserted */
|
444
|
2653 replacement = concat2 (accum, Fsubstring (replacement,
|
|
2654 make_int (literal_start),
|
|
2655 make_int (strpos)));
|
428
|
2656 else
|
444
|
2657 replacement = accum;
|
428
|
2658 }
|
|
2659
|
444
|
2660 /* replacement can be nil. */
|
|
2661 if (NILP (replacement))
|
|
2662 replacement = build_string ("");
|
|
2663
|
428
|
2664 if (case_action == all_caps)
|
444
|
2665 replacement = Fupcase (replacement, buffer);
|
428
|
2666 else if (case_action == cap_initial)
|
444
|
2667 replacement = Fupcase_initials (replacement, buffer);
|
428
|
2668
|
|
2669 /* Now finally, we need to process the \U's, \E's, etc. */
|
|
2670 if (ul_pos_dynarr)
|
|
2671 {
|
|
2672 int i = 0;
|
|
2673 int cur_action = 'E';
|
826
|
2674 Charcount stlen = string_char_length (replacement);
|
428
|
2675 Charcount strpos;
|
|
2676
|
|
2677 for (strpos = 0; strpos < stlen; strpos++)
|
|
2678 {
|
867
|
2679 Ichar curchar = string_ichar (replacement, strpos);
|
|
2680 Ichar newchar = -1;
|
428
|
2681 if (i < Dynarr_length (ul_pos_dynarr) &&
|
|
2682 strpos == Dynarr_at (ul_pos_dynarr, i))
|
|
2683 {
|
|
2684 int new_action = Dynarr_at (ul_action_dynarr, i);
|
|
2685 i++;
|
|
2686 if (new_action == 'u')
|
|
2687 newchar = UPCASE (buf, curchar);
|
|
2688 else if (new_action == 'l')
|
|
2689 newchar = DOWNCASE (buf, curchar);
|
|
2690 else
|
|
2691 cur_action = new_action;
|
|
2692 }
|
|
2693 if (newchar == -1)
|
|
2694 {
|
|
2695 if (cur_action == 'U')
|
|
2696 newchar = UPCASE (buf, curchar);
|
|
2697 else if (cur_action == 'L')
|
|
2698 newchar = DOWNCASE (buf, curchar);
|
|
2699 else
|
|
2700 newchar = curchar;
|
|
2701 }
|
|
2702 if (newchar != curchar)
|
793
|
2703 set_string_char (replacement, strpos, newchar);
|
428
|
2704 }
|
|
2705 }
|
|
2706
|
|
2707 /* frees the Dynarrs if necessary. */
|
771
|
2708 unbind_to (speccount);
|
444
|
2709 return concat3 (before, replacement, after);
|
428
|
2710 }
|
|
2711
|
707
|
2712 mc_count = begin_multiple_change (buf, search_regs.start[sub],
|
|
2713 search_regs.end[sub]);
|
428
|
2714
|
|
2715 /* begin_multiple_change() records an unwind-protect, so we need to
|
|
2716 record this value now. */
|
|
2717 speccount = specpdl_depth ();
|
|
2718
|
|
2719 /* We insert the replacement text before the old text, and then
|
|
2720 delete the original text. This means that markers at the
|
|
2721 beginning or end of the original will float to the corresponding
|
|
2722 position in the replacement. */
|
707
|
2723 BUF_SET_PT (buf, search_regs.start[sub]);
|
428
|
2724 if (!NILP (literal))
|
444
|
2725 Finsert (1, &replacement);
|
428
|
2726 else
|
|
2727 {
|
826
|
2728 Charcount stlen = string_char_length (replacement);
|
428
|
2729 Charcount strpos;
|
|
2730 struct gcpro gcpro1;
|
444
|
2731 GCPRO1 (replacement);
|
428
|
2732 for (strpos = 0; strpos < stlen; strpos++)
|
|
2733 {
|
707
|
2734 /* on the first iteration assert(offset==0),
|
|
2735 exactly complementing BUF_SET_PT() above.
|
|
2736 During the loop, it keeps track of the amount inserted.
|
|
2737 */
|
|
2738 Charcount offset = BUF_PT (buf) - search_regs.start[sub];
|
428
|
2739
|
867
|
2740 c = string_ichar (replacement, strpos);
|
428
|
2741 if (c == '\\' && strpos < stlen - 1)
|
|
2742 {
|
707
|
2743 /* XXX FIXME: replacing just a substring non-literally
|
|
2744 using backslash refs to the match looks dangerous. But
|
|
2745 <15366.18513.698042.156573@ns.caldera.de> from Torsten Duwe
|
|
2746 <duwe@caldera.de> claims Finsert_buffer_substring already
|
|
2747 handles this correctly.
|
|
2748 */
|
867
|
2749 c = string_ichar (replacement, ++strpos);
|
428
|
2750 if (c == '&')
|
|
2751 Finsert_buffer_substring
|
|
2752 (buffer,
|
|
2753 make_int (search_regs.start[0] + offset),
|
|
2754 make_int (search_regs.end[0] + offset));
|
|
2755 else if (c >= '1' && c <= '9' &&
|
|
2756 c <= search_regs.num_regs + '0')
|
|
2757 {
|
|
2758 if (search_regs.start[c - '0'] >= 1)
|
|
2759 Finsert_buffer_substring
|
|
2760 (buffer,
|
|
2761 make_int (search_regs.start[c - '0'] + offset),
|
|
2762 make_int (search_regs.end[c - '0'] + offset));
|
|
2763 }
|
|
2764 else if (c == 'U' || c == 'u' || c == 'L' || c == 'l' ||
|
|
2765 c == 'E')
|
|
2766 {
|
|
2767 /* Keep track of all case changes requested, but don't
|
|
2768 make them now. Do them later so we override
|
|
2769 everything else. */
|
|
2770 if (!ul_pos_dynarr)
|
|
2771 {
|
|
2772 ul_pos_dynarr = Dynarr_new (int);
|
|
2773 ul_action_dynarr = Dynarr_new (int);
|
|
2774 record_unwind_protect
|
|
2775 (free_created_dynarrs,
|
|
2776 Fcons (make_opaque_ptr (ul_pos_dynarr),
|
|
2777 make_opaque_ptr (ul_action_dynarr)));
|
|
2778 }
|
|
2779 Dynarr_add (ul_pos_dynarr, BUF_PT (buf));
|
|
2780 Dynarr_add (ul_action_dynarr, c);
|
|
2781 }
|
|
2782 else
|
|
2783 buffer_insert_emacs_char (buf, c);
|
|
2784 }
|
|
2785 else
|
|
2786 buffer_insert_emacs_char (buf, c);
|
|
2787 }
|
|
2788 UNGCPRO;
|
|
2789 }
|
|
2790
|
707
|
2791 inslen = BUF_PT (buf) - (search_regs.start[sub]);
|
|
2792 buffer_delete_range (buf, search_regs.start[sub] + inslen,
|
|
2793 search_regs.end[sub] + inslen, 0);
|
428
|
2794
|
|
2795 if (case_action == all_caps)
|
|
2796 Fupcase_region (make_int (BUF_PT (buf) - inslen),
|
|
2797 make_int (BUF_PT (buf)), buffer);
|
|
2798 else if (case_action == cap_initial)
|
|
2799 Fupcase_initials_region (make_int (BUF_PT (buf) - inslen),
|
|
2800 make_int (BUF_PT (buf)), buffer);
|
|
2801
|
|
2802 /* Now go through and make all the case changes that were requested
|
|
2803 in the replacement string. */
|
|
2804 if (ul_pos_dynarr)
|
|
2805 {
|
665
|
2806 Charbpos eend = BUF_PT (buf);
|
428
|
2807 int i = 0;
|
|
2808 int cur_action = 'E';
|
|
2809
|
|
2810 for (pos = BUF_PT (buf) - inslen; pos < eend; pos++)
|
|
2811 {
|
867
|
2812 Ichar curchar = BUF_FETCH_CHAR (buf, pos);
|
|
2813 Ichar newchar = -1;
|
428
|
2814 if (i < Dynarr_length (ul_pos_dynarr) &&
|
|
2815 pos == Dynarr_at (ul_pos_dynarr, i))
|
|
2816 {
|
|
2817 int new_action = Dynarr_at (ul_action_dynarr, i);
|
|
2818 i++;
|
|
2819 if (new_action == 'u')
|
|
2820 newchar = UPCASE (buf, curchar);
|
|
2821 else if (new_action == 'l')
|
|
2822 newchar = DOWNCASE (buf, curchar);
|
|
2823 else
|
|
2824 cur_action = new_action;
|
|
2825 }
|
|
2826 if (newchar == -1)
|
|
2827 {
|
|
2828 if (cur_action == 'U')
|
|
2829 newchar = UPCASE (buf, curchar);
|
|
2830 else if (cur_action == 'L')
|
|
2831 newchar = DOWNCASE (buf, curchar);
|
|
2832 else
|
|
2833 newchar = curchar;
|
|
2834 }
|
|
2835 if (newchar != curchar)
|
|
2836 buffer_replace_char (buf, pos, newchar, 0, 0);
|
|
2837 }
|
|
2838 }
|
|
2839
|
|
2840 /* frees the Dynarrs if necessary. */
|
771
|
2841 unbind_to (speccount);
|
428
|
2842 end_multiple_change (buf, mc_count);
|
|
2843
|
|
2844 return Qnil;
|
|
2845 }
|
|
2846
|
|
2847 static Lisp_Object
|
|
2848 match_limit (Lisp_Object num, int beginningp)
|
|
2849 {
|
|
2850 int n;
|
|
2851
|
|
2852 CHECK_INT (num);
|
|
2853 n = XINT (num);
|
|
2854 if (n < 0 || n >= search_regs.num_regs)
|
|
2855 args_out_of_range (num, make_int (search_regs.num_regs));
|
|
2856 if (search_regs.num_regs == 0 ||
|
|
2857 search_regs.start[n] < 0)
|
|
2858 return Qnil;
|
|
2859 return make_int (beginningp ? search_regs.start[n] : search_regs.end[n]);
|
|
2860 }
|
|
2861
|
|
2862 DEFUN ("match-beginning", Fmatch_beginning, 1, 1, 0, /*
|
|
2863 Return position of start of text matched by last regexp search.
|
|
2864 NUM, specifies which parenthesized expression in the last regexp.
|
|
2865 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
|
|
2866 Zero means the entire text matched by the whole regexp or whole string.
|
|
2867 */
|
|
2868 (num))
|
|
2869 {
|
|
2870 return match_limit (num, 1);
|
|
2871 }
|
|
2872
|
|
2873 DEFUN ("match-end", Fmatch_end, 1, 1, 0, /*
|
|
2874 Return position of end of text matched by last regexp search.
|
|
2875 NUM specifies which parenthesized expression in the last regexp.
|
|
2876 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.
|
|
2877 Zero means the entire text matched by the whole regexp or whole string.
|
|
2878 */
|
|
2879 (num))
|
|
2880 {
|
|
2881 return match_limit (num, 0);
|
|
2882 }
|
|
2883
|
|
2884 DEFUN ("match-data", Fmatch_data, 0, 2, 0, /*
|
|
2885 Return a list containing all info on what the last regexp search matched.
|
|
2886 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.
|
|
2887 All the elements are markers or nil (nil if the Nth pair didn't match)
|
|
2888 if the last match was on a buffer; integers or nil if a string was matched.
|
|
2889 Use `store-match-data' to reinstate the data in this list.
|
|
2890
|
|
2891 If INTEGERS (the optional first argument) is non-nil, always use integers
|
|
2892 \(rather than markers) to represent buffer positions.
|
|
2893 If REUSE is a list, reuse it as part of the value. If REUSE is long enough
|
|
2894 to hold all the values, and if INTEGERS is non-nil, no consing is done.
|
|
2895 */
|
|
2896 (integers, reuse))
|
|
2897 {
|
|
2898 Lisp_Object tail, prev;
|
|
2899 Lisp_Object *data;
|
|
2900 int i;
|
|
2901 Charcount len;
|
|
2902
|
|
2903 if (NILP (last_thing_searched))
|
563
|
2904 /*error ("match-data called before any match found", Qunbound);*/
|
428
|
2905 return Qnil;
|
|
2906
|
|
2907 data = alloca_array (Lisp_Object, 2 * search_regs.num_regs);
|
|
2908
|
|
2909 len = -1;
|
|
2910 for (i = 0; i < search_regs.num_regs; i++)
|
|
2911 {
|
665
|
2912 Charbpos start = search_regs.start[i];
|
428
|
2913 if (start >= 0)
|
|
2914 {
|
|
2915 if (EQ (last_thing_searched, Qt)
|
|
2916 || !NILP (integers))
|
|
2917 {
|
|
2918 data[2 * i] = make_int (start);
|
|
2919 data[2 * i + 1] = make_int (search_regs.end[i]);
|
|
2920 }
|
|
2921 else if (BUFFERP (last_thing_searched))
|
|
2922 {
|
|
2923 data[2 * i] = Fmake_marker ();
|
|
2924 Fset_marker (data[2 * i],
|
|
2925 make_int (start),
|
|
2926 last_thing_searched);
|
|
2927 data[2 * i + 1] = Fmake_marker ();
|
|
2928 Fset_marker (data[2 * i + 1],
|
|
2929 make_int (search_regs.end[i]),
|
|
2930 last_thing_searched);
|
|
2931 }
|
|
2932 else
|
|
2933 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
|
|
2934 abort ();
|
|
2935
|
|
2936 len = i;
|
|
2937 }
|
|
2938 else
|
|
2939 data[2 * i] = data [2 * i + 1] = Qnil;
|
|
2940 }
|
|
2941 if (!CONSP (reuse))
|
|
2942 return Flist (2 * len + 2, data);
|
|
2943
|
|
2944 /* If REUSE is a list, store as many value elements as will fit
|
|
2945 into the elements of REUSE. */
|
|
2946 for (prev = Qnil, i = 0, tail = reuse; CONSP (tail); i++, tail = XCDR (tail))
|
|
2947 {
|
|
2948 if (i < 2 * len + 2)
|
|
2949 XCAR (tail) = data[i];
|
|
2950 else
|
|
2951 XCAR (tail) = Qnil;
|
|
2952 prev = tail;
|
|
2953 }
|
|
2954
|
|
2955 /* If we couldn't fit all value elements into REUSE,
|
|
2956 cons up the rest of them and add them to the end of REUSE. */
|
|
2957 if (i < 2 * len + 2)
|
|
2958 XCDR (prev) = Flist (2 * len + 2 - i, data + i);
|
|
2959
|
|
2960 return reuse;
|
|
2961 }
|
|
2962
|
|
2963
|
|
2964 DEFUN ("store-match-data", Fstore_match_data, 1, 1, 0, /*
|
|
2965 Set internal data on last search match from elements of LIST.
|
1468
|
2966 LIST should have been created by calling `match-data' previously,
|
|
2967 or be nil, to clear the internal match data.
|
428
|
2968 */
|
|
2969 (list))
|
|
2970 {
|
|
2971 REGISTER int i;
|
|
2972 REGISTER Lisp_Object marker;
|
|
2973 int num_regs;
|
|
2974 int length;
|
|
2975
|
853
|
2976 /* Some FSF junk with running_asynch_code, to preserve the match
|
|
2977 data. Not necessary because we don't call process filters
|
|
2978 asynchronously (i.e. from within QUIT). */
|
428
|
2979
|
|
2980 CONCHECK_LIST (list);
|
|
2981
|
|
2982 /* Unless we find a marker with a buffer in LIST, assume that this
|
|
2983 match data came from a string. */
|
|
2984 last_thing_searched = Qt;
|
|
2985
|
|
2986 /* Allocate registers if they don't already exist. */
|
|
2987 length = XINT (Flength (list)) / 2;
|
|
2988 num_regs = search_regs.num_regs;
|
|
2989
|
|
2990 if (length > num_regs)
|
|
2991 {
|
|
2992 if (search_regs.num_regs == 0)
|
|
2993 {
|
|
2994 search_regs.start = xnew_array (regoff_t, length);
|
|
2995 search_regs.end = xnew_array (regoff_t, length);
|
|
2996 }
|
|
2997 else
|
|
2998 {
|
|
2999 XREALLOC_ARRAY (search_regs.start, regoff_t, length);
|
|
3000 XREALLOC_ARRAY (search_regs.end, regoff_t, length);
|
|
3001 }
|
|
3002
|
|
3003 search_regs.num_regs = length;
|
|
3004 }
|
|
3005
|
|
3006 for (i = 0; i < num_regs; i++)
|
|
3007 {
|
|
3008 marker = Fcar (list);
|
|
3009 if (NILP (marker))
|
|
3010 {
|
|
3011 search_regs.start[i] = -1;
|
|
3012 list = Fcdr (list);
|
|
3013 }
|
|
3014 else
|
|
3015 {
|
|
3016 if (MARKERP (marker))
|
|
3017 {
|
|
3018 if (XMARKER (marker)->buffer == 0)
|
|
3019 marker = Qzero;
|
|
3020 else
|
793
|
3021 last_thing_searched = wrap_buffer (XMARKER (marker)->buffer);
|
428
|
3022 }
|
|
3023
|
|
3024 CHECK_INT_COERCE_MARKER (marker);
|
|
3025 search_regs.start[i] = XINT (marker);
|
|
3026 list = Fcdr (list);
|
|
3027
|
|
3028 marker = Fcar (list);
|
|
3029 if (MARKERP (marker) && XMARKER (marker)->buffer == 0)
|
|
3030 marker = Qzero;
|
|
3031
|
|
3032 CHECK_INT_COERCE_MARKER (marker);
|
|
3033 search_regs.end[i] = XINT (marker);
|
|
3034 }
|
|
3035 list = Fcdr (list);
|
|
3036 }
|
|
3037
|
|
3038 return Qnil;
|
|
3039 }
|
|
3040
|
|
3041 /* Quote a string to inactivate reg-expr chars */
|
|
3042
|
|
3043 DEFUN ("regexp-quote", Fregexp_quote, 1, 1, 0, /*
|
|
3044 Return a regexp string which matches exactly STRING and nothing else.
|
|
3045 */
|
444
|
3046 (string))
|
428
|
3047 {
|
867
|
3048 REGISTER Ibyte *in, *out, *end;
|
|
3049 REGISTER Ibyte *temp;
|
428
|
3050
|
444
|
3051 CHECK_STRING (string);
|
428
|
3052
|
867
|
3053 temp = (Ibyte *) ALLOCA (XSTRING_LENGTH (string) * 2);
|
428
|
3054
|
|
3055 /* Now copy the data into the new string, inserting escapes. */
|
|
3056
|
444
|
3057 in = XSTRING_DATA (string);
|
|
3058 end = in + XSTRING_LENGTH (string);
|
428
|
3059 out = temp;
|
|
3060
|
|
3061 while (in < end)
|
|
3062 {
|
867
|
3063 Ichar c = itext_ichar (in);
|
428
|
3064
|
|
3065 if (c == '[' || c == ']'
|
|
3066 || c == '*' || c == '.' || c == '\\'
|
|
3067 || c == '?' || c == '+'
|
|
3068 || c == '^' || c == '$')
|
|
3069 *out++ = '\\';
|
867
|
3070 out += set_itext_ichar (out, c);
|
|
3071 INC_IBYTEPTR (in);
|
428
|
3072 }
|
|
3073
|
|
3074 return make_string (temp, out - temp);
|
|
3075 }
|
|
3076
|
|
3077 DEFUN ("set-word-regexp", Fset_word_regexp, 1, 1, 0, /*
|
|
3078 Set the regexp to be used to match a word in regular-expression searching.
|
|
3079 #### Not yet implemented. Currently does nothing.
|
|
3080 #### Do not use this yet. Its calling interface is likely to change.
|
|
3081 */
|
|
3082 (regexp))
|
|
3083 {
|
|
3084 return Qnil;
|
|
3085 }
|
|
3086
|
|
3087
|
|
3088 /************************************************************************/
|
|
3089 /* initialization */
|
|
3090 /************************************************************************/
|
|
3091
|
|
3092 void
|
|
3093 syms_of_search (void)
|
|
3094 {
|
|
3095
|
442
|
3096 DEFERROR_STANDARD (Qsearch_failed, Qinvalid_operation);
|
|
3097 DEFERROR_STANDARD (Qinvalid_regexp, Qsyntax_error);
|
563
|
3098 Fput (Qinvalid_regexp, Qerror_lacks_explanatory_string, Qt);
|
428
|
3099
|
|
3100 DEFSUBR (Flooking_at);
|
|
3101 DEFSUBR (Fposix_looking_at);
|
|
3102 DEFSUBR (Fstring_match);
|
|
3103 DEFSUBR (Fposix_string_match);
|
|
3104 DEFSUBR (Fskip_chars_forward);
|
|
3105 DEFSUBR (Fskip_chars_backward);
|
|
3106 DEFSUBR (Fskip_syntax_forward);
|
|
3107 DEFSUBR (Fskip_syntax_backward);
|
|
3108 DEFSUBR (Fsearch_forward);
|
|
3109 DEFSUBR (Fsearch_backward);
|
|
3110 DEFSUBR (Fword_search_forward);
|
|
3111 DEFSUBR (Fword_search_backward);
|
|
3112 DEFSUBR (Fre_search_forward);
|
|
3113 DEFSUBR (Fre_search_backward);
|
|
3114 DEFSUBR (Fposix_search_forward);
|
|
3115 DEFSUBR (Fposix_search_backward);
|
|
3116 DEFSUBR (Freplace_match);
|
|
3117 DEFSUBR (Fmatch_beginning);
|
|
3118 DEFSUBR (Fmatch_end);
|
|
3119 DEFSUBR (Fmatch_data);
|
|
3120 DEFSUBR (Fstore_match_data);
|
|
3121 DEFSUBR (Fregexp_quote);
|
|
3122 DEFSUBR (Fset_word_regexp);
|
|
3123 }
|
|
3124
|
|
3125 void
|
|
3126 reinit_vars_of_search (void)
|
|
3127 {
|
|
3128 int i;
|
|
3129
|
|
3130 last_thing_searched = Qnil;
|
|
3131 staticpro_nodump (&last_thing_searched);
|
|
3132
|
|
3133 for (i = 0; i < REGEXP_CACHE_SIZE; ++i)
|
|
3134 {
|
|
3135 searchbufs[i].buf.allocated = 100;
|
|
3136 searchbufs[i].buf.buffer = (unsigned char *) xmalloc (100);
|
|
3137 searchbufs[i].buf.fastmap = searchbufs[i].fastmap;
|
|
3138 searchbufs[i].regexp = Qnil;
|
|
3139 staticpro_nodump (&searchbufs[i].regexp);
|
|
3140 searchbufs[i].next = (i == REGEXP_CACHE_SIZE-1 ? 0 : &searchbufs[i+1]);
|
|
3141 }
|
|
3142 searchbuf_head = &searchbufs[0];
|
|
3143 }
|
|
3144
|
|
3145 void
|
|
3146 vars_of_search (void)
|
|
3147 {
|
|
3148 reinit_vars_of_search ();
|
|
3149
|
|
3150 DEFVAR_LISP ("forward-word-regexp", &Vforward_word_regexp /*
|
|
3151 *Regular expression to be used in `forward-word'.
|
|
3152 #### Not yet implemented.
|
|
3153 */ );
|
|
3154 Vforward_word_regexp = Qnil;
|
|
3155
|
|
3156 DEFVAR_LISP ("backward-word-regexp", &Vbackward_word_regexp /*
|
|
3157 *Regular expression to be used in `backward-word'.
|
|
3158 #### Not yet implemented.
|
|
3159 */ );
|
|
3160 Vbackward_word_regexp = Qnil;
|
502
|
3161
|
|
3162 DEFVAR_INT ("warn-about-possibly-incompatible-back-references",
|
|
3163 &warn_about_possibly_incompatible_back_references /*
|
|
3164 If true, issue warnings when new-semantics back references occur.
|
|
3165 This is to catch places where old code might inadvertently have changed
|
|
3166 semantics. This will occur in old code only where more than nine groups
|
|
3167 occur and a back reference to one of them is directly followed by a digit.
|
|
3168 */ );
|
|
3169 warn_about_possibly_incompatible_back_references = 1;
|
814
|
3170
|
428
|
3171 Vskip_chars_range_table = Fmake_range_table ();
|
|
3172 staticpro (&Vskip_chars_range_table);
|
|
3173 }
|