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