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