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