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