aboutsummaryrefslogtreecommitdiffstats
path: root/src/search.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/search.c')
-rw-r--r--src/search.c98
1 files changed, 37 insertions, 61 deletions
diff --git a/src/search.c b/src/search.c
index f54f44c8818..33cb02aa7af 100644
--- a/src/search.c
+++ b/src/search.c
@@ -99,6 +99,25 @@ matcher_overflow (void)
99 error ("Stack overflow in regexp matcher"); 99 error ("Stack overflow in regexp matcher");
100} 100}
101 101
102static void
103freeze_buffer_relocation (void)
104{
105#ifdef REL_ALLOC
106 /* Prevent ralloc.c from relocating the current buffer while
107 searching it. */
108 r_alloc_inhibit_buffer_relocation (1);
109 record_unwind_protect_int (r_alloc_inhibit_buffer_relocation, 0);
110#endif
111}
112
113static void
114thaw_buffer_relocation (void)
115{
116#ifdef REL_ALLOC
117 unbind_to (SPECPDL_INDEX () - 1, Qnil);
118#endif
119}
120
102/* Compile a regexp and signal a Lisp error if anything goes wrong. 121/* Compile a regexp and signal a Lisp error if anything goes wrong.
103 PATTERN is the pattern to compile. 122 PATTERN is the pattern to compile.
104 CP is the place to put the result. 123 CP is the place to put the result.
@@ -277,7 +296,6 @@ looking_at_1 (Lisp_Object string, bool posix)
277 !NILP (BVAR (current_buffer, enable_multibyte_characters))); 296 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
278 297
279 /* Do a pending quit right away, to avoid paradoxical behavior */ 298 /* Do a pending quit right away, to avoid paradoxical behavior */
280 immediate_quit = true;
281 maybe_quit (); 299 maybe_quit ();
282 300
283 /* Get pointers and sizes of the two strings 301 /* Get pointers and sizes of the two strings
@@ -301,20 +319,13 @@ looking_at_1 (Lisp_Object string, bool posix)
301 319
302 re_match_object = Qnil; 320 re_match_object = Qnil;
303 321
304#ifdef REL_ALLOC 322 freeze_buffer_relocation ();
305 /* Prevent ralloc.c from relocating the current buffer while
306 searching it. */
307 r_alloc_inhibit_buffer_relocation (1);
308#endif
309 i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2, 323 i = re_match_2 (bufp, (char *) p1, s1, (char *) p2, s2,
310 PT_BYTE - BEGV_BYTE, 324 PT_BYTE - BEGV_BYTE,
311 (NILP (Vinhibit_changing_match_data) 325 (NILP (Vinhibit_changing_match_data)
312 ? &search_regs : NULL), 326 ? &search_regs : NULL),
313 ZV_BYTE - BEGV_BYTE); 327 ZV_BYTE - BEGV_BYTE);
314 immediate_quit = false; 328 thaw_buffer_relocation ();
315#ifdef REL_ALLOC
316 r_alloc_inhibit_buffer_relocation (0);
317#endif
318 329
319 if (i == -2) 330 if (i == -2)
320 matcher_overflow (); 331 matcher_overflow ();
@@ -399,7 +410,6 @@ string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start,
399 ? BVAR (current_buffer, case_canon_table) : Qnil), 410 ? BVAR (current_buffer, case_canon_table) : Qnil),
400 posix, 411 posix,
401 STRING_MULTIBYTE (string)); 412 STRING_MULTIBYTE (string));
402 immediate_quit = true;
403 re_match_object = string; 413 re_match_object = string;
404 414
405 val = re_search (bufp, SSDATA (string), 415 val = re_search (bufp, SSDATA (string),
@@ -407,7 +417,6 @@ string_match_1 (Lisp_Object regexp, Lisp_Object string, Lisp_Object start,
407 SBYTES (string) - pos_byte, 417 SBYTES (string) - pos_byte,
408 (NILP (Vinhibit_changing_match_data) 418 (NILP (Vinhibit_changing_match_data)
409 ? &search_regs : NULL)); 419 ? &search_regs : NULL));
410 immediate_quit = false;
411 420
412 /* Set last_thing_searched only when match data is changed. */ 421 /* Set last_thing_searched only when match data is changed. */
413 if (NILP (Vinhibit_changing_match_data)) 422 if (NILP (Vinhibit_changing_match_data))
@@ -471,13 +480,11 @@ fast_string_match_internal (Lisp_Object regexp, Lisp_Object string,
471 480
472 bufp = compile_pattern (regexp, 0, table, 481 bufp = compile_pattern (regexp, 0, table,
473 0, STRING_MULTIBYTE (string)); 482 0, STRING_MULTIBYTE (string));
474 immediate_quit = true;
475 re_match_object = string; 483 re_match_object = string;
476 484
477 val = re_search (bufp, SSDATA (string), 485 val = re_search (bufp, SSDATA (string),
478 SBYTES (string), 0, 486 SBYTES (string), 0,
479 SBYTES (string), 0); 487 SBYTES (string), 0);
480 immediate_quit = false;
481 return val; 488 return val;
482} 489}
483 490
@@ -498,9 +505,7 @@ fast_c_string_match_ignore_case (Lisp_Object regexp,
498 bufp = compile_pattern (regexp, 0, 505 bufp = compile_pattern (regexp, 0,
499 Vascii_canon_table, 0, 506 Vascii_canon_table, 0,
500 0); 507 0);
501 immediate_quit = true;
502 val = re_search (bufp, string, len, 0, len, 0); 508 val = re_search (bufp, string, len, 0, len, 0);
503 immediate_quit = false;
504 return val; 509 return val;
505} 510}
506 511
@@ -561,18 +566,10 @@ fast_looking_at (Lisp_Object regexp, ptrdiff_t pos, ptrdiff_t pos_byte,
561 } 566 }
562 567
563 buf = compile_pattern (regexp, 0, Qnil, 0, multibyte); 568 buf = compile_pattern (regexp, 0, Qnil, 0, multibyte);
564 immediate_quit = true; 569 freeze_buffer_relocation ();
565#ifdef REL_ALLOC
566 /* Prevent ralloc.c from relocating the current buffer while
567 searching it. */
568 r_alloc_inhibit_buffer_relocation (1);
569#endif
570 len = re_match_2 (buf, (char *) p1, s1, (char *) p2, s2, 570 len = re_match_2 (buf, (char *) p1, s1, (char *) p2, s2,
571 pos_byte, NULL, limit_byte); 571 pos_byte, NULL, limit_byte);
572#ifdef REL_ALLOC 572 thaw_buffer_relocation ();
573 r_alloc_inhibit_buffer_relocation (0);
574#endif
575 immediate_quit = false;
576 573
577 return len; 574 return len;
578} 575}
@@ -649,7 +646,7 @@ newline_cache_on_off (struct buffer *buf)
649 If BYTEPOS is not NULL, set *BYTEPOS to the byte position corresponding 646 If BYTEPOS is not NULL, set *BYTEPOS to the byte position corresponding
650 to the returned character position. 647 to the returned character position.
651 648
652 If ALLOW_QUIT, set immediate_quit. That's good to do 649 If ALLOW_QUIT, check for quitting. That's good to do
653 except when inside redisplay. */ 650 except when inside redisplay. */
654 651
655ptrdiff_t 652ptrdiff_t
@@ -685,8 +682,6 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
685 if (shortage != 0) 682 if (shortage != 0)
686 *shortage = 0; 683 *shortage = 0;
687 684
688 immediate_quit = allow_quit;
689
690 if (count > 0) 685 if (count > 0)
691 while (start != end) 686 while (start != end)
692 { 687 {
@@ -704,7 +699,6 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
704 ptrdiff_t next_change; 699 ptrdiff_t next_change;
705 int result = 1; 700 int result = 1;
706 701
707 immediate_quit = false;
708 while (start < end && result) 702 while (start < end && result)
709 { 703 {
710 ptrdiff_t lim1; 704 ptrdiff_t lim1;
@@ -757,7 +751,6 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
757 start_byte = end_byte; 751 start_byte = end_byte;
758 break; 752 break;
759 } 753 }
760 immediate_quit = allow_quit;
761 754
762 /* START should never be after END. */ 755 /* START should never be after END. */
763 if (start_byte > ceiling_byte) 756 if (start_byte > ceiling_byte)
@@ -810,11 +803,12 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
810 803
811 if (--count == 0) 804 if (--count == 0)
812 { 805 {
813 immediate_quit = false;
814 if (bytepos) 806 if (bytepos)
815 *bytepos = lim_byte + next; 807 *bytepos = lim_byte + next;
816 return BYTE_TO_CHAR (lim_byte + next); 808 return BYTE_TO_CHAR (lim_byte + next);
817 } 809 }
810 if (allow_quit)
811 maybe_quit ();
818 } 812 }
819 813
820 start_byte = lim_byte; 814 start_byte = lim_byte;
@@ -833,7 +827,6 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
833 ptrdiff_t next_change; 827 ptrdiff_t next_change;
834 int result = 1; 828 int result = 1;
835 829
836 immediate_quit = false;
837 while (start > end && result) 830 while (start > end && result)
838 { 831 {
839 ptrdiff_t lim1; 832 ptrdiff_t lim1;
@@ -870,7 +863,6 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
870 start_byte = end_byte; 863 start_byte = end_byte;
871 break; 864 break;
872 } 865 }
873 immediate_quit = allow_quit;
874 866
875 /* Start should never be at or before end. */ 867 /* Start should never be at or before end. */
876 if (start_byte <= ceiling_byte) 868 if (start_byte <= ceiling_byte)
@@ -918,11 +910,12 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
918 910
919 if (++count >= 0) 911 if (++count >= 0)
920 { 912 {
921 immediate_quit = false;
922 if (bytepos) 913 if (bytepos)
923 *bytepos = ceiling_byte + prev + 1; 914 *bytepos = ceiling_byte + prev + 1;
924 return BYTE_TO_CHAR (ceiling_byte + prev + 1); 915 return BYTE_TO_CHAR (ceiling_byte + prev + 1);
925 } 916 }
917 if (allow_quit)
918 maybe_quit ();
926 } 919 }
927 920
928 start_byte = ceiling_byte; 921 start_byte = ceiling_byte;
@@ -930,7 +923,6 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
930 } 923 }
931 } 924 }
932 925
933 immediate_quit = false;
934 if (shortage) 926 if (shortage)
935 *shortage = count * direction; 927 *shortage = count * direction;
936 if (bytepos) 928 if (bytepos)
@@ -954,7 +946,7 @@ find_newline (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
954 the number of line boundaries left unfound, and position at 946 the number of line boundaries left unfound, and position at
955 the limit we bumped up against. 947 the limit we bumped up against.
956 948
957 If ALLOW_QUIT, set immediate_quit. That's good to do 949 If ALLOW_QUIT, check for quitting. That's good to do
958 except in special cases. */ 950 except in special cases. */
959 951
960ptrdiff_t 952ptrdiff_t
@@ -1197,9 +1189,6 @@ search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
1197 trt, posix, 1189 trt, posix,
1198 !NILP (BVAR (current_buffer, enable_multibyte_characters))); 1190 !NILP (BVAR (current_buffer, enable_multibyte_characters)));
1199 1191
1200 immediate_quit = true; /* Quit immediately if user types ^G,
1201 because letting this function finish
1202 can take too long. */
1203 maybe_quit (); /* Do a pending quit right away, 1192 maybe_quit (); /* Do a pending quit right away,
1204 to avoid paradoxical behavior */ 1193 to avoid paradoxical behavior */
1205 /* Get pointers and sizes of the two strings 1194 /* Get pointers and sizes of the two strings
@@ -1222,11 +1211,7 @@ search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
1222 } 1211 }
1223 re_match_object = Qnil; 1212 re_match_object = Qnil;
1224 1213
1225#ifdef REL_ALLOC 1214 freeze_buffer_relocation ();
1226 /* Prevent ralloc.c from relocating the current buffer while
1227 searching it. */
1228 r_alloc_inhibit_buffer_relocation (1);
1229#endif
1230 1215
1231 while (n < 0) 1216 while (n < 0)
1232 { 1217 {
@@ -1268,13 +1253,11 @@ search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
1268 } 1253 }
1269 else 1254 else
1270 { 1255 {
1271 immediate_quit = false; 1256 thaw_buffer_relocation ();
1272#ifdef REL_ALLOC
1273 r_alloc_inhibit_buffer_relocation (0);
1274#endif
1275 return (n); 1257 return (n);
1276 } 1258 }
1277 n++; 1259 n++;
1260 maybe_quit ();
1278 } 1261 }
1279 while (n > 0) 1262 while (n > 0)
1280 { 1263 {
@@ -1313,18 +1296,13 @@ search_buffer (Lisp_Object string, ptrdiff_t pos, ptrdiff_t pos_byte,
1313 } 1296 }
1314 else 1297 else
1315 { 1298 {
1316 immediate_quit = false; 1299 thaw_buffer_relocation ();
1317#ifdef REL_ALLOC
1318 r_alloc_inhibit_buffer_relocation (0);
1319#endif
1320 return (0 - n); 1300 return (0 - n);
1321 } 1301 }
1322 n--; 1302 n--;
1303 maybe_quit ();
1323 } 1304 }
1324 immediate_quit = false; 1305 thaw_buffer_relocation ();
1325#ifdef REL_ALLOC
1326 r_alloc_inhibit_buffer_relocation (0);
1327#endif
1328 return (pos); 1306 return (pos);
1329 } 1307 }
1330 else /* non-RE case */ 1308 else /* non-RE case */
@@ -3231,8 +3209,6 @@ find_newline1 (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
3231 if (shortage != 0) 3209 if (shortage != 0)
3232 *shortage = 0; 3210 *shortage = 0;
3233 3211
3234 immediate_quit = allow_quit;
3235
3236 if (count > 0) 3212 if (count > 0)
3237 while (start != end) 3213 while (start != end)
3238 { 3214 {
@@ -3275,11 +3251,12 @@ find_newline1 (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
3275 3251
3276 if (--count == 0) 3252 if (--count == 0)
3277 { 3253 {
3278 immediate_quit = false;
3279 if (bytepos) 3254 if (bytepos)
3280 *bytepos = lim_byte + next; 3255 *bytepos = lim_byte + next;
3281 return BYTE_TO_CHAR (lim_byte + next); 3256 return BYTE_TO_CHAR (lim_byte + next);
3282 } 3257 }
3258 if (allow_quit)
3259 maybe_quit ();
3283 } 3260 }
3284 3261
3285 start_byte = lim_byte; 3262 start_byte = lim_byte;
@@ -3287,7 +3264,6 @@ find_newline1 (ptrdiff_t start, ptrdiff_t start_byte, ptrdiff_t end,
3287 } 3264 }
3288 } 3265 }
3289 3266
3290 immediate_quit = false;
3291 if (shortage) 3267 if (shortage)
3292 *shortage = count; 3268 *shortage = count;
3293 if (bytepos) 3269 if (bytepos)