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