Mercurial > emacs
annotate src/search.c @ 2439:b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
This isn't specific to X, and it allows us to avoid #including
xterm.h in files that don't really have anything to do with X.
* blockinput.h: New file.
* xterm.h (BLOCK_INPUT, UNBLOCK_INPUT, TOTALLY_UNBLOCK_INPUT,
UNBLOCK_INPUT_RESIGNAL): These are now in blockinput.h.
(x_input_blocked, x_pending_input): Deleted; there are analogs
in blockinput.h called interrupt_input_blocked and
interrupt_input_pending.
* keyboard.c (interrupt_input_blocked, interrupt_input_pending):
New variables, used by the macros in blockinput.h.
* xterm.c: #include blockinput.h.
(x_input_blocked, x_pending_input): Deleted.
(XTread_socket): Test and set interrupt_input_blocked and
interrupt_input_pending instead of the old variables.
* alloc.c, xfaces.c, xfns.c, xmenu.c, xselect.c, keymap.c:
#include blockinput.h.
* eval.c: #include blockinput.h instead of xterm.h.
* keyboard.c: #include blockinput.h.
(input_poll_signal): Just test
interrupt_input_blocked, instead of testing HAVE_X_WINDOWS and
x_input_blocked.
Block the processing of interrupt input while we're manipulating the
malloc heap.
* alloc.c: (xfree): New function, to make it easy to free things
safely.
(xmalloc, xrealloc): Block X input while doing the deed.
(VALIDATE_LISP_STORAGE, gc_sweep, compact_strings): Use xfree
instead of free.
(uninterrupt_malloc): New function, to install input-blocking
hooks into the GNU malloc routines.
* emacs.c [not SYSTEM_MALLOC] (main): Call uninterrupt_malloc
on startup.
* alloc.c: (make_interval, make_float, Fcons, Fmake_vector,
Fmake_symbol, Fmake_marker, make_uninit_string, Fgarbage_collect):
Use xmalloc instead of malloc; don't bother to check if out of
memory here.
(Fgarbage_collect): Call xrealloc instead of realloc.
* buffer.c: Use xmalloc and xfree instead of malloc and free;
don't bother to check if out of memory here.
(Fget_buffer_create): Put BLOCK_INPUT/UNBLOCK_INPUT pair around
calls to ralloc routines.
* insdel.c: Same.
* lisp.h (xfree): New extern declaration.
* xfaces.c (xfree): Don't #define this to be free; use the
definition in alloc.c.
* dispnew.c, doc.c, doprnt.c, fileio.c, lread.c, term.c, xfns.c,
xmenu.c, xterm.c: Use xfree instead of free.
* hftctl.c: Use xfree and xmalloc instead of free and malloc.
* keymap.c (current_minor_maps): BLOCK_INPUT while calling realloc
and malloc.
* search.c: Since the regexp routines can malloc, BLOCK_INPUT
while runing them. #include blockinput.h.
* sysdep.c: #include blockinput.h. Call xfree and xmalloc instead
of free and malloc. BLOCK_INPUT around routines which we know
will call malloc.
ymakefile (keyboard.o, keymap.o, search.o, sysdep.o, xfaces.o,
xfns.o, xmenu.o, xterm.o, xselect.o, alloc.o, eval.o): Note that
these depend on blockinput.h.
author | Jim Blandy <jimb@redhat.com> |
---|---|
date | Wed, 31 Mar 1993 10:55:33 +0000 |
parents | a35d2c5cbb3b |
children | 052bbdf1b817 |
rev | line source |
---|---|
603 | 1 /* String search routines for GNU Emacs. |
2 Copyright (C) 1985, 1986, 1987, 1992 Free Software Foundation, Inc. | |
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 | |
21 #include "config.h" | |
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 | |
72 static void | |
73 matcher_overflow () | |
74 { | |
75 error ("Stack overflow in regexp matcher"); | |
76 } | |
77 | |
78 #ifdef __STDC__ | |
79 #define CONST const | |
80 #else | |
81 #define CONST | |
82 #endif | |
83 | |
84 /* Compile a regexp and signal a Lisp error if anything goes wrong. */ | |
85 | |
708 | 86 compile_pattern (pattern, bufp, regp, translate) |
603 | 87 Lisp_Object pattern; |
88 struct re_pattern_buffer *bufp; | |
708 | 89 struct re_registers *regp; |
603 | 90 char *translate; |
91 { | |
92 CONST char *val; | |
93 Lisp_Object dummy; | |
94 | |
95 if (EQ (pattern, last_regexp) | |
96 && translate == bufp->translate) | |
97 return; | |
708 | 98 |
603 | 99 last_regexp = Qnil; |
100 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
|
101 BLOCK_INPUT; |
603 | 102 val = re_compile_pattern ((char *) XSTRING (pattern)->data, |
103 XSTRING (pattern)->size, | |
104 bufp); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
105 UNBLOCK_INPUT; |
603 | 106 if (val) |
107 { | |
108 dummy = build_string (val); | |
109 while (1) | |
110 Fsignal (Qinvalid_regexp, Fcons (dummy, Qnil)); | |
111 } | |
708 | 112 |
603 | 113 last_regexp = pattern; |
708 | 114 |
115 /* Advise the searching functions about the space we have allocated | |
116 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
|
117 BLOCK_INPUT; |
808 | 118 if (regp) |
119 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
|
120 UNBLOCK_INPUT; |
708 | 121 |
603 | 122 return; |
123 } | |
124 | |
125 /* Error condition used for failing searches */ | |
126 Lisp_Object Qsearch_failed; | |
127 | |
128 Lisp_Object | |
129 signal_failure (arg) | |
130 Lisp_Object arg; | |
131 { | |
132 Fsignal (Qsearch_failed, Fcons (arg, Qnil)); | |
133 return Qnil; | |
134 } | |
135 | |
136 DEFUN ("looking-at", Flooking_at, Slooking_at, 1, 1, 0, | |
638 | 137 "Return t if text after point matches regular expression PAT.\n\ |
138 This function modifies the match data that `match-beginning',\n\ | |
139 `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
|
140 data if you want to preserve them.") |
603 | 141 (string) |
142 Lisp_Object string; | |
143 { | |
144 Lisp_Object val; | |
145 unsigned char *p1, *p2; | |
146 int s1, s2; | |
147 register int i; | |
148 | |
149 CHECK_STRING (string, 0); | |
708 | 150 compile_pattern (string, &searchbuf, &search_regs, |
603 | 151 !NILP (current_buffer->case_fold_search) ? DOWNCASE_TABLE : 0); |
152 | |
153 immediate_quit = 1; | |
154 QUIT; /* Do a pending quit right away, to avoid paradoxical behavior */ | |
155 | |
156 /* Get pointers and sizes of the two strings | |
157 that make up the visible portion of the buffer. */ | |
158 | |
159 p1 = BEGV_ADDR; | |
160 s1 = GPT - BEGV; | |
161 p2 = GAP_END_ADDR; | |
162 s2 = ZV - GPT; | |
163 if (s1 < 0) | |
164 { | |
165 p2 = p1; | |
166 s2 = ZV - BEGV; | |
167 s1 = 0; | |
168 } | |
169 if (s2 < 0) | |
170 { | |
171 s1 = ZV - BEGV; | |
172 s2 = 0; | |
173 } | |
174 | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
175 BLOCK_INPUT; |
603 | 176 i = re_match_2 (&searchbuf, (char *) p1, s1, (char *) p2, s2, |
177 point - BEGV, &search_regs, | |
178 ZV - BEGV); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
179 UNBLOCK_INPUT; |
603 | 180 if (i == -2) |
181 matcher_overflow (); | |
182 | |
183 val = (0 <= i ? Qt : Qnil); | |
621 | 184 for (i = 0; i < search_regs.num_regs; i++) |
603 | 185 if (search_regs.start[i] >= 0) |
186 { | |
187 search_regs.start[i] += BEGV; | |
188 search_regs.end[i] += BEGV; | |
189 } | |
727 | 190 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 191 immediate_quit = 0; |
192 return val; | |
193 } | |
194 | |
195 DEFUN ("string-match", Fstring_match, Sstring_match, 2, 3, 0, | |
196 "Return index of start of first match for REGEXP in STRING, or nil.\n\ | |
197 If third arg START is non-nil, start search at that index in STRING.\n\ | |
198 For index of first char beyond the match, do (match-end 0).\n\ | |
199 `match-end' and `match-beginning' also give indices of substrings\n\ | |
200 matched by parenthesis constructs in the pattern.") | |
201 (regexp, string, start) | |
202 Lisp_Object regexp, string, start; | |
203 { | |
204 int val; | |
205 int s; | |
206 | |
207 CHECK_STRING (regexp, 0); | |
208 CHECK_STRING (string, 1); | |
209 | |
210 if (NILP (start)) | |
211 s = 0; | |
212 else | |
213 { | |
214 int len = XSTRING (string)->size; | |
215 | |
216 CHECK_NUMBER (start, 2); | |
217 s = XINT (start); | |
218 if (s < 0 && -s <= len) | |
219 s = len - s; | |
220 else if (0 > s || s > len) | |
221 args_out_of_range (string, start); | |
222 } | |
223 | |
708 | 224 compile_pattern (regexp, &searchbuf, &search_regs, |
603 | 225 !NILP (current_buffer->case_fold_search) ? DOWNCASE_TABLE : 0); |
226 immediate_quit = 1; | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
227 BLOCK_INPUT; |
603 | 228 val = re_search (&searchbuf, (char *) XSTRING (string)->data, |
229 XSTRING (string)->size, s, XSTRING (string)->size - s, | |
230 &search_regs); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
231 UNBLOCK_INPUT; |
603 | 232 immediate_quit = 0; |
727 | 233 last_thing_searched = Qt; |
603 | 234 if (val == -2) |
235 matcher_overflow (); | |
236 if (val < 0) return Qnil; | |
237 return make_number (val); | |
238 } | |
842 | 239 |
240 /* Match REGEXP against STRING, searching all of STRING, | |
241 and return the index of the match, or negative on failure. | |
242 This does not clobber the match data. */ | |
243 | |
244 int | |
245 fast_string_match (regexp, string) | |
246 Lisp_Object regexp, string; | |
247 { | |
248 int val; | |
249 | |
250 compile_pattern (regexp, &searchbuf, 0, 0); | |
251 immediate_quit = 1; | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
252 BLOCK_INPUT; |
842 | 253 val = re_search (&searchbuf, (char *) XSTRING (string)->data, |
254 XSTRING (string)->size, 0, XSTRING (string)->size, | |
255 0); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
256 UNBLOCK_INPUT; |
842 | 257 immediate_quit = 0; |
258 return val; | |
259 } | |
603 | 260 |
648 | 261 /* Search for COUNT instances of the character TARGET, starting at START. |
262 If COUNT is negative, search backwards. | |
263 | |
264 If we find COUNT instances, set *SHORTAGE to zero, and return the | |
1413 | 265 position after the COUNTth match. Note that for reverse motion |
266 this is not the same as the usual convention for Emacs motion commands. | |
648 | 267 |
268 If we don't find COUNT instances before reaching the end of the | |
269 buffer (or the beginning, if scanning backwards), set *SHORTAGE to | |
270 the number of TARGETs left unfound, and return the end of the | |
271 buffer we bumped up against. */ | |
272 | |
273 scan_buffer (target, start, count, shortage) | |
274 int *shortage, start; | |
275 register int count, target; | |
603 | 276 { |
648 | 277 int limit = ((count > 0) ? ZV - 1 : BEGV); |
278 int direction = ((count > 0) ? 1 : -1); | |
279 | |
280 register unsigned char *cursor; | |
603 | 281 unsigned char *base; |
648 | 282 |
283 register int ceiling; | |
284 register unsigned char *ceiling_addr; | |
603 | 285 |
286 if (shortage != 0) | |
287 *shortage = 0; | |
288 | |
289 immediate_quit = 1; | |
290 | |
648 | 291 if (count > 0) |
292 while (start != limit + 1) | |
603 | 293 { |
648 | 294 ceiling = BUFFER_CEILING_OF (start); |
295 ceiling = min (limit, ceiling); | |
296 ceiling_addr = &FETCH_CHAR (ceiling) + 1; | |
297 base = (cursor = &FETCH_CHAR (start)); | |
603 | 298 while (1) |
299 { | |
648 | 300 while (*cursor != target && ++cursor != ceiling_addr) |
603 | 301 ; |
648 | 302 if (cursor != ceiling_addr) |
603 | 303 { |
648 | 304 if (--count == 0) |
603 | 305 { |
306 immediate_quit = 0; | |
648 | 307 return (start + cursor - base + 1); |
603 | 308 } |
309 else | |
648 | 310 if (++cursor == ceiling_addr) |
603 | 311 break; |
312 } | |
313 else | |
314 break; | |
315 } | |
648 | 316 start += cursor - base; |
603 | 317 } |
318 else | |
319 { | |
648 | 320 start--; /* first character we scan */ |
321 while (start > limit - 1) | |
322 { /* we WILL scan under start */ | |
323 ceiling = BUFFER_FLOOR_OF (start); | |
324 ceiling = max (limit, ceiling); | |
325 ceiling_addr = &FETCH_CHAR (ceiling) - 1; | |
326 base = (cursor = &FETCH_CHAR (start)); | |
603 | 327 cursor++; |
328 while (1) | |
329 { | |
648 | 330 while (--cursor != ceiling_addr && *cursor != target) |
603 | 331 ; |
648 | 332 if (cursor != ceiling_addr) |
603 | 333 { |
648 | 334 if (++count == 0) |
603 | 335 { |
336 immediate_quit = 0; | |
648 | 337 return (start + cursor - base + 1); |
603 | 338 } |
339 } | |
340 else | |
341 break; | |
342 } | |
648 | 343 start += cursor - base; |
603 | 344 } |
345 } | |
346 immediate_quit = 0; | |
347 if (shortage != 0) | |
648 | 348 *shortage = count * direction; |
349 return (start + ((direction == 1 ? 0 : 1))); | |
603 | 350 } |
351 | |
352 int | |
353 find_next_newline (from, cnt) | |
354 register int from, cnt; | |
355 { | |
356 return (scan_buffer ('\n', from, cnt, (int *) 0)); | |
357 } | |
358 | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
359 Lisp_Object skip_chars (); |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
360 |
603 | 361 DEFUN ("skip-chars-forward", Fskip_chars_forward, Sskip_chars_forward, 1, 2, 0, |
362 "Move point forward, stopping before a char not in CHARS, or at position LIM.\n\ | |
363 CHARS is like the inside of a `[...]' in a regular expression\n\ | |
364 except that `]' is never special and `\\' quotes `^', `-' or `\\'.\n\ | |
365 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
|
366 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
|
367 Returns the distance traveled, either zero or positive.") |
603 | 368 (string, lim) |
369 Lisp_Object string, lim; | |
370 { | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
371 return skip_chars (1, 0, string, lim); |
603 | 372 } |
373 | |
374 DEFUN ("skip-chars-backward", Fskip_chars_backward, Sskip_chars_backward, 1, 2, 0, | |
375 "Move point backward, stopping after a char not in CHARS, or at position LIM.\n\ | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
376 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
|
377 Returns the distance traveled, either zero or negative.") |
603 | 378 (string, lim) |
379 Lisp_Object string, lim; | |
380 { | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
381 return skip_chars (0, 0, string, lim); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
382 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
383 |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
384 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
|
385 "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
|
386 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
|
387 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
|
388 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
|
389 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
|
390 (syntax, lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
391 Lisp_Object syntax, lim; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
392 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
393 return skip_chars (1, 1, syntax, lim); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
394 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
395 |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
396 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
|
397 "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
|
398 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
|
399 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
|
400 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
|
401 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
|
402 (syntax, lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
403 Lisp_Object syntax, lim; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
404 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
405 return skip_chars (0, 1, syntax, lim); |
603 | 406 } |
407 | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
408 Lisp_Object |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
409 skip_chars (forwardp, syntaxp, string, lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
410 int forwardp, syntaxp; |
603 | 411 Lisp_Object string, lim; |
412 { | |
413 register unsigned char *p, *pend; | |
414 register unsigned char c; | |
415 unsigned char fastmap[0400]; | |
416 int negate = 0; | |
417 register int i; | |
418 | |
419 CHECK_STRING (string, 0); | |
420 | |
421 if (NILP (lim)) | |
422 XSET (lim, Lisp_Int, forwardp ? ZV : BEGV); | |
423 else | |
424 CHECK_NUMBER_COERCE_MARKER (lim, 1); | |
425 | |
426 #if 0 /* This breaks some things... jla. */ | |
427 /* In any case, don't allow scan outside bounds of buffer. */ | |
428 if (XFASTINT (lim) > ZV) | |
429 XFASTINT (lim) = ZV; | |
430 if (XFASTINT (lim) < BEGV) | |
431 XFASTINT (lim) = BEGV; | |
432 #endif | |
433 | |
434 p = XSTRING (string)->data; | |
435 pend = p + XSTRING (string)->size; | |
436 bzero (fastmap, sizeof fastmap); | |
437 | |
438 if (p != pend && *p == '^') | |
439 { | |
440 negate = 1; p++; | |
441 } | |
442 | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
443 /* 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
|
444 If syntaxp, each character counts as itself. |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
445 Otherwise, handle backslashes and ranges specially */ |
603 | 446 |
447 while (p != pend) | |
448 { | |
449 c = *p++; | |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
450 if (syntaxp) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
451 fastmap[c] = 1; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
452 else |
603 | 453 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
454 if (c == '\\') |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
455 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
456 if (p == pend) break; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
457 c = *p++; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
458 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
459 if (p != pend && *p == '-') |
603 | 460 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
461 p++; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
462 if (p == pend) break; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
463 while (c <= *p) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
464 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
465 fastmap[c] = 1; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
466 c++; |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
467 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
468 p++; |
603 | 469 } |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
470 else |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
471 fastmap[c] = 1; |
603 | 472 } |
473 } | |
474 | |
475 /* If ^ was the first character, complement the fastmap. */ | |
476 | |
477 if (negate) | |
478 for (i = 0; i < sizeof fastmap; i++) | |
479 fastmap[i] ^= 1; | |
480 | |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
481 { |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
482 int start_point = point; |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
483 |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
484 immediate_quit = 1; |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
485 if (syntaxp) |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
486 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
487 |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
488 if (forwardp) |
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 while (point < XINT (lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
491 && 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
|
492 SET_PT (point + 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
493 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
494 else |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
495 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
496 while (point > XINT (lim) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
497 && 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
|
498 SET_PT (point - 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
499 } |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
500 } |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
501 else |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
502 { |
1896
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
503 if (forwardp) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
504 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
505 while (point < XINT (lim) && fastmap[FETCH_CHAR (point)]) |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
506 SET_PT (point + 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
507 } |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
508 else |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
509 { |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
510 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
|
511 SET_PT (point - 1); |
10895ac08bc6
(Fskip_syntax_backward): New function.
Richard M. Stallman <rms@gnu.org>
parents:
1878
diff
changeset
|
512 } |
1684
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
513 } |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
514 immediate_quit = 0; |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
515 |
f4d848dea8ff
* search.c (Fskip_chars_forward, Fskip_chars_backward): Return the
Jim Blandy <jimb@redhat.com>
parents:
1523
diff
changeset
|
516 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
|
517 } |
603 | 518 } |
519 | |
520 /* Subroutines of Lisp buffer search functions. */ | |
521 | |
522 static Lisp_Object | |
523 search_command (string, bound, noerror, count, direction, RE) | |
524 Lisp_Object string, bound, noerror, count; | |
525 int direction; | |
526 int RE; | |
527 { | |
528 register int np; | |
529 int lim; | |
530 int n = direction; | |
531 | |
532 if (!NILP (count)) | |
533 { | |
534 CHECK_NUMBER (count, 3); | |
535 n *= XINT (count); | |
536 } | |
537 | |
538 CHECK_STRING (string, 0); | |
539 if (NILP (bound)) | |
540 lim = n > 0 ? ZV : BEGV; | |
541 else | |
542 { | |
543 CHECK_NUMBER_COERCE_MARKER (bound, 1); | |
544 lim = XINT (bound); | |
545 if (n > 0 ? lim < point : lim > point) | |
546 error ("Invalid search bound (wrong side of point)"); | |
547 if (lim > ZV) | |
548 lim = ZV; | |
549 if (lim < BEGV) | |
550 lim = BEGV; | |
551 } | |
552 | |
553 np = search_buffer (string, point, lim, n, RE, | |
554 (!NILP (current_buffer->case_fold_search) | |
555 ? XSTRING (current_buffer->case_canon_table)->data : 0), | |
556 (!NILP (current_buffer->case_fold_search) | |
557 ? XSTRING (current_buffer->case_eqv_table)->data : 0)); | |
558 if (np <= 0) | |
559 { | |
560 if (NILP (noerror)) | |
561 return signal_failure (string); | |
562 if (!EQ (noerror, Qt)) | |
563 { | |
564 if (lim < BEGV || lim > ZV) | |
565 abort (); | |
1878
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
566 SET_PT (lim); |
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
567 return Qnil; |
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
568 #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
|
569 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
|
570 np = lim; |
1878
1c26d0049d4f
(search_command): #if 0 previous change.
Richard M. Stallman <rms@gnu.org>
parents:
1877
diff
changeset
|
571 #endif |
603 | 572 } |
1877
7786f61ec635
(search_command): When moving to LIM on failure, return LIM.
Richard M. Stallman <rms@gnu.org>
parents:
1684
diff
changeset
|
573 else |
7786f61ec635
(search_command): When moving to LIM on failure, return LIM.
Richard M. Stallman <rms@gnu.org>
parents:
1684
diff
changeset
|
574 return Qnil; |
603 | 575 } |
576 | |
577 if (np < BEGV || np > ZV) | |
578 abort (); | |
579 | |
580 SET_PT (np); | |
581 | |
582 return make_number (np); | |
583 } | |
584 | |
585 /* search for the n'th occurrence of STRING in the current buffer, | |
586 starting at position POS and stopping at position LIM, | |
587 treating PAT as a literal string if RE is false or as | |
588 a regular expression if RE is true. | |
589 | |
590 If N is positive, searching is forward and LIM must be greater than POS. | |
591 If N is negative, searching is backward and LIM must be less than POS. | |
592 | |
593 Returns -x if only N-x occurrences found (x > 0), | |
594 or else the position at the beginning of the Nth occurrence | |
595 (if searching backward) or the end (if searching forward). */ | |
596 | |
597 search_buffer (string, pos, lim, n, RE, trt, inverse_trt) | |
598 Lisp_Object string; | |
599 int pos; | |
600 int lim; | |
601 int n; | |
602 int RE; | |
603 register unsigned char *trt; | |
604 register unsigned char *inverse_trt; | |
605 { | |
606 int len = XSTRING (string)->size; | |
607 unsigned char *base_pat = XSTRING (string)->data; | |
608 register int *BM_tab; | |
609 int *BM_tab_base; | |
610 register int direction = ((n > 0) ? 1 : -1); | |
611 register int dirlen; | |
612 int infinity, limit, k, stride_for_teases; | |
613 register unsigned char *pat, *cursor, *p_limit; | |
614 register int i, j; | |
615 unsigned char *p1, *p2; | |
616 int s1, s2; | |
617 | |
618 /* Null string is found at starting position. */ | |
619 if (!len) | |
620 return pos; | |
621 | |
622 if (RE) | |
708 | 623 compile_pattern (string, &searchbuf, &search_regs, (char *) trt); |
603 | 624 |
625 if (RE /* Here we detect whether the */ | |
626 /* generality of an RE search is */ | |
627 /* really needed. */ | |
628 /* first item is "exact match" */ | |
621 | 629 && *(searchbuf.buffer) == (char) RE_EXACTN_VALUE |
603 | 630 && searchbuf.buffer[1] + 2 == searchbuf.used) /*first is ONLY item */ |
631 { | |
632 RE = 0; /* can do straight (non RE) search */ | |
633 pat = (base_pat = (unsigned char *) searchbuf.buffer + 2); | |
634 /* trt already applied */ | |
635 len = searchbuf.used - 2; | |
636 } | |
637 else if (!RE) | |
638 { | |
639 pat = (unsigned char *) alloca (len); | |
640 | |
641 for (i = len; i--;) /* Copy the pattern; apply trt */ | |
642 *pat++ = (((int) trt) ? trt [*base_pat++] : *base_pat++); | |
643 pat -= len; base_pat = pat; | |
644 } | |
645 | |
646 if (RE) | |
647 { | |
648 immediate_quit = 1; /* Quit immediately if user types ^G, | |
649 because letting this function finish | |
650 can take too long. */ | |
651 QUIT; /* Do a pending quit right away, | |
652 to avoid paradoxical behavior */ | |
653 /* Get pointers and sizes of the two strings | |
654 that make up the visible portion of the buffer. */ | |
655 | |
656 p1 = BEGV_ADDR; | |
657 s1 = GPT - BEGV; | |
658 p2 = GAP_END_ADDR; | |
659 s2 = ZV - GPT; | |
660 if (s1 < 0) | |
661 { | |
662 p2 = p1; | |
663 s2 = ZV - BEGV; | |
664 s1 = 0; | |
665 } | |
666 if (s2 < 0) | |
667 { | |
668 s1 = ZV - BEGV; | |
669 s2 = 0; | |
670 } | |
671 while (n < 0) | |
672 { | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
673 BLOCK_INPUT; |
603 | 674 int val = re_search_2 (&searchbuf, (char *) p1, s1, (char *) p2, s2, |
675 pos - BEGV, lim - pos, &search_regs, | |
676 /* Don't allow match past current point */ | |
677 pos - BEGV); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
678 UNBLOCK_INPUT; |
603 | 679 if (val == -2) |
680 matcher_overflow (); | |
681 if (val >= 0) | |
682 { | |
683 j = BEGV; | |
621 | 684 for (i = 0; i < search_regs.num_regs; i++) |
603 | 685 if (search_regs.start[i] >= 0) |
686 { | |
687 search_regs.start[i] += j; | |
688 search_regs.end[i] += j; | |
689 } | |
727 | 690 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 691 /* Set pos to the new position. */ |
692 pos = search_regs.start[0]; | |
693 } | |
694 else | |
695 { | |
696 immediate_quit = 0; | |
697 return (n); | |
698 } | |
699 n++; | |
700 } | |
701 while (n > 0) | |
702 { | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
703 BLOCK_INPUT; |
603 | 704 int val = re_search_2 (&searchbuf, (char *) p1, s1, (char *) p2, s2, |
705 pos - BEGV, lim - pos, &search_regs, | |
706 lim - BEGV); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
707 UNBLOCK_INPUT; |
603 | 708 if (val == -2) |
709 matcher_overflow (); | |
710 if (val >= 0) | |
711 { | |
712 j = BEGV; | |
621 | 713 for (i = 0; i < search_regs.num_regs; i++) |
603 | 714 if (search_regs.start[i] >= 0) |
715 { | |
716 search_regs.start[i] += j; | |
717 search_regs.end[i] += j; | |
718 } | |
727 | 719 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 720 pos = search_regs.end[0]; |
721 } | |
722 else | |
723 { | |
724 immediate_quit = 0; | |
725 return (0 - n); | |
726 } | |
727 n--; | |
728 } | |
729 immediate_quit = 0; | |
730 return (pos); | |
731 } | |
732 else /* non-RE case */ | |
733 { | |
734 #ifdef C_ALLOCA | |
735 int BM_tab_space[0400]; | |
736 BM_tab = &BM_tab_space[0]; | |
737 #else | |
738 BM_tab = (int *) alloca (0400 * sizeof (int)); | |
739 #endif | |
740 /* The general approach is that we are going to maintain that we know */ | |
741 /* the first (closest to the present position, in whatever direction */ | |
742 /* we're searching) character that could possibly be the last */ | |
743 /* (furthest from present position) character of a valid match. We */ | |
744 /* advance the state of our knowledge by looking at that character */ | |
745 /* and seeing whether it indeed matches the last character of the */ | |
746 /* pattern. If it does, we take a closer look. If it does not, we */ | |
747 /* move our pointer (to putative last characters) as far as is */ | |
748 /* logically possible. This amount of movement, which I call a */ | |
749 /* stride, will be the length of the pattern if the actual character */ | |
750 /* appears nowhere in the pattern, otherwise it will be the distance */ | |
751 /* from the last occurrence of that character to the end of the */ | |
752 /* pattern. */ | |
753 /* As a coding trick, an enormous stride is coded into the table for */ | |
754 /* characters that match the last character. This allows use of only */ | |
755 /* a single test, a test for having gone past the end of the */ | |
756 /* permissible match region, to test for both possible matches (when */ | |
757 /* the stride goes past the end immediately) and failure to */ | |
758 /* match (where you get nudged past the end one stride at a time). */ | |
759 | |
760 /* Here we make a "mickey mouse" BM table. The stride of the search */ | |
761 /* is determined only by the last character of the putative match. */ | |
762 /* If that character does not match, we will stride the proper */ | |
763 /* distance to propose a match that superimposes it on the last */ | |
764 /* instance of a character that matches it (per trt), or misses */ | |
765 /* it entirely if there is none. */ | |
766 | |
767 dirlen = len * direction; | |
768 infinity = dirlen - (lim + pos + len + len) * direction; | |
769 if (direction < 0) | |
770 pat = (base_pat += len - 1); | |
771 BM_tab_base = BM_tab; | |
772 BM_tab += 0400; | |
773 j = dirlen; /* to get it in a register */ | |
774 /* A character that does not appear in the pattern induces a */ | |
775 /* stride equal to the pattern length. */ | |
776 while (BM_tab_base != BM_tab) | |
777 { | |
778 *--BM_tab = j; | |
779 *--BM_tab = j; | |
780 *--BM_tab = j; | |
781 *--BM_tab = j; | |
782 } | |
783 i = 0; | |
784 while (i != infinity) | |
785 { | |
786 j = pat[i]; i += direction; | |
787 if (i == dirlen) i = infinity; | |
788 if ((int) trt) | |
789 { | |
790 k = (j = trt[j]); | |
791 if (i == infinity) | |
792 stride_for_teases = BM_tab[j]; | |
793 BM_tab[j] = dirlen - i; | |
794 /* A translation table is accompanied by its inverse -- see */ | |
795 /* comment following downcase_table for details */ | |
796 while ((j = inverse_trt[j]) != k) | |
797 BM_tab[j] = dirlen - i; | |
798 } | |
799 else | |
800 { | |
801 if (i == infinity) | |
802 stride_for_teases = BM_tab[j]; | |
803 BM_tab[j] = dirlen - i; | |
804 } | |
805 /* stride_for_teases tells how much to stride if we get a */ | |
806 /* match on the far character but are subsequently */ | |
807 /* disappointed, by recording what the stride would have been */ | |
808 /* for that character if the last character had been */ | |
809 /* different. */ | |
810 } | |
811 infinity = dirlen - infinity; | |
812 pos += dirlen - ((direction > 0) ? direction : 0); | |
813 /* loop invariant - pos points at where last char (first char if reverse) | |
814 of pattern would align in a possible match. */ | |
815 while (n != 0) | |
816 { | |
817 if ((lim - pos - (direction > 0)) * direction < 0) | |
818 return (n * (0 - direction)); | |
819 /* First we do the part we can by pointers (maybe nothing) */ | |
820 QUIT; | |
821 pat = base_pat; | |
822 limit = pos - dirlen + direction; | |
823 limit = ((direction > 0) | |
824 ? BUFFER_CEILING_OF (limit) | |
825 : BUFFER_FLOOR_OF (limit)); | |
826 /* LIMIT is now the last (not beyond-last!) value | |
827 POS can take on without hitting edge of buffer or the gap. */ | |
828 limit = ((direction > 0) | |
829 ? min (lim - 1, min (limit, pos + 20000)) | |
830 : max (lim, max (limit, pos - 20000))); | |
831 if ((limit - pos) * direction > 20) | |
832 { | |
833 p_limit = &FETCH_CHAR (limit); | |
834 p2 = (cursor = &FETCH_CHAR (pos)); | |
835 /* In this loop, pos + cursor - p2 is the surrogate for pos */ | |
836 while (1) /* use one cursor setting as long as i can */ | |
837 { | |
838 if (direction > 0) /* worth duplicating */ | |
839 { | |
840 /* Use signed comparison if appropriate | |
841 to make cursor+infinity sure to be > p_limit. | |
842 Assuming that the buffer lies in a range of addresses | |
843 that are all "positive" (as ints) or all "negative", | |
844 either kind of comparison will work as long | |
845 as we don't step by infinity. So pick the kind | |
846 that works when we do step by infinity. */ | |
847 if ((int) (p_limit + infinity) > (int) p_limit) | |
848 while ((int) cursor <= (int) p_limit) | |
849 cursor += BM_tab[*cursor]; | |
850 else | |
851 while ((unsigned int) cursor <= (unsigned int) p_limit) | |
852 cursor += BM_tab[*cursor]; | |
853 } | |
854 else | |
855 { | |
856 if ((int) (p_limit + infinity) < (int) p_limit) | |
857 while ((int) cursor >= (int) p_limit) | |
858 cursor += BM_tab[*cursor]; | |
859 else | |
860 while ((unsigned int) cursor >= (unsigned int) p_limit) | |
861 cursor += BM_tab[*cursor]; | |
862 } | |
863 /* If you are here, cursor is beyond the end of the searched region. */ | |
864 /* This can happen if you match on the far character of the pattern, */ | |
865 /* because the "stride" of that character is infinity, a number able */ | |
866 /* to throw you well beyond the end of the search. It can also */ | |
867 /* happen if you fail to match within the permitted region and would */ | |
868 /* otherwise try a character beyond that region */ | |
869 if ((cursor - p_limit) * direction <= len) | |
870 break; /* a small overrun is genuine */ | |
871 cursor -= infinity; /* large overrun = hit */ | |
872 i = dirlen - direction; | |
873 if ((int) trt) | |
874 { | |
875 while ((i -= direction) + direction != 0) | |
876 if (pat[i] != trt[*(cursor -= direction)]) | |
877 break; | |
878 } | |
879 else | |
880 { | |
881 while ((i -= direction) + direction != 0) | |
882 if (pat[i] != *(cursor -= direction)) | |
883 break; | |
884 } | |
885 cursor += dirlen - i - direction; /* fix cursor */ | |
886 if (i + direction == 0) | |
887 { | |
888 cursor -= direction; | |
708 | 889 |
890 /* Make sure we have registers in which to store | |
891 the match position. */ | |
892 if (search_regs.num_regs == 0) | |
893 { | |
894 regoff_t *starts, *ends; | |
895 | |
896 starts = | |
897 (regoff_t *) xmalloc (2 * sizeof (regoff_t)); | |
898 ends = | |
899 (regoff_t *) xmalloc (2 * sizeof (regoff_t)); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
900 BLOCK_INPUT; |
708 | 901 re_set_registers (&searchbuf, |
902 &search_regs, | |
903 2, starts, ends); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
904 UNBLOCK_INPUT; |
708 | 905 } |
906 | |
603 | 907 search_regs.start[0] |
908 = pos + cursor - p2 + ((direction > 0) | |
909 ? 1 - len : 0); | |
910 search_regs.end[0] = len + search_regs.start[0]; | |
727 | 911 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 912 if ((n -= direction) != 0) |
913 cursor += dirlen; /* to resume search */ | |
914 else | |
915 return ((direction > 0) | |
916 ? search_regs.end[0] : search_regs.start[0]); | |
917 } | |
918 else | |
919 cursor += stride_for_teases; /* <sigh> we lose - */ | |
920 } | |
921 pos += cursor - p2; | |
922 } | |
923 else | |
924 /* Now we'll pick up a clump that has to be done the hard */ | |
925 /* way because it covers a discontinuity */ | |
926 { | |
927 limit = ((direction > 0) | |
928 ? BUFFER_CEILING_OF (pos - dirlen + 1) | |
929 : BUFFER_FLOOR_OF (pos - dirlen - 1)); | |
930 limit = ((direction > 0) | |
931 ? min (limit + len, lim - 1) | |
932 : max (limit - len, lim)); | |
933 /* LIMIT is now the last value POS can have | |
934 and still be valid for a possible match. */ | |
935 while (1) | |
936 { | |
937 /* This loop can be coded for space rather than */ | |
938 /* speed because it will usually run only once. */ | |
939 /* (the reach is at most len + 21, and typically */ | |
940 /* does not exceed len) */ | |
941 while ((limit - pos) * direction >= 0) | |
942 pos += BM_tab[FETCH_CHAR(pos)]; | |
943 /* now run the same tests to distinguish going off the */ | |
944 /* end, a match or a phoney match. */ | |
945 if ((pos - limit) * direction <= len) | |
946 break; /* ran off the end */ | |
947 /* Found what might be a match. | |
948 Set POS back to last (first if reverse) char pos. */ | |
949 pos -= infinity; | |
950 i = dirlen - direction; | |
951 while ((i -= direction) + direction != 0) | |
952 { | |
953 pos -= direction; | |
954 if (pat[i] != (((int) trt) | |
955 ? trt[FETCH_CHAR(pos)] | |
956 : FETCH_CHAR (pos))) | |
957 break; | |
958 } | |
959 /* Above loop has moved POS part or all the way | |
960 back to the first char pos (last char pos if reverse). | |
961 Set it once again at the last (first if reverse) char. */ | |
962 pos += dirlen - i- direction; | |
963 if (i + direction == 0) | |
964 { | |
965 pos -= direction; | |
708 | 966 |
967 /* Make sure we have registers in which to store | |
968 the match position. */ | |
969 if (search_regs.num_regs == 0) | |
970 { | |
971 regoff_t *starts, *ends; | |
972 | |
973 starts = | |
974 (regoff_t *) xmalloc (2 * sizeof (regoff_t)); | |
975 ends = | |
976 (regoff_t *) xmalloc (2 * sizeof (regoff_t)); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
977 BLOCK_INPUT; |
708 | 978 re_set_registers (&searchbuf, |
979 &search_regs, | |
980 2, starts, ends); | |
2439
b6c62e4abf59
Put interrupt input blocking in a separate file from xterm.h.
Jim Blandy <jimb@redhat.com>
parents:
2393
diff
changeset
|
981 UNBLOCK_INPUT; |
708 | 982 } |
983 | |
603 | 984 search_regs.start[0] |
985 = pos + ((direction > 0) ? 1 - len : 0); | |
986 search_regs.end[0] = len + search_regs.start[0]; | |
727 | 987 XSET (last_thing_searched, Lisp_Buffer, current_buffer); |
603 | 988 if ((n -= direction) != 0) |
989 pos += dirlen; /* to resume search */ | |
990 else | |
991 return ((direction > 0) | |
992 ? search_regs.end[0] : search_regs.start[0]); | |
993 } | |
994 else | |
995 pos += stride_for_teases; | |
996 } | |
997 } | |
998 /* We have done one clump. Can we continue? */ | |
999 if ((lim - pos) * direction < 0) | |
1000 return ((0 - n) * direction); | |
1001 } | |
1002 return pos; | |
1003 } | |
1004 } | |
1005 | |
1006 /* Given a string of words separated by word delimiters, | |
1007 compute a regexp that matches those exact words | |
1008 separated by arbitrary punctuation. */ | |
1009 | |
1010 static Lisp_Object | |
1011 wordify (string) | |
1012 Lisp_Object string; | |
1013 { | |
1014 register unsigned char *p, *o; | |
1015 register int i, len, punct_count = 0, word_count = 0; | |
1016 Lisp_Object val; | |
1017 | |
1018 CHECK_STRING (string, 0); | |
1019 p = XSTRING (string)->data; | |
1020 len = XSTRING (string)->size; | |
1021 | |
1022 for (i = 0; i < len; i++) | |
1023 if (SYNTAX (p[i]) != Sword) | |
1024 { | |
1025 punct_count++; | |
1026 if (i > 0 && SYNTAX (p[i-1]) == Sword) word_count++; | |
1027 } | |
1028 if (SYNTAX (p[len-1]) == Sword) word_count++; | |
1029 if (!word_count) return build_string (""); | |
1030 | |
1031 val = make_string (p, len - punct_count + 5 * (word_count - 1) + 4); | |
1032 | |
1033 o = XSTRING (val)->data; | |
1034 *o++ = '\\'; | |
1035 *o++ = 'b'; | |
1036 | |
1037 for (i = 0; i < len; i++) | |
1038 if (SYNTAX (p[i]) == Sword) | |
1039 *o++ = p[i]; | |
1040 else if (i > 0 && SYNTAX (p[i-1]) == Sword && --word_count) | |
1041 { | |
1042 *o++ = '\\'; | |
1043 *o++ = 'W'; | |
1044 *o++ = '\\'; | |
1045 *o++ = 'W'; | |
1046 *o++ = '*'; | |
1047 } | |
1048 | |
1049 *o++ = '\\'; | |
1050 *o++ = 'b'; | |
1051 | |
1052 return val; | |
1053 } | |
1054 | |
1055 DEFUN ("search-backward", Fsearch_backward, Ssearch_backward, 1, 4, | |
1056 "sSearch backward: ", | |
1057 "Search backward from point for STRING.\n\ | |
1058 Set point to the beginning of the occurrence found, and return point.\n\ | |
1059 An optional second argument bounds the search; it is a buffer position.\n\ | |
1060 The match found must not extend before that position.\n\ | |
1061 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1062 If not nil and not t, position at limit of search and return nil.\n\ | |
1063 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1064 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1065 (string, bound, noerror, count) | |
1066 Lisp_Object string, bound, noerror, count; | |
1067 { | |
1068 return search_command (string, bound, noerror, count, -1, 0); | |
1069 } | |
1070 | |
1071 DEFUN ("search-forward", Fsearch_forward, Ssearch_forward, 1, 4, "sSearch: ", | |
1072 "Search forward from point for STRING.\n\ | |
1073 Set point to the end of the occurrence found, and return point.\n\ | |
1074 An optional second argument bounds the search; it is a buffer position.\n\ | |
1075 The match found must not extend after that position. nil is equivalent\n\ | |
1076 to (point-max).\n\ | |
1077 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1078 If not nil and not t, move to limit of search and return nil.\n\ | |
1079 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1080 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1081 (string, bound, noerror, count) | |
1082 Lisp_Object string, bound, noerror, count; | |
1083 { | |
1084 return search_command (string, bound, noerror, count, 1, 0); | |
1085 } | |
1086 | |
1087 DEFUN ("word-search-backward", Fword_search_backward, Sword_search_backward, 1, 4, | |
1088 "sWord search backward: ", | |
1089 "Search backward from point for STRING, ignoring differences in punctuation.\n\ | |
1090 Set point to the beginning of the occurrence found, and return point.\n\ | |
1091 An optional second argument bounds the search; it is a buffer position.\n\ | |
1092 The match found must not extend before that position.\n\ | |
1093 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1094 If not nil and not t, move to limit of search and return nil.\n\ | |
1095 Optional fourth argument is repeat count--search for successive occurrences.") | |
1096 (string, bound, noerror, count) | |
1097 Lisp_Object string, bound, noerror, count; | |
1098 { | |
1099 return search_command (wordify (string), bound, noerror, count, -1, 1); | |
1100 } | |
1101 | |
1102 DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4, | |
1103 "sWord search: ", | |
1104 "Search forward from point for STRING, ignoring differences in punctuation.\n\ | |
1105 Set point to the end of the occurrence found, and return point.\n\ | |
1106 An optional second argument bounds the search; it is a buffer position.\n\ | |
1107 The match found must not extend after that position.\n\ | |
1108 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1109 If not nil and not t, move to limit of search and return nil.\n\ | |
1110 Optional fourth argument is repeat count--search for successive occurrences.") | |
1111 (string, bound, noerror, count) | |
1112 Lisp_Object string, bound, noerror, count; | |
1113 { | |
1114 return search_command (wordify (string), bound, noerror, count, 1, 1); | |
1115 } | |
1116 | |
1117 DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4, | |
1118 "sRE search backward: ", | |
1119 "Search backward from point for match for regular expression REGEXP.\n\ | |
1120 Set point to the beginning of the match, and return point.\n\ | |
1121 The match found is the one starting last in the buffer\n\ | |
1122 and yet ending before the place the origin of the search.\n\ | |
1123 An optional second argument bounds the search; it is a buffer position.\n\ | |
1124 The match found must start at or after that position.\n\ | |
1125 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1126 If not nil and not t, move to limit of search and return nil.\n\ | |
1127 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1128 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1129 (string, bound, noerror, count) | |
1130 Lisp_Object string, bound, noerror, count; | |
1131 { | |
1132 return search_command (string, bound, noerror, count, -1, 1); | |
1133 } | |
1134 | |
1135 DEFUN ("re-search-forward", Fre_search_forward, Sre_search_forward, 1, 4, | |
1136 "sRE search: ", | |
1137 "Search forward from point for regular expression REGEXP.\n\ | |
1138 Set point to the end of the occurrence found, and return point.\n\ | |
1139 An optional second argument bounds the search; it is a buffer position.\n\ | |
1140 The match found must not extend after that position.\n\ | |
1141 Optional third argument, if t, means if fail just return nil (no error).\n\ | |
1142 If not nil and not t, move to limit of search and return nil.\n\ | |
1143 Optional fourth argument is repeat count--search for successive occurrences.\n\ | |
1144 See also the functions `match-beginning', `match-end' and `replace-match'.") | |
1145 (string, bound, noerror, count) | |
1146 Lisp_Object string, bound, noerror, count; | |
1147 { | |
1148 return search_command (string, bound, noerror, count, 1, 1); | |
1149 } | |
1150 | |
1151 DEFUN ("replace-match", Freplace_match, Sreplace_match, 1, 3, 0, | |
1152 "Replace text matched by last search with NEWTEXT.\n\ | |
1153 If second arg FIXEDCASE is non-nil, do not alter case of replacement text.\n\ | |
1154 Otherwise convert to all caps or cap initials, like replaced text.\n\ | |
1155 If third arg LITERAL is non-nil, insert NEWTEXT literally.\n\ | |
1156 Otherwise treat `\\' as special:\n\ | |
1157 `\\&' in NEWTEXT means substitute original matched text.\n\ | |
1158 `\\N' means substitute what matched the Nth `\\(...\\)'.\n\ | |
1159 If Nth parens didn't match, substitute nothing.\n\ | |
1160 `\\\\' means insert one `\\'.\n\ | |
708 | 1161 FIXEDCASE and LITERAL are optional arguments.\n\ |
603 | 1162 Leaves point at end of replacement text.") |
1163 (string, fixedcase, literal) | |
1164 Lisp_Object string, fixedcase, literal; | |
1165 { | |
1166 enum { nochange, all_caps, cap_initial } case_action; | |
1167 register int pos, last; | |
1168 int some_multiletter_word; | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1169 int some_lowercase; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1170 int some_uppercase_initial; |
603 | 1171 register int c, prevc; |
1172 int inslen; | |
1173 | |
1174 CHECK_STRING (string, 0); | |
1175 | |
1176 case_action = nochange; /* We tried an initialization */ | |
1177 /* but some C compilers blew it */ | |
621 | 1178 |
1179 if (search_regs.num_regs <= 0) | |
1180 error ("replace-match called before any match found"); | |
1181 | |
603 | 1182 if (search_regs.start[0] < BEGV |
1183 || search_regs.start[0] > search_regs.end[0] | |
1184 || 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
|
1185 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
|
1186 make_number (search_regs.end[0])); |
603 | 1187 |
1188 if (NILP (fixedcase)) | |
1189 { | |
1190 /* Decide how to casify by examining the matched text. */ | |
1191 | |
1192 last = search_regs.end[0]; | |
1193 prevc = '\n'; | |
1194 case_action = all_caps; | |
1195 | |
1196 /* some_multiletter_word is set nonzero if any original word | |
1197 is more than one letter long. */ | |
1198 some_multiletter_word = 0; | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1199 some_lowercase = 0; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1200 some_uppercase_initial = 0; |
603 | 1201 |
1202 for (pos = search_regs.start[0]; pos < last; pos++) | |
1203 { | |
1204 c = FETCH_CHAR (pos); | |
1205 if (LOWERCASEP (c)) | |
1206 { | |
1207 /* Cannot be all caps if any original char is lower case */ | |
1208 | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1209 some_lowercase = 1; |
603 | 1210 if (SYNTAX (prevc) != Sword) |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1211 ; |
603 | 1212 else |
1213 some_multiletter_word = 1; | |
1214 } | |
1215 else if (!NOCASEP (c)) | |
1216 { | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1217 if (SYNTAX (prevc) != Sword) |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1218 some_uppercase_initial = 1; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1219 else |
603 | 1220 some_multiletter_word = 1; |
1221 } | |
1222 | |
1223 prevc = c; | |
1224 } | |
1225 | |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1226 /* 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
|
1227 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
|
1228 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
|
1229 case_action = all_caps; |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1230 /* 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
|
1231 else if (some_uppercase_initial) |
603 | 1232 case_action = cap_initial; |
2393
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1233 else |
a35d2c5cbb3b
(Freplace_match): Clean up criterion about converting case.
Richard M. Stallman <rms@gnu.org>
parents:
1926
diff
changeset
|
1234 case_action = nochange; |
603 | 1235 } |
1236 | |
1237 SET_PT (search_regs.end[0]); | |
1238 if (!NILP (literal)) | |
1239 Finsert (1, &string); | |
1240 else | |
1241 { | |
1242 struct gcpro gcpro1; | |
1243 GCPRO1 (string); | |
1244 | |
1245 for (pos = 0; pos < XSTRING (string)->size; pos++) | |
1246 { | |
1247 c = XSTRING (string)->data[pos]; | |
1248 if (c == '\\') | |
1249 { | |
1250 c = XSTRING (string)->data[++pos]; | |
1251 if (c == '&') | |
1252 Finsert_buffer_substring (Fcurrent_buffer (), | |
1253 make_number (search_regs.start[0]), | |
1254 make_number (search_regs.end[0])); | |
621 | 1255 else if (c >= '1' && c <= search_regs.num_regs + '0') |
603 | 1256 { |
1257 if (search_regs.start[c - '0'] >= 1) | |
1258 Finsert_buffer_substring (Fcurrent_buffer (), | |
1259 make_number (search_regs.start[c - '0']), | |
1260 make_number (search_regs.end[c - '0'])); | |
1261 } | |
1262 else | |
1263 insert_char (c); | |
1264 } | |
1265 else | |
1266 insert_char (c); | |
1267 } | |
1268 UNGCPRO; | |
1269 } | |
1270 | |
1271 inslen = point - (search_regs.end[0]); | |
1272 del_range (search_regs.start[0], search_regs.end[0]); | |
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\ | |
1301 ARG, a number, specifies which parenthesized expression in the last regexp.\n\ | |
1302 Value is nil if ARGth pair didn't match, or there were less than ARG pairs.\n\ | |
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 } |