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