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