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