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