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