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