603
|
1 /* String search routines for GNU Emacs.
|
26088
b7aa6ac26872
Add support for large files, 64-bit Solaris, system locale codings.
Paul Eggert <eggert@twinsun.com>
diff
changeset
|
2 Copyright (C) 1985, 86,87,93,94,97,98, 1999 Free Software Foundation, Inc.
|
603
|
3
|
|
4 This file is part of GNU Emacs.
|
|
5
|
|
6 GNU Emacs is free software; you can redistribute it and/or modify
|
|
7 it under the terms of the GNU General Public License as published by
|
12244
|
8 the Free Software Foundation; either version 2, or (at your option)
|
603
|
9 any later version.
|
|
10
|
|
11 GNU Emacs is distributed in the hope that it will be useful,
|
|
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
14 GNU General Public License for more details.
|
|
15
|
|
16 You should have received a copy of the GNU General Public License
|
|
17 along with GNU Emacs; see the file COPYING. If not, write to
|
14186
|
18 the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
|
|
19 Boston, MA 02111-1307, USA. */
|
603
|
20
|
|
21
|
4696
|
22 #include <config.h>
|
603
|
23 #include "lisp.h"
|
|
24 #include "syntax.h"
|
17053
|
25 #include "category.h"
|
603
|
26 #include "buffer.h"
|
17053
|
27 #include "charset.h"
|
9410
|
28 #include "region-cache.h"
|
603
|
29 #include "commands.h"
|
2439
|
30 #include "blockinput.h"
|
20347
|
31 #include "intervals.h"
|
621
|
32
|
603
|
33 #include <sys/types.h>
|
|
34 #include "regex.h"
|
|
35
|
21457
|
36 #define min(a, b) ((a) < (b) ? (a) : (b))
|
|
37 #define max(a, b) ((a) > (b) ? (a) : (b))
|
|
38
|
16275
|
39 #define REGEXP_CACHE_SIZE 20
|
603
|
40
|
9605
|
41 /* If the regexp is non-nil, then the buffer contains the compiled form
|
|
42 of that regexp, suitable for searching. */
|
16275
|
43 struct regexp_cache
|
|
44 {
|
9605
|
45 struct regexp_cache *next;
|
|
46 Lisp_Object regexp;
|
|
47 struct re_pattern_buffer buf;
|
|
48 char fastmap[0400];
|
10020
|
49 /* Nonzero means regexp was compiled to do full POSIX backtracking. */
|
|
50 char posix;
|
9605
|
51 };
|
603
|
52
|
9605
|
53 /* The instances of that struct. */
|
|
54 struct regexp_cache searchbufs[REGEXP_CACHE_SIZE];
|
603
|
55
|
9605
|
56 /* The head of the linked list; points to the most recently used buffer. */
|
|
57 struct regexp_cache *searchbuf_head;
|
|
58
|
603
|
59
|
621
|
60 /* Every call to re_match, etc., must pass &search_regs as the regs
|
|
61 argument unless you can show it is unnecessary (i.e., if re_match
|
|
62 is certainly going to be called again before region-around-match
|
|
63 can be called).
|
|
64
|
|
65 Since the registers are now dynamically allocated, we need to make
|
|
66 sure not to refer to the Nth register before checking that it has
|
708
|
67 been allocated by checking search_regs.num_regs.
|
603
|
68
|
708
|
69 The regex code keeps track of whether it has allocated the search
|
9605
|
70 buffer using bits in the re_pattern_buffer. This means that whenever
|
|
71 you compile a new pattern, it completely forgets whether it has
|
708
|
72 allocated any registers, and will allocate new registers the next
|
|
73 time you call a searching or matching function. Therefore, we need
|
|
74 to call re_set_registers after compiling a new pattern or after
|
|
75 setting the match registers, so that the regex functions will be
|
|
76 able to free or re-allocate it properly. */
|
603
|
77 static struct re_registers search_regs;
|
|
78
|
727
|
79 /* The buffer in which the last search was performed, or
|
|
80 Qt if the last search was done in a string;
|
|
81 Qnil if no searching has been done yet. */
|
|
82 static Lisp_Object last_thing_searched;
|
603
|
83
|
14036
|
84 /* error condition signaled when regexp compile_pattern fails */
|
603
|
85
|
|
86 Lisp_Object Qinvalid_regexp;
|
|
87
|
5556
|
88 static void set_search_regs ();
|
10055
|
89 static void save_search_regs ();
|
20869
|
90 static int simple_search ();
|
|
91 static int boyer_moore ();
|
10020
|
92 static int search_buffer ();
|
|
93
|
603
|
94 static void
|
|
95 matcher_overflow ()
|
|
96 {
|
|
97 error ("Stack overflow in regexp matcher");
|
|
98 }
|
|
99
|
|
100 #ifdef __STDC__
|
|
101 #define CONST const
|
|
102 #else
|
|
103 #define CONST
|
|
104 #endif
|
|
105
|
10020
|
106 /* Compile a regexp and signal a Lisp error if anything goes wrong.
|
|
107 PATTERN is the pattern to compile.
|
|
108 CP is the place to put the result.
|
20869
|
109 TRANSLATE is a translation table for ignoring case, or nil for none.
|
10020
|
110 REGP is the structure that says where to store the "register"
|
|
111 values that will result from matching this pattern.
|
|
112 If it is 0, we should compile the pattern not to record any
|
|
113 subexpression bounds.
|
|
114 POSIX is nonzero if we want full backtracking (POSIX style)
|
17053
|
115 for this pattern. 0 means backtrack only enough to get a valid match.
|
|
116 MULTIBYTE is nonzero if we want to handle multibyte characters in
|
|
117 PATTERN. 0 means all multibyte characters are recognized just as
|
|
118 sequences of binary data. */
|
603
|
119
|
9605
|
120 static void
|
17053
|
121 compile_pattern_1 (cp, pattern, translate, regp, posix, multibyte)
|
9605
|
122 struct regexp_cache *cp;
|
603
|
123 Lisp_Object pattern;
|
20869
|
124 Lisp_Object translate;
|
708
|
125 struct re_registers *regp;
|
10020
|
126 int posix;
|
17053
|
127 int multibyte;
|
603
|
128 {
|
21915
|
129 unsigned char *raw_pattern;
|
20671
|
130 int raw_pattern_size;
|
18762
|
131 char *val;
|
10020
|
132 reg_syntax_t old;
|
603
|
133
|
20671
|
134 /* MULTIBYTE says whether the text to be searched is multibyte.
|
|
135 We must convert PATTERN to match that, or we will not really
|
|
136 find things right. */
|
|
137
|
|
138 if (multibyte == STRING_MULTIBYTE (pattern))
|
|
139 {
|
21915
|
140 raw_pattern = (unsigned char *) XSTRING (pattern)->data;
|
21244
|
141 raw_pattern_size = STRING_BYTES (XSTRING (pattern));
|
20671
|
142 }
|
|
143 else if (multibyte)
|
|
144 {
|
|
145 raw_pattern_size = count_size_as_multibyte (XSTRING (pattern)->data,
|
|
146 XSTRING (pattern)->size);
|
21915
|
147 raw_pattern = (unsigned char *) alloca (raw_pattern_size + 1);
|
20671
|
148 copy_text (XSTRING (pattern)->data, raw_pattern,
|
|
149 XSTRING (pattern)->size, 0, 1);
|
|
150 }
|
|
151 else
|
|
152 {
|
|
153 /* Converting multibyte to single-byte.
|
|
154
|
|
155 ??? Perhaps this conversion should be done in a special way
|
|
156 by subtracting nonascii-insert-offset from each non-ASCII char,
|
|
157 so that only the multibyte chars which really correspond to
|
|
158 the chosen single-byte character set can possibly match. */
|
|
159 raw_pattern_size = XSTRING (pattern)->size;
|
21915
|
160 raw_pattern = (unsigned char *) alloca (raw_pattern_size + 1);
|
20671
|
161 copy_text (XSTRING (pattern)->data, raw_pattern,
|
21244
|
162 STRING_BYTES (XSTRING (pattern)), 1, 0);
|
20671
|
163 }
|
|
164
|
9605
|
165 cp->regexp = Qnil;
|
21531
|
166 cp->buf.translate = (! NILP (translate) ? translate : make_number (0));
|
10020
|
167 cp->posix = posix;
|
17053
|
168 cp->buf.multibyte = multibyte;
|
2439
|
169 BLOCK_INPUT;
|
25441
|
170 old = re_set_syntax (RE_SYNTAX_EMACS | RE_CHAR_CLASSES
|
10020
|
171 | (posix ? 0 : RE_NO_POSIX_BACKTRACKING));
|
21915
|
172 val = (char *) re_compile_pattern ((char *)raw_pattern,
|
|
173 raw_pattern_size, &cp->buf);
|
10020
|
174 re_set_syntax (old);
|
2439
|
175 UNBLOCK_INPUT;
|
603
|
176 if (val)
|
9605
|
177 Fsignal (Qinvalid_regexp, Fcons (build_string (val), Qnil));
|
708
|
178
|
9605
|
179 cp->regexp = Fcopy_sequence (pattern);
|
|
180 }
|
708
|
181
|
22221
|
182 /* Shrink each compiled regexp buffer in the cache
|
|
183 to the size actually used right now.
|
|
184 This is called from garbage collection. */
|
|
185
|
|
186 void
|
|
187 shrink_regexp_cache ()
|
|
188 {
|
|
189 struct regexp_cache *cp, **cpp;
|
|
190
|
|
191 for (cp = searchbuf_head; cp != 0; cp = cp->next)
|
|
192 {
|
|
193 cp->buf.allocated = cp->buf.used;
|
|
194 cp->buf.buffer
|
|
195 = (unsigned char *) realloc (cp->buf.buffer, cp->buf.used);
|
|
196 }
|
|
197 }
|
|
198
|
9605
|
199 /* Compile a regexp if necessary, but first check to see if there's one in
|
10020
|
200 the cache.
|
|
201 PATTERN is the pattern to compile.
|
20869
|
202 TRANSLATE is a translation table for ignoring case, or nil for none.
|
10020
|
203 REGP is the structure that says where to store the "register"
|
|
204 values that will result from matching this pattern.
|
|
205 If it is 0, we should compile the pattern not to record any
|
|
206 subexpression bounds.
|
|
207 POSIX is nonzero if we want full backtracking (POSIX style)
|
|
208 for this pattern. 0 means backtrack only enough to get a valid match. */
|
9605
|
209
|
|
210 struct re_pattern_buffer *
|
20588
|
211 compile_pattern (pattern, regp, translate, posix, multibyte)
|
9605
|
212 Lisp_Object pattern;
|
|
213 struct re_registers *regp;
|
20869
|
214 Lisp_Object translate;
|
20588
|
215 int posix, multibyte;
|
9605
|
216 {
|
|
217 struct regexp_cache *cp, **cpp;
|
|
218
|
|
219 for (cpp = &searchbuf_head; ; cpp = &cp->next)
|
|
220 {
|
|
221 cp = *cpp;
|
16275
|
222 if (XSTRING (cp->regexp)->size == XSTRING (pattern)->size
|
|
223 && !NILP (Fstring_equal (cp->regexp, pattern))
|
21531
|
224 && EQ (cp->buf.translate, (! NILP (translate) ? translate : make_number (0)))
|
17053
|
225 && cp->posix == posix
|
|
226 && cp->buf.multibyte == multibyte)
|
9605
|
227 break;
|
|
228
|
|
229 /* If we're at the end of the cache, compile into the last cell. */
|
|
230 if (cp->next == 0)
|
|
231 {
|
17053
|
232 compile_pattern_1 (cp, pattern, translate, regp, posix, multibyte);
|
9605
|
233 break;
|
|
234 }
|
|
235 }
|
|
236
|
|
237 /* When we get here, cp (aka *cpp) contains the compiled pattern,
|
|
238 either because we found it in the cache or because we just compiled it.
|
|
239 Move it to the front of the queue to mark it as most recently used. */
|
|
240 *cpp = cp->next;
|
|
241 cp->next = searchbuf_head;
|
|
242 searchbuf_head = cp;
|
|
243
|
10141
|
244 /* Advise the searching functions about the space we have allocated
|
|
245 for register data. */
|
|
246 if (regp)
|
|
247 re_set_registers (&cp->buf, regp, regp->num_regs, regp->start, regp->end);
|
|
248
|
9605
|
249 return &cp->buf;
|
603
|
250 }
|
|
251
|
|
252 /* Error condition used for failing searches */
|
|
253 Lisp_Object Qsearch_failed;
|
|
254
|
|
255 Lisp_Object
|
|
256 signal_failure (arg)
|
|
257 Lisp_Object arg;
|
|
258 {
|
|
259 Fsignal (Qsearch_failed, Fcons (arg, Qnil));
|
|
260 return Qnil;
|
|
261 }
|
|
262
|
10020
|
263 static Lisp_Object
|
|
264 looking_at_1 (string, posix)
|
603
|
265 Lisp_Object string;
|
10020
|
266 int posix;
|
603
|
267 {
|
|
268 Lisp_Object val;
|
|
269 unsigned char *p1, *p2;
|
|
270 int s1, s2;
|
|
271 register int i;
|
9605
|
272 struct re_pattern_buffer *bufp;
|
603
|
273
|
10032
|
274 if (running_asynch_code)
|
|
275 save_search_regs ();
|
|
276
|
603
|
277 CHECK_STRING (string, 0);
|
9605
|
278 bufp = compile_pattern (string, &search_regs,
|
|
279 (!NILP (current_buffer->case_fold_search)
|
20869
|
280 ? DOWNCASE_TABLE : Qnil),
|
20588
|
281 posix,
|
|
282 !NILP (current_buffer->enable_multibyte_characters));
|
603
|
283
|
|
284 immediate_quit = 1;
|
|
285 QUIT; /* Do a pending quit right away, to avoid paradoxical behavior */
|
|
286
|
|
287 /* Get pointers and sizes of the two strings
|
|
288 that make up the visible portion of the buffer. */
|
|
289
|
|
290 p1 = BEGV_ADDR;
|
20545
|
291 s1 = GPT_BYTE - BEGV_BYTE;
|
603
|
292 p2 = GAP_END_ADDR;
|
20545
|
293 s2 = ZV_BYTE - GPT_BYTE;
|
603
|
294 if (s1 < 0)
|
|
295 {
|
|
296 p2 = p1;
|
20545
|
297 s2 = ZV_BYTE - BEGV_BYTE;
|
603
|
298 s1 = 0;
|
|
299 }
|
|
300 if (s2 < 0)
|
|
301 {
|
20545
|
302 s1 = ZV_BYTE - BEGV_BYTE;
|
603
|
303 s2 = 0;
|
|
304 }
|
17463
|
305
|
|
306 re_match_object = Qnil;
|
603
|
307
|
9605
|
308 i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2,
|
20545
|
309 PT_BYTE - BEGV_BYTE, &search_regs,
|
|
310 ZV_BYTE - BEGV_BYTE);
|
26985
|
311 immediate_quit = 0;
|
|
312
|
603
|
313 if (i == -2)
|
|
314 matcher_overflow ();
|
|
315
|
|
316 val = (0 <= i ? Qt : Qnil);
|
20545
|
317 if (i >= 0)
|
|
318 for (i = 0; i < search_regs.num_regs; i++)
|
|
319 if (search_regs.start[i] >= 0)
|
|
320 {
|
|
321 search_regs.start[i]
|
|
322 = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE);
|
|
323 search_regs.end[i]
|
|
324 = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE);
|
|
325 }
|
9278
f2138d548313
(Flooking_at, skip_chars, search_buffer, set_search_regs, Fstore_match_data):
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
326 XSETBUFFER (last_thing_searched, current_buffer);
|
603
|
327 return val;
|
|
328 }
|
|
329
|
10020
|
330 DEFUN ("looking-at", Flooking_at, Slooking_at, 1, 1, 0,
|
11213
|
331 "Return t if text after point matches regular expression REGEXP.\n\
|
10020
|
332 This function modifies the match data that `match-beginning',\n\
|
|
333 `match-end' and `match-data' access; save and restore the match\n\
|
|
334 data if you want to preserve them.")
|
11213
|
335 (regexp)
|
|
336 Lisp_Object regexp;
|
10020
|
337 {
|
11213
|
338 return looking_at_1 (regexp, 0);
|
10020
|
339 }
|
|
340
|
|
341 DEFUN ("posix-looking-at", Fposix_looking_at, Sposix_looking_at, 1, 1, 0,
|
11213
|
342 "Return t if text after point matches regular expression REGEXP.\n\
|
10020
|
343 Find the longest match, in accord with Posix regular expression rules.\n\
|
|
344 This function modifies the match data that `match-beginning',\n\
|
|
345 `match-end' and `match-data' access; save and restore the match\n\
|
|
346 data if you want to preserve them.")
|
11213
|
347 (regexp)
|
|
348 Lisp_Object regexp;
|
10020
|
349 {
|
11213
|
350 return looking_at_1 (regexp, 1);
|
10020
|
351 }
|
|
352
|
|
353 static Lisp_Object
|
|
354 string_match_1 (regexp, string, start, posix)
|
603
|
355 Lisp_Object regexp, string, start;
|
10020
|
356 int posix;
|
603
|
357 {
|
|
358 int val;
|
9605
|
359 struct re_pattern_buffer *bufp;
|
20588
|
360 int pos, pos_byte;
|
|
361 int i;
|
603
|
362
|
10032
|
363 if (running_asynch_code)
|
|
364 save_search_regs ();
|
|
365
|
603
|
366 CHECK_STRING (regexp, 0);
|
|
367 CHECK_STRING (string, 1);
|
|
368
|
|
369 if (NILP (start))
|
20588
|
370 pos = 0, pos_byte = 0;
|
603
|
371 else
|
|
372 {
|
|
373 int len = XSTRING (string)->size;
|
|
374
|
|
375 CHECK_NUMBER (start, 2);
|
20588
|
376 pos = XINT (start);
|
|
377 if (pos < 0 && -pos <= len)
|
|
378 pos = len + pos;
|
|
379 else if (0 > pos || pos > len)
|
603
|
380 args_out_of_range (string, start);
|
20588
|
381 pos_byte = string_char_to_byte (string, pos);
|
603
|
382 }
|
|
383
|
9605
|
384 bufp = compile_pattern (regexp, &search_regs,
|
|
385 (!NILP (current_buffer->case_fold_search)
|
20869
|
386 ? DOWNCASE_TABLE : Qnil),
|
20588
|
387 posix,
|
|
388 STRING_MULTIBYTE (string));
|
603
|
389 immediate_quit = 1;
|
17463
|
390 re_match_object = string;
|
|
391
|
9605
|
392 val = re_search (bufp, (char *) XSTRING (string)->data,
|
21244
|
393 STRING_BYTES (XSTRING (string)), pos_byte,
|
|
394 STRING_BYTES (XSTRING (string)) - pos_byte,
|
603
|
395 &search_regs);
|
|
396 immediate_quit = 0;
|
727
|
397 last_thing_searched = Qt;
|
603
|
398 if (val == -2)
|
|
399 matcher_overflow ();
|
|
400 if (val < 0) return Qnil;
|
20588
|
401
|
|
402 for (i = 0; i < search_regs.num_regs; i++)
|
|
403 if (search_regs.start[i] >= 0)
|
|
404 {
|
|
405 search_regs.start[i]
|
|
406 = string_byte_to_char (string, search_regs.start[i]);
|
|
407 search_regs.end[i]
|
|
408 = string_byte_to_char (string, search_regs.end[i]);
|
|
409 }
|
|
410
|
|
411 return make_number (string_byte_to_char (string, val));
|
603
|
412 }
|
842
|
413
|
10020
|
414 DEFUN ("string-match", Fstring_match, Sstring_match, 2, 3, 0,
|
|
415 "Return index of start of first match for REGEXP in STRING, or nil.\n\
|
24433
|
416 Case is ignored if `case-fold-search' is non-nil in the current buffer.\n\
|
10020
|
417 If third arg START is non-nil, start search at that index in STRING.\n\
|
|
418 For index of first char beyond the match, do (match-end 0).\n\
|
|
419 `match-end' and `match-beginning' also give indices of substrings\n\
|
|
420 matched by parenthesis constructs in the pattern.")
|
|
421 (regexp, string, start)
|
|
422 Lisp_Object regexp, string, start;
|
|
423 {
|
|
424 return string_match_1 (regexp, string, start, 0);
|
|
425 }
|
|
426
|
|
427 DEFUN ("posix-string-match", Fposix_string_match, Sposix_string_match, 2, 3, 0,
|
|
428 "Return index of start of first match for REGEXP in STRING, or nil.\n\
|
|
429 Find the longest match, in accord with Posix regular expression rules.\n\
|
24433
|
430 Case is ignored if `case-fold-search' is non-nil in the current buffer.\n\
|
10020
|
431 If third arg START is non-nil, start search at that index in STRING.\n\
|
|
432 For index of first char beyond the match, do (match-end 0).\n\
|
|
433 `match-end' and `match-beginning' also give indices of substrings\n\
|
|
434 matched by parenthesis constructs in the pattern.")
|
|
435 (regexp, string, start)
|
|
436 Lisp_Object regexp, string, start;
|
|
437 {
|
|
438 return string_match_1 (regexp, string, start, 1);
|
|
439 }
|
|
440
|
842
|
441 /* Match REGEXP against STRING, searching all of STRING,
|
|
442 and return the index of the match, or negative on failure.
|
|
443 This does not clobber the match data. */
|
|
444
|
|
445 int
|
|
446 fast_string_match (regexp, string)
|
|
447 Lisp_Object regexp, string;
|
|
448 {
|
|
449 int val;
|
9605
|
450 struct re_pattern_buffer *bufp;
|
842
|
451
|
20869
|
452 bufp = compile_pattern (regexp, 0, Qnil,
|
|
453 0, STRING_MULTIBYTE (string));
|
842
|
454 immediate_quit = 1;
|
17463
|
455 re_match_object = string;
|
|
456
|
9605
|
457 val = re_search (bufp, (char *) XSTRING (string)->data,
|
21244
|
458 STRING_BYTES (XSTRING (string)), 0,
|
|
459 STRING_BYTES (XSTRING (string)), 0);
|
842
|
460 immediate_quit = 0;
|
|
461 return val;
|
|
462 }
|
17053
|
463
|
|
464 /* Match REGEXP against STRING, searching all of STRING ignoring case,
|
|
465 and return the index of the match, or negative on failure.
|
20588
|
466 This does not clobber the match data.
|
|
467 We assume that STRING contains single-byte characters. */
|
17053
|
468
|
|
469 extern Lisp_Object Vascii_downcase_table;
|
|
470
|
|
471 int
|
18193
|
472 fast_c_string_match_ignore_case (regexp, string)
|
17053
|
473 Lisp_Object regexp;
|
|
474 char *string;
|
|
475 {
|
|
476 int val;
|
|
477 struct re_pattern_buffer *bufp;
|
|
478 int len = strlen (string);
|
|
479
|
20588
|
480 regexp = string_make_unibyte (regexp);
|
18193
|
481 re_match_object = Qt;
|
17053
|
482 bufp = compile_pattern (regexp, 0,
|
20869
|
483 Vascii_downcase_table, 0,
|
20671
|
484 0);
|
17053
|
485 immediate_quit = 1;
|
|
486 val = re_search (bufp, string, len, 0, len, 0);
|
|
487 immediate_quit = 0;
|
|
488 return val;
|
|
489 }
|
603
|
490
|
9410
|
491 /* The newline cache: remembering which sections of text have no newlines. */
|
|
492
|
|
493 /* If the user has requested newline caching, make sure it's on.
|
|
494 Otherwise, make sure it's off.
|
|
495 This is our cheezy way of associating an action with the change of
|
|
496 state of a buffer-local variable. */
|
|
497 static void
|
|
498 newline_cache_on_off (buf)
|
|
499 struct buffer *buf;
|
|
500 {
|
|
501 if (NILP (buf->cache_long_line_scans))
|
|
502 {
|
|
503 /* It should be off. */
|
|
504 if (buf->newline_cache)
|
|
505 {
|
|
506 free_region_cache (buf->newline_cache);
|
|
507 buf->newline_cache = 0;
|
|
508 }
|
|
509 }
|
|
510 else
|
|
511 {
|
|
512 /* It should be on. */
|
|
513 if (buf->newline_cache == 0)
|
|
514 buf->newline_cache = new_region_cache ();
|
|
515 }
|
|
516 }
|
|
517
|
|
518
|
|
519 /* Search for COUNT instances of the character TARGET between START and END.
|
|
520
|
|
521 If COUNT is positive, search forwards; END must be >= START.
|
|
522 If COUNT is negative, search backwards for the -COUNTth instance;
|
|
523 END must be <= START.
|
|
524 If COUNT is zero, do anything you please; run rogue, for all I care.
|
|
525
|
|
526 If END is zero, use BEGV or ZV instead, as appropriate for the
|
|
527 direction indicated by COUNT.
|
648
|
528
|
|
529 If we find COUNT instances, set *SHORTAGE to zero, and return the
|
1413
|
530 position after the COUNTth match. Note that for reverse motion
|
|
531 this is not the same as the usual convention for Emacs motion commands.
|
648
|
532
|
9410
|
533 If we don't find COUNT instances before reaching END, set *SHORTAGE
|
|
534 to the number of TARGETs left unfound, and return END.
|
648
|
535
|
5756
|
536 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
|
|
537 except when inside redisplay. */
|
|
538
|
21514
|
539 int
|
9410
|
540 scan_buffer (target, start, end, count, shortage, allow_quit)
|
|
541 register int target;
|
|
542 int start, end;
|
|
543 int count;
|
|
544 int *shortage;
|
5756
|
545 int allow_quit;
|
603
|
546 {
|
9410
|
547 struct region_cache *newline_cache;
|
|
548 int direction;
|
648
|
549
|
9410
|
550 if (count > 0)
|
|
551 {
|
|
552 direction = 1;
|
|
553 if (! end) end = ZV;
|
|
554 }
|
|
555 else
|
|
556 {
|
|
557 direction = -1;
|
|
558 if (! end) end = BEGV;
|
|
559 }
|
648
|
560
|
9410
|
561 newline_cache_on_off (current_buffer);
|
|
562 newline_cache = current_buffer->newline_cache;
|
603
|
563
|
|
564 if (shortage != 0)
|
|
565 *shortage = 0;
|
|
566
|
5756
|
567 immediate_quit = allow_quit;
|
603
|
568
|
648
|
569 if (count > 0)
|
9410
|
570 while (start != end)
|
603
|
571 {
|
9410
|
572 /* Our innermost scanning loop is very simple; it doesn't know
|
|
573 about gaps, buffer ends, or the newline cache. ceiling is
|
|
574 the position of the last character before the next such
|
|
575 obstacle --- the last character the dumb search loop should
|
|
576 examine. */
|
20545
|
577 int ceiling_byte = CHAR_TO_BYTE (end) - 1;
|
|
578 int start_byte = CHAR_TO_BYTE (start);
|
21457
|
579 int tem;
|
9410
|
580
|
|
581 /* If we're looking for a newline, consult the newline cache
|
|
582 to see where we can avoid some scanning. */
|
|
583 if (target == '\n' && newline_cache)
|
|
584 {
|
|
585 int next_change;
|
|
586 immediate_quit = 0;
|
|
587 while (region_cache_forward
|
20545
|
588 (current_buffer, newline_cache, start_byte, &next_change))
|
|
589 start_byte = next_change;
|
9452
76f75b9091f1
(scan_buffer): After temporarily turning immediate_quit off, turn it
Jim Blandy <jimb@redhat.com>
diff
changeset
|
590 immediate_quit = allow_quit;
|
9410
|
591
|
20545
|
592 /* START should never be after END. */
|
|
593 if (start_byte > ceiling_byte)
|
|
594 start_byte = ceiling_byte;
|
9410
|
595
|
|
596 /* Now the text after start is an unknown region, and
|
|
597 next_change is the position of the next known region. */
|
20545
|
598 ceiling_byte = min (next_change - 1, ceiling_byte);
|
9410
|
599 }
|
|
600
|
|
601 /* The dumb loop can only scan text stored in contiguous
|
|
602 bytes. BUFFER_CEILING_OF returns the last character
|
|
603 position that is contiguous, so the ceiling is the
|
|
604 position after that. */
|
21457
|
605 tem = BUFFER_CEILING_OF (start_byte);
|
|
606 ceiling_byte = min (tem, ceiling_byte);
|
9410
|
607
|
|
608 {
|
|
609 /* The termination address of the dumb loop. */
|
20545
|
610 register unsigned char *ceiling_addr
|
|
611 = BYTE_POS_ADDR (ceiling_byte) + 1;
|
|
612 register unsigned char *cursor
|
|
613 = BYTE_POS_ADDR (start_byte);
|
9410
|
614 unsigned char *base = cursor;
|
|
615
|
|
616 while (cursor < ceiling_addr)
|
|
617 {
|
|
618 unsigned char *scan_start = cursor;
|
|
619
|
|
620 /* The dumb loop. */
|
|
621 while (*cursor != target && ++cursor < ceiling_addr)
|
|
622 ;
|
|
623
|
|
624 /* If we're looking for newlines, cache the fact that
|
|
625 the region from start to cursor is free of them. */
|
|
626 if (target == '\n' && newline_cache)
|
|
627 know_region_cache (current_buffer, newline_cache,
|
20545
|
628 start_byte + scan_start - base,
|
|
629 start_byte + cursor - base);
|
9410
|
630
|
|
631 /* Did we find the target character? */
|
|
632 if (cursor < ceiling_addr)
|
|
633 {
|
|
634 if (--count == 0)
|
|
635 {
|
|
636 immediate_quit = 0;
|
20545
|
637 return BYTE_TO_CHAR (start_byte + cursor - base + 1);
|
9410
|
638 }
|
|
639 cursor++;
|
|
640 }
|
|
641 }
|
|
642
|
20545
|
643 start = BYTE_TO_CHAR (start_byte + cursor - base);
|
9410
|
644 }
|
603
|
645 }
|
|
646 else
|
9410
|
647 while (start > end)
|
|
648 {
|
|
649 /* The last character to check before the next obstacle. */
|
20545
|
650 int ceiling_byte = CHAR_TO_BYTE (end);
|
|
651 int start_byte = CHAR_TO_BYTE (start);
|
21457
|
652 int tem;
|
9410
|
653
|
|
654 /* Consult the newline cache, if appropriate. */
|
|
655 if (target == '\n' && newline_cache)
|
|
656 {
|
|
657 int next_change;
|
|
658 immediate_quit = 0;
|
|
659 while (region_cache_backward
|
20545
|
660 (current_buffer, newline_cache, start_byte, &next_change))
|
|
661 start_byte = next_change;
|
9452
76f75b9091f1
(scan_buffer): After temporarily turning immediate_quit off, turn it
Jim Blandy <jimb@redhat.com>
diff
changeset
|
662 immediate_quit = allow_quit;
|
9410
|
663
|
|
664 /* Start should never be at or before end. */
|
20545
|
665 if (start_byte <= ceiling_byte)
|
|
666 start_byte = ceiling_byte + 1;
|
9410
|
667
|
|
668 /* Now the text before start is an unknown region, and
|
|
669 next_change is the position of the next known region. */
|
20545
|
670 ceiling_byte = max (next_change, ceiling_byte);
|
9410
|
671 }
|
|
672
|
|
673 /* Stop scanning before the gap. */
|
21457
|
674 tem = BUFFER_FLOOR_OF (start_byte - 1);
|
|
675 ceiling_byte = max (tem, ceiling_byte);
|
9410
|
676
|
|
677 {
|
|
678 /* The termination address of the dumb loop. */
|
20545
|
679 register unsigned char *ceiling_addr = BYTE_POS_ADDR (ceiling_byte);
|
|
680 register unsigned char *cursor = BYTE_POS_ADDR (start_byte - 1);
|
9410
|
681 unsigned char *base = cursor;
|
|
682
|
|
683 while (cursor >= ceiling_addr)
|
|
684 {
|
|
685 unsigned char *scan_start = cursor;
|
|
686
|
|
687 while (*cursor != target && --cursor >= ceiling_addr)
|
|
688 ;
|
|
689
|
|
690 /* If we're looking for newlines, cache the fact that
|
|
691 the region from after the cursor to start is free of them. */
|
|
692 if (target == '\n' && newline_cache)
|
|
693 know_region_cache (current_buffer, newline_cache,
|
20545
|
694 start_byte + cursor - base,
|
|
695 start_byte + scan_start - base);
|
9410
|
696
|
|
697 /* Did we find the target character? */
|
|
698 if (cursor >= ceiling_addr)
|
|
699 {
|
|
700 if (++count >= 0)
|
|
701 {
|
|
702 immediate_quit = 0;
|
20545
|
703 return BYTE_TO_CHAR (start_byte + cursor - base);
|
9410
|
704 }
|
|
705 cursor--;
|
|
706 }
|
|
707 }
|
|
708
|
20545
|
709 start = BYTE_TO_CHAR (start_byte + cursor - base);
|
9410
|
710 }
|
|
711 }
|
|
712
|
603
|
713 immediate_quit = 0;
|
|
714 if (shortage != 0)
|
648
|
715 *shortage = count * direction;
|
9410
|
716 return start;
|
603
|
717 }
|
20545
|
718
|
|
719 /* Search for COUNT instances of a line boundary, which means either a
|
|
720 newline or (if selective display enabled) a carriage return.
|
|
721 Start at START. If COUNT is negative, search backwards.
|
|
722
|
|
723 We report the resulting position by calling TEMP_SET_PT_BOTH.
|
|
724
|
|
725 If we find COUNT instances. we position after (always after,
|
|
726 even if scanning backwards) the COUNTth match, and return 0.
|
|
727
|
|
728 If we don't find COUNT instances before reaching the end of the
|
|
729 buffer (or the beginning, if scanning backwards), we return
|
|
730 the number of line boundaries left unfound, and position at
|
|
731 the limit we bumped up against.
|
|
732
|
|
733 If ALLOW_QUIT is non-zero, set immediate_quit. That's good to do
|
20547
|
734 except in special cases. */
|
20545
|
735
|
|
736 int
|
|
737 scan_newline (start, start_byte, limit, limit_byte, count, allow_quit)
|
|
738 int start, start_byte;
|
|
739 int limit, limit_byte;
|
|
740 register int count;
|
|
741 int allow_quit;
|
|
742 {
|
|
743 int direction = ((count > 0) ? 1 : -1);
|
|
744
|
|
745 register unsigned char *cursor;
|
|
746 unsigned char *base;
|
|
747
|
|
748 register int ceiling;
|
|
749 register unsigned char *ceiling_addr;
|
|
750
|
20547
|
751 int old_immediate_quit = immediate_quit;
|
|
752
|
20545
|
753 /* If we are not in selective display mode,
|
|
754 check only for newlines. */
|
|
755 int selective_display = (!NILP (current_buffer->selective_display)
|
|
756 && !INTEGERP (current_buffer->selective_display));
|
|
757
|
|
758 /* The code that follows is like scan_buffer
|
|
759 but checks for either newline or carriage return. */
|
|
760
|
20547
|
761 if (allow_quit)
|
|
762 immediate_quit++;
|
20545
|
763
|
|
764 start_byte = CHAR_TO_BYTE (start);
|
|
765
|
|
766 if (count > 0)
|
|
767 {
|
|
768 while (start_byte < limit_byte)
|
|
769 {
|
|
770 ceiling = BUFFER_CEILING_OF (start_byte);
|
|
771 ceiling = min (limit_byte - 1, ceiling);
|
|
772 ceiling_addr = BYTE_POS_ADDR (ceiling) + 1;
|
|
773 base = (cursor = BYTE_POS_ADDR (start_byte));
|
|
774 while (1)
|
|
775 {
|
|
776 while (*cursor != '\n' && ++cursor != ceiling_addr)
|
|
777 ;
|
|
778
|
|
779 if (cursor != ceiling_addr)
|
|
780 {
|
|
781 if (--count == 0)
|
|
782 {
|
20547
|
783 immediate_quit = old_immediate_quit;
|
20545
|
784 start_byte = start_byte + cursor - base + 1;
|
|
785 start = BYTE_TO_CHAR (start_byte);
|
|
786 TEMP_SET_PT_BOTH (start, start_byte);
|
|
787 return 0;
|
|
788 }
|
|
789 else
|
|
790 if (++cursor == ceiling_addr)
|
|
791 break;
|
|
792 }
|
|
793 else
|
|
794 break;
|
|
795 }
|
|
796 start_byte += cursor - base;
|
|
797 }
|
|
798 }
|
|
799 else
|
|
800 {
|
|
801 while (start_byte > limit_byte)
|
|
802 {
|
|
803 ceiling = BUFFER_FLOOR_OF (start_byte - 1);
|
|
804 ceiling = max (limit_byte, ceiling);
|
|
805 ceiling_addr = BYTE_POS_ADDR (ceiling) - 1;
|
|
806 base = (cursor = BYTE_POS_ADDR (start_byte - 1) + 1);
|
|
807 while (1)
|
|
808 {
|
|
809 while (--cursor != ceiling_addr && *cursor != '\n')
|
|
810 ;
|
|
811
|
|
812 if (cursor != ceiling_addr)
|
|
813 {
|
|
814 if (++count == 0)
|
|
815 {
|
20547
|
816 immediate_quit = old_immediate_quit;
|
20545
|
817 /* Return the position AFTER the match we found. */
|
|
818 start_byte = start_byte + cursor - base + 1;
|
|
819 start = BYTE_TO_CHAR (start_byte);
|
|
820 TEMP_SET_PT_BOTH (start, start_byte);
|
|
821 return 0;
|
|
822 }
|
|
823 }
|
|
824 else
|
|
825 break;
|
|
826 }
|
|
827 /* Here we add 1 to compensate for the last decrement
|
|
828 of CURSOR, which took it past the valid range. */
|
|
829 start_byte += cursor - base + 1;
|
|
830 }
|
|
831 }
|
|
832
|
|
833 TEMP_SET_PT_BOTH (limit, limit_byte);
|
20547
|
834 immediate_quit = old_immediate_quit;
|
20545
|
835
|
|
836 return count * direction;
|
|
837 }
|
603
|
838
|
|
839 int
|
7891
|
840 find_next_newline_no_quit (from, cnt)
|
|
841 register int from, cnt;
|
|
842 {
|
9410
|
843 return scan_buffer ('\n', from, 0, cnt, (int *) 0, 0);
|
7891
|
844 }
|
|
845
|
9410
|
846 /* Like find_next_newline, but returns position before the newline,
|
|
847 not after, and only search up to TO. This isn't just
|
|
848 find_next_newline (...)-1, because you might hit TO. */
|
20545
|
849
|
9410
|
850 int
|
|
851 find_before_next_newline (from, to, cnt)
|
9452
76f75b9091f1
(scan_buffer): After temporarily turning immediate_quit off, turn it
Jim Blandy <jimb@redhat.com>
diff
changeset
|
852 int from, to, cnt;
|
9410
|
853 {
|
|
854 int shortage;
|
|
855 int pos = scan_buffer ('\n', from, to, cnt, &shortage, 1);
|
|
856
|
|
857 if (shortage == 0)
|
|
858 pos--;
|
|
859
|
|
860 return pos;
|
603
|
861 }
|
|
862
|
|
863 /* Subroutines of Lisp buffer search functions. */
|
|
864
|
|
865 static Lisp_Object
|
10020
|
866 search_command (string, bound, noerror, count, direction, RE, posix)
|
603
|
867 Lisp_Object string, bound, noerror, count;
|
|
868 int direction;
|
|
869 int RE;
|
10020
|
870 int posix;
|
603
|
871 {
|
|
872 register int np;
|
20824
|
873 int lim, lim_byte;
|
603
|
874 int n = direction;
|
|
875
|
|
876 if (!NILP (count))
|
|
877 {
|
|
878 CHECK_NUMBER (count, 3);
|
|
879 n *= XINT (count);
|
|
880 }
|
|
881
|
|
882 CHECK_STRING (string, 0);
|
|
883 if (NILP (bound))
|
20824
|
884 {
|
|
885 if (n > 0)
|
|
886 lim = ZV, lim_byte = ZV_BYTE;
|
|
887 else
|
|
888 lim = BEGV, lim_byte = BEGV_BYTE;
|
|
889 }
|
603
|
890 else
|
|
891 {
|
|
892 CHECK_NUMBER_COERCE_MARKER (bound, 1);
|
|
893 lim = XINT (bound);
|
16039
|
894 if (n > 0 ? lim < PT : lim > PT)
|
603
|
895 error ("Invalid search bound (wrong side of point)");
|
|
896 if (lim > ZV)
|
20824
|
897 lim = ZV, lim_byte = ZV_BYTE;
|
20924
|
898 else if (lim < BEGV)
|
20824
|
899 lim = BEGV, lim_byte = BEGV_BYTE;
|
20924
|
900 else
|
|
901 lim_byte = CHAR_TO_BYTE (lim);
|
603
|
902 }
|
|
903
|
20824
|
904 np = search_buffer (string, PT, PT_BYTE, lim, lim_byte, n, RE,
|
603
|
905 (!NILP (current_buffer->case_fold_search)
|
20869
|
906 ? current_buffer->case_canon_table
|
20875
|
907 : Qnil),
|
603
|
908 (!NILP (current_buffer->case_fold_search)
|
20869
|
909 ? current_buffer->case_eqv_table
|
20875
|
910 : Qnil),
|
10020
|
911 posix);
|
603
|
912 if (np <= 0)
|
|
913 {
|
|
914 if (NILP (noerror))
|
|
915 return signal_failure (string);
|
|
916 if (!EQ (noerror, Qt))
|
|
917 {
|
|
918 if (lim < BEGV || lim > ZV)
|
|
919 abort ();
|
20824
|
920 SET_PT_BOTH (lim, lim_byte);
|
1878
|
921 return Qnil;
|
|
922 #if 0 /* This would be clean, but maybe programs depend on
|
|
923 a value of nil here. */
|
1877
|
924 np = lim;
|
1878
|
925 #endif
|
603
|
926 }
|
1877
|
927 else
|
|
928 return Qnil;
|
603
|
929 }
|
|
930
|
|
931 if (np < BEGV || np > ZV)
|
|
932 abort ();
|
|
933
|
|
934 SET_PT (np);
|
|
935
|
|
936 return make_number (np);
|
|
937 }
|
|
938
|
20545
|
939 /* Return 1 if REGEXP it matches just one constant string. */
|
|
940
|
8950
|
941 static int
|
|
942 trivial_regexp_p (regexp)
|
|
943 Lisp_Object regexp;
|
|
944 {
|
21244
|
945 int len = STRING_BYTES (XSTRING (regexp));
|
8950
|
946 unsigned char *s = XSTRING (regexp)->data;
|
|
947 unsigned char c;
|
|
948 while (--len >= 0)
|
|
949 {
|
|
950 switch (*s++)
|
|
951 {
|
|
952 case '.': case '*': case '+': case '?': case '[': case '^': case '$':
|
|
953 return 0;
|
|
954 case '\\':
|
|
955 if (--len < 0)
|
|
956 return 0;
|
|
957 switch (*s++)
|
|
958 {
|
|
959 case '|': case '(': case ')': case '`': case '\'': case 'b':
|
|
960 case 'B': case '<': case '>': case 'w': case 'W': case 's':
|
12069
|
961 case 'S': case '=':
|
17053
|
962 case 'c': case 'C': /* for categoryspec and notcategoryspec */
|
12069
|
963 case '1': case '2': case '3': case '4': case '5':
|
8950
|
964 case '6': case '7': case '8': case '9':
|
|
965 return 0;
|
|
966 }
|
|
967 }
|
|
968 }
|
|
969 return 1;
|
|
970 }
|
|
971
|
5556
|
972 /* Search for the n'th occurrence of STRING in the current buffer,
|
603
|
973 starting at position POS and stopping at position LIM,
|
10020
|
974 treating STRING as a literal string if RE is false or as
|
603
|
975 a regular expression if RE is true.
|
|
976
|
|
977 If N is positive, searching is forward and LIM must be greater than POS.
|
|
978 If N is negative, searching is backward and LIM must be less than POS.
|
|
979
|
20869
|
980 Returns -x if x occurrences remain to be found (x > 0),
|
603
|
981 or else the position at the beginning of the Nth occurrence
|
10020
|
982 (if searching backward) or the end (if searching forward).
|
603
|
983
|
10020
|
984 POSIX is nonzero if we want full backtracking (POSIX style)
|
|
985 for this pattern. 0 means backtrack only enough to get a valid match. */
|
|
986
|
20898
|
987 #define TRANSLATE(out, trt, d) \
|
|
988 do \
|
|
989 { \
|
|
990 if (! NILP (trt)) \
|
|
991 { \
|
|
992 Lisp_Object temp; \
|
|
993 temp = Faref (trt, make_number (d)); \
|
|
994 if (INTEGERP (temp)) \
|
|
995 out = XINT (temp); \
|
|
996 else \
|
|
997 out = d; \
|
|
998 } \
|
|
999 else \
|
|
1000 out = d; \
|
|
1001 } \
|
|
1002 while (0)
|
20869
|
1003
|
10020
|
1004 static int
|
20824
|
1005 search_buffer (string, pos, pos_byte, lim, lim_byte, n,
|
|
1006 RE, trt, inverse_trt, posix)
|
603
|
1007 Lisp_Object string;
|
|
1008 int pos;
|
20824
|
1009 int pos_byte;
|
603
|
1010 int lim;
|
20824
|
1011 int lim_byte;
|
603
|
1012 int n;
|
|
1013 int RE;
|
20869
|
1014 Lisp_Object trt;
|
|
1015 Lisp_Object inverse_trt;
|
10020
|
1016 int posix;
|
603
|
1017 {
|
|
1018 int len = XSTRING (string)->size;
|
21244
|
1019 int len_byte = STRING_BYTES (XSTRING (string));
|
20869
|
1020 register int i;
|
603
|
1021
|
10032
|
1022 if (running_asynch_code)
|
|
1023 save_search_regs ();
|
|
1024
|
22082
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1025 /* Searching 0 times means don't move. */
|
603
|
1026 /* Null string is found at starting position. */
|
22082
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1027 if (len == 0 || n == 0)
|
5556
|
1028 {
|
|
1029 set_search_regs (pos, 0);
|
|
1030 return pos;
|
|
1031 }
|
4299
|
1032
|
8950
|
1033 if (RE && !trivial_regexp_p (string))
|
603
|
1034 {
|
20869
|
1035 unsigned char *p1, *p2;
|
|
1036 int s1, s2;
|
9605
|
1037 struct re_pattern_buffer *bufp;
|
|
1038
|
20588
|
1039 bufp = compile_pattern (string, &search_regs, trt, posix,
|
|
1040 !NILP (current_buffer->enable_multibyte_characters));
|
603
|
1041
|
|
1042 immediate_quit = 1; /* Quit immediately if user types ^G,
|
|
1043 because letting this function finish
|
|
1044 can take too long. */
|
|
1045 QUIT; /* Do a pending quit right away,
|
|
1046 to avoid paradoxical behavior */
|
|
1047 /* Get pointers and sizes of the two strings
|
|
1048 that make up the visible portion of the buffer. */
|
|
1049
|
|
1050 p1 = BEGV_ADDR;
|
20545
|
1051 s1 = GPT_BYTE - BEGV_BYTE;
|
603
|
1052 p2 = GAP_END_ADDR;
|
20545
|
1053 s2 = ZV_BYTE - GPT_BYTE;
|
603
|
1054 if (s1 < 0)
|
|
1055 {
|
|
1056 p2 = p1;
|
20545
|
1057 s2 = ZV_BYTE - BEGV_BYTE;
|
603
|
1058 s1 = 0;
|
|
1059 }
|
|
1060 if (s2 < 0)
|
|
1061 {
|
20545
|
1062 s1 = ZV_BYTE - BEGV_BYTE;
|
603
|
1063 s2 = 0;
|
|
1064 }
|
17463
|
1065 re_match_object = Qnil;
|
|
1066
|
603
|
1067 while (n < 0)
|
|
1068 {
|
2475
|
1069 int val;
|
9605
|
1070 val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2,
|
20792
|
1071 pos_byte - BEGV_BYTE, lim_byte - pos_byte,
|
|
1072 &search_regs,
|
2475
|
1073 /* Don't allow match past current point */
|
20792
|
1074 pos_byte - BEGV_BYTE);
|
603
|
1075 if (val == -2)
|
8950
|
1076 {
|
|
1077 matcher_overflow ();
|
|
1078 }
|
603
|
1079 if (val >= 0)
|
|
1080 {
|
20927
|
1081 pos_byte = search_regs.start[0] + BEGV_BYTE;
|
621
|
1082 for (i = 0; i < search_regs.num_regs; i++)
|
603
|
1083 if (search_regs.start[i] >= 0)
|
|
1084 {
|
20545
|
1085 search_regs.start[i]
|
|
1086 = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE);
|
|
1087 search_regs.end[i]
|
|
1088 = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE);
|
603
|
1089 }
|
9278
f2138d548313
(Flooking_at, skip_chars, search_buffer, set_search_regs, Fstore_match_data):
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1090 XSETBUFFER (last_thing_searched, current_buffer);
|
603
|
1091 /* Set pos to the new position. */
|
|
1092 pos = search_regs.start[0];
|
|
1093 }
|
|
1094 else
|
|
1095 {
|
|
1096 immediate_quit = 0;
|
|
1097 return (n);
|
|
1098 }
|
|
1099 n++;
|
|
1100 }
|
|
1101 while (n > 0)
|
|
1102 {
|
2475
|
1103 int val;
|
9605
|
1104 val = re_search_2 (bufp, (char *) p1, s1, (char *) p2, s2,
|
20792
|
1105 pos_byte - BEGV_BYTE, lim_byte - pos_byte,
|
|
1106 &search_regs,
|
|
1107 lim_byte - BEGV_BYTE);
|
603
|
1108 if (val == -2)
|
8950
|
1109 {
|
|
1110 matcher_overflow ();
|
|
1111 }
|
603
|
1112 if (val >= 0)
|
|
1113 {
|
20927
|
1114 pos_byte = search_regs.end[0] + BEGV_BYTE;
|
621
|
1115 for (i = 0; i < search_regs.num_regs; i++)
|
603
|
1116 if (search_regs.start[i] >= 0)
|
|
1117 {
|
20545
|
1118 search_regs.start[i]
|
|
1119 = BYTE_TO_CHAR (search_regs.start[i] + BEGV_BYTE);
|
|
1120 search_regs.end[i]
|
|
1121 = BYTE_TO_CHAR (search_regs.end[i] + BEGV_BYTE);
|
603
|
1122 }
|
9278
f2138d548313
(Flooking_at, skip_chars, search_buffer, set_search_regs, Fstore_match_data):
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1123 XSETBUFFER (last_thing_searched, current_buffer);
|
603
|
1124 pos = search_regs.end[0];
|
|
1125 }
|
|
1126 else
|
|
1127 {
|
|
1128 immediate_quit = 0;
|
|
1129 return (0 - n);
|
|
1130 }
|
|
1131 n--;
|
|
1132 }
|
|
1133 immediate_quit = 0;
|
|
1134 return (pos);
|
|
1135 }
|
|
1136 else /* non-RE case */
|
|
1137 {
|
20869
|
1138 unsigned char *raw_pattern, *pat;
|
|
1139 int raw_pattern_size;
|
|
1140 int raw_pattern_size_byte;
|
|
1141 unsigned char *patbuf;
|
|
1142 int multibyte = !NILP (current_buffer->enable_multibyte_characters);
|
|
1143 unsigned char *base_pat = XSTRING (string)->data;
|
|
1144 int charset_base = -1;
|
23876
|
1145 int boyer_moore_ok = 1;
|
20869
|
1146
|
|
1147 /* MULTIBYTE says whether the text to be searched is multibyte.
|
|
1148 We must convert PATTERN to match that, or we will not really
|
|
1149 find things right. */
|
|
1150
|
|
1151 if (multibyte == STRING_MULTIBYTE (string))
|
|
1152 {
|
21915
|
1153 raw_pattern = (unsigned char *) XSTRING (string)->data;
|
20869
|
1154 raw_pattern_size = XSTRING (string)->size;
|
21244
|
1155 raw_pattern_size_byte = STRING_BYTES (XSTRING (string));
|
20869
|
1156 }
|
|
1157 else if (multibyte)
|
|
1158 {
|
|
1159 raw_pattern_size = XSTRING (string)->size;
|
|
1160 raw_pattern_size_byte
|
|
1161 = count_size_as_multibyte (XSTRING (string)->data,
|
|
1162 raw_pattern_size);
|
21915
|
1163 raw_pattern = (unsigned char *) alloca (raw_pattern_size_byte + 1);
|
20869
|
1164 copy_text (XSTRING (string)->data, raw_pattern,
|
|
1165 XSTRING (string)->size, 0, 1);
|
|
1166 }
|
|
1167 else
|
|
1168 {
|
|
1169 /* Converting multibyte to single-byte.
|
|
1170
|
|
1171 ??? Perhaps this conversion should be done in a special way
|
|
1172 by subtracting nonascii-insert-offset from each non-ASCII char,
|
|
1173 so that only the multibyte chars which really correspond to
|
|
1174 the chosen single-byte character set can possibly match. */
|
|
1175 raw_pattern_size = XSTRING (string)->size;
|
|
1176 raw_pattern_size_byte = XSTRING (string)->size;
|
21915
|
1177 raw_pattern = (unsigned char *) alloca (raw_pattern_size + 1);
|
20869
|
1178 copy_text (XSTRING (string)->data, raw_pattern,
|
21244
|
1179 STRING_BYTES (XSTRING (string)), 1, 0);
|
20869
|
1180 }
|
|
1181
|
|
1182 /* Copy and optionally translate the pattern. */
|
|
1183 len = raw_pattern_size;
|
|
1184 len_byte = raw_pattern_size_byte;
|
|
1185 patbuf = (unsigned char *) alloca (len_byte);
|
|
1186 pat = patbuf;
|
|
1187 base_pat = raw_pattern;
|
|
1188 if (multibyte)
|
|
1189 {
|
|
1190 while (--len >= 0)
|
|
1191 {
|
26869
|
1192 unsigned char str[MAX_MULTIBYTE_LENGTH];
|
20898
|
1193 int c, translated, inverse;
|
20869
|
1194 int in_charlen, charlen;
|
|
1195
|
|
1196 /* If we got here and the RE flag is set, it's because we're
|
|
1197 dealing with a regexp known to be trivial, so the backslash
|
|
1198 just quotes the next character. */
|
|
1199 if (RE && *base_pat == '\\')
|
|
1200 {
|
|
1201 len--;
|
|
1202 len_byte--;
|
|
1203 base_pat++;
|
|
1204 }
|
|
1205
|
|
1206 c = STRING_CHAR_AND_LENGTH (base_pat, len_byte, in_charlen);
|
23876
|
1207
|
20869
|
1208 /* Translate the character, if requested. */
|
20898
|
1209 TRANSLATE (translated, trt, c);
|
20869
|
1210 /* If translation changed the byte-length, go back
|
|
1211 to the original character. */
|
26869
|
1212 charlen = CHAR_STRING (translated, str);
|
20869
|
1213 if (in_charlen != charlen)
|
|
1214 {
|
|
1215 translated = c;
|
26869
|
1216 charlen = CHAR_STRING (c, str);
|
20869
|
1217 }
|
|
1218
|
24014
|
1219 /* If we are searching for something strange,
|
|
1220 an invalid multibyte code, don't use boyer-moore. */
|
|
1221 if (! ASCII_BYTE_P (translated)
|
|
1222 && (charlen == 1 /* 8bit code */
|
|
1223 || charlen != in_charlen /* invalid multibyte code */
|
|
1224 ))
|
|
1225 boyer_moore_ok = 0;
|
|
1226
|
20898
|
1227 TRANSLATE (inverse, inverse_trt, c);
|
|
1228
|
20869
|
1229 /* Did this char actually get translated?
|
|
1230 Would any other char get translated into it? */
|
20898
|
1231 if (translated != c || inverse != c)
|
20869
|
1232 {
|
|
1233 /* Keep track of which character set row
|
|
1234 contains the characters that need translation. */
|
24014
|
1235 int charset_base_code = c & ~CHAR_FIELD3_MASK;
|
20869
|
1236 if (charset_base == -1)
|
|
1237 charset_base = charset_base_code;
|
|
1238 else if (charset_base != charset_base_code)
|
|
1239 /* If two different rows appear, needing translation,
|
|
1240 then we cannot use boyer_moore search. */
|
23876
|
1241 boyer_moore_ok = 0;
|
20898
|
1242 }
|
20869
|
1243
|
|
1244 /* Store this character into the translated pattern. */
|
|
1245 bcopy (str, pat, charlen);
|
|
1246 pat += charlen;
|
|
1247 base_pat += in_charlen;
|
|
1248 len_byte -= in_charlen;
|
|
1249 }
|
|
1250 }
|
|
1251 else
|
|
1252 {
|
23876
|
1253 /* Unibyte buffer. */
|
|
1254 charset_base = 0;
|
20869
|
1255 while (--len >= 0)
|
|
1256 {
|
23876
|
1257 int c, translated;
|
20869
|
1258
|
|
1259 /* If we got here and the RE flag is set, it's because we're
|
|
1260 dealing with a regexp known to be trivial, so the backslash
|
|
1261 just quotes the next character. */
|
|
1262 if (RE && *base_pat == '\\')
|
|
1263 {
|
|
1264 len--;
|
|
1265 base_pat++;
|
|
1266 }
|
|
1267 c = *base_pat++;
|
20898
|
1268 TRANSLATE (translated, trt, c);
|
20869
|
1269 *pat++ = translated;
|
|
1270 }
|
|
1271 }
|
|
1272
|
|
1273 len_byte = pat - patbuf;
|
|
1274 len = raw_pattern_size;
|
|
1275 pat = base_pat = patbuf;
|
|
1276
|
23876
|
1277 if (boyer_moore_ok)
|
20869
|
1278 return boyer_moore (n, pat, len, len_byte, trt, inverse_trt,
|
20898
|
1279 pos, pos_byte, lim, lim_byte,
|
|
1280 charset_base);
|
20869
|
1281 else
|
|
1282 return simple_search (n, pat, len, len_byte, trt,
|
|
1283 pos, pos_byte, lim, lim_byte);
|
|
1284 }
|
|
1285 }
|
|
1286
|
|
1287 /* Do a simple string search N times for the string PAT,
|
|
1288 whose length is LEN/LEN_BYTE,
|
|
1289 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
|
|
1290 TRT is the translation table.
|
|
1291
|
|
1292 Return the character position where the match is found.
|
|
1293 Otherwise, if M matches remained to be found, return -M.
|
|
1294
|
|
1295 This kind of search works regardless of what is in PAT and
|
|
1296 regardless of what is in TRT. It is used in cases where
|
|
1297 boyer_moore cannot work. */
|
|
1298
|
|
1299 static int
|
|
1300 simple_search (n, pat, len, len_byte, trt, pos, pos_byte, lim, lim_byte)
|
|
1301 int n;
|
|
1302 unsigned char *pat;
|
|
1303 int len, len_byte;
|
|
1304 Lisp_Object trt;
|
|
1305 int pos, pos_byte;
|
|
1306 int lim, lim_byte;
|
|
1307 {
|
|
1308 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
|
21117
|
1309 int forward = n > 0;
|
20869
|
1310
|
|
1311 if (lim > pos && multibyte)
|
|
1312 while (n > 0)
|
8950
|
1313 {
|
20869
|
1314 while (1)
|
20671
|
1315 {
|
20869
|
1316 /* Try matching at position POS. */
|
|
1317 int this_pos = pos;
|
|
1318 int this_pos_byte = pos_byte;
|
|
1319 int this_len = len;
|
|
1320 int this_len_byte = len_byte;
|
|
1321 unsigned char *p = pat;
|
|
1322 if (pos + len > lim)
|
|
1323 goto stop;
|
|
1324
|
|
1325 while (this_len > 0)
|
|
1326 {
|
|
1327 int charlen, buf_charlen;
|
21117
|
1328 int pat_ch, buf_ch;
|
|
1329
|
|
1330 pat_ch = STRING_CHAR_AND_LENGTH (p, this_len_byte, charlen);
|
|
1331 buf_ch = STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte),
|
|
1332 ZV_BYTE - this_pos_byte,
|
|
1333 buf_charlen);
|
|
1334 TRANSLATE (buf_ch, trt, buf_ch);
|
|
1335
|
|
1336 if (buf_ch != pat_ch)
|
|
1337 break;
|
20869
|
1338
|
|
1339 this_len_byte -= charlen;
|
|
1340 this_len--;
|
|
1341 p += charlen;
|
20671
|
1342
|
20869
|
1343 this_pos_byte += buf_charlen;
|
|
1344 this_pos++;
|
|
1345 }
|
|
1346
|
|
1347 if (this_len == 0)
|
|
1348 {
|
|
1349 pos += len;
|
|
1350 pos_byte += len_byte;
|
|
1351 break;
|
|
1352 }
|
|
1353
|
|
1354 INC_BOTH (pos, pos_byte);
|
20671
|
1355 }
|
|
1356
|
20869
|
1357 n--;
|
|
1358 }
|
|
1359 else if (lim > pos)
|
|
1360 while (n > 0)
|
|
1361 {
|
|
1362 while (1)
|
8950
|
1363 {
|
20869
|
1364 /* Try matching at position POS. */
|
|
1365 int this_pos = pos;
|
|
1366 int this_len = len;
|
|
1367 unsigned char *p = pat;
|
|
1368
|
|
1369 if (pos + len > lim)
|
|
1370 goto stop;
|
|
1371
|
|
1372 while (this_len > 0)
|
8950
|
1373 {
|
20869
|
1374 int pat_ch = *p++;
|
|
1375 int buf_ch = FETCH_BYTE (this_pos);
|
20898
|
1376 TRANSLATE (buf_ch, trt, buf_ch);
|
20869
|
1377
|
|
1378 if (buf_ch != pat_ch)
|
|
1379 break;
|
21117
|
1380
|
|
1381 this_len--;
|
|
1382 this_pos++;
|
8950
|
1383 }
|
20869
|
1384
|
|
1385 if (this_len == 0)
|
|
1386 {
|
|
1387 pos += len;
|
|
1388 break;
|
|
1389 }
|
|
1390
|
|
1391 pos++;
|
8950
|
1392 }
|
20869
|
1393
|
|
1394 n--;
|
8950
|
1395 }
|
20869
|
1396 /* Backwards search. */
|
|
1397 else if (lim < pos && multibyte)
|
|
1398 while (n < 0)
|
|
1399 {
|
|
1400 while (1)
|
|
1401 {
|
|
1402 /* Try matching at position POS. */
|
|
1403 int this_pos = pos - len;
|
|
1404 int this_pos_byte = pos_byte - len_byte;
|
|
1405 int this_len = len;
|
|
1406 int this_len_byte = len_byte;
|
|
1407 unsigned char *p = pat;
|
|
1408
|
|
1409 if (pos - len < lim)
|
|
1410 goto stop;
|
|
1411
|
|
1412 while (this_len > 0)
|
|
1413 {
|
|
1414 int charlen, buf_charlen;
|
21117
|
1415 int pat_ch, buf_ch;
|
|
1416
|
|
1417 pat_ch = STRING_CHAR_AND_LENGTH (p, this_len_byte, charlen);
|
|
1418 buf_ch = STRING_CHAR_AND_LENGTH (BYTE_POS_ADDR (this_pos_byte),
|
|
1419 ZV_BYTE - this_pos_byte,
|
|
1420 buf_charlen);
|
|
1421 TRANSLATE (buf_ch, trt, buf_ch);
|
|
1422
|
|
1423 if (buf_ch != pat_ch)
|
|
1424 break;
|
20869
|
1425
|
|
1426 this_len_byte -= charlen;
|
|
1427 this_len--;
|
|
1428 p += charlen;
|
|
1429 this_pos_byte += buf_charlen;
|
|
1430 this_pos++;
|
|
1431 }
|
|
1432
|
|
1433 if (this_len == 0)
|
|
1434 {
|
|
1435 pos -= len;
|
|
1436 pos_byte -= len_byte;
|
|
1437 break;
|
|
1438 }
|
|
1439
|
|
1440 DEC_BOTH (pos, pos_byte);
|
|
1441 }
|
|
1442
|
|
1443 n++;
|
|
1444 }
|
|
1445 else if (lim < pos)
|
|
1446 while (n < 0)
|
|
1447 {
|
|
1448 while (1)
|
|
1449 {
|
|
1450 /* Try matching at position POS. */
|
|
1451 int this_pos = pos - len;
|
|
1452 int this_len = len;
|
|
1453 unsigned char *p = pat;
|
|
1454
|
|
1455 if (pos - len < lim)
|
|
1456 goto stop;
|
|
1457
|
|
1458 while (this_len > 0)
|
|
1459 {
|
|
1460 int pat_ch = *p++;
|
|
1461 int buf_ch = FETCH_BYTE (this_pos);
|
20898
|
1462 TRANSLATE (buf_ch, trt, buf_ch);
|
20869
|
1463
|
|
1464 if (buf_ch != pat_ch)
|
|
1465 break;
|
21117
|
1466 this_len--;
|
|
1467 this_pos++;
|
20869
|
1468 }
|
|
1469
|
|
1470 if (this_len == 0)
|
|
1471 {
|
|
1472 pos -= len;
|
|
1473 break;
|
|
1474 }
|
|
1475
|
|
1476 pos--;
|
|
1477 }
|
|
1478
|
|
1479 n++;
|
|
1480 }
|
603
|
1481
|
20869
|
1482 stop:
|
|
1483 if (n == 0)
|
20898
|
1484 {
|
21117
|
1485 if (forward)
|
|
1486 set_search_regs ((multibyte ? pos_byte : pos) - len_byte, len_byte);
|
|
1487 else
|
|
1488 set_search_regs (multibyte ? pos_byte : pos, len_byte);
|
20898
|
1489
|
|
1490 return pos;
|
|
1491 }
|
20869
|
1492 else if (n > 0)
|
|
1493 return -n;
|
|
1494 else
|
|
1495 return n;
|
|
1496 }
|
|
1497
|
|
1498 /* Do Boyer-Moore search N times for the string PAT,
|
|
1499 whose length is LEN/LEN_BYTE,
|
|
1500 from buffer position POS/POS_BYTE until LIM/LIM_BYTE.
|
|
1501 DIRECTION says which direction we search in.
|
|
1502 TRT and INVERSE_TRT are translation tables.
|
|
1503
|
|
1504 This kind of search works if all the characters in PAT that have
|
|
1505 nontrivial translation are the same aside from the last byte. This
|
|
1506 makes it possible to translate just the last byte of a character,
|
|
1507 and do so after just a simple test of the context.
|
|
1508
|
|
1509 If that criterion is not satisfied, do not call this function. */
|
|
1510
|
|
1511 static int
|
|
1512 boyer_moore (n, base_pat, len, len_byte, trt, inverse_trt,
|
20898
|
1513 pos, pos_byte, lim, lim_byte, charset_base)
|
20869
|
1514 int n;
|
|
1515 unsigned char *base_pat;
|
|
1516 int len, len_byte;
|
|
1517 Lisp_Object trt;
|
|
1518 Lisp_Object inverse_trt;
|
|
1519 int pos, pos_byte;
|
|
1520 int lim, lim_byte;
|
20898
|
1521 int charset_base;
|
20869
|
1522 {
|
|
1523 int direction = ((n > 0) ? 1 : -1);
|
|
1524 register int dirlen;
|
|
1525 int infinity, limit, k, stride_for_teases;
|
|
1526 register int *BM_tab;
|
|
1527 int *BM_tab_base;
|
|
1528 register unsigned char *cursor, *p_limit;
|
|
1529 register int i, j;
|
21945
|
1530 unsigned char *pat, *pat_end;
|
20869
|
1531 int multibyte = ! NILP (current_buffer->enable_multibyte_characters);
|
|
1532
|
|
1533 unsigned char simple_translate[0400];
|
|
1534 int translate_prev_byte;
|
|
1535 int translate_anteprev_byte;
|
603
|
1536
|
20869
|
1537 #ifdef C_ALLOCA
|
|
1538 int BM_tab_space[0400];
|
|
1539 BM_tab = &BM_tab_space[0];
|
|
1540 #else
|
|
1541 BM_tab = (int *) alloca (0400 * sizeof (int));
|
|
1542 #endif
|
|
1543 /* The general approach is that we are going to maintain that we know */
|
|
1544 /* the first (closest to the present position, in whatever direction */
|
|
1545 /* we're searching) character that could possibly be the last */
|
|
1546 /* (furthest from present position) character of a valid match. We */
|
|
1547 /* advance the state of our knowledge by looking at that character */
|
|
1548 /* and seeing whether it indeed matches the last character of the */
|
|
1549 /* pattern. If it does, we take a closer look. If it does not, we */
|
|
1550 /* move our pointer (to putative last characters) as far as is */
|
|
1551 /* logically possible. This amount of movement, which I call a */
|
|
1552 /* stride, will be the length of the pattern if the actual character */
|
|
1553 /* appears nowhere in the pattern, otherwise it will be the distance */
|
|
1554 /* from the last occurrence of that character to the end of the */
|
|
1555 /* pattern. */
|
|
1556 /* As a coding trick, an enormous stride is coded into the table for */
|
|
1557 /* characters that match the last character. This allows use of only */
|
|
1558 /* a single test, a test for having gone past the end of the */
|
|
1559 /* permissible match region, to test for both possible matches (when */
|
|
1560 /* the stride goes past the end immediately) and failure to */
|
|
1561 /* match (where you get nudged past the end one stride at a time). */
|
|
1562
|
|
1563 /* Here we make a "mickey mouse" BM table. The stride of the search */
|
|
1564 /* is determined only by the last character of the putative match. */
|
|
1565 /* If that character does not match, we will stride the proper */
|
|
1566 /* distance to propose a match that superimposes it on the last */
|
|
1567 /* instance of a character that matches it (per trt), or misses */
|
|
1568 /* it entirely if there is none. */
|
|
1569
|
|
1570 dirlen = len_byte * direction;
|
|
1571 infinity = dirlen - (lim_byte + pos_byte + len_byte + len_byte) * direction;
|
21945
|
1572
|
|
1573 /* Record position after the end of the pattern. */
|
|
1574 pat_end = base_pat + len_byte;
|
|
1575 /* BASE_PAT points to a character that we start scanning from.
|
|
1576 It is the first character in a forward search,
|
|
1577 the last character in a backward search. */
|
20869
|
1578 if (direction < 0)
|
21945
|
1579 base_pat = pat_end - 1;
|
|
1580
|
20869
|
1581 BM_tab_base = BM_tab;
|
|
1582 BM_tab += 0400;
|
|
1583 j = dirlen; /* to get it in a register */
|
|
1584 /* A character that does not appear in the pattern induces a */
|
|
1585 /* stride equal to the pattern length. */
|
|
1586 while (BM_tab_base != BM_tab)
|
|
1587 {
|
|
1588 *--BM_tab = j;
|
|
1589 *--BM_tab = j;
|
|
1590 *--BM_tab = j;
|
|
1591 *--BM_tab = j;
|
|
1592 }
|
|
1593
|
|
1594 /* We use this for translation, instead of TRT itself.
|
|
1595 We fill this in to handle the characters that actually
|
|
1596 occur in the pattern. Others don't matter anyway! */
|
|
1597 bzero (simple_translate, sizeof simple_translate);
|
|
1598 for (i = 0; i < 0400; i++)
|
|
1599 simple_translate[i] = i;
|
|
1600
|
|
1601 i = 0;
|
|
1602 while (i != infinity)
|
|
1603 {
|
21945
|
1604 unsigned char *ptr = base_pat + i;
|
20869
|
1605 i += direction;
|
|
1606 if (i == dirlen)
|
|
1607 i = infinity;
|
|
1608 if (! NILP (trt))
|
603
|
1609 {
|
20869
|
1610 int ch;
|
20898
|
1611 int untranslated;
|
20869
|
1612 int this_translated = 1;
|
|
1613
|
|
1614 if (multibyte
|
21945
|
1615 /* Is *PTR the last byte of a character? */
|
|
1616 && (pat_end - ptr == 1 || CHAR_HEAD_P (ptr[1])))
|
603
|
1617 {
|
20869
|
1618 unsigned char *charstart = ptr;
|
|
1619 while (! CHAR_HEAD_P (*charstart))
|
|
1620 charstart--;
|
20898
|
1621 untranslated = STRING_CHAR (charstart, ptr - charstart + 1);
|
24716
|
1622 if (charset_base == (untranslated & ~CHAR_FIELD3_MASK))
|
20869
|
1623 {
|
21117
|
1624 TRANSLATE (ch, trt, untranslated);
|
20898
|
1625 if (! CHAR_HEAD_P (*ptr))
|
|
1626 {
|
|
1627 translate_prev_byte = ptr[-1];
|
|
1628 if (! CHAR_HEAD_P (translate_prev_byte))
|
|
1629 translate_anteprev_byte = ptr[-2];
|
|
1630 }
|
20869
|
1631 }
|
20898
|
1632 else
|
21117
|
1633 {
|
|
1634 this_translated = 0;
|
|
1635 ch = *ptr;
|
|
1636 }
|
603
|
1637 }
|
20869
|
1638 else if (!multibyte)
|
20898
|
1639 TRANSLATE (ch, trt, *ptr);
|
603
|
1640 else
|
|
1641 {
|
20869
|
1642 ch = *ptr;
|
|
1643 this_translated = 0;
|
603
|
1644 }
|
20869
|
1645
|
21117
|
1646 if (ch > 0400)
|
|
1647 j = ((unsigned char) ch) | 0200;
|
|
1648 else
|
|
1649 j = (unsigned char) ch;
|
|
1650
|
20869
|
1651 if (i == infinity)
|
|
1652 stride_for_teases = BM_tab[j];
|
21117
|
1653
|
20869
|
1654 BM_tab[j] = dirlen - i;
|
|
1655 /* A translation table is accompanied by its inverse -- see */
|
|
1656 /* comment following downcase_table for details */
|
|
1657 if (this_translated)
|
21117
|
1658 {
|
|
1659 int starting_ch = ch;
|
|
1660 int starting_j = j;
|
|
1661 while (1)
|
|
1662 {
|
|
1663 TRANSLATE (ch, inverse_trt, ch);
|
|
1664 if (ch > 0400)
|
|
1665 j = ((unsigned char) ch) | 0200;
|
|
1666 else
|
|
1667 j = (unsigned char) ch;
|
|
1668
|
|
1669 /* For all the characters that map into CH,
|
|
1670 set up simple_translate to map the last byte
|
|
1671 into STARTING_J. */
|
|
1672 simple_translate[j] = starting_j;
|
|
1673 if (ch == starting_ch)
|
|
1674 break;
|
|
1675 BM_tab[j] = dirlen - i;
|
|
1676 }
|
|
1677 }
|
603
|
1678 }
|
20869
|
1679 else
|
603
|
1680 {
|
20869
|
1681 j = *ptr;
|
|
1682
|
|
1683 if (i == infinity)
|
|
1684 stride_for_teases = BM_tab[j];
|
|
1685 BM_tab[j] = dirlen - i;
|
|
1686 }
|
|
1687 /* stride_for_teases tells how much to stride if we get a */
|
|
1688 /* match on the far character but are subsequently */
|
|
1689 /* disappointed, by recording what the stride would have been */
|
|
1690 /* for that character if the last character had been */
|
|
1691 /* different. */
|
|
1692 }
|
|
1693 infinity = dirlen - infinity;
|
|
1694 pos_byte += dirlen - ((direction > 0) ? direction : 0);
|
|
1695 /* loop invariant - POS_BYTE points at where last char (first
|
|
1696 char if reverse) of pattern would align in a possible match. */
|
|
1697 while (n != 0)
|
|
1698 {
|
|
1699 int tail_end;
|
|
1700 unsigned char *tail_end_ptr;
|
|
1701
|
|
1702 /* It's been reported that some (broken) compiler thinks that
|
|
1703 Boolean expressions in an arithmetic context are unsigned.
|
|
1704 Using an explicit ?1:0 prevents this. */
|
|
1705 if ((lim_byte - pos_byte - ((direction > 0) ? 1 : 0)) * direction
|
|
1706 < 0)
|
|
1707 return (n * (0 - direction));
|
|
1708 /* First we do the part we can by pointers (maybe nothing) */
|
|
1709 QUIT;
|
|
1710 pat = base_pat;
|
|
1711 limit = pos_byte - dirlen + direction;
|
21457
|
1712 if (direction > 0)
|
|
1713 {
|
|
1714 limit = BUFFER_CEILING_OF (limit);
|
|
1715 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
|
|
1716 can take on without hitting edge of buffer or the gap. */
|
|
1717 limit = min (limit, pos_byte + 20000);
|
|
1718 limit = min (limit, lim_byte - 1);
|
|
1719 }
|
|
1720 else
|
|
1721 {
|
|
1722 limit = BUFFER_FLOOR_OF (limit);
|
|
1723 /* LIMIT is now the last (not beyond-last!) value POS_BYTE
|
|
1724 can take on without hitting edge of buffer or the gap. */
|
|
1725 limit = max (limit, pos_byte - 20000);
|
|
1726 limit = max (limit, lim_byte);
|
|
1727 }
|
20869
|
1728 tail_end = BUFFER_CEILING_OF (pos_byte) + 1;
|
|
1729 tail_end_ptr = BYTE_POS_ADDR (tail_end);
|
|
1730
|
|
1731 if ((limit - pos_byte) * direction > 20)
|
|
1732 {
|
|
1733 unsigned char *p2;
|
|
1734
|
|
1735 p_limit = BYTE_POS_ADDR (limit);
|
|
1736 p2 = (cursor = BYTE_POS_ADDR (pos_byte));
|
|
1737 /* In this loop, pos + cursor - p2 is the surrogate for pos */
|
|
1738 while (1) /* use one cursor setting as long as i can */
|
603
|
1739 {
|
20869
|
1740 if (direction > 0) /* worth duplicating */
|
603
|
1741 {
|
20869
|
1742 /* Use signed comparison if appropriate
|
|
1743 to make cursor+infinity sure to be > p_limit.
|
|
1744 Assuming that the buffer lies in a range of addresses
|
|
1745 that are all "positive" (as ints) or all "negative",
|
|
1746 either kind of comparison will work as long
|
|
1747 as we don't step by infinity. So pick the kind
|
|
1748 that works when we do step by infinity. */
|
|
1749 if ((EMACS_INT) (p_limit + infinity) > (EMACS_INT) p_limit)
|
|
1750 while ((EMACS_INT) cursor <= (EMACS_INT) p_limit)
|
|
1751 cursor += BM_tab[*cursor];
|
603
|
1752 else
|
20869
|
1753 while ((EMACS_UINT) cursor <= (EMACS_UINT) p_limit)
|
|
1754 cursor += BM_tab[*cursor];
|
|
1755 }
|
|
1756 else
|
|
1757 {
|
|
1758 if ((EMACS_INT) (p_limit + infinity) < (EMACS_INT) p_limit)
|
|
1759 while ((EMACS_INT) cursor >= (EMACS_INT) p_limit)
|
|
1760 cursor += BM_tab[*cursor];
|
|
1761 else
|
|
1762 while ((EMACS_UINT) cursor >= (EMACS_UINT) p_limit)
|
|
1763 cursor += BM_tab[*cursor];
|
|
1764 }
|
603
|
1765 /* If you are here, cursor is beyond the end of the searched region. */
|
20869
|
1766 /* This can happen if you match on the far character of the pattern, */
|
|
1767 /* because the "stride" of that character is infinity, a number able */
|
|
1768 /* to throw you well beyond the end of the search. It can also */
|
|
1769 /* happen if you fail to match within the permitted region and would */
|
|
1770 /* otherwise try a character beyond that region */
|
|
1771 if ((cursor - p_limit) * direction <= len_byte)
|
|
1772 break; /* a small overrun is genuine */
|
|
1773 cursor -= infinity; /* large overrun = hit */
|
|
1774 i = dirlen - direction;
|
|
1775 if (! NILP (trt))
|
603
|
1776 {
|
|
1777 while ((i -= direction) + direction != 0)
|
|
1778 {
|
20869
|
1779 int ch;
|
|
1780 cursor -= direction;
|
|
1781 /* Translate only the last byte of a character. */
|
|
1782 if (! multibyte
|
|
1783 || ((cursor == tail_end_ptr
|
|
1784 || CHAR_HEAD_P (cursor[1]))
|
|
1785 && (CHAR_HEAD_P (cursor[0])
|
|
1786 || (translate_prev_byte == cursor[-1]
|
|
1787 && (CHAR_HEAD_P (translate_prev_byte)
|
|
1788 || translate_anteprev_byte == cursor[-2])))))
|
|
1789 ch = simple_translate[*cursor];
|
|
1790 else
|
|
1791 ch = *cursor;
|
|
1792 if (pat[i] != ch)
|
|
1793 break;
|
|
1794 }
|
|
1795 }
|
|
1796 else
|
|
1797 {
|
|
1798 while ((i -= direction) + direction != 0)
|
|
1799 {
|
|
1800 cursor -= direction;
|
|
1801 if (pat[i] != *cursor)
|
603
|
1802 break;
|
|
1803 }
|
20869
|
1804 }
|
|
1805 cursor += dirlen - i - direction; /* fix cursor */
|
|
1806 if (i + direction == 0)
|
|
1807 {
|
|
1808 int position;
|
708
|
1809
|
20869
|
1810 cursor -= direction;
|
20588
|
1811
|
20869
|
1812 position = pos_byte + cursor - p2 + ((direction > 0)
|
|
1813 ? 1 - len_byte : 0);
|
|
1814 set_search_regs (position, len_byte);
|
708
|
1815
|
20869
|
1816 if ((n -= direction) != 0)
|
|
1817 cursor += dirlen; /* to resume search */
|
603
|
1818 else
|
20869
|
1819 return ((direction > 0)
|
|
1820 ? search_regs.end[0] : search_regs.start[0]);
|
603
|
1821 }
|
20869
|
1822 else
|
|
1823 cursor += stride_for_teases; /* <sigh> we lose - */
|
|
1824 }
|
|
1825 pos_byte += cursor - p2;
|
603
|
1826 }
|
20869
|
1827 else
|
|
1828 /* Now we'll pick up a clump that has to be done the hard */
|
|
1829 /* way because it covers a discontinuity */
|
|
1830 {
|
|
1831 limit = ((direction > 0)
|
|
1832 ? BUFFER_CEILING_OF (pos_byte - dirlen + 1)
|
|
1833 : BUFFER_FLOOR_OF (pos_byte - dirlen - 1));
|
|
1834 limit = ((direction > 0)
|
|
1835 ? min (limit + len_byte, lim_byte - 1)
|
|
1836 : max (limit - len_byte, lim_byte));
|
|
1837 /* LIMIT is now the last value POS_BYTE can have
|
|
1838 and still be valid for a possible match. */
|
|
1839 while (1)
|
|
1840 {
|
|
1841 /* This loop can be coded for space rather than */
|
|
1842 /* speed because it will usually run only once. */
|
|
1843 /* (the reach is at most len + 21, and typically */
|
|
1844 /* does not exceed len) */
|
|
1845 while ((limit - pos_byte) * direction >= 0)
|
|
1846 pos_byte += BM_tab[FETCH_BYTE (pos_byte)];
|
|
1847 /* now run the same tests to distinguish going off the */
|
|
1848 /* end, a match or a phony match. */
|
|
1849 if ((pos_byte - limit) * direction <= len_byte)
|
|
1850 break; /* ran off the end */
|
|
1851 /* Found what might be a match.
|
|
1852 Set POS_BYTE back to last (first if reverse) pos. */
|
|
1853 pos_byte -= infinity;
|
|
1854 i = dirlen - direction;
|
|
1855 while ((i -= direction) + direction != 0)
|
|
1856 {
|
|
1857 int ch;
|
|
1858 unsigned char *ptr;
|
|
1859 pos_byte -= direction;
|
|
1860 ptr = BYTE_POS_ADDR (pos_byte);
|
|
1861 /* Translate only the last byte of a character. */
|
|
1862 if (! multibyte
|
|
1863 || ((ptr == tail_end_ptr
|
|
1864 || CHAR_HEAD_P (ptr[1]))
|
|
1865 && (CHAR_HEAD_P (ptr[0])
|
|
1866 || (translate_prev_byte == ptr[-1]
|
|
1867 && (CHAR_HEAD_P (translate_prev_byte)
|
|
1868 || translate_anteprev_byte == ptr[-2])))))
|
|
1869 ch = simple_translate[*ptr];
|
|
1870 else
|
|
1871 ch = *ptr;
|
|
1872 if (pat[i] != ch)
|
|
1873 break;
|
|
1874 }
|
|
1875 /* Above loop has moved POS_BYTE part or all the way
|
|
1876 back to the first pos (last pos if reverse).
|
|
1877 Set it once again at the last (first if reverse) char. */
|
|
1878 pos_byte += dirlen - i- direction;
|
|
1879 if (i + direction == 0)
|
|
1880 {
|
|
1881 int position;
|
|
1882 pos_byte -= direction;
|
|
1883
|
|
1884 position = pos_byte + ((direction > 0) ? 1 - len_byte : 0);
|
|
1885
|
|
1886 set_search_regs (position, len_byte);
|
|
1887
|
|
1888 if ((n -= direction) != 0)
|
|
1889 pos_byte += dirlen; /* to resume search */
|
|
1890 else
|
|
1891 return ((direction > 0)
|
|
1892 ? search_regs.end[0] : search_regs.start[0]);
|
|
1893 }
|
|
1894 else
|
|
1895 pos_byte += stride_for_teases;
|
|
1896 }
|
|
1897 }
|
|
1898 /* We have done one clump. Can we continue? */
|
|
1899 if ((lim_byte - pos_byte) * direction < 0)
|
|
1900 return ((0 - n) * direction);
|
603
|
1901 }
|
20869
|
1902 return BYTE_TO_CHAR (pos_byte);
|
603
|
1903 }
|
5556
|
1904
|
20545
|
1905 /* Record beginning BEG_BYTE and end BEG_BYTE + NBYTES
|
22082
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1906 for the overall match just found in the current buffer.
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1907 Also clear out the match data for registers 1 and up. */
|
5556
|
1908
|
|
1909 static void
|
20545
|
1910 set_search_regs (beg_byte, nbytes)
|
|
1911 int beg_byte, nbytes;
|
5556
|
1912 {
|
22082
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1913 int i;
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1914
|
5556
|
1915 /* Make sure we have registers in which to store
|
|
1916 the match position. */
|
|
1917 if (search_regs.num_regs == 0)
|
|
1918 {
|
10250
|
1919 search_regs.start = (regoff_t *) xmalloc (2 * sizeof (regoff_t));
|
|
1920 search_regs.end = (regoff_t *) xmalloc (2 * sizeof (regoff_t));
|
9605
|
1921 search_regs.num_regs = 2;
|
5556
|
1922 }
|
|
1923
|
22082
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1924 /* Clear out the other registers. */
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1925 for (i = 1; i < search_regs.num_regs; i++)
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1926 {
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1927 search_regs.start[i] = -1;
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1928 search_regs.end[i] = -1;
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1929 }
|
84bcdbc46d71
(search_buffer): Set search regs for all success with an empty string.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
1930
|
20545
|
1931 search_regs.start[0] = BYTE_TO_CHAR (beg_byte);
|
|
1932 search_regs.end[0] = BYTE_TO_CHAR (beg_byte + nbytes);
|
9278
f2138d548313
(Flooking_at, skip_chars, search_buffer, set_search_regs, Fstore_match_data):
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
1933 XSETBUFFER (last_thing_searched, current_buffer);
|
5556
|
1934 }
|
603
|
1935
|
|
1936 /* Given a string of words separated by word delimiters,
|
|
1937 compute a regexp that matches those exact words
|
|
1938 separated by arbitrary punctuation. */
|
|
1939
|
|
1940 static Lisp_Object
|
|
1941 wordify (string)
|
|
1942 Lisp_Object string;
|
|
1943 {
|
|
1944 register unsigned char *p, *o;
|
20588
|
1945 register int i, i_byte, len, punct_count = 0, word_count = 0;
|
603
|
1946 Lisp_Object val;
|
20588
|
1947 int prev_c = 0;
|
|
1948 int adjust;
|
603
|
1949
|
|
1950 CHECK_STRING (string, 0);
|
|
1951 p = XSTRING (string)->data;
|
|
1952 len = XSTRING (string)->size;
|
|
1953
|
20588
|
1954 for (i = 0, i_byte = 0; i < len; )
|
|
1955 {
|
|
1956 int c;
|
|
1957
|
|
1958 if (STRING_MULTIBYTE (string))
|
|
1959 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
|
|
1960 else
|
|
1961 c = XSTRING (string)->data[i++];
|
603
|
1962
|
20588
|
1963 if (SYNTAX (c) != Sword)
|
|
1964 {
|
|
1965 punct_count++;
|
|
1966 if (i > 0 && SYNTAX (prev_c) == Sword)
|
|
1967 word_count++;
|
|
1968 }
|
|
1969
|
|
1970 prev_c = c;
|
|
1971 }
|
|
1972
|
|
1973 if (SYNTAX (prev_c) == Sword)
|
|
1974 word_count++;
|
|
1975 if (!word_count)
|
|
1976 return build_string ("");
|
|
1977
|
|
1978 adjust = - punct_count + 5 * (word_count - 1) + 4;
|
22640
|
1979 if (STRING_MULTIBYTE (string))
|
|
1980 val = make_uninit_multibyte_string (len + adjust,
|
|
1981 STRING_BYTES (XSTRING (string))
|
|
1982 + adjust);
|
|
1983 else
|
|
1984 val = make_uninit_string (len + adjust);
|
603
|
1985
|
|
1986 o = XSTRING (val)->data;
|
|
1987 *o++ = '\\';
|
|
1988 *o++ = 'b';
|
21887
|
1989 prev_c = 0;
|
603
|
1990
|
21887
|
1991 for (i = 0, i_byte = 0; i < len; )
|
|
1992 {
|
|
1993 int c;
|
|
1994 int i_byte_orig = i_byte;
|
|
1995
|
|
1996 if (STRING_MULTIBYTE (string))
|
|
1997 FETCH_STRING_CHAR_ADVANCE (c, string, i, i_byte);
|
|
1998 else
|
22640
|
1999 {
|
|
2000 c = XSTRING (string)->data[i++];
|
|
2001 i_byte++;
|
|
2002 }
|
21887
|
2003
|
|
2004 if (SYNTAX (c) == Sword)
|
|
2005 {
|
|
2006 bcopy (&XSTRING (string)->data[i_byte_orig], o,
|
|
2007 i_byte - i_byte_orig);
|
|
2008 o += i_byte - i_byte_orig;
|
|
2009 }
|
|
2010 else if (i > 0 && SYNTAX (prev_c) == Sword && --word_count)
|
|
2011 {
|
|
2012 *o++ = '\\';
|
|
2013 *o++ = 'W';
|
|
2014 *o++ = '\\';
|
|
2015 *o++ = 'W';
|
|
2016 *o++ = '*';
|
|
2017 }
|
|
2018
|
|
2019 prev_c = c;
|
|
2020 }
|
603
|
2021
|
|
2022 *o++ = '\\';
|
|
2023 *o++ = 'b';
|
|
2024
|
|
2025 return val;
|
|
2026 }
|
|
2027
|
|
2028 DEFUN ("search-backward", Fsearch_backward, Ssearch_backward, 1, 4,
|
19541
|
2029 "MSearch backward: ",
|
603
|
2030 "Search backward from point for STRING.\n\
|
|
2031 Set point to the beginning of the occurrence found, and return point.\n\
|
|
2032 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2033 The match found must not extend before that position.\n\
|
|
2034 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2035 If not nil and not t, position at limit of search and return nil.\n\
|
|
2036 Optional fourth argument is repeat count--search for successive occurrences.\n\
|
|
2037 See also the functions `match-beginning', `match-end' and `replace-match'.")
|
|
2038 (string, bound, noerror, count)
|
|
2039 Lisp_Object string, bound, noerror, count;
|
|
2040 {
|
10020
|
2041 return search_command (string, bound, noerror, count, -1, 0, 0);
|
603
|
2042 }
|
|
2043
|
19541
|
2044 DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "MSearch: ",
|
603
|
2045 "Search forward from point for STRING.\n\
|
|
2046 Set point to the end of the occurrence found, and return point.\n\
|
|
2047 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2048 The match found must not extend after that position. nil is equivalent\n\
|
|
2049 to (point-max).\n\
|
|
2050 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2051 If not nil and not t, move to limit of search and return nil.\n\
|
|
2052 Optional fourth argument is repeat count--search for successive occurrences.\n\
|
|
2053 See also the functions `match-beginning', `match-end' and `replace-match'.")
|
|
2054 (string, bound, noerror, count)
|
|
2055 Lisp_Object string, bound, noerror, count;
|
|
2056 {
|
10020
|
2057 return search_command (string, bound, noerror, count, 1, 0, 0);
|
603
|
2058 }
|
|
2059
|
|
2060 DEFUN ("word-search-backward", Fword_search_backward, Sword_search_backward, 1, 4,
|
|
2061 "sWord search backward: ",
|
|
2062 "Search backward from point for STRING, ignoring differences in punctuation.\n\
|
|
2063 Set point to the beginning of the occurrence found, and return point.\n\
|
|
2064 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2065 The match found must not extend before that position.\n\
|
|
2066 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2067 If not nil and not t, move to limit of search and return nil.\n\
|
|
2068 Optional fourth argument is repeat count--search for successive occurrences.")
|
|
2069 (string, bound, noerror, count)
|
|
2070 Lisp_Object string, bound, noerror, count;
|
|
2071 {
|
10020
|
2072 return search_command (wordify (string), bound, noerror, count, -1, 1, 0);
|
603
|
2073 }
|
|
2074
|
|
2075 DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4,
|
|
2076 "sWord search: ",
|
|
2077 "Search forward from point for STRING, ignoring differences in punctuation.\n\
|
|
2078 Set point to the end of the occurrence found, and return point.\n\
|
|
2079 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2080 The match found must not extend after that position.\n\
|
|
2081 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2082 If not nil and not t, move to limit of search and return nil.\n\
|
|
2083 Optional fourth argument is repeat count--search for successive occurrences.")
|
|
2084 (string, bound, noerror, count)
|
|
2085 Lisp_Object string, bound, noerror, count;
|
|
2086 {
|
10020
|
2087 return search_command (wordify (string), bound, noerror, count, 1, 1, 0);
|
603
|
2088 }
|
|
2089
|
|
2090 DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4,
|
|
2091 "sRE search backward: ",
|
|
2092 "Search backward from point for match for regular expression REGEXP.\n\
|
|
2093 Set point to the beginning of the match, and return point.\n\
|
|
2094 The match found is the one starting last in the buffer\n\
|
6297
|
2095 and yet ending before the origin of the search.\n\
|
603
|
2096 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2097 The match found must start at or after that position.\n\
|
|
2098 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2099 If not nil and not t, move to limit of search and return nil.\n\
|
|
2100 Optional fourth argument is repeat count--search for successive occurrences.\n\
|
|
2101 See also the functions `match-beginning', `match-end' and `replace-match'.")
|
6297
|
2102 (regexp, bound, noerror, count)
|
|
2103 Lisp_Object regexp, bound, noerror, count;
|
603
|
2104 {
|
10020
|
2105 return search_command (regexp, bound, noerror, count, -1, 1, 0);
|
603
|
2106 }
|
|
2107
|
|
2108 DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4,
|
|
2109 "sRE search: ",
|
|
2110 "Search forward from point for regular expression REGEXP.\n\
|
|
2111 Set point to the end of the occurrence found, and return point.\n\
|
|
2112 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2113 The match found must not extend after that position.\n\
|
|
2114 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2115 If not nil and not t, move to limit of search and return nil.\n\
|
|
2116 Optional fourth argument is repeat count--search for successive occurrences.\n\
|
|
2117 See also the functions `match-beginning', `match-end' and `replace-match'.")
|
6297
|
2118 (regexp, bound, noerror, count)
|
|
2119 Lisp_Object regexp, bound, noerror, count;
|
603
|
2120 {
|
10020
|
2121 return search_command (regexp, bound, noerror, count, 1, 1, 0);
|
|
2122 }
|
|
2123
|
|
2124 DEFUN ("posix-search-backward", Fposix_search_backward, Sposix_search_backward, 1, 4,
|
|
2125 "sPosix search backward: ",
|
|
2126 "Search backward from point for match for regular expression REGEXP.\n\
|
|
2127 Find the longest match in accord with Posix regular expression rules.\n\
|
|
2128 Set point to the beginning of the match, and return point.\n\
|
|
2129 The match found is the one starting last in the buffer\n\
|
|
2130 and yet ending before the origin of the search.\n\
|
|
2131 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2132 The match found must start at or after that position.\n\
|
|
2133 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2134 If not nil and not t, move to limit of search and return nil.\n\
|
|
2135 Optional fourth argument is repeat count--search for successive occurrences.\n\
|
|
2136 See also the functions `match-beginning', `match-end' and `replace-match'.")
|
|
2137 (regexp, bound, noerror, count)
|
|
2138 Lisp_Object regexp, bound, noerror, count;
|
|
2139 {
|
|
2140 return search_command (regexp, bound, noerror, count, -1, 1, 1);
|
|
2141 }
|
|
2142
|
|
2143 DEFUN ("posix-search-forward", Fposix_search_forward, Sposix_search_forward, 1, 4,
|
|
2144 "sPosix search: ",
|
|
2145 "Search forward from point for regular expression REGEXP.\n\
|
|
2146 Find the longest match in accord with Posix regular expression rules.\n\
|
|
2147 Set point to the end of the occurrence found, and return point.\n\
|
|
2148 An optional second argument bounds the search; it is a buffer position.\n\
|
|
2149 The match found must not extend after that position.\n\
|
|
2150 Optional third argument, if t, means if fail just return nil (no error).\n\
|
|
2151 If not nil and not t, move to limit of search and return nil.\n\
|
|
2152 Optional fourth argument is repeat count--search for successive occurrences.\n\
|
|
2153 See also the functions `match-beginning', `match-end' and `replace-match'.")
|
|
2154 (regexp, bound, noerror, count)
|
|
2155 Lisp_Object regexp, bound, noerror, count;
|
|
2156 {
|
|
2157 return search_command (regexp, bound, noerror, count, 1, 1, 1);
|
603
|
2158 }
|
|
2159
|
12807
|
2160 DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 5, 0,
|
603
|
2161 "Replace text matched by last search with NEWTEXT.\n\
|
|
2162 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.\n\
|
6543
|
2163 Otherwise maybe capitalize the whole text, or maybe just word initials,\n\
|
|
2164 based on the replaced text.\n\
|
|
2165 If the replaced text has only capital letters\n\
|
|
2166 and has at least one multiletter word, convert NEWTEXT to all caps.\n\
|
|
2167 If the replaced text has at least one word starting with a capital letter,\n\
|
|
2168 then capitalize each word in NEWTEXT.\n\n\
|
603
|
2169 If third arg LITERAL is non-nil, insert NEWTEXT literally.\n\
|
|
2170 Otherwise treat `\\' as special:\n\
|
|
2171 `\\&' in NEWTEXT means substitute original matched text.\n\
|
|
2172 `\\N' means substitute what matched the Nth `\\(...\\)'.\n\
|
|
2173 If Nth parens didn't match, substitute nothing.\n\
|
|
2174 `\\\\' means insert one `\\'.\n\
|
708
|
2175 FIXEDCASE and LITERAL are optional arguments.\n\
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2176 Leaves point at end of replacement text.\n\
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2177 \n\
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2178 The optional fourth argument STRING can be a string to modify.\n\
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2179 In that case, this function creates and returns a new string\n\
|
12807
|
2180 which is made by replacing the part of STRING that was matched.\n\
|
|
2181 \n\
|
|
2182 The optional fifth argument SUBEXP specifies a subexpression of the match.\n\
|
|
2183 It says to replace just that subexpression instead of the whole match.\n\
|
|
2184 This is useful only after a regular expression search or match\n\
|
|
2185 since only regular expressions have distinguished subexpressions.")
|
|
2186 (newtext, fixedcase, literal, string, subexp)
|
|
2187 Lisp_Object newtext, fixedcase, literal, string, subexp;
|
603
|
2188 {
|
|
2189 enum { nochange, all_caps, cap_initial } case_action;
|
22533
|
2190 register int pos, pos_byte;
|
603
|
2191 int some_multiletter_word;
|
2393
|
2192 int some_lowercase;
|
7674
|
2193 int some_uppercase;
|
8526
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2194 int some_nonuppercase_initial;
|
603
|
2195 register int c, prevc;
|
|
2196 int inslen;
|
12807
|
2197 int sub;
|
18081
|
2198 int opoint, newpoint;
|
603
|
2199
|
4882
|
2200 CHECK_STRING (newtext, 0);
|
603
|
2201
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2202 if (! NILP (string))
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2203 CHECK_STRING (string, 4);
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2204
|
603
|
2205 case_action = nochange; /* We tried an initialization */
|
|
2206 /* but some C compilers blew it */
|
621
|
2207
|
|
2208 if (search_regs.num_regs <= 0)
|
|
2209 error ("replace-match called before any match found");
|
|
2210
|
12807
|
2211 if (NILP (subexp))
|
|
2212 sub = 0;
|
|
2213 else
|
|
2214 {
|
|
2215 CHECK_NUMBER (subexp, 3);
|
|
2216 sub = XINT (subexp);
|
|
2217 if (sub < 0 || sub >= search_regs.num_regs)
|
|
2218 args_out_of_range (subexp, make_number (search_regs.num_regs));
|
|
2219 }
|
|
2220
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2221 if (NILP (string))
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2222 {
|
12807
|
2223 if (search_regs.start[sub] < BEGV
|
|
2224 || search_regs.start[sub] > search_regs.end[sub]
|
|
2225 || search_regs.end[sub] > ZV)
|
|
2226 args_out_of_range (make_number (search_regs.start[sub]),
|
|
2227 make_number (search_regs.end[sub]));
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2228 }
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2229 else
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2230 {
|
12807
|
2231 if (search_regs.start[sub] < 0
|
|
2232 || search_regs.start[sub] > search_regs.end[sub]
|
|
2233 || search_regs.end[sub] > XSTRING (string)->size)
|
|
2234 args_out_of_range (make_number (search_regs.start[sub]),
|
|
2235 make_number (search_regs.end[sub]));
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2236 }
|
603
|
2237
|
|
2238 if (NILP (fixedcase))
|
|
2239 {
|
|
2240 /* Decide how to casify by examining the matched text. */
|
22533
|
2241 int last;
|
|
2242
|
|
2243 pos = search_regs.start[sub];
|
|
2244 last = search_regs.end[sub];
|
603
|
2245
|
20545
|
2246 if (NILP (string))
|
22533
|
2247 pos_byte = CHAR_TO_BYTE (pos);
|
20545
|
2248 else
|
22533
|
2249 pos_byte = string_char_to_byte (string, pos);
|
20545
|
2250
|
603
|
2251 prevc = '\n';
|
|
2252 case_action = all_caps;
|
|
2253
|
|
2254 /* some_multiletter_word is set nonzero if any original word
|
|
2255 is more than one letter long. */
|
|
2256 some_multiletter_word = 0;
|
2393
|
2257 some_lowercase = 0;
|
8526
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2258 some_nonuppercase_initial = 0;
|
7674
|
2259 some_uppercase = 0;
|
603
|
2260
|
22533
|
2261 while (pos < last)
|
603
|
2262 {
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2263 if (NILP (string))
|
22533
|
2264 {
|
|
2265 c = FETCH_CHAR (pos_byte);
|
|
2266 INC_BOTH (pos, pos_byte);
|
|
2267 }
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2268 else
|
22533
|
2269 FETCH_STRING_CHAR_ADVANCE (c, string, pos, pos_byte);
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2270
|
603
|
2271 if (LOWERCASEP (c))
|
|
2272 {
|
|
2273 /* Cannot be all caps if any original char is lower case */
|
|
2274
|
2393
|
2275 some_lowercase = 1;
|
603
|
2276 if (SYNTAX (prevc) != Sword)
|
8526
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2277 some_nonuppercase_initial = 1;
|
603
|
2278 else
|
|
2279 some_multiletter_word = 1;
|
|
2280 }
|
|
2281 else if (!NOCASEP (c))
|
|
2282 {
|
7674
|
2283 some_uppercase = 1;
|
2393
|
2284 if (SYNTAX (prevc) != Sword)
|
6679
490b7e2db978
(Freplace_match): Don't capitalize unless all matched words are capitalized.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2285 ;
|
2393
|
2286 else
|
603
|
2287 some_multiletter_word = 1;
|
|
2288 }
|
8526
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2289 else
|
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2290 {
|
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2291 /* If the initial is a caseless word constituent,
|
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2292 treat that like a lowercase initial. */
|
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2293 if (SYNTAX (prevc) != Sword)
|
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2294 some_nonuppercase_initial = 1;
|
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2295 }
|
603
|
2296
|
|
2297 prevc = c;
|
|
2298 }
|
|
2299
|
2393
|
2300 /* Convert to all caps if the old text is all caps
|
|
2301 and has at least one multiletter word. */
|
|
2302 if (! some_lowercase && some_multiletter_word)
|
|
2303 case_action = all_caps;
|
6679
490b7e2db978
(Freplace_match): Don't capitalize unless all matched words are capitalized.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2304 /* Capitalize each word, if the old text has all capitalized words. */
|
8526
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2305 else if (!some_nonuppercase_initial && some_multiletter_word)
|
603
|
2306 case_action = cap_initial;
|
8526
2b7b23059f1b
(Freplace_match): Treat caseless initial like a lowercase initial.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2307 else if (!some_nonuppercase_initial && some_uppercase)
|
7674
|
2308 /* Should x -> yz, operating on X, give Yz or YZ?
|
|
2309 We'll assume the latter. */
|
|
2310 case_action = all_caps;
|
2393
|
2311 else
|
|
2312 case_action = nochange;
|
603
|
2313 }
|
|
2314
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2315 /* Do replacement in a string. */
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2316 if (!NILP (string))
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2317 {
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2318 Lisp_Object before, after;
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2319
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2320 before = Fsubstring (string, make_number (0),
|
12807
|
2321 make_number (search_regs.start[sub]));
|
|
2322 after = Fsubstring (string, make_number (search_regs.end[sub]), Qnil);
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2323
|
17225
|
2324 /* Substitute parts of the match into NEWTEXT
|
|
2325 if desired. */
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2326 if (NILP (literal))
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2327 {
|
21988
|
2328 int lastpos = 0;
|
|
2329 int lastpos_byte = 0;
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2330 /* We build up the substituted string in ACCUM. */
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2331 Lisp_Object accum;
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2332 Lisp_Object middle;
|
22533
|
2333 int length = STRING_BYTES (XSTRING (newtext));
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2334
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2335 accum = Qnil;
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2336
|
22533
|
2337 for (pos_byte = 0, pos = 0; pos_byte < length;)
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2338 {
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2339 int substart = -1;
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2340 int subend;
|
12148
|
2341 int delbackslash = 0;
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2342
|
20588
|
2343 FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
|
|
2344
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2345 if (c == '\\')
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2346 {
|
20588
|
2347 FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2348 if (c == '&')
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2349 {
|
12807
|
2350 substart = search_regs.start[sub];
|
|
2351 subend = search_regs.end[sub];
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2352 }
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2353 else if (c >= '1' && c <= '9' && c <= search_regs.num_regs + '0')
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2354 {
|
12147
|
2355 if (search_regs.start[c - '0'] >= 0)
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2356 {
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2357 substart = search_regs.start[c - '0'];
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2358 subend = search_regs.end[c - '0'];
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2359 }
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2360 }
|
12148
|
2361 else if (c == '\\')
|
|
2362 delbackslash = 1;
|
17225
|
2363 else
|
|
2364 error ("Invalid use of `\\' in replacement text");
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2365 }
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2366 if (substart >= 0)
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2367 {
|
21988
|
2368 if (pos - 2 != lastpos)
|
|
2369 middle = substring_both (newtext, lastpos,
|
|
2370 lastpos_byte,
|
|
2371 pos - 2, pos_byte - 2);
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2372 else
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2373 middle = Qnil;
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2374 accum = concat3 (accum, middle,
|
20588
|
2375 Fsubstring (string,
|
|
2376 make_number (substart),
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2377 make_number (subend)));
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2378 lastpos = pos;
|
20588
|
2379 lastpos_byte = pos_byte;
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2380 }
|
12148
|
2381 else if (delbackslash)
|
|
2382 {
|
21988
|
2383 middle = substring_both (newtext, lastpos,
|
|
2384 lastpos_byte,
|
|
2385 pos - 1, pos_byte - 1);
|
20588
|
2386
|
12148
|
2387 accum = concat2 (accum, middle);
|
|
2388 lastpos = pos;
|
20588
|
2389 lastpos_byte = pos_byte;
|
12148
|
2390 }
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2391 }
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2392
|
21988
|
2393 if (pos != lastpos)
|
|
2394 middle = substring_both (newtext, lastpos,
|
|
2395 lastpos_byte,
|
20588
|
2396 pos, pos_byte);
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2397 else
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2398 middle = Qnil;
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2399
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2400 newtext = concat2 (accum, middle);
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2401 }
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2402
|
17225
|
2403 /* Do case substitution in NEWTEXT if desired. */
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2404 if (case_action == all_caps)
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2405 newtext = Fupcase (newtext);
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2406 else if (case_action == cap_initial)
|
12092
b932b2ed40f5
(Freplace_match): Calls to upcase_initials and upcase_initials_region changed
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2407 newtext = Fupcase_initials (newtext);
|
9029
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2408
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2409 return concat3 (before, newtext, after);
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2410 }
|
f0d89b62dd27
(Freplace_match): New 4th arg OBJECT can specify string to replace in.
Richard M. Stallman <rms@gnu.org>
diff
changeset
|
2411
|
18077
|
2412 /* Record point, the move (quietly) to the start of the match. */
|
23790
|
2413 if (PT >= search_regs.end[sub])
|
18077
|
2414 opoint = PT - ZV;
|
23790
|
2415 else if (PT > search_regs.start[sub])
|
|
2416 opoint = search_regs.end[sub] - ZV;
|
18077
|
2417 else
|
|
2418 opoint = PT;
|
|
2419
|
20545
|
2420 TEMP_SET_PT (search_regs.start[sub]);
|
18077
|
2421
|
2655
|
2422 /* We insert the replacement text before the old text, and then
|
|
2423 delete the original text. This means that markers at the
|
|
2424 beginning or end of the original will float to the corresponding
|
|
2425 position in the replacement. */
|
603
|
2426 if (!NILP (literal))
|
4882
|
2427 Finsert_and_inherit (1, &newtext);
|
603
|
2428 else
|
|
2429 {
|
22533
|
2430 int length = STRING_BYTES (XSTRING (newtext));
|
26982
|
2431 unsigned char *substed;
|
|
2432 int substed_alloc_size, substed_len;
|
22533
|
2433
|
26982
|
2434 substed_alloc_size = length * 2 + 100;
|
|
2435 substed = (unsigned char *) xmalloc (substed_alloc_size + 1);
|
|
2436 substed_len = 0;
|
|
2437
|
|
2438 /* Go thru NEWTEXT, producing the actual text to insert in SUBSTED. */
|
603
|
2439
|
22533
|
2440 for (pos_byte = 0, pos = 0; pos_byte < length;)
|
603
|
2441 {
|
26982
|
2442 unsigned char str[MAX_MULTIBYTE_LENGTH];
|
|
2443 unsigned char *add_stuff;
|
|
2444 int add_len;
|
|
2445 int idx = -1;
|
2655
|
2446
|
22533
|
2447 FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
|
|
2448
|
26982
|
2449 /* Either set ADD_STUFF and ADD_LEN to the text to put in SUBSTED,
|
|
2450 or set IDX to a match index, which means put that part
|
|
2451 of the buffer text into SUBSTED. */
|
|
2452
|
603
|
2453 if (c == '\\')
|
|
2454 {
|
22533
|
2455 FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
|
603
|
2456 if (c == '&')
|
26982
|
2457 idx = sub;
|
7856
|
2458 else if (c >= '1' && c <= '9' && c <= search_regs.num_regs + '0')
|
603
|
2459 {
|
|
2460 if (search_regs.start[c - '0'] >= 1)
|
26982
|
2461 idx = c - '0';
|
603
|
2462 }
|
17225
|
2463 else if (c == '\\')
|
26982
|
2464 add_len = 1, add_stuff = "\\";
|
603
|
2465 else
|
17225
|
2466 error ("Invalid use of `\\' in replacement text");
|
603
|
2467 }
|
|
2468 else
|
26982
|
2469 {
|
|
2470 add_len = CHAR_STRING (c, str);
|
|
2471 add_stuff = str;
|
|
2472 }
|
|
2473
|
|
2474 /* If we want to copy part of a previous match,
|
|
2475 set up ADD_STUFF and ADD_LEN to point to it. */
|
|
2476 if (idx >= 0)
|
|
2477 {
|
|
2478 int begbyte = CHAR_TO_BYTE (search_regs.start[idx]);
|
|
2479 add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte;
|
|
2480 if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx])
|
|
2481 move_gap (search_regs.start[idx]);
|
|
2482 add_stuff = BYTE_POS_ADDR (begbyte);
|
|
2483 }
|
|
2484
|
|
2485 /* Now the stuff we want to add to SUBSTED
|
|
2486 is invariably ADD_LEN bytes starting at ADD_STUFF. */
|
|
2487
|
|
2488 /* Make sure SUBSTED is big enough. */
|
|
2489 if (substed_len + add_len >= substed_alloc_size)
|
|
2490 {
|
|
2491 substed_alloc_size = substed_len + add_len + 500;
|
|
2492 substed = (unsigned char *) xrealloc (substed,
|
|
2493 substed_alloc_size + 1);
|
|
2494 }
|
|
2495
|
|
2496 /* Now add to the end of SUBSTED. */
|
|
2497 bcopy (add_stuff, substed + substed_len, add_len);
|
|
2498 substed_len += add_len;
|
603
|
2499 }
|
26982
|
2500
|
|
2501 /* Now insert what we accumulated. */
|
|
2502 insert_and_inherit (substed, substed_len);
|
|
2503
|
|
2504 xfree (substed);
|
603
|
2505 }
|
|
2506
|
16039
|
2507 inslen = PT - (search_regs.start[sub]);
|
12807
|
2508 del_range (search_regs.start[sub] + inslen, search_regs.end[sub] + inslen);
|
603
|
2509
|
|
2510 if (case_action == all_caps)
|
16039
|
2511 Fupcase_region (make_number (PT - inslen), make_number (PT));
|
603
|
2512 else if (case_action == cap_initial)
|
16039
|
2513 Fupcase_initials_region (make_number (PT - inslen), make_number (PT));
|
18077
|
2514
|
18081
|
2515 newpoint = PT;
|
|
2516
|
18077
|
2517 /* Put point back where it was in the text. */
|
18124
|
2518 if (opoint <= 0)
|
20545
|
2519 TEMP_SET_PT (opoint + ZV);
|
18077
|
2520 else
|
20545
|
2521 TEMP_SET_PT (opoint);
|
18077
|
2522
|
|
2523 /* Now move point "officially" to the start of the inserted replacement. */
|
18081
|
2524 move_if_not_intangible (newpoint);
|
18077
|
2525
|
603
|
2526 return Qnil;
|
|
2527 }
|
|
2528
|
|
2529 static Lisp_Object
|
|
2530 match_limit (num, beginningp)
|
|
2531 Lisp_Object num;
|
|
2532 int beginningp;
|
|
2533 {
|
|
2534 register int n;
|
|
2535
|
|
2536 CHECK_NUMBER (num, 0);
|
|
2537 n = XINT (num);
|
621
|
2538 if (n < 0 || n >= search_regs.num_regs)
|
|
2539 args_out_of_range (num, make_number (search_regs.num_regs));
|
|
2540 if (search_regs.num_regs <= 0
|
|
2541 || search_regs.start[n] < 0)
|
603
|
2542 return Qnil;
|
|
2543 return (make_number ((beginningp) ? search_regs.start[n]
|
|
2544 : search_regs.end[n]));
|
|
2545 }
|
|
2546
|
|
2547 DEFUN ("match-beginning", Fmatch_beginning, Smatch_beginning, 1, 1, 0,
|
|
2548 "Return position of start of text matched by last search.\n\
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2549 SUBEXP, a number, specifies which parenthesized expression in the last\n\
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2550 regexp.\n\
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2551 Value is nil if SUBEXPth pair didn't match, or there were less than\n\
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2552 SUBEXP pairs.\n\
|
603
|
2553 Zero means the entire text matched by the whole regexp or whole string.")
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2554 (subexp)
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2555 Lisp_Object subexp;
|
603
|
2556 {
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2557 return match_limit (subexp, 1);
|
603
|
2558 }
|
|
2559
|
|
2560 DEFUN ("match-end", Fmatch_end, Smatch_end, 1, 1, 0,
|
|
2561 "Return position of end of text matched by last search.\n\
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2562 SUBEXP, a number, specifies which parenthesized expression in the last\n\
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2563 regexp.\n\
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2564 Value is nil if SUBEXPth pair didn't match, or there were less than\n\
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2565 SUBEXP pairs.\n\
|
603
|
2566 Zero means the entire text matched by the whole regexp or whole string.")
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2567 (subexp)
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2568 Lisp_Object subexp;
|
603
|
2569 {
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2570 return match_limit (subexp, 0);
|
603
|
2571 }
|
|
2572
|
16724
|
2573 DEFUN ("match-data", Fmatch_data, Smatch_data, 0, 2, 0,
|
603
|
2574 "Return a list containing all info on what the last search matched.\n\
|
|
2575 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.\n\
|
|
2576 All the elements are markers or nil (nil if the Nth pair didn't match)\n\
|
|
2577 if the last match was on a buffer; integers or nil if a string was matched.\n\
|
16724
|
2578 Use `store-match-data' to reinstate the data in this list.\n\
|
|
2579 \n\
|
|
2580 If INTEGERS (the optional first argument) is non-nil, always use integers\n\
|
16731
|
2581 \(rather than markers) to represent buffer positions.\n\
|
16724
|
2582 If REUSE is a list, reuse it as part of the value. If REUSE is long enough\n\
|
|
2583 to hold all the values, and if INTEGERS is non-nil, no consing is done.")
|
|
2584 (integers, reuse)
|
|
2585 Lisp_Object integers, reuse;
|
603
|
2586 {
|
16724
|
2587 Lisp_Object tail, prev;
|
621
|
2588 Lisp_Object *data;
|
603
|
2589 int i, len;
|
|
2590
|
727
|
2591 if (NILP (last_thing_searched))
|
15667
|
2592 return Qnil;
|
727
|
2593
|
621
|
2594 data = (Lisp_Object *) alloca ((2 * search_regs.num_regs)
|
|
2595 * sizeof (Lisp_Object));
|
|
2596
|
603
|
2597 len = -1;
|
621
|
2598 for (i = 0; i < search_regs.num_regs; i++)
|
603
|
2599 {
|
|
2600 int start = search_regs.start[i];
|
|
2601 if (start >= 0)
|
|
2602 {
|
16724
|
2603 if (EQ (last_thing_searched, Qt)
|
|
2604 || ! NILP (integers))
|
603
|
2605 {
|
9319
7969182b6cc6
(skip_chars, Fmatch_data, Fstore_match_data): Don't use XFASTINT as an lvalue.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2606 XSETFASTINT (data[2 * i], start);
|
7969182b6cc6
(skip_chars, Fmatch_data, Fstore_match_data): Don't use XFASTINT as an lvalue.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2607 XSETFASTINT (data[2 * i + 1], search_regs.end[i]);
|
603
|
2608 }
|
9113
|
2609 else if (BUFFERP (last_thing_searched))
|
603
|
2610 {
|
|
2611 data[2 * i] = Fmake_marker ();
|
727
|
2612 Fset_marker (data[2 * i],
|
|
2613 make_number (start),
|
|
2614 last_thing_searched);
|
603
|
2615 data[2 * i + 1] = Fmake_marker ();
|
|
2616 Fset_marker (data[2 * i + 1],
|
727
|
2617 make_number (search_regs.end[i]),
|
|
2618 last_thing_searched);
|
603
|
2619 }
|
727
|
2620 else
|
|
2621 /* last_thing_searched must always be Qt, a buffer, or Qnil. */
|
|
2622 abort ();
|
|
2623
|
603
|
2624 len = i;
|
|
2625 }
|
|
2626 else
|
|
2627 data[2 * i] = data [2 * i + 1] = Qnil;
|
|
2628 }
|
16724
|
2629
|
|
2630 /* If REUSE is not usable, cons up the values and return them. */
|
|
2631 if (! CONSP (reuse))
|
|
2632 return Flist (2 * len + 2, data);
|
|
2633
|
|
2634 /* If REUSE is a list, store as many value elements as will fit
|
|
2635 into the elements of REUSE. */
|
|
2636 for (i = 0, tail = reuse; CONSP (tail);
|
25663
|
2637 i++, tail = XCDR (tail))
|
16724
|
2638 {
|
|
2639 if (i < 2 * len + 2)
|
25663
|
2640 XCAR (tail) = data[i];
|
16724
|
2641 else
|
25663
|
2642 XCAR (tail) = Qnil;
|
16724
|
2643 prev = tail;
|
|
2644 }
|
|
2645
|
|
2646 /* If we couldn't fit all value elements into REUSE,
|
|
2647 cons up the rest of them and add them to the end of REUSE. */
|
|
2648 if (i < 2 * len + 2)
|
25663
|
2649 XCDR (prev) = Flist (2 * len + 2 - i, data + i);
|
16724
|
2650
|
|
2651 return reuse;
|
603
|
2652 }
|
|
2653
|
|
2654
|
21171
|
2655 DEFUN ("set-match-data", Fset_match_data, Sset_match_data, 1, 1, 0,
|
603
|
2656 "Set internal data on last search match from elements of LIST.\n\
|
|
2657 LIST should have been created by calling `match-data' previously.")
|
|
2658 (list)
|
|
2659 register Lisp_Object list;
|
|
2660 {
|
|
2661 register int i;
|
|
2662 register Lisp_Object marker;
|
|
2663
|
10032
|
2664 if (running_asynch_code)
|
|
2665 save_search_regs ();
|
|
2666
|
603
|
2667 if (!CONSP (list) && !NILP (list))
|
1926
|
2668 list = wrong_type_argument (Qconsp, list);
|
603
|
2669
|
727
|
2670 /* Unless we find a marker with a buffer in LIST, assume that this
|
|
2671 match data came from a string. */
|
|
2672 last_thing_searched = Qt;
|
|
2673
|
621
|
2674 /* Allocate registers if they don't already exist. */
|
|
2675 {
|
1523
|
2676 int length = XFASTINT (Flength (list)) / 2;
|
621
|
2677
|
|
2678 if (length > search_regs.num_regs)
|
|
2679 {
|
708
|
2680 if (search_regs.num_regs == 0)
|
|
2681 {
|
|
2682 search_regs.start
|
|
2683 = (regoff_t *) xmalloc (length * sizeof (regoff_t));
|
|
2684 search_regs.end
|
|
2685 = (regoff_t *) xmalloc (length * sizeof (regoff_t));
|
|
2686 }
|
621
|
2687 else
|
708
|
2688 {
|
|
2689 search_regs.start
|
|
2690 = (regoff_t *) xrealloc (search_regs.start,
|
|
2691 length * sizeof (regoff_t));
|
|
2692 search_regs.end
|
|
2693 = (regoff_t *) xrealloc (search_regs.end,
|
|
2694 length * sizeof (regoff_t));
|
|
2695 }
|
621
|
2696
|
9605
|
2697 search_regs.num_regs = length;
|
621
|
2698 }
|
|
2699 }
|
|
2700
|
|
2701 for (i = 0; i < search_regs.num_regs; i++)
|
603
|
2702 {
|
|
2703 marker = Fcar (list);
|
|
2704 if (NILP (marker))
|
|
2705 {
|
|
2706 search_regs.start[i] = -1;
|
|
2707 list = Fcdr (list);
|
|
2708 }
|
|
2709 else
|
|
2710 {
|
9113
|
2711 if (MARKERP (marker))
|
727
|
2712 {
|
|
2713 if (XMARKER (marker)->buffer == 0)
|
9319
7969182b6cc6
(skip_chars, Fmatch_data, Fstore_match_data): Don't use XFASTINT as an lvalue.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2714 XSETFASTINT (marker, 0);
|
727
|
2715 else
|
9278
f2138d548313
(Flooking_at, skip_chars, search_buffer, set_search_regs, Fstore_match_data):
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2716 XSETBUFFER (last_thing_searched, XMARKER (marker)->buffer);
|
727
|
2717 }
|
603
|
2718
|
|
2719 CHECK_NUMBER_COERCE_MARKER (marker, 0);
|
|
2720 search_regs.start[i] = XINT (marker);
|
|
2721 list = Fcdr (list);
|
|
2722
|
|
2723 marker = Fcar (list);
|
9113
|
2724 if (MARKERP (marker) && XMARKER (marker)->buffer == 0)
|
9319
7969182b6cc6
(skip_chars, Fmatch_data, Fstore_match_data): Don't use XFASTINT as an lvalue.
Karl Heuer <kwzh@gnu.org>
diff
changeset
|
2725 XSETFASTINT (marker, 0);
|
603
|
2726
|
|
2727 CHECK_NUMBER_COERCE_MARKER (marker, 0);
|
|
2728 search_regs.end[i] = XINT (marker);
|
|
2729 }
|
|
2730 list = Fcdr (list);
|
|
2731 }
|
|
2732
|
|
2733 return Qnil;
|
|
2734 }
|
|
2735
|
10032
|
2736 /* If non-zero the match data have been saved in saved_search_regs
|
|
2737 during the execution of a sentinel or filter. */
|
10128
|
2738 static int search_regs_saved;
|
10032
|
2739 static struct re_registers saved_search_regs;
|
|
2740
|
|
2741 /* Called from Flooking_at, Fstring_match, search_buffer, Fstore_match_data
|
|
2742 if asynchronous code (filter or sentinel) is running. */
|
|
2743 static void
|
|
2744 save_search_regs ()
|
|
2745 {
|
|
2746 if (!search_regs_saved)
|
|
2747 {
|
|
2748 saved_search_regs.num_regs = search_regs.num_regs;
|
|
2749 saved_search_regs.start = search_regs.start;
|
|
2750 saved_search_regs.end = search_regs.end;
|
|
2751 search_regs.num_regs = 0;
|
10250
|
2752 search_regs.start = 0;
|
|
2753 search_regs.end = 0;
|
10032
|
2754
|
|
2755 search_regs_saved = 1;
|
|
2756 }
|
|
2757 }
|
|
2758
|
|
2759 /* Called upon exit from filters and sentinels. */
|
|
2760 void
|
|
2761 restore_match_data ()
|
|
2762 {
|
|
2763 if (search_regs_saved)
|
|
2764 {
|
|
2765 if (search_regs.num_regs > 0)
|
|
2766 {
|
|
2767 xfree (search_regs.start);
|
|
2768 xfree (search_regs.end);
|
|
2769 }
|
|
2770 search_regs.num_regs = saved_search_regs.num_regs;
|
|
2771 search_regs.start = saved_search_regs.start;
|
|
2772 search_regs.end = saved_search_regs.end;
|
|
2773
|
|
2774 search_regs_saved = 0;
|
|
2775 }
|
|
2776 }
|
|
2777
|
603
|
2778 /* Quote a string to inactivate reg-expr chars */
|
|
2779
|
|
2780 DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0,
|
|
2781 "Return a regexp string which matches exactly STRING and nothing else.")
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2782 (string)
|
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2783 Lisp_Object string;
|
603
|
2784 {
|
|
2785 register unsigned char *in, *out, *end;
|
|
2786 register unsigned char *temp;
|
20588
|
2787 int backslashes_added = 0;
|
603
|
2788
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2789 CHECK_STRING (string, 0);
|
603
|
2790
|
21244
|
2791 temp = (unsigned char *) alloca (STRING_BYTES (XSTRING (string)) * 2);
|
603
|
2792
|
|
2793 /* Now copy the data into the new string, inserting escapes. */
|
|
2794
|
14086
a410808fda15
(Fmatch_end, Fregexp_quote): Harmonize arguments with documentation.
Erik Naggum <erik@naggum.no>
diff
changeset
|
2795 in = XSTRING (string)->data;
|
21244
|
2796 end = in + STRING_BYTES (XSTRING (string));
|
603
|
2797 out = temp;
|
|
2798
|
|
2799 for (; in != end; in++)
|
|
2800 {
|
|
2801 if (*in == '[' || *in == ']'
|
|
2802 || *in == '*' || *in == '.' || *in == '\\'
|
|
2803 || *in == '?' || *in == '+'
|
|
2804 || *in == '^' || *in == '$')
|
20588
|
2805 *out++ = '\\', backslashes_added++;
|
603
|
2806 *out++ = *in;
|
|
2807 }
|
|
2808
|
21248
|
2809 return make_specified_string (temp,
|
20588
|
2810 XSTRING (string)->size + backslashes_added,
|
21248
|
2811 out - temp,
|
|
2812 STRING_MULTIBYTE (string));
|
603
|
2813 }
|
|
2814
|
21514
|
2815 void
|
603
|
2816 syms_of_search ()
|
|
2817 {
|
|
2818 register int i;
|
|
2819
|
9605
|
2820 for (i = 0; i < REGEXP_CACHE_SIZE; ++i)
|
|
2821 {
|
|
2822 searchbufs[i].buf.allocated = 100;
|
|
2823 searchbufs[i].buf.buffer = (unsigned char *) malloc (100);
|
|
2824 searchbufs[i].buf.fastmap = searchbufs[i].fastmap;
|
|
2825 searchbufs[i].regexp = Qnil;
|
|
2826 staticpro (&searchbufs[i].regexp);
|
|
2827 searchbufs[i].next = (i == REGEXP_CACHE_SIZE-1 ? 0 : &searchbufs[i+1]);
|
|
2828 }
|
|
2829 searchbuf_head = &searchbufs[0];
|
603
|
2830
|
|
2831 Qsearch_failed = intern ("search-failed");
|
|
2832 staticpro (&Qsearch_failed);
|
|
2833 Qinvalid_regexp = intern ("invalid-regexp");
|
|
2834 staticpro (&Qinvalid_regexp);
|
|
2835
|
|
2836 Fput (Qsearch_failed, Qerror_conditions,
|
|
2837 Fcons (Qsearch_failed, Fcons (Qerror, Qnil)));
|
|
2838 Fput (Qsearch_failed, Qerror_message,
|
|
2839 build_string ("Search failed"));
|
|
2840
|
|
2841 Fput (Qinvalid_regexp, Qerror_conditions,
|
|
2842 Fcons (Qinvalid_regexp, Fcons (Qerror, Qnil)));
|
|
2843 Fput (Qinvalid_regexp, Qerror_message,
|
|
2844 build_string ("Invalid regexp"));
|
|
2845
|
727
|
2846 last_thing_searched = Qnil;
|
|
2847 staticpro (&last_thing_searched);
|
|
2848
|
10020
|
2849 defsubr (&Slooking_at);
|
|
2850 defsubr (&Sposix_looking_at);
|
603
|
2851 defsubr (&Sstring_match);
|
10020
|
2852 defsubr (&Sposix_string_match);
|
603
|
2853 defsubr (&Ssearch_forward);
|
|
2854 defsubr (&Ssearch_backward);
|
|
2855 defsubr (&Sword_search_forward);
|
|
2856 defsubr (&Sword_search_backward);
|
|
2857 defsubr (&Sre_search_forward);
|
|
2858 defsubr (&Sre_search_backward);
|
10020
|
2859 defsubr (&Sposix_search_forward);
|
|
2860 defsubr (&Sposix_search_backward);
|
603
|
2861 defsubr (&Sreplace_match);
|
|
2862 defsubr (&Smatch_beginning);
|
|
2863 defsubr (&Smatch_end);
|
|
2864 defsubr (&Smatch_data);
|
21171
|
2865 defsubr (&Sset_match_data);
|
603
|
2866 defsubr (&Sregexp_quote);
|
|
2867 }
|