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