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