comparison src/search.c @ 1468:0f42689481f0

[xemacs-hg @ 2003-05-09 14:52:37 by stephent] revert clobber match data <873cjoi38t.fsf@tleepslib.sk.tsukuba.ac.jp>
author stephent
date Fri, 09 May 2003 14:52:43 +0000
parents 74cb069b8417
children 4af49f9a7a5c
comparison
equal deleted inserted replaced
1467:e5da225ea2ca 1468:0f42689481f0
91 that gets real ugly real fast since the buffer might have changed and 91 that gets real ugly real fast since the buffer might have changed and
92 the positions might be out of sync or out of range. 92 the positions might be out of sync or out of range.
93 */ 93 */
94 static struct re_registers search_regs; 94 static struct re_registers search_regs;
95 95
96 /* Every function that _may_ set the match data _must_ clear the search 96 /* Every function that sets the match data _must_ clear unused search
97 registers on entry. An unsuccessful search should leave the search 97 registers on success. An unsuccessful search or match _must_ preserve
98 registers cleared. Applications that are no-ops by definition (eg, 98 the search registers. The traditional documentation implied that
99 searches with a repetition count of 0) _must not_ clear the search 99 any match operation might trash the registers, but in fact failures
100 have always preserved the match data (in GNU Emacs as well). Some
101 plausible code depends on this behavior (cf. `w3-configuration-data'
102 in library "w3-cfg").
103
104 Ordinary string searchs use set_search_regs to set the whole-string
105 match. That function takes care of clearing the unused subexpression
100 registers. 106 registers.
101
102 XEmacs 21.5 up to beta 11 may have permitted the following idiom to
103 "win" in the sense that the match data was set to the last successful
104 match's match data, and not cleared as the current implemenation does:
105
106 (while (search_forward "string"))
107 (use-match-data-of-last-successful-search)
108
109 This no longer can work. You must use save-match-data to preserve the
110 match data:
111
112 (let (md)
113 (while (when (search-forward "string") (setq md (match-data))))
114 (set-match-data md))
115 (use-match-data-of-last-successful-search)
116 */ 107 */
117 static void set_search_regs (struct buffer *buf, Charbpos beg, Charcount len); 108 static void set_search_regs (struct buffer *buf, Charbpos beg, Charcount len);
118 static void clear_search_regs (struct re_registers *regp); 109 static void clear_search_regs (void);
119 110
120 /* The buffer in which the last search was performed, or 111 /* The buffer in which the last search was performed, or
121 Qt if the last search was done in a string; 112 Qt if the last search was done in a string;
122 Qnil if no searching has been done yet. */ 113 Qnil if no searching has been done yet. */
123 static Lisp_Object last_thing_searched; 114 static Lisp_Object last_thing_searched;
324 REGISTER int i; 315 REGISTER int i;
325 struct re_pattern_buffer *bufp; 316 struct re_pattern_buffer *bufp;
326 struct syntax_cache scache_struct; 317 struct syntax_cache scache_struct;
327 struct syntax_cache *scache = &scache_struct; 318 struct syntax_cache *scache = &scache_struct;
328 319
329 /* clear search registers *now*. no mercy, not even for errors */
330 clear_search_regs (&search_regs);
331
332 CHECK_STRING (string); 320 CHECK_STRING (string);
333 bufp = compile_pattern (string, &search_regs, 321 bufp = compile_pattern (string, &search_regs,
334 (!NILP (buf->case_fold_search) 322 (!NILP (buf->case_fold_search)
335 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil), 323 ? XCASE_TABLE_DOWNCASE (buf->case_table) : Qnil),
336 wrap_buffer (buf), buf, posix, ERROR_ME); 324 wrap_buffer (buf), buf, posix, ERROR_ME);
380 return val; 368 return val;
381 } 369 }
382 370
383 DEFUN ("looking-at", Flooking_at, 1, 2, 0, /* 371 DEFUN ("looking-at", Flooking_at, 1, 2, 0, /*
384 Return t if text after point matches regular expression REGEXP. 372 Return t if text after point matches regular expression REGEXP.
385 This function modifies the match data that `match-beginning', 373 When the match is successful, this function modifies the match data
386 `match-end' and `match-data' access; save and restore the match 374 that `match-beginning', `match-end' and `match-data' access; save the
387 data if you want to preserve them. 375 match data with `match-data' and restore it with `store-match-data' if
376 you want to preserve them. If the match fails, the match data from the
377 previous success match is preserved.
388 378
389 Optional argument BUFFER defaults to the current buffer. 379 Optional argument BUFFER defaults to the current buffer.
390 */ 380 */
391 (regexp, buffer)) 381 (regexp, buffer))
392 { 382 {
394 } 384 }
395 385
396 DEFUN ("posix-looking-at", Fposix_looking_at, 1, 2, 0, /* 386 DEFUN ("posix-looking-at", Fposix_looking_at, 1, 2, 0, /*
397 Return t if text after point matches regular expression REGEXP. 387 Return t if text after point matches regular expression REGEXP.
398 Find the longest match, in accord with Posix regular expression rules. 388 Find the longest match, in accord with Posix regular expression rules.
399 This function modifies the match data that `match-beginning', 389 When the match is successful, this function modifies the match data
400 `match-end' and `match-data' access; save and restore the match 390 that `match-beginning', `match-end' and `match-data' access; save the
401 data if you want to preserve them. 391 match data with `match-data' and restore it with `store-match-data' if
392 you want to preserve them. If the match fails, the match data from the
393 previous success match is preserved.
402 394
403 Optional argument BUFFER defaults to the current buffer. 395 Optional argument BUFFER defaults to the current buffer.
404 */ 396 */
405 (regexp, buffer)) 397 (regexp, buffer))
406 { 398 {
416 struct re_pattern_buffer *bufp; 408 struct re_pattern_buffer *bufp;
417 409
418 /* Some FSF junk with running_asynch_code, to preserve the match 410 /* Some FSF junk with running_asynch_code, to preserve the match
419 data. Not necessary because we don't call process filters 411 data. Not necessary because we don't call process filters
420 asynchronously (i.e. from within QUIT). */ 412 asynchronously (i.e. from within QUIT). */
421
422 /* clear search registers *now*. no mercy, not even for errors */
423 clear_search_regs (&search_regs);
424 413
425 CHECK_STRING (regexp); 414 CHECK_STRING (regexp);
426 CHECK_STRING (string); 415 CHECK_STRING (string);
427 416
428 if (NILP (start)) 417 if (NILP (start))
489 478
490 (let ((case-fold-search nil)) 479 (let ((case-fold-search nil))
491 (string-match "^foo.*bar" string)) 480 (string-match "^foo.*bar" string))
492 481
493 but the case, syntax, and category tables come from the standard tables, 482 but the case, syntax, and category tables come from the standard tables,
494 which are accessed through functions `default-{case,syntax,category}-table' and serve as the parents of the 483 which are accessed through functions `default-{case,syntax,category}-table'
495 tables in particular buffer 484 and serve as the parents of the tables in particular buffer.
496 485
486 When the match is successful, this function modifies the match data
487 that `match-beginning', `match-end' and `match-data' access; save the
488 match data with `match-data' and restore it with `store-match-data' if
489 you want to preserve them. If the match fails, the match data from the
490 previous success match is preserved.
497 */ 491 */
498 (regexp, string, start, buffer)) 492 (regexp, string, start, buffer))
499 { 493 {
500 /* &&#### implement new interp for buffer arg; check code to see if it 494 /* &&#### implement new interp for buffer arg; check code to see if it
501 makes more sense than prev */ 495 makes more sense than prev */
511 matched by parenthesis constructs in the pattern. 505 matched by parenthesis constructs in the pattern.
512 506
513 Optional arg BUFFER controls how case folding is done (according to 507 Optional arg BUFFER controls how case folding is done (according to
514 the value of `case-fold-search' in that buffer and that buffer's case 508 the value of `case-fold-search' in that buffer and that buffer's case
515 tables) and defaults to the current buffer. 509 tables) and defaults to the current buffer.
510
511 When the match is successful, this function modifies the match data
512 that `match-beginning', `match-end' and `match-data' access; save the
513 match data with `match-data' and restore it with `store-match-data' if
514 you want to preserve them. If the match fails, the match data from the
515 previous success match is preserved.
516 */ 516 */
517 (regexp, string, start, buffer)) 517 (regexp, string, start, buffer))
518 { 518 {
519 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 1); 519 return string_match_1 (regexp, string, start, decode_buffer (buffer, 0), 1);
520 } 520 }
1249 1249
1250 /* Searching 0 times means noop---don't move, don't touch registers. */ 1250 /* Searching 0 times means noop---don't move, don't touch registers. */
1251 if (n == 0) 1251 if (n == 0)
1252 return charbpos; 1252 return charbpos;
1253 1253
1254 /* clear the search regs now */
1255 clear_search_regs (&search_regs);
1256
1257 /* Null string is found at starting position. */ 1254 /* Null string is found at starting position. */
1258 if (len == 0) 1255 if (len == 0)
1259 { 1256 {
1260 set_search_regs (buf, charbpos, 0); 1257 set_search_regs (buf, charbpos, 0);
1261 return charbpos; 1258 return charbpos;
1644 in the pattern. Others don't matter anyway! */ 1641 in the pattern. Others don't matter anyway! */
1645 xzero (simple_translate); 1642 xzero (simple_translate);
1646 for (i = 0; i < 0400; i++) 1643 for (i = 0; i < 0400; i++)
1647 simple_translate[i] = (Ibyte) i; 1644 simple_translate[i] = (Ibyte) i;
1648 i = 0; 1645 i = 0;
1649
1650 /* clear search regs now */
1651 clear_search_regs (&search_regs);
1652 1646
1653 while (i != infinity) 1647 while (i != infinity)
1654 { 1648 {
1655 Ibyte *ptr = base_pat + i; 1649 Ibyte *ptr = base_pat + i;
1656 i += direction; 1650 i += direction;
2002 search_regs.start = xnew (regoff_t); 1996 search_regs.start = xnew (regoff_t);
2003 search_regs.end = xnew (regoff_t); 1997 search_regs.end = xnew (regoff_t);
2004 search_regs.num_regs = 1; 1998 search_regs.num_regs = 1;
2005 } 1999 }
2006 2000
2001 clear_search_regs ();
2007 search_regs.start[0] = beg; 2002 search_regs.start[0] = beg;
2008 search_regs.end[0] = beg + len; 2003 search_regs.end[0] = beg + len;
2009 last_thing_searched = wrap_buffer (buf); 2004 last_thing_searched = wrap_buffer (buf);
2010 } 2005 }
2011 2006
2012 /* Clear search registers so match data will be null. 2007 /* Clear search registers so match data will be null. */
2013 REGP is a pointer to the register structure to clear, usually the global
2014 search_regs. */
2015 2008
2016 static void 2009 static void
2017 clear_search_regs (struct re_registers *regp) 2010 clear_search_regs (void)
2018 { 2011 {
2019 /* This function has been Mule-ized. */ 2012 /* This function has been Mule-ized. */
2020 int i; 2013 int i;
2021 2014
2022 for (i = 0; i < regp->num_regs; i++) 2015 for (i = 0; i < search_regs.num_regs; i++)
2023 regp->start[i] = regp->end[i] = -1; 2016 search_regs.start[i] = search_regs.end[i] = -1;
2024 } 2017 }
2025 2018
2026 2019
2027 /* Given a string of words separated by word delimiters, 2020 /* Given a string of words separated by word delimiters,
2028 compute a regexp that matches those exact words 2021 compute a regexp that matches those exact words
2104 successive occurrences. 2097 successive occurrences.
2105 2098
2106 Optional fifth argument BUFFER specifies the buffer to search in and 2099 Optional fifth argument BUFFER specifies the buffer to search in and
2107 defaults to the current buffer. 2100 defaults to the current buffer.
2108 2101
2109 See also the functions `match-beginning', `match-end' and `replace-match'. 2102 When the match is successful, this function modifies the match data
2103 that `match-beginning', `match-end' and `match-data' access; save the
2104 match data with `match-data' and restore it with `store-match-data' if
2105 you want to preserve them. If the match fails, the match data from the
2106 previous success match is preserved.
2107
2108 See also the function `replace-match'.
2110 */ 2109 */
2111 (string, limit, noerror, count, buffer)) 2110 (string, limit, noerror, count, buffer))
2112 { 2111 {
2113 return search_command (string, limit, noerror, count, buffer, -1, 0, 0); 2112 return search_command (string, limit, noerror, count, buffer, -1, 0, 0);
2114 } 2113 }
2129 successive occurrences. 2128 successive occurrences.
2130 2129
2131 Optional fifth argument BUFFER specifies the buffer to search in and 2130 Optional fifth argument BUFFER specifies the buffer to search in and
2132 defaults to the current buffer. 2131 defaults to the current buffer.
2133 2132
2134 See also the functions `match-beginning', `match-end' and `replace-match'. 2133 When the match is successful, this function modifies the match data
2134 that `match-beginning', `match-end' and `match-data' access; save the
2135 match data with `match-data' and restore it with `store-match-data' if
2136 you want to preserve them. If the match fails, the match data from the
2137 previous success match is preserved.
2138
2139 See also the function `replace-match'.
2135 */ 2140 */
2136 (string, limit, noerror, count, buffer)) 2141 (string, limit, noerror, count, buffer))
2137 { 2142 {
2138 return search_command (string, limit, noerror, count, buffer, 1, 0, 0); 2143 return search_command (string, limit, noerror, count, buffer, 1, 0, 0);
2139 } 2144 }
2155 successive occurrences. 2160 successive occurrences.
2156 2161
2157 Optional fifth argument BUFFER specifies the buffer to search in and 2162 Optional fifth argument BUFFER specifies the buffer to search in and
2158 defaults to the current buffer. 2163 defaults to the current buffer.
2159 2164
2160 See also the functions `match-beginning', `match-end' and `replace-match'. 2165 When the match is successful, this function modifies the match data
2166 that `match-beginning', `match-end' and `match-data' access; save the
2167 match data with `match-data' and restore it with `store-match-data' if
2168 you want to preserve them. If the match fails, the match data from the
2169 previous success match is preserved.
2170
2171 See also the function `replace-match'.
2161 */ 2172 */
2162 (string, limit, noerror, count, buffer)) 2173 (string, limit, noerror, count, buffer))
2163 { 2174 {
2164 return search_command (wordify (buffer, string), limit, noerror, count, 2175 return search_command (wordify (buffer, string), limit, noerror, count,
2165 buffer, -1, 1, 0); 2176 buffer, -1, 1, 0);
2181 successive occurrences. 2192 successive occurrences.
2182 2193
2183 Optional fifth argument BUFFER specifies the buffer to search in and 2194 Optional fifth argument BUFFER specifies the buffer to search in and
2184 defaults to the current buffer. 2195 defaults to the current buffer.
2185 2196
2186 See also the functions `match-beginning', `match-end' and `replace-match'. 2197 When the match is successful, this function modifies the match data
2198 that `match-beginning', `match-end' and `match-data' access; save the
2199 match data with `match-data' and restore it with `store-match-data' if
2200 you want to preserve them. If the match fails, the match data from the
2201 previous success match is preserved.
2202
2203 See also the function `replace-match'.
2187 */ 2204 */
2188 (string, limit, noerror, count, buffer)) 2205 (string, limit, noerror, count, buffer))
2189 { 2206 {
2190 return search_command (wordify (buffer, string), limit, noerror, count, 2207 return search_command (wordify (buffer, string), limit, noerror, count,
2191 buffer, 1, 1, 0); 2208 buffer, 1, 1, 0);
2210 successive occurrences. 2227 successive occurrences.
2211 2228
2212 Optional fifth argument BUFFER specifies the buffer to search in and 2229 Optional fifth argument BUFFER specifies the buffer to search in and
2213 defaults to the current buffer. 2230 defaults to the current buffer.
2214 2231
2215 See also the functions `match-beginning', `match-end' and `replace-match'. 2232 When the match is successful, this function modifies the match data
2233 that `match-beginning', `match-end' and `match-data' access; save the
2234 match data with `match-data' and restore it with `store-match-data' if
2235 you want to preserve them. If the match fails, the match data from the
2236 previous success match is preserved.
2237
2238 See also the function `replace-match'.
2216 */ 2239 */
2217 (regexp, limit, noerror, count, buffer)) 2240 (regexp, limit, noerror, count, buffer))
2218 { 2241 {
2219 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 0); 2242 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 0);
2220 } 2243 }
2235 successive occurrences. 2258 successive occurrences.
2236 2259
2237 Optional fifth argument BUFFER specifies the buffer to search in and 2260 Optional fifth argument BUFFER specifies the buffer to search in and
2238 defaults to the current buffer. 2261 defaults to the current buffer.
2239 2262
2240 See also the functions `match-beginning', `match-end' and `replace-match'. 2263 When the match is successful, this function modifies the match data
2264 that `match-beginning', `match-end' and `match-data' access; save the
2265 match data with `match-data' and restore it with `store-match-data' if
2266 you want to preserve them. If the match fails, the match data from the
2267 previous success match is preserved.
2268
2269 See also the function `replace-match'.
2241 */ 2270 */
2242 (regexp, limit, noerror, count, buffer)) 2271 (regexp, limit, noerror, count, buffer))
2243 { 2272 {
2244 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 0); 2273 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 0);
2245 } 2274 }
2264 successive occurrences. 2293 successive occurrences.
2265 2294
2266 Optional fifth argument BUFFER specifies the buffer to search in and 2295 Optional fifth argument BUFFER specifies the buffer to search in and
2267 defaults to the current buffer. 2296 defaults to the current buffer.
2268 2297
2269 See also the functions `match-beginning', `match-end' and `replace-match'. 2298 When the match is successful, this function modifies the match data
2299 that `match-beginning', `match-end' and `match-data' access; save the
2300 match data with `match-data' and restore it with `store-match-data' if
2301 you want to preserve them. If the match fails, the match data from the
2302 previous success match is preserved.
2303
2304 See also the function `replace-match'.
2270 */ 2305 */
2271 (regexp, limit, noerror, count, buffer)) 2306 (regexp, limit, noerror, count, buffer))
2272 { 2307 {
2273 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 1); 2308 return search_command (regexp, limit, noerror, count, buffer, -1, 1, 1);
2274 } 2309 }
2290 successive occurrences. 2325 successive occurrences.
2291 2326
2292 Optional fifth argument BUFFER specifies the buffer to search in and 2327 Optional fifth argument BUFFER specifies the buffer to search in and
2293 defaults to the current buffer. 2328 defaults to the current buffer.
2294 2329
2295 See also the functions `match-beginning', `match-end' and `replace-match'. 2330 When the match is successful, this function modifies the match data
2331 that `match-beginning', `match-end' and `match-data' access; save the
2332 match data with `match-data' and restore it with `store-match-data' if
2333 you want to preserve them. If the match fails, the match data from the
2334 previous success match is preserved.
2335
2336 See also the function `replace-match'.
2296 */ 2337 */
2297 (regexp, limit, noerror, count, buffer)) 2338 (regexp, limit, noerror, count, buffer))
2298 { 2339 {
2299 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 1); 2340 return search_command (regexp, limit, noerror, count, buffer, 1, 1, 1);
2300 } 2341 }
2349 When fourth argument is nil, STRBUFFER specifies a subexpression of 2390 When fourth argument is nil, STRBUFFER specifies a subexpression of
2350 the match. It says to replace just that subexpression instead of the 2391 the match. It says to replace just that subexpression instead of the
2351 whole match. This is useful only after a regular expression search or 2392 whole match. This is useful only after a regular expression search or
2352 match since only regular expressions have distinguished subexpressions. 2393 match since only regular expressions have distinguished subexpressions.
2353 2394
2354 If no match (including searches) has been conducted, the last match 2395 If no match (including searches) has been conducted or the requested
2355 operation failed, or the requested subexpression was not matched, an 2396 subexpression was not matched, an `args-out-of-range' error will be
2356 `args-out-of-range' error will be signaled. (If no match has ever been 2397 signaled. (If no match has ever been conducted in this instance of
2357 conducted in this instance of XEmacs, an `invalid-operation' error will 2398 XEmacs, an `invalid-operation' error will be signaled. This is very
2358 be signaled. This is very rare.) 2399 rare.)
2359 */ 2400 */
2360 (replacement, fixedcase, literal, string, strbuffer)) 2401 (replacement, fixedcase, literal, string, strbuffer))
2361 { 2402 {
2362 /* This function can GC */ 2403 /* This function can GC */
2363 enum { nochange, all_caps, cap_initial } case_action; 2404 enum { nochange, all_caps, cap_initial } case_action;
2917 } 2958 }
2918 2959
2919 2960
2920 DEFUN ("store-match-data", Fstore_match_data, 1, 1, 0, /* 2961 DEFUN ("store-match-data", Fstore_match_data, 1, 1, 0, /*
2921 Set internal data on last search match from elements of LIST. 2962 Set internal data on last search match from elements of LIST.
2922 LIST should have been created by calling `match-data' previously. 2963 LIST should have been created by calling `match-data' previously,
2964 or be nil, to clear the internal match data.
2923 */ 2965 */
2924 (list)) 2966 (list))
2925 { 2967 {
2926 REGISTER int i; 2968 REGISTER int i;
2927 REGISTER Lisp_Object marker; 2969 REGISTER Lisp_Object marker;