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