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