Mercurial > emacs
annotate src/search.c @ 5746:94535442be19
(ad-execute-defadvices): Don't allocate advice-infos in pure space, in case we
want to preload this file.
author | Karl Heuer <kwzh@gnu.org> |
---|---|
date | Wed, 02 Feb 1994 21:35:07 +0000 |
parents | 14161cfec24a |
children | a54c236b43c6 |
rev | line source |
---|---|
603 | 1 /* String search routines for GNU Emacs. |
2961 | 2 Copyright (C) 1985, 1986, 1987, 1993 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 | |
8 the Free Software Foundation; either version 1, or (at your option) | |
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 | |
18 the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. */ | |
19 | |
20 | |
4696
1fc792473491
Include <config.h> instead of "config.h".
Roland McGrath <roland@gnu.org>
parents:
4635
diff
changeset
|
21 #include <config.h> |
603 | 22 #include "lisp.h" |
23 #include "syntax.h" | |
24 #include "buffer.h" | |
25 #include "commands.h" | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
26 #include "blockinput.h" |
621 | 27 |
603 | 28 #include <sys/types.h> |
29 #include "regex.h" | |
30 | |
31 #define max(a, b) ((a) > (b) ? (a) : (b)) | |
32 #define min(a, b) ((a) < (b) ? (a) : (b)) | |
33 | |
34 /* We compile regexps into this buffer and then use it for searching. */ | |
35 | |
36 struct re_pattern_buffer searchbuf; | |
37 | |
38 char search_fastmap[0400]; | |
39 | |
40 /* Last regexp we compiled */ | |
41 | |
42 Lisp_Object last_regexp; | |
43 | |
621 | 44 /* Every call to re_match, etc., must pass &search_regs as the regs |
45 argument unless you can show it is unnecessary (i.e., if re_match | |
46 is certainly going to be called again before region-around-match | |
47 can be called). | |
48 | |
49 Since the registers are now dynamically allocated, we need to make | |
50 sure not to refer to the Nth register before checking that it has | |
708 | 51 been allocated by checking search_regs.num_regs. |
603 | 52 |
708 | 53 The regex code keeps track of whether it has allocated the search |
54 buffer using bits in searchbuf. This means that whenever you | |
55 compile a new pattern, it completely forgets whether it has | |
56 allocated any registers, and will allocate new registers the next | |
57 time you call a searching or matching function. Therefore, we need | |
58 to call re_set_registers after compiling a new pattern or after | |
59 setting the match registers, so that the regex functions will be | |
60 able to free or re-allocate it properly. */ | |
603 | 61 static struct re_registers search_regs; |
62 | |
727 | 63 /* The buffer in which the last search was performed, or |
64 Qt if the last search was done in a string; | |
65 Qnil if no searching has been done yet. */ | |
66 static Lisp_Object last_thing_searched; | |
603 | 67 |
68 /* error condition signalled when regexp compile_pattern fails */ | |
69 | |
70 Lisp_Object Qinvalid_regexp; | |
71 | |
5556
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
72 static void set_search_regs (); |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
73 |
603 | 74 static void |
75 matcher_overflow () | |
76 { | |
77 error ("Stack overflow in regexp matcher"); | |
78 } | |
79 | |
80 #ifdef __STDC__ | |
81 #define CONST const | |
82 #else | |
83 #define CONST | |
84 #endif | |
85 | |
86 /* Compile a regexp and signal a Lisp error if anything goes wrong. */ | |
87 | |
708 | 88 compile_pattern (pattern, bufp, regp, translate) |
603 | 89 Lisp_Object pattern; |
90 struct re_pattern_buffer *bufp; | |
708 | 91 struct re_registers *regp; |
603 | 92 char *translate; |
93 { | |
94 CONST char *val; | |
95 Lisp_Object dummy; | |
96 | |
97 if (EQ (pattern, last_regexp) | |
98 && translate == bufp->translate) | |
99 return; | |
708 | 100 |
603 | 101 last_regexp = Qnil; |
102 bufp->translate = translate; | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
103 BLOCK_INPUT; |
4635
ad4add779dac
(compile_pattern): Cast result of re_compile_pattern.
Richard M. Stallman <rms@gnu.org>
parents:
4299
diff
changeset
|
104 val = (CONST char *) re_compile_pattern ((char *) XSTRING (pattern)->data, |
ad4add779dac
(compile_pattern): Cast result of re_compile_pattern.
Richard M. Stallman <rms@gnu.org>
parents:
4299
diff
changeset
|
105 XSTRING (pattern)->size, bufp); |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
106 UNBLOCK_INPUT; |
603 | 107 if (val) |
108 { | |
109 dummy = build_string (val); | |
110 while (1) | |
111 Fsignal (Qinvalid_regexp, Fcons (dummy, Qnil)); | |
112 } | |
708 | 113 |
603 | 114 last_regexp = pattern; |
708 | 115 |
116 /* Advise the searching functions about the space we have allocated | |
117 for register data. */ | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
118 BLOCK_INPUT; |
808 | 119 if (regp) |
120 re_set_registers (bufp, regp, regp->num_regs, regp->start, regp->end); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
121 UNBLOCK_INPUT; |
708 | 122 |
603 | 123 return; |
124 } | |
125 | |
126 /* Error condition used for failing searches */ | |
127 Lisp_Object Qsearch_failed; | |
128 | |
129 Lisp_Object | |
130 signal_failure (arg) | |
131 Lisp_Object arg; | |
132 { | |
133 Fsignal (Qsearch_failed, Fcons (arg, Qnil)); | |
134 return Qnil; | |
135 } | |
136 | |
137 DEFUN ("looking-at", Flooking_at, Slooking_at, 1, 1, 0, | |
638 | 138 "Return t if text after point matches regular expression PAT.\n\ |
139 This function modifies the match data that `match-beginning',\n\ | |
140 `match-end' and `match-data' access; save and restore the match\n\ | |
635
197f38dd0105
*** empty log message ***
Richard M. Stallman <rms@gnu.org>
parents:
621
diff
changeset
|
141 data if you want to preserve them.") |
603 | 142 (string) |
143 Lisp_Object string; | |
144 { | |
145 Lisp_Object val; | |
146 unsigned char *p1, *p2; | |
147 int s1, s2; | |
148 register int i; | |
149 | |
150 CHECK_STRING (string, 0); | |
708 | 151 compile_pattern (string, &searchbuf, &search_regs, |
603 | 152 !NILP (current_buffer->case_fold_search) ? DOWNCASE_TABLE : 0); |
153 | |
154 immediate_quit = 1; | |
155 QUIT; /* Do a pending quit right away, to avoid paradoxical behavior */ | |
156 | |
157 /* Get pointers and sizes of the two strings | |
158 that make up the visible portion of the buffer. */ | |
159 | |
160 p1 = BEGV_ADDR; | |
161 s1 = GPT - BEGV; | |
162 p2 = GAP_END_ADDR; | |
163 s2 = ZV - GPT; | |
164 if (s1 < 0) | |
165 { | |
166 p2 = p1; | |
167 s2 = ZV - BEGV; | |
168 s1 = 0; | |
169 } | |
170 if (s2 < 0) | |
171 { | |
172 s1 = ZV - BEGV; | |
173 s2 = 0; | |
174 } | |
175 | |
176 i = re_match_2 (&searchbuf, (char *) p1, s1, (char *) p2, s2, | |
177 point - BEGV, &search_regs, | |
178 ZV - BEGV); | |
179 if (i == -2) | |
180 matcher_overflow (); | |
181 | |
182 val = (0 <= i ? Qt : Qnil); | |
621 | 183 for (i = 0; i < search_regs.num_regs; i++) |
603 | 184 if (search_regs.start[i] >= 0) |
185 { | |
186 search_regs.start[i] += BEGV; | |
187 search_regs.end[i] += BEGV; | |
188 } | |
727 | 189 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 190 immediate_quit = 0; |
191 return val; | |
192 } | |
193 | |
194 DEFUN ("string-match", Fstring_match, Sstring_match, 2, 3, 0, | |
195 "Return index of start of first match for REGEXP in STRING, or nil.\n\ | |
196 If third arg START is non-nil, start search at that index in STRING.\n\ | |
197 For index of first char beyond the match, do (match-end 0).\n\ | |
198 `match-end' and `match-beginning' also give indices of substrings\n\ | |
199 matched by parenthesis constructs in the pattern.") | |
200 (regexp, string, start) | |
201 Lisp_Object regexp, string, start; | |
202 { | |
203 int val; | |
204 int s; | |
205 | |
206 CHECK_STRING (regexp, 0); | |
207 CHECK_STRING (string, 1); | |
208 | |
209 if (NILP (start)) | |
210 s = 0; | |
211 else | |
212 { | |
213 int len = XSTRING (string)->size; | |
214 | |
215 CHECK_NUMBER (start, 2); | |
216 s = XINT (start); | |
217 if (s < 0 && -s <= len) | |
218 s = len - s; | |
219 else if (0 > s || s > len) | |
220 args_out_of_range (string, start); | |
221 } | |
222 | |
708 | 223 compile_pattern (regexp, &searchbuf, &search_regs, |
603 | 224 !NILP (current_buffer->case_fold_search) ? DOWNCASE_TABLE : 0); |
225 immediate_quit = 1; | |
226 val = re_search (&searchbuf, (char *) XSTRING (string)->data, | |
227 XSTRING (string)->size, s, XSTRING (string)->size - s, | |
228 &search_regs); | |
229 immediate_quit = 0; | |
727 | 230 last_thing_searched = Qt; |
603 | 231 if (val == -2) |
232 matcher_overflow (); | |
233 if (val < 0) return Qnil; | |
234 return make_number (val); | |
235 } | |
842 | 236 |
237 /* Match REGEXP against STRING, searching all of STRING, | |
238 and return the index of the match, or negative on failure. | |
239 This does not clobber the match data. */ | |
240 | |
241 int | |
242 fast_string_match (regexp, string) | |
243 Lisp_Object regexp, string; | |
244 { | |
245 int val; | |
246 | |
247 compile_pattern (regexp, &searchbuf, 0, 0); | |
248 immediate_quit = 1; | |
249 val = re_search (&searchbuf, (char *) XSTRING (string)->data, | |
250 XSTRING (string)->size, 0, XSTRING (string)->size, | |
251 0); | |
252 immediate_quit = 0; | |
253 return val; | |
254 } | |
603 | 255 |
648 | 256 /* Search for COUNT instances of the character TARGET, starting at START. |
257 If COUNT is negative, search backwards. | |
258 | |
259 If we find COUNT instances, set *SHORTAGE to zero, and return the | |
1413 | 260 position after the COUNTth match. Note that for reverse motion |
261 this is not the same as the usual convention for Emacs motion commands. | |
648 | 262 |
263 If we don't find COUNT instances before reaching the end of the | |
264 buffer (or the beginning, if scanning backwards), set *SHORTAGE to | |
265 the number of TARGETs left unfound, and return the end of the | |
266 buffer we bumped up against. */ | |
267 | |
268 scan_buffer (target, start, count, shortage) | |
269 int *shortage, start; | |
270 register int count, target; | |
603 | 271 { |
648 | 272 int limit = ((count > 0) ? ZV - 1 : BEGV); |
273 int direction = ((count > 0) ? 1 : -1); | |
274 | |
275 register unsigned char *cursor; | |
603 | 276 unsigned char *base; |
648 | 277 |
278 register int ceiling; | |
279 register unsigned char *ceiling_addr; | |
603 | 280 |
281 if (shortage != 0) | |
282 *shortage = 0; | |
283 | |
284 immediate_quit = 1; | |
285 | |
648 | 286 if (count > 0) |
287 while (start != limit + 1) | |
603 | 288 { |
648 | 289 ceiling = BUFFER_CEILING_OF (start); |
290 ceiling = min (limit, ceiling); | |
291 ceiling_addr = &FETCH_CHAR (ceiling) + 1; | |
292 base = (cursor = &FETCH_CHAR (start)); | |
603 | 293 while (1) |
294 { | |
648 | 295 while (*cursor != target && ++cursor != ceiling_addr) |
603 | 296 ; |
648 | 297 if (cursor != ceiling_addr) |
603 | 298 { |
648 | 299 if (--count == 0) |
603 | 300 { |
301 immediate_quit = 0; | |
648 | 302 return (start + cursor - base + 1); |
603 | 303 } |
304 else | |
648 | 305 if (++cursor == ceiling_addr) |
603 | 306 break; |
307 } | |
308 else | |
309 break; | |
310 } | |
648 | 311 start += cursor - base; |
603 | 312 } |
313 else | |
314 { | |
648 | 315 start--; /* first character we scan */ |
316 while (start > limit - 1) | |
317 { /* we WILL scan under start */ | |
318 ceiling = BUFFER_FLOOR_OF (start); | |
319 ceiling = max (limit, ceiling); | |
320 ceiling_addr = &FETCH_CHAR (ceiling) - 1; | |
321 base = (cursor = &FETCH_CHAR (start)); | |
603 | 322 cursor++; |
323 while (1) | |
324 { | |
648 | 325 while (--cursor != ceiling_addr && *cursor != target) |
603 | 326 ; |
648 | 327 if (cursor != ceiling_addr) |
603 | 328 { |
648 | 329 if (++count == 0) |
603 | 330 { |
331 immediate_quit = 0; | |
648 | 332 return (start + cursor - base + 1); |
603 | 333 } |
334 } | |
335 else | |
336 break; | |
337 } | |
648 | 338 start += cursor - base; |
603 | 339 } |
340 } | |
341 immediate_quit = 0; | |
342 if (shortage != 0) | |
648 | 343 *shortage = count * direction; |
344 return (start + ((direction == 1 ? 0 : 1))); | |
603 | 345 } |
346 | |
347 int | |
348 find_next_newline (from, cnt) | |
349 register int from, cnt; | |
350 { | |
351 return (scan_buffer ('\n', from, cnt, (int *) 0)); | |
352 } | |
353 | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
354 Lisp_Object skip_chars (); |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
355 |
603 | 356 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0, |
4954
dc4d4f874b5c
(Fskip_chars_backward, Fskip_chars_forward): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
4951
diff
changeset
|
357 "Move point forward, stopping before a char not in STRING, or at pos LIM.\n\ |
dc4d4f874b5c
(Fskip_chars_backward, Fskip_chars_forward): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
4951
diff
changeset
|
358 STRING is like the inside of a `[...]' in a regular expression\n\ |
603 | 359 except that `]' is never special and `\\' quotes `^', `-' or `\\'.\n\ |
360 Thus, with arg \"a-zA-Z\", this skips letters stopping before first nonletter.\n\ | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
361 With arg \"^a-zA-Z\", skips nonletters stopping before first letter.\n\ |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
362 Returns the distance traveled, either zero or positive.") |
603 | 363 (string, lim) |
364 Lisp_Object string, lim; | |
365 { | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
366 return skip_chars (1, 0, string, lim); |
603 | 367 } |
368 | |
369 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0, | |
4954
dc4d4f874b5c
(Fskip_chars_backward, Fskip_chars_forward): Doc fix.
Richard M. Stallman <rms@gnu.org>
parents:
4951
diff
changeset
|
370 "Move point backward, stopping after a char not in STRING, or at pos LIM.\n\ |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
371 See `skip-chars-forward' for details.\n\ |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
372 Returns the distance traveled, either zero or negative.") |
603 | 373 (string, lim) |
374 Lisp_Object string, lim; | |
375 { | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
376 return skip_chars (0, 0, string, lim); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
377 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
378 |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
379 DEFUN ("skip-syntax-forward", Fskip_syntax_forward, Sskip_syntax_forward, 1, 2, 0, |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
380 "Move point forward across chars in specified syntax classes.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
381 SYNTAX is a string of syntax code characters.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
382 Stop before a char whose syntax is not in SYNTAX, or at position LIM.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
383 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
384 This function returns the distance traveled, either zero or positive.") |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
385 (syntax, lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
386 Lisp_Object syntax, lim; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
387 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
388 return skip_chars (1, 1, syntax, lim); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
389 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
390 |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
391 DEFUN ("skip-syntax-backward", Fskip_syntax_backward, Sskip_syntax_backward, 1, 2, 0, |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
392 "Move point backward across chars in specified syntax classes.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
393 SYNTAX is a string of syntax code characters.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
394 Stop on reaching a char whose syntax is not in SYNTAX, or at position LIM.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
395 If SYNTAX starts with ^, skip characters whose syntax is NOT in SYNTAX.\n\ |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
396 This function returns the distance traveled, either zero or negative.") |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
397 (syntax, lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
398 Lisp_Object syntax, lim; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
399 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
400 return skip_chars (0, 1, syntax, lim); |
603 | 401 } |
402 | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
403 Lisp_Object |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
404 skip_chars (forwardp, syntaxp, string, lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
405 int forwardp, syntaxp; |
603 | 406 Lisp_Object string, lim; |
407 { | |
408 register unsigned char *p, *pend; | |
409 register unsigned char c; | |
410 unsigned char fastmap[0400]; | |
411 int negate = 0; | |
412 register int i; | |
413 | |
414 CHECK_STRING (string, 0); | |
415 | |
416 if (NILP (lim)) | |
417 XSET (lim, Lisp_Int, forwardp ? ZV : BEGV); | |
418 else | |
419 CHECK_NUMBER_COERCE_MARKER (lim, 1); | |
420 | |
4831
66a523672100
(skip_chars): Reinstate check for end of buffer, ignoring cryptic
Brian Fox <bfox@gnu.org>
parents:
4713
diff
changeset
|
421 /* In any case, don't allow scan outside bounds of buffer. */ |
4951
be690aaa7194
(skip_chars): Finish reenabling checks for buffer bounds.
Richard M. Stallman <rms@gnu.org>
parents:
4882
diff
changeset
|
422 /* jla turned this off, for no known reason. |
be690aaa7194
(skip_chars): Finish reenabling checks for buffer bounds.
Richard M. Stallman <rms@gnu.org>
parents:
4882
diff
changeset
|
423 bfox turned the ZV part on, and rms turned the |
be690aaa7194
(skip_chars): Finish reenabling checks for buffer bounds.
Richard M. Stallman <rms@gnu.org>
parents:
4882
diff
changeset
|
424 BEGV part back on. */ |
be690aaa7194
(skip_chars): Finish reenabling checks for buffer bounds.
Richard M. Stallman <rms@gnu.org>
parents:
4882
diff
changeset
|
425 if (XINT (lim) > ZV) |
603 | 426 XFASTINT (lim) = ZV; |
4951
be690aaa7194
(skip_chars): Finish reenabling checks for buffer bounds.
Richard M. Stallman <rms@gnu.org>
parents:
4882
diff
changeset
|
427 if (XINT (lim) < BEGV) |
603 | 428 XFASTINT (lim) = BEGV; |
429 | |
430 p = XSTRING (string)->data; | |
431 pend = p + XSTRING (string)->size; | |
432 bzero (fastmap, sizeof fastmap); | |
433 | |
434 if (p != pend && *p == '^') | |
435 { | |
436 negate = 1; p++; | |
437 } | |
438 | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
439 /* Find the characters specified and set their elements of fastmap. |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
440 If syntaxp, each character counts as itself. |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
441 Otherwise, handle backslashes and ranges specially */ |
603 | 442 |
443 while (p != pend) | |
444 { | |
445 c = *p++; | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
446 if (syntaxp) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
447 fastmap[c] = 1; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
448 else |
603 | 449 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
450 if (c == '\\') |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
451 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
452 if (p == pend) break; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
453 c = *p++; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
454 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
455 if (p != pend && *p == '-') |
603 | 456 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
457 p++; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
458 if (p == pend) break; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
459 while (c <= *p) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
460 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
461 fastmap[c] = 1; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
462 c++; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
463 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
464 p++; |
603 | 465 } |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
466 else |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
467 fastmap[c] = 1; |
603 | 468 } |
469 } | |
470 | |
471 /* If ^ was the first character, complement the fastmap. */ | |
472 | |
473 if (negate) | |
474 for (i = 0; i < sizeof fastmap; i++) | |
475 fastmap[i] ^= 1; | |
476 | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
477 { |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
478 int start_point = point; |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
479 |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
480 immediate_quit = 1; |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
481 if (syntaxp) |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
482 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
483 |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
484 if (forwardp) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
485 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
486 while (point < XINT (lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
487 && fastmap[(unsigned char) syntax_code_spec[(int) SYNTAX (FETCH_CHAR (point))]]) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
488 SET_PT (point + 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
489 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
490 else |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
491 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
492 while (point > XINT (lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
493 && fastmap[(unsigned char) syntax_code_spec[(int) SYNTAX (FETCH_CHAR (point - 1))]]) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
494 SET_PT (point - 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
495 } |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
496 } |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
497 else |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
498 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
499 if (forwardp) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
500 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
501 while (point < XINT (lim) && fastmap[FETCH_CHAR (point)]) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
502 SET_PT (point + 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
503 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
504 else |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
505 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
506 while (point > XINT (lim) && fastmap[FETCH_CHAR (point - 1)]) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
507 SET_PT (point - 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
508 } |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
509 } |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
510 immediate_quit = 0; |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
511 |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
512 return make_number (point - start_point); |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
513 } |
603 | 514 } |
515 | |
516 /* Subroutines of Lisp buffer search functions. */ | |
517 | |
518 static Lisp_Object | |
519 search_command (string, bound, noerror, count, direction, RE) | |
520 Lisp_Object string, bound, noerror, count; | |
521 int direction; | |
522 int RE; | |
523 { | |
524 register int np; | |
525 int lim; | |
526 int n = direction; | |
527 | |
528 if (!NILP (count)) | |
529 { | |
530 CHECK_NUMBER (count, 3); | |
531 n *= XINT (count); | |
532 } | |
533 | |
534 CHECK_STRING (string, 0); | |
535 if (NILP (bound)) | |
536 lim = n > 0 ? ZV : BEGV; | |
537 else | |
538 { | |
539 CHECK_NUMBER_COERCE_MARKER (bound, 1); | |
540 lim = XINT (bound); | |
541 if (n > 0 ? lim < point : lim > point) | |
542 error ("Invalid search bound (wrong side of point)"); | |
543 if (lim > ZV) | |
544 lim = ZV; | |
545 if (lim < BEGV) | |
546 lim = BEGV; | |
547 } | |
548 | |
549 np = search_buffer (string, point, lim, n, RE, | |
550 (!NILP (current_buffer->case_fold_search) | |
551 ? XSTRING (current_buffer->case_canon_table)->data : 0), | |
552 (!NILP (current_buffer->case_fold_search) | |
553 ? XSTRING (current_buffer->case_eqv_table)->data : 0)); | |
554 if (np <= 0) | |
555 { | |
556 if (NILP (noerror)) | |
557 return signal_failure (string); | |
558 if (!EQ (noerror, Qt)) | |
559 { | |
560 if (lim < BEGV || lim > ZV) | |
561 abort (); | |
1878
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
562 SET_PT (lim); |
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
563 return Qnil; |
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
564 #if 0 /* This would be clean, but maybe programs depend on |
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
565 a value of nil here. */ |
1877
7786f61ec635
(search_command): When moving to LIM on failure, return LIM.
Richard M. Stallman <rms@gnu.org>
parents:
1684
diff
changeset
|
566 np = lim; |
1878
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
567 #endif |
603 | 568 } |
1877
7786f61ec635
(search_command): When moving to LIM on failure, return LIM.
Richard M. Stallman <rms@gnu.org>
parents:
1684
diff
changeset
|
569 else |
7786f61ec635
(search_command): When moving to LIM on failure, return LIM.
Richard M. Stallman <rms@gnu.org>
parents:
1684
diff
changeset
|
570 return Qnil; |
603 | 571 } |
572 | |
573 if (np < BEGV || np > ZV) | |
574 abort (); | |
575 | |
576 SET_PT (np); | |
577 | |
578 return make_number (np); | |
579 } | |
580 | |
5556
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
581 /* Search for the n'th occurrence of STRING in the current buffer, |
603 | 582 starting at position POS and stopping at position LIM, |
583 treating PAT as a literal string if RE is false or as | |
584 a regular expression if RE is true. | |
585 | |
586 If N is positive, searching is forward and LIM must be greater than POS. | |
587 If N is negative, searching is backward and LIM must be less than POS. | |
588 | |
589 Returns -x if only N-x occurrences found (x > 0), | |
590 or else the position at the beginning of the Nth occurrence | |
591 (if searching backward) or the end (if searching forward). */ | |
592 | |
593 search_buffer (string, pos, lim, n, RE, trt, inverse_trt) | |
594 Lisp_Object string; | |
595 int pos; | |
596 int lim; | |
597 int n; | |
598 int RE; | |
599 register unsigned char *trt; | |
600 register unsigned char *inverse_trt; | |
601 { | |
602 int len = XSTRING (string)->size; | |
603 unsigned char *base_pat = XSTRING (string)->data; | |
604 register int *BM_tab; | |
605 int *BM_tab_base; | |
606 register int direction = ((n > 0) ? 1 : -1); | |
607 register int dirlen; | |
608 int infinity, limit, k, stride_for_teases; | |
609 register unsigned char *pat, *cursor, *p_limit; | |
610 register int i, j; | |
611 unsigned char *p1, *p2; | |
612 int s1, s2; | |
613 | |
614 /* Null string is found at starting position. */ | |
4299
7a2e1d7362c5
(search_buffer): If n is 0, just return POS.
Richard M. Stallman <rms@gnu.org>
parents:
3615
diff
changeset
|
615 if (len == 0) |
5556
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
616 { |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
617 set_search_regs (pos, 0); |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
618 return pos; |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
619 } |
4299
7a2e1d7362c5
(search_buffer): If n is 0, just return POS.
Richard M. Stallman <rms@gnu.org>
parents:
3615
diff
changeset
|
620 |
7a2e1d7362c5
(search_buffer): If n is 0, just return POS.
Richard M. Stallman <rms@gnu.org>
parents:
3615
diff
changeset
|
621 /* Searching 0 times means don't move. */ |
7a2e1d7362c5
(search_buffer): If n is 0, just return POS.
Richard M. Stallman <rms@gnu.org>
parents:
3615
diff
changeset
|
622 if (n == 0) |
603 | 623 return pos; |
624 | |
625 if (RE) | |
708 | 626 compile_pattern (string, &searchbuf, &search_regs, (char *) trt); |
603 | 627 |
628 if (RE /* Here we detect whether the */ | |
629 /* generality of an RE search is */ | |
630 /* really needed. */ | |
631 /* first item is "exact match" */ | |
621 | 632 && *(searchbuf.buffer) == (char) RE_EXACTN_VALUE |
603 | 633 && searchbuf.buffer[1] + 2 == searchbuf.used) /*first is ONLY item */ |
634 { | |
635 RE = 0; /* can do straight (non RE) search */ | |
636 pat = (base_pat = (unsigned char *) searchbuf.buffer + 2); | |
637 /* trt already applied */ | |
638 len = searchbuf.used - 2; | |
639 } | |
640 else if (!RE) | |
641 { | |
642 pat = (unsigned char *) alloca (len); | |
643 | |
644 for (i = len; i--;) /* Copy the pattern; apply trt */ | |
645 *pat++ = (((int) trt) ? trt [*base_pat++] : *base_pat++); | |
646 pat -= len; base_pat = pat; | |
647 } | |
648 | |
649 if (RE) | |
650 { | |
651 immediate_quit = 1; /* Quit immediately if user types ^G, | |
652 because letting this function finish | |
653 can take too long. */ | |
654 QUIT; /* Do a pending quit right away, | |
655 to avoid paradoxical behavior */ | |
656 /* Get pointers and sizes of the two strings | |
657 that make up the visible portion of the buffer. */ | |
658 | |
659 p1 = BEGV_ADDR; | |
660 s1 = GPT - BEGV; | |
661 p2 = GAP_END_ADDR; | |
662 s2 = ZV - GPT; | |
663 if (s1 < 0) | |
664 { | |
665 p2 = p1; | |
666 s2 = ZV - BEGV; | |
667 s1 = 0; | |
668 } | |
669 if (s2 < 0) | |
670 { | |
671 s1 = ZV - BEGV; | |
672 s2 = 0; | |
673 } | |
674 while (n < 0) | |
675 { | |
2475
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
676 int val; |
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
677 val = re_search_2 (&searchbuf, (char *) p1, s1, (char *) p2, s2, |
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
678 pos - BEGV, lim - pos, &search_regs, |
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
679 /* Don't allow match past current point */ |
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
680 pos - BEGV); |
603 | 681 if (val == -2) |
682 matcher_overflow (); | |
683 if (val >= 0) | |
684 { | |
685 j = BEGV; | |
621 | 686 for (i = 0; i < search_regs.num_regs; i++) |
603 | 687 if (search_regs.start[i] >= 0) |
688 { | |
689 search_regs.start[i] += j; | |
690 search_regs.end[i] += j; | |
691 } | |
727 | 692 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 693 /* Set pos to the new position. */ |
694 pos = search_regs.start[0]; | |
695 } | |
696 else | |
697 { | |
698 immediate_quit = 0; | |
699 return (n); | |
700 } | |
701 n++; | |
702 } | |
703 while (n > 0) | |
704 { | |
2475
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
705 int val; |
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
706 val = re_search_2 (&searchbuf, (char *) p1, s1, (char *) p2, s2, |
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
707 pos - BEGV, lim - pos, &search_regs, |
052bbdf1b817
(search_buffer): Fix typo in previous change.
Richard M. Stallman <rms@gnu.org>
parents:
2439
diff
changeset
|
708 lim - BEGV); |
603 | 709 if (val == -2) |
710 matcher_overflow (); | |
711 if (val >= 0) | |
712 { | |
713 j = BEGV; | |
621 | 714 for (i = 0; i < search_regs.num_regs; i++) |
603 | 715 if (search_regs.start[i] >= 0) |
716 { | |
717 search_regs.start[i] += j; | |
718 search_regs.end[i] += j; | |
719 } | |
727 | 720 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 721 pos = search_regs.end[0]; |
722 } | |
723 else | |
724 { | |
725 immediate_quit = 0; | |
726 return (0 - n); | |
727 } | |
728 n--; | |
729 } | |
730 immediate_quit = 0; | |
731 return (pos); | |
732 } | |
733 else /* non-RE case */ | |
734 { | |
735 #ifdef C_ALLOCA | |
736 int BM_tab_space[0400]; | |
737 BM_tab = &BM_tab_space[0]; | |
738 #else | |
739 BM_tab = (int *) alloca (0400 * sizeof (int)); | |
740 #endif | |
741 /* The general approach is that we are going to maintain that we know */ | |
742 /* the first (closest to the present position, in whatever direction */ | |
743 /* we're searching) character that could possibly be the last */ | |
744 /* (furthest from present position) character of a valid match. We */ | |
745 /* advance the state of our knowledge by looking at that character */ | |
746 /* and seeing whether it indeed matches the last character of the */ | |
747 /* pattern. If it does, we take a closer look. If it does not, we */ | |
748 /* move our pointer (to putative last characters) as far as is */ | |
749 /* logically possible. This amount of movement, which I call a */ | |
750 /* stride, will be the length of the pattern if the actual character */ | |
751 /* appears nowhere in the pattern, otherwise it will be the distance */ | |
752 /* from the last occurrence of that character to the end of the */ | |
753 /* pattern. */ | |
754 /* As a coding trick, an enormous stride is coded into the table for */ | |
755 /* characters that match the last character. This allows use of only */ | |
756 /* a single test, a test for having gone past the end of the */ | |
757 /* permissible match region, to test for both possible matches (when */ | |
758 /* the stride goes past the end immediately) and failure to */ | |
759 /* match (where you get nudged past the end one stride at a time). */ | |
760 | |
761 /* Here we make a "mickey mouse" BM table. The stride of the search */ | |
762 /* is determined only by the last character of the putative match. */ | |
763 /* If that character does not match, we will stride the proper */ | |
764 /* distance to propose a match that superimposes it on the last */ | |
765 /* instance of a character that matches it (per trt), or misses */ | |
766 /* it entirely if there is none. */ | |
767 | |
768 dirlen = len * direction; | |
769 infinity = dirlen - (lim + pos + len + len) * direction; | |
770 if (direction < 0) | |
771 pat = (base_pat += len - 1); | |
772 BM_tab_base = BM_tab; | |
773 BM_tab += 0400; | |
774 j = dirlen; /* to get it in a register */ | |
775 /* A character that does not appear in the pattern induces a */ | |
776 /* stride equal to the pattern length. */ | |
777 while (BM_tab_base != BM_tab) | |
778 { | |
779 *--BM_tab = j; | |
780 *--BM_tab = j; | |
781 *--BM_tab = j; | |
782 *--BM_tab = j; | |
783 } | |
784 i = 0; | |
785 while (i != infinity) | |
786 { | |
787 j = pat[i]; i += direction; | |
788 if (i == dirlen) i = infinity; | |
789 if ((int) trt) | |
790 { | |
791 k = (j = trt[j]); | |
792 if (i == infinity) | |
793 stride_for_teases = BM_tab[j]; | |
794 BM_tab[j] = dirlen - i; | |
795 /* A translation table is accompanied by its inverse -- see */ | |
796 /* comment following downcase_table for details */ | |
797 while ((j = inverse_trt[j]) != k) | |
798 BM_tab[j] = dirlen - i; | |
799 } | |
800 else | |
801 { | |
802 if (i == infinity) | |
803 stride_for_teases = BM_tab[j]; | |
804 BM_tab[j] = dirlen - i; | |
805 } | |
806 /* stride_for_teases tells how much to stride if we get a */ | |
807 /* match on the far character but are subsequently */ | |
808 /* disappointed, by recording what the stride would have been */ | |
809 /* for that character if the last character had been */ | |
810 /* different. */ | |
811 } | |
812 infinity = dirlen - infinity; | |
813 pos += dirlen - ((direction > 0) ? direction : 0); | |
814 /* loop invariant - pos points at where last char (first char if reverse) | |
815 of pattern would align in a possible match. */ | |
816 while (n != 0) | |
817 { | |
818 if ((lim - pos - (direction > 0)) * direction < 0) | |
819 return (n * (0 - direction)); | |
820 /* First we do the part we can by pointers (maybe nothing) */ | |
821 QUIT; | |
822 pat = base_pat; | |
823 limit = pos - dirlen + direction; | |
824 limit = ((direction > 0) | |
825 ? BUFFER_CEILING_OF (limit) | |
826 : BUFFER_FLOOR_OF (limit)); | |
827 /* LIMIT is now the last (not beyond-last!) value | |
828 POS can take on without hitting edge of buffer or the gap. */ | |
829 limit = ((direction > 0) | |
830 ? min (lim - 1, min (limit, pos + 20000)) | |
831 : max (lim, max (limit, pos - 20000))); | |
832 if ((limit - pos) * direction > 20) | |
833 { | |
834 p_limit = &FETCH_CHAR (limit); | |
835 p2 = (cursor = &FETCH_CHAR (pos)); | |
836 /* In this loop, pos + cursor - p2 is the surrogate for pos */ | |
837 while (1) /* use one cursor setting as long as i can */ | |
838 { | |
839 if (direction > 0) /* worth duplicating */ | |
840 { | |
841 /* Use signed comparison if appropriate | |
842 to make cursor+infinity sure to be > p_limit. | |
843 Assuming that the buffer lies in a range of addresses | |
844 that are all "positive" (as ints) or all "negative", | |
845 either kind of comparison will work as long | |
846 as we don't step by infinity. So pick the kind | |
847 that works when we do step by infinity. */ | |
848 if ((int) (p_limit + infinity) > (int) p_limit) | |
849 while ((int) cursor <= (int) p_limit) | |
850 cursor += BM_tab[*cursor]; | |
851 else | |
852 while ((unsigned int) cursor <= (unsigned int) p_limit) | |
853 cursor += BM_tab[*cursor]; | |
854 } | |
855 else | |
856 { | |
857 if ((int) (p_limit + infinity) < (int) p_limit) | |
858 while ((int) cursor >= (int) p_limit) | |
859 cursor += BM_tab[*cursor]; | |
860 else | |
861 while ((unsigned int) cursor >= (unsigned int) p_limit) | |
862 cursor += BM_tab[*cursor]; | |
863 } | |
864 /* If you are here, cursor is beyond the end of the searched region. */ | |
865 /* This can happen if you match on the far character of the pattern, */ | |
866 /* because the "stride" of that character is infinity, a number able */ | |
867 /* to throw you well beyond the end of the search. It can also */ | |
868 /* happen if you fail to match within the permitted region and would */ | |
869 /* otherwise try a character beyond that region */ | |
870 if ((cursor - p_limit) * direction <= len) | |
871 break; /* a small overrun is genuine */ | |
872 cursor -= infinity; /* large overrun = hit */ | |
873 i = dirlen - direction; | |
874 if ((int) trt) | |
875 { | |
876 while ((i -= direction) + direction != 0) | |
877 if (pat[i] != trt[*(cursor -= direction)]) | |
878 break; | |
879 } | |
880 else | |
881 { | |
882 while ((i -= direction) + direction != 0) | |
883 if (pat[i] != *(cursor -= direction)) | |
884 break; | |
885 } | |
886 cursor += dirlen - i - direction; /* fix cursor */ | |
887 if (i + direction == 0) | |
888 { | |
889 cursor -= direction; | |
708 | 890 |
5556
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
891 set_search_regs (pos + cursor - p2 + ((direction > 0) |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
892 ? 1 - len : 0), |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
893 len); |
708 | 894 |
603 | 895 if ((n -= direction) != 0) |
896 cursor += dirlen; /* to resume search */ | |
897 else | |
898 return ((direction > 0) | |
899 ? search_regs.end[0] : search_regs.start[0]); | |
900 } | |
901 else | |
902 cursor += stride_for_teases; /* <sigh> we lose - */ | |
903 } | |
904 pos += cursor - p2; | |
905 } | |
906 else | |
907 /* Now we'll pick up a clump that has to be done the hard */ | |
908 /* way because it covers a discontinuity */ | |
909 { | |
910 limit = ((direction > 0) | |
911 ? BUFFER_CEILING_OF (pos - dirlen + 1) | |
912 : BUFFER_FLOOR_OF (pos - dirlen - 1)); | |
913 limit = ((direction > 0) | |
914 ? min (limit + len, lim - 1) | |
915 : max (limit - len, lim)); | |
916 /* LIMIT is now the last value POS can have | |
917 and still be valid for a possible match. */ | |
918 while (1) | |
919 { | |
920 /* This loop can be coded for space rather than */ | |
921 /* speed because it will usually run only once. */ | |
922 /* (the reach is at most len + 21, and typically */ | |
923 /* does not exceed len) */ | |
924 while ((limit - pos) * direction >= 0) | |
925 pos += BM_tab[FETCH_CHAR(pos)]; | |
926 /* now run the same tests to distinguish going off the */ | |
3591
507f64624555
Apply typo patches from Paul Eggert.
Jim Blandy <jimb@redhat.com>
parents:
2961
diff
changeset
|
927 /* end, a match or a phony match. */ |
603 | 928 if ((pos - limit) * direction <= len) |
929 break; /* ran off the end */ | |
930 /* Found what might be a match. | |
931 Set POS back to last (first if reverse) char pos. */ | |
932 pos -= infinity; | |
933 i = dirlen - direction; | |
934 while ((i -= direction) + direction != 0) | |
935 { | |
936 pos -= direction; | |
937 if (pat[i] != (((int) trt) | |
938 ? trt[FETCH_CHAR(pos)] | |
939 : FETCH_CHAR (pos))) | |
940 break; | |
941 } | |
942 /* Above loop has moved POS part or all the way | |
943 back to the first char pos (last char pos if reverse). | |
944 Set it once again at the last (first if reverse) char. */ | |
945 pos += dirlen - i- direction; | |
946 if (i + direction == 0) | |
947 { | |
948 pos -= direction; | |
708 | 949 |
5556
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
950 set_search_regs (pos + ((direction > 0) ? 1 - len : 0), |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
951 len); |
708 | 952 |
603 | 953 if ((n -= direction) != 0) |
954 pos += dirlen; /* to resume search */ | |
955 else | |
956 return ((direction > 0) | |
957 ? search_regs.end[0] : search_regs.start[0]); | |
958 } | |
959 else | |
960 pos += stride_for_teases; | |
961 } | |
962 } | |
963 /* We have done one clump. Can we continue? */ | |
964 if ((lim - pos) * direction < 0) | |
965 return ((0 - n) * direction); | |
966 } | |
967 return pos; | |
968 } | |
969 } | |
5556
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
970 |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
971 /* Record beginning BEG and end BEG + LEN |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
972 for a match just found in the current buffer. */ |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
973 |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
974 static void |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
975 set_search_regs (beg, len) |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
976 int beg, len; |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
977 { |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
978 /* Make sure we have registers in which to store |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
979 the match position. */ |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
980 if (search_regs.num_regs == 0) |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
981 { |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
982 regoff_t *starts, *ends; |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
983 |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
984 starts = (regoff_t *) xmalloc (2 * sizeof (regoff_t)); |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
985 ends = (regoff_t *) xmalloc (2 * sizeof (regoff_t)); |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
986 BLOCK_INPUT; |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
987 re_set_registers (&searchbuf, |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
988 &search_regs, |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
989 2, starts, ends); |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
990 UNBLOCK_INPUT; |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
991 } |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
992 |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
993 search_regs.start[0] = beg; |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
994 search_regs.end[0] = beg + len; |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
995 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
14161cfec24a
(set_search_regs): New subroutine.
Richard M. Stallman <rms@gnu.org>
parents:
4954
diff
changeset
|
996 } |
603 | 997 |
998 /* Given a string of words separated by word delimiters, | |
999 compute a regexp that matches those exact words | |
1000 separated by arbitrary punctuation. */ | |
1001 | |
1002 static Lisp_Object | |
1003 wordify (string) | |
1004 Lisp_Object string; | |
1005 { | |
1006 register unsigned char *p, *o; | |
1007 register int i, len, punct_count = 0, word_count = 0; | |
1008 Lisp_Object val; | |
1009 | |
1010 CHECK_STRING (string, 0); | |
1011 p = XSTRING (string)->data; | |
1012 len = XSTRING (string)->size; | |
1013 | |
1014 for (i = 0; i < len; i++) | |
1015 if (SYNTAX (p[i]) != Sword) | |
1016 { | |
1017 punct_count++; | |
1018 if (i > 0 && SYNTAX (p[i-1]) == Sword) word_count++; | |
1019 } | |
1020 if (SYNTAX (p[len-1]) == Sword) word_count++; | |
1021 if (!word_count) return build_string (""); | |
1022 | |
1023 val = make_string (p, len - punct_count + 5 * (word_count - 1) + 4); | |
1024 | |
1025 o = XSTRING (val)->data; | |
1026 *o++ = '\\'; | |
1027 *o++ = 'b'; | |
1028 | |
1029 for (i = 0; i < len; i++) | |
1030 if (SYNTAX (p[i]) == Sword) | |
1031 *o++ = p[i]; | |
1032 else if (i > 0 && SYNTAX (p[i-1]) == Sword && --word_count) | |
1033 { | |
1034 *o++ = '\\'; | |
1035 *o++ = 'W'; | |
1036 *o++ = '\\'; | |
1037 *o++ = 'W'; | |
1038 *o++ = '*'; | |
1039 } | |
1040 | |
1041 *o++ = '\\'; | |
1042 *o++ = 'b'; | |
1043 | |
1044 return val; | |
1045 } | |
1046 | |
1047 DEFUN ("search-backward", Fsearch_backward, Ssearch_backward, 1, 4, | |
1048 "sSearch backward: ", | |
1049 "Search backward from point for STRING.\n\ | |
1050 Set point to the beginning of the occurrence found, and return point.\n\ | |
1051 An optional second argument bounds the search; it is a buffer position.\n\ | |
1052 The match found must not extend before that position.\n\ | |
1053 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1054 If not nil and not t, position at limit of search and return nil.\n\ | |
1055 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1056 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1057 (string, bound, noerror, count) | |
1058 Lisp_Object string, bound, noerror, count; | |
1059 { | |
1060 return search_command (string, bound, noerror, count, -1, 0); | |
1061 } | |
1062 | |
1063 DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "sSearch: ", | |
1064 "Search forward from point for STRING.\n\ | |
1065 Set point to the end of the occurrence found, and return point.\n\ | |
1066 An optional second argument bounds the search; it is a buffer position.\n\ | |
1067 The match found must not extend after that position. nil is equivalent\n\ | |
1068 to (point-max).\n\ | |
1069 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1070 If not nil and not t, move to limit of search and return nil.\n\ | |
1071 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1072 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1073 (string, bound, noerror, count) | |
1074 Lisp_Object string, bound, noerror, count; | |
1075 { | |
1076 return search_command (string, bound, noerror, count, 1, 0); | |
1077 } | |
1078 | |
1079 DEFUN ("word-search-backward", Fword_search_backward, Sword_search_backward, 1, 4, | |
1080 "sWord search backward: ", | |
1081 "Search backward from point for STRING, ignoring differences in punctuation.\n\ | |
1082 Set point to the beginning of the occurrence found, and return point.\n\ | |
1083 An optional second argument bounds the search; it is a buffer position.\n\ | |
1084 The match found must not extend before that position.\n\ | |
1085 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1086 If not nil and not t, move to limit of search and return nil.\n\ | |
1087 Optional fourth argument is repeat count--search for successive occurrences.") | |
1088 (string, bound, noerror, count) | |
1089 Lisp_Object string, bound, noerror, count; | |
1090 { | |
1091 return search_command (wordify (string), bound, noerror, count, -1, 1); | |
1092 } | |
1093 | |
1094 DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4, | |
1095 "sWord search: ", | |
1096 "Search forward from point for STRING, ignoring differences in punctuation.\n\ | |
1097 Set point to the end of the occurrence found, and return point.\n\ | |
1098 An optional second argument bounds the search; it is a buffer position.\n\ | |
1099 The match found must not extend after that position.\n\ | |
1100 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1101 If not nil and not t, move to limit of search and return nil.\n\ | |
1102 Optional fourth argument is repeat count--search for successive occurrences.") | |
1103 (string, bound, noerror, count) | |
1104 Lisp_Object string, bound, noerror, count; | |
1105 { | |
1106 return search_command (wordify (string), bound, noerror, count, 1, 1); | |
1107 } | |
1108 | |
1109 DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4, | |
1110 "sRE search backward: ", | |
1111 "Search backward from point for match for regular expression REGEXP.\n\ | |
1112 Set point to the beginning of the match, and return point.\n\ | |
1113 The match found is the one starting last in the buffer\n\ | |
1114 and yet ending before the place the origin of the search.\n\ | |
1115 An optional second argument bounds the search; it is a buffer position.\n\ | |
1116 The match found must start at or after that position.\n\ | |
1117 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1118 If not nil and not t, move to limit of search and return nil.\n\ | |
1119 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1120 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1121 (string, bound, noerror, count) | |
1122 Lisp_Object string, bound, noerror, count; | |
1123 { | |
1124 return search_command (string, bound, noerror, count, -1, 1); | |
1125 } | |
1126 | |
1127 DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4, | |
1128 "sRE search: ", | |
1129 "Search forward from point for regular expression REGEXP.\n\ | |
1130 Set point to the end of the occurrence found, and return point.\n\ | |
1131 An optional second argument bounds the search; it is a buffer position.\n\ | |
1132 The match found must not extend after that position.\n\ | |
1133 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1134 If not nil and not t, move to limit of search and return nil.\n\ | |
1135 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1136 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1137 (string, bound, noerror, count) | |
1138 Lisp_Object string, bound, noerror, count; | |
1139 { | |
1140 return search_command (string, bound, noerror, count, 1, 1); | |
1141 } | |
1142 | |
1143 DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 3, 0, | |
1144 "Replace text matched by last search with NEWTEXT.\n\ | |
1145 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.\n\ | |
1146 Otherwise convert to all caps or cap initials, like replaced text.\n\ | |
1147 If third arg LITERAL is non-nil, insert NEWTEXT literally.\n\ | |
1148 Otherwise treat `\\' as special:\n\ | |
1149 `\\&' in NEWTEXT means substitute original matched text.\n\ | |
1150 `\\N' means substitute what matched the Nth `\\(...\\)'.\n\ | |
1151 If Nth parens didn't match, substitute nothing.\n\ | |
1152 `\\\\' means insert one `\\'.\n\ | |
708 | 1153 FIXEDCASE and LITERAL are optional arguments.\n\ |
603 | 1154 Leaves point at end of replacement text.") |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1155 (newtext, fixedcase, literal) |
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1156 Lisp_Object newtext, fixedcase, literal; |
603 | 1157 { |
1158 enum { nochange, all_caps, cap_initial } case_action; | |
1159 register int pos, last; | |
1160 int some_multiletter_word; | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1161 int some_lowercase; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1162 int some_uppercase_initial; |
603 | 1163 register int c, prevc; |
1164 int inslen; | |
1165 | |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1166 CHECK_STRING (newtext, 0); |
603 | 1167 |
1168 case_action = nochange; /* We tried an initialization */ | |
1169 /* but some C compilers blew it */ | |
621 | 1170 |
1171 if (search_regs.num_regs <= 0) | |
1172 error ("replace-match called before any match found"); | |
1173 | |
603 | 1174 if (search_regs.start[0] < BEGV |
1175 || search_regs.start[0] > search_regs.end[0] | |
1176 || search_regs.end[0] > ZV) | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1177 args_out_of_range (make_number (search_regs.start[0]), |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1178 make_number (search_regs.end[0])); |
603 | 1179 |
1180 if (NILP (fixedcase)) | |
1181 { | |
1182 /* Decide how to casify by examining the matched text. */ | |
1183 | |
1184 last = search_regs.end[0]; | |
1185 prevc = '\n'; | |
1186 case_action = all_caps; | |
1187 | |
1188 /* some_multiletter_word is set nonzero if any original word | |
1189 is more than one letter long. */ | |
1190 some_multiletter_word = 0; | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1191 some_lowercase = 0; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1192 some_uppercase_initial = 0; |
603 | 1193 |
1194 for (pos = search_regs.start[0]; pos < last; pos++) | |
1195 { | |
1196 c = FETCH_CHAR (pos); | |
1197 if (LOWERCASEP (c)) | |
1198 { | |
1199 /* Cannot be all caps if any original char is lower case */ | |
1200 | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1201 some_lowercase = 1; |
603 | 1202 if (SYNTAX (prevc) != Sword) |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1203 ; |
603 | 1204 else |
1205 some_multiletter_word = 1; | |
1206 } | |
1207 else if (!NOCASEP (c)) | |
1208 { | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1209 if (SYNTAX (prevc) != Sword) |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1210 some_uppercase_initial = 1; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1211 else |
603 | 1212 some_multiletter_word = 1; |
1213 } | |
1214 | |
1215 prevc = c; | |
1216 } | |
1217 | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1218 /* Convert to all caps if the old text is all caps |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1219 and has at least one multiletter word. */ |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1220 if (! some_lowercase && some_multiletter_word) |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1221 case_action = all_caps; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1222 /* Capitalize each word, if the old text has a capitalized word. */ |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1223 else if (some_uppercase_initial) |
603 | 1224 case_action = cap_initial; |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1225 else |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1226 case_action = nochange; |
603 | 1227 } |
1228 | |
2655
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1229 /* We insert the replacement text before the old text, and then |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1230 delete the original text. This means that markers at the |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1231 beginning or end of the original will float to the corresponding |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1232 position in the replacement. */ |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1233 SET_PT (search_regs.start[0]); |
603 | 1234 if (!NILP (literal)) |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1235 Finsert_and_inherit (1, &newtext); |
603 | 1236 else |
1237 { | |
1238 struct gcpro gcpro1; | |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1239 GCPRO1 (newtext); |
603 | 1240 |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1241 for (pos = 0; pos < XSTRING (newtext)->size; pos++) |
603 | 1242 { |
2655
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1243 int offset = point - search_regs.start[0]; |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1244 |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1245 c = XSTRING (newtext)->data[pos]; |
603 | 1246 if (c == '\\') |
1247 { | |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1248 c = XSTRING (newtext)->data[++pos]; |
603 | 1249 if (c == '&') |
2655
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1250 Finsert_buffer_substring |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1251 (Fcurrent_buffer (), |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1252 make_number (search_regs.start[0] + offset), |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1253 make_number (search_regs.end[0] + offset)); |
621 | 1254 else if (c >= '1' && c <= search_regs.num_regs + '0') |
603 | 1255 { |
1256 if (search_regs.start[c - '0'] >= 1) | |
2655
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1257 Finsert_buffer_substring |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1258 (Fcurrent_buffer (), |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1259 make_number (search_regs.start[c - '0'] + offset), |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1260 make_number (search_regs.end[c - '0'] + offset)); |
603 | 1261 } |
1262 else | |
1263 insert_char (c); | |
1264 } | |
1265 else | |
1266 insert_char (c); | |
1267 } | |
1268 UNGCPRO; | |
1269 } | |
1270 | |
2655
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1271 inslen = point - (search_regs.start[0]); |
594a33ffed85
* search.c (Freplace_match): Arrange for markers sitting at the
Jim Blandy <jimb@redhat.com>
parents:
2475
diff
changeset
|
1272 del_range (search_regs.start[0] + inslen, search_regs.end[0] + inslen); |
603 | 1273 |
1274 if (case_action == all_caps) | |
1275 Fupcase_region (make_number (point - inslen), make_number (point)); | |
1276 else if (case_action == cap_initial) | |
1277 upcase_initials_region (make_number (point - inslen), make_number (point)); | |
1278 return Qnil; | |
1279 } | |
1280 | |
1281 static Lisp_Object | |
1282 match_limit (num, beginningp) | |
1283 Lisp_Object num; | |
1284 int beginningp; | |
1285 { | |
1286 register int n; | |
1287 | |
1288 CHECK_NUMBER (num, 0); | |
1289 n = XINT (num); | |
621 | 1290 if (n < 0 || n >= search_regs.num_regs) |
1291 args_out_of_range (num, make_number (search_regs.num_regs)); | |
1292 if (search_regs.num_regs <= 0 | |
1293 || search_regs.start[n] < 0) | |
603 | 1294 return Qnil; |
1295 return (make_number ((beginningp) ? search_regs.start[n] | |
1296 : search_regs.end[n])); | |
1297 } | |
1298 | |
1299 DEFUN ("match-beginning", Fmatch_beginning, Smatch_beginning, 1, 1, 0, | |
1300 "Return position of start of text matched by last search.\n\ | |
4882
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1301 NUM specifies which parenthesized expression in the last regexp.\n\ |
8c09f87f5087
(Freplace_match): Fix argument names to match doc string.
Brian Fox <bfox@gnu.org>
parents:
4832
diff
changeset
|
1302 Value is nil if NUMth pair didn't match, or there were less than NUM pairs.\n\ |
603 | 1303 Zero means the entire text matched by the whole regexp or whole string.") |
1304 (num) | |
1305 Lisp_Object num; | |
1306 { | |
1307 return match_limit (num, 1); | |
1308 } | |
1309 | |
1310 DEFUN ("match-end", Fmatch_end, Smatch_end, 1, 1, 0, | |
1311 "Return position of end of text matched by last search.\n\ | |
1312 ARG, a number, specifies which parenthesized expression in the last regexp.\n\ | |
1313 Value is nil if ARGth pair didn't match, or there were less than ARG pairs.\n\ | |
1314 Zero means the entire text matched by the whole regexp or whole string.") | |
1315 (num) | |
1316 Lisp_Object num; | |
1317 { | |
1318 return match_limit (num, 0); | |
1319 } | |
1320 | |
1321 DEFUN ("match-data", Fmatch_data, Smatch_data, 0, 0, 0, | |
1322 "Return a list containing all info on what the last search matched.\n\ | |
1323 Element 2N is `(match-beginning N)'; element 2N + 1 is `(match-end N)'.\n\ | |
1324 All the elements are markers or nil (nil if the Nth pair didn't match)\n\ | |
1325 if the last match was on a buffer; integers or nil if a string was matched.\n\ | |
1326 Use `store-match-data' to reinstate the data in this list.") | |
1327 () | |
1328 { | |
621 | 1329 Lisp_Object *data; |
603 | 1330 int i, len; |
1331 | |
727 | 1332 if (NILP (last_thing_searched)) |
1333 error ("match-data called before any match found"); | |
1334 | |
621 | 1335 data = (Lisp_Object *) alloca ((2 * search_regs.num_regs) |
1336 * sizeof (Lisp_Object)); | |
1337 | |
603 | 1338 len = -1; |
621 | 1339 for (i = 0; i < search_regs.num_regs; i++) |
603 | 1340 { |
1341 int start = search_regs.start[i]; | |
1342 if (start >= 0) | |
1343 { | |
727 | 1344 if (EQ (last_thing_searched, Qt)) |
603 | 1345 { |
1346 XFASTINT (data[2 * i]) = start; | |
1347 XFASTINT (data[2 * i + 1]) = search_regs.end[i]; | |
1348 } | |
727 | 1349 else if (XTYPE (last_thing_searched) == Lisp_Buffer) |
603 | 1350 { |
1351 data[2 * i] = Fmake_marker (); | |
727 | 1352 Fset_marker (data[2 * i], |
1353 make_number (start), | |
1354 last_thing_searched); | |
603 | 1355 data[2 * i + 1] = Fmake_marker (); |
1356 Fset_marker (data[2 * i + 1], | |
727 | 1357 make_number (search_regs.end[i]), |
1358 last_thing_searched); | |
603 | 1359 } |
727 | 1360 else |
1361 /* last_thing_searched must always be Qt, a buffer, or Qnil. */ | |
1362 abort (); | |
1363 | |
603 | 1364 len = i; |
1365 } | |
1366 else | |
1367 data[2 * i] = data [2 * i + 1] = Qnil; | |
1368 } | |
1369 return Flist (2 * len + 2, data); | |
1370 } | |
1371 | |
1372 | |
1373 DEFUN ("store-match-data", Fstore_match_data, Sstore_match_data, 1, 1, 0, | |
1374 "Set internal data on last search match from elements of LIST.\n\ | |
1375 LIST should have been created by calling `match-data' previously.") | |
1376 (list) | |
1377 register Lisp_Object list; | |
1378 { | |
1379 register int i; | |
1380 register Lisp_Object marker; | |
1381 | |
1382 if (!CONSP (list) && !NILP (list)) | |
1926
952f2a18f83d
* callint.c (Fcall_interactively): Pass the correct number of
Jim Blandy <jimb@redhat.com>
parents:
1896
diff
changeset
|
1383 list = wrong_type_argument (Qconsp, list); |
603 | 1384 |
727 | 1385 /* Unless we find a marker with a buffer in LIST, assume that this |
1386 match data came from a string. */ | |
1387 last_thing_searched = Qt; | |
1388 | |
621 | 1389 /* Allocate registers if they don't already exist. */ |
1390 { | |
1523
bd61aaa7828b
* search.c (Fstore_match_data): Don't assume Flength returns an
Jim Blandy <jimb@redhat.com>
parents:
1413
diff
changeset
|
1391 int length = XFASTINT (Flength (list)) / 2; |
621 | 1392 |
1393 if (length > search_regs.num_regs) | |
1394 { | |
708 | 1395 if (search_regs.num_regs == 0) |
1396 { | |
1397 search_regs.start | |
1398 = (regoff_t *) xmalloc (length * sizeof (regoff_t)); | |
1399 search_regs.end | |
1400 = (regoff_t *) xmalloc (length * sizeof (regoff_t)); | |
1401 } | |
621 | 1402 else |
708 | 1403 { |
1404 search_regs.start | |
1405 = (regoff_t *) xrealloc (search_regs.start, | |
1406 length * sizeof (regoff_t)); | |
1407 search_regs.end | |
1408 = (regoff_t *) xrealloc (search_regs.end, | |
1409 length * sizeof (regoff_t)); | |
1410 } | |
621 | 1411 |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
1412 BLOCK_INPUT; |
708 | 1413 re_set_registers (&searchbuf, &search_regs, length, |
1414 search_regs.start, search_regs.end); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
1415 UNBLOCK_INPUT; |
621 | 1416 } |
1417 } | |
1418 | |
1419 for (i = 0; i < search_regs.num_regs; i++) | |
603 | 1420 { |
1421 marker = Fcar (list); | |
1422 if (NILP (marker)) | |
1423 { | |
1424 search_regs.start[i] = -1; | |
1425 list = Fcdr (list); | |
1426 } | |
1427 else | |
1428 { | |
727 | 1429 if (XTYPE (marker) == Lisp_Marker) |
1430 { | |
1431 if (XMARKER (marker)->buffer == 0) | |
1432 XFASTINT (marker) = 0; | |
1433 else | |
1434 XSET (last_thing_searched, Lisp_Buffer, | |
1435 XMARKER (marker)->buffer); | |
1436 } | |
603 | 1437 |
1438 CHECK_NUMBER_COERCE_MARKER (marker, 0); | |
1439 search_regs.start[i] = XINT (marker); | |
1440 list = Fcdr (list); | |
1441 | |
1442 marker = Fcar (list); | |
1443 if (XTYPE (marker) == Lisp_Marker | |
1444 && XMARKER (marker)->buffer == 0) | |
1445 XFASTINT (marker) = 0; | |
1446 | |
1447 CHECK_NUMBER_COERCE_MARKER (marker, 0); | |
1448 search_regs.end[i] = XINT (marker); | |
1449 } | |
1450 list = Fcdr (list); | |
1451 } | |
1452 | |
1453 return Qnil; | |
1454 } | |
1455 | |
1456 /* Quote a string to inactivate reg-expr chars */ | |
1457 | |
1458 DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0, | |
1459 "Return a regexp string which matches exactly STRING and nothing else.") | |
1460 (str) | |
1461 Lisp_Object str; | |
1462 { | |
1463 register unsigned char *in, *out, *end; | |
1464 register unsigned char *temp; | |
1465 | |
1466 CHECK_STRING (str, 0); | |
1467 | |
1468 temp = (unsigned char *) alloca (XSTRING (str)->size * 2); | |
1469 | |
1470 /* Now copy the data into the new string, inserting escapes. */ | |
1471 | |
1472 in = XSTRING (str)->data; | |
1473 end = in + XSTRING (str)->size; | |
1474 out = temp; | |
1475 | |
1476 for (; in != end; in++) | |
1477 { | |
1478 if (*in == '[' || *in == ']' | |
1479 || *in == '*' || *in == '.' || *in == '\\' | |
1480 || *in == '?' || *in == '+' | |
1481 || *in == '^' || *in == '$') | |
1482 *out++ = '\\'; | |
1483 *out++ = *in; | |
1484 } | |
1485 | |
1486 return make_string (temp, out - temp); | |
1487 } | |
1488 | |
1489 syms_of_search () | |
1490 { | |
1491 register int i; | |
1492 | |
1493 searchbuf.allocated = 100; | |
605 | 1494 searchbuf.buffer = (unsigned char *) malloc (searchbuf.allocated); |
603 | 1495 searchbuf.fastmap = search_fastmap; |
1496 | |
1497 Qsearch_failed = intern ("search-failed"); | |
1498 staticpro (&Qsearch_failed); | |
1499 Qinvalid_regexp = intern ("invalid-regexp"); | |
1500 staticpro (&Qinvalid_regexp); | |
1501 | |
1502 Fput (Qsearch_failed, Qerror_conditions, | |
1503 Fcons (Qsearch_failed, Fcons (Qerror, Qnil))); | |
1504 Fput (Qsearch_failed, Qerror_message, | |
1505 build_string ("Search failed")); | |
1506 | |
1507 Fput (Qinvalid_regexp, Qerror_conditions, | |
1508 Fcons (Qinvalid_regexp, Fcons (Qerror, Qnil))); | |
1509 Fput (Qinvalid_regexp, Qerror_message, | |
1510 build_string ("Invalid regexp")); | |
1511 | |
1512 last_regexp = Qnil; | |
1513 staticpro (&last_regexp); | |
1514 | |
727 | 1515 last_thing_searched = Qnil; |
1516 staticpro (&last_thing_searched); | |
1517 | |
603 | 1518 defsubr (&Sstring_match); |
1519 defsubr (&Slooking_at); | |
1520 defsubr (&Sskip_chars_forward); | |
1521 defsubr (&Sskip_chars_backward); | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
1522 defsubr (&Sskip_syntax_forward); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
1523 defsubr (&Sskip_syntax_backward); |
603 | 1524 defsubr (&Ssearch_forward); |
1525 defsubr (&Ssearch_backward); | |
1526 defsubr (&Sword_search_forward); | |
1527 defsubr (&Sword_search_backward); | |
1528 defsubr (&Sre_search_forward); | |
1529 defsubr (&Sre_search_backward); | |
1530 defsubr (&Sreplace_match); | |
1531 defsubr (&Smatch_beginning); | |
1532 defsubr (&Smatch_end); | |
1533 defsubr (&Smatch_data); | |
1534 defsubr (&Sstore_match_data); | |
1535 defsubr (&Sregexp_quote); | |
1536 } |