diff options
Diffstat (limited to 'src/search.c')
| -rw-r--r-- | src/search.c | 176 |
1 files changed, 103 insertions, 73 deletions
diff --git a/src/search.c b/src/search.c index d29a51c695b..55a6d893479 100644 --- a/src/search.c +++ b/src/search.c | |||
| @@ -1,5 +1,5 @@ | |||
| 1 | /* String search routines for GNU Emacs. | 1 | /* String search routines for GNU Emacs. |
| 2 | Copyright (C) 1985-1987, 1993-1994, 1997-1999, 2001-2011 | 2 | Copyright (C) 1985-1987, 1993-1994, 1997-1999, 2001-2012 |
| 3 | Free Software Foundation, Inc. | 3 | Free Software Foundation, Inc. |
| 4 | 4 | ||
| 5 | This file is part of GNU Emacs. | 5 | This file is part of GNU Emacs. |
| @@ -83,11 +83,10 @@ static struct re_registers search_regs; | |||
| 83 | Qnil if no searching has been done yet. */ | 83 | Qnil if no searching has been done yet. */ |
| 84 | static Lisp_Object last_thing_searched; | 84 | static Lisp_Object last_thing_searched; |
| 85 | 85 | ||
| 86 | /* error condition signaled when regexp compile_pattern fails */ | 86 | /* Error condition signaled when regexp compile_pattern fails. */ |
| 87 | |||
| 88 | static Lisp_Object Qinvalid_regexp; | 87 | static Lisp_Object Qinvalid_regexp; |
| 89 | 88 | ||
| 90 | /* Error condition used for failing searches */ | 89 | /* Error condition used for failing searches. */ |
| 91 | static Lisp_Object Qsearch_failed; | 90 | static Lisp_Object Qsearch_failed; |
| 92 | 91 | ||
| 93 | static void set_search_regs (EMACS_INT, EMACS_INT); | 92 | static void set_search_regs (EMACS_INT, EMACS_INT); |
| @@ -683,7 +682,7 @@ scan_buffer (register int target, EMACS_INT start, EMACS_INT end, | |||
| 683 | to see where we can avoid some scanning. */ | 682 | to see where we can avoid some scanning. */ |
| 684 | if (target == '\n' && newline_cache) | 683 | if (target == '\n' && newline_cache) |
| 685 | { | 684 | { |
| 686 | EMACS_INT next_change; | 685 | ptrdiff_t next_change; |
| 687 | immediate_quit = 0; | 686 | immediate_quit = 0; |
| 688 | while (region_cache_forward | 687 | while (region_cache_forward |
| 689 | (current_buffer, newline_cache, start_byte, &next_change)) | 688 | (current_buffer, newline_cache, start_byte, &next_change)) |
| @@ -726,8 +725,8 @@ scan_buffer (register int target, EMACS_INT start, EMACS_INT end, | |||
| 726 | the region from start to cursor is free of them. */ | 725 | the region from start to cursor is free of them. */ |
| 727 | if (target == '\n' && newline_cache) | 726 | if (target == '\n' && newline_cache) |
| 728 | know_region_cache (current_buffer, newline_cache, | 727 | know_region_cache (current_buffer, newline_cache, |
| 729 | start_byte + scan_start - base, | 728 | BYTE_TO_CHAR (start_byte + scan_start - base), |
| 730 | start_byte + cursor - base); | 729 | BYTE_TO_CHAR (start_byte + cursor - base)); |
| 731 | 730 | ||
| 732 | /* Did we find the target character? */ | 731 | /* Did we find the target character? */ |
| 733 | if (cursor < ceiling_addr) | 732 | if (cursor < ceiling_addr) |
| @@ -755,7 +754,7 @@ scan_buffer (register int target, EMACS_INT start, EMACS_INT end, | |||
| 755 | /* Consult the newline cache, if appropriate. */ | 754 | /* Consult the newline cache, if appropriate. */ |
| 756 | if (target == '\n' && newline_cache) | 755 | if (target == '\n' && newline_cache) |
| 757 | { | 756 | { |
| 758 | EMACS_INT next_change; | 757 | ptrdiff_t next_change; |
| 759 | immediate_quit = 0; | 758 | immediate_quit = 0; |
| 760 | while (region_cache_backward | 759 | while (region_cache_backward |
| 761 | (current_buffer, newline_cache, start_byte, &next_change)) | 760 | (current_buffer, newline_cache, start_byte, &next_change)) |
| @@ -792,8 +791,8 @@ scan_buffer (register int target, EMACS_INT start, EMACS_INT end, | |||
| 792 | the region from after the cursor to start is free of them. */ | 791 | the region from after the cursor to start is free of them. */ |
| 793 | if (target == '\n' && newline_cache) | 792 | if (target == '\n' && newline_cache) |
| 794 | know_region_cache (current_buffer, newline_cache, | 793 | know_region_cache (current_buffer, newline_cache, |
| 795 | start_byte + cursor - base, | 794 | BYTE_TO_CHAR (start_byte + cursor - base), |
| 796 | start_byte + scan_start - base); | 795 | BYTE_TO_CHAR (start_byte + scan_start - base)); |
| 797 | 796 | ||
| 798 | /* Did we find the target character? */ | 797 | /* Did we find the target character? */ |
| 799 | if (cursor >= ceiling_addr) | 798 | if (cursor >= ceiling_addr) |
| @@ -1303,7 +1302,7 @@ search_buffer (Lisp_Object string, EMACS_INT pos, EMACS_INT pos_byte, | |||
| 1303 | checking if we can use boyer-moore search. If TRT is | 1302 | checking if we can use boyer-moore search. If TRT is |
| 1304 | non-nil, we can use boyer-moore search only if TRT can be | 1303 | non-nil, we can use boyer-moore search only if TRT can be |
| 1305 | represented by the byte array of 256 elements. For that, | 1304 | represented by the byte array of 256 elements. For that, |
| 1306 | all non-ASCII case-equivalents of all case-senstive | 1305 | all non-ASCII case-equivalents of all case-sensitive |
| 1307 | characters in STRING must belong to the same charset and | 1306 | characters in STRING must belong to the same charset and |
| 1308 | row. */ | 1307 | row. */ |
| 1309 | 1308 | ||
| @@ -1760,7 +1759,7 @@ boyer_moore (EMACS_INT n, unsigned char *base_pat, | |||
| 1760 | ch = -1; | 1759 | ch = -1; |
| 1761 | } | 1760 | } |
| 1762 | 1761 | ||
| 1763 | if (ch >= 0200) | 1762 | if (ch >= 0200 && multibyte) |
| 1764 | j = (ch & 0x3F) | 0200; | 1763 | j = (ch & 0x3F) | 0200; |
| 1765 | else | 1764 | else |
| 1766 | j = *ptr; | 1765 | j = *ptr; |
| @@ -1779,7 +1778,7 @@ boyer_moore (EMACS_INT n, unsigned char *base_pat, | |||
| 1779 | while (1) | 1778 | while (1) |
| 1780 | { | 1779 | { |
| 1781 | TRANSLATE (ch, inverse_trt, ch); | 1780 | TRANSLATE (ch, inverse_trt, ch); |
| 1782 | if (ch >= 0200) | 1781 | if (ch >= 0200 && multibyte) |
| 1783 | j = (ch & 0x3F) | 0200; | 1782 | j = (ch & 0x3F) | 0200; |
| 1784 | else | 1783 | else |
| 1785 | j = ch; | 1784 | j = ch; |
| @@ -1892,7 +1891,7 @@ boyer_moore (EMACS_INT n, unsigned char *base_pat, | |||
| 1892 | || CHAR_HEAD_P (cursor[1])) | 1891 | || CHAR_HEAD_P (cursor[1])) |
| 1893 | && (CHAR_HEAD_P (cursor[0]) | 1892 | && (CHAR_HEAD_P (cursor[0]) |
| 1894 | /* Check if this is the last byte of | 1893 | /* Check if this is the last byte of |
| 1895 | a translable character. */ | 1894 | a translatable character. */ |
| 1896 | || (translate_prev_byte1 == cursor[-1] | 1895 | || (translate_prev_byte1 == cursor[-1] |
| 1897 | && (CHAR_HEAD_P (translate_prev_byte1) | 1896 | && (CHAR_HEAD_P (translate_prev_byte1) |
| 1898 | || (translate_prev_byte2 == cursor[-2] | 1897 | || (translate_prev_byte2 == cursor[-2] |
| @@ -1991,7 +1990,7 @@ boyer_moore (EMACS_INT n, unsigned char *base_pat, | |||
| 1991 | || CHAR_HEAD_P (ptr[1])) | 1990 | || CHAR_HEAD_P (ptr[1])) |
| 1992 | && (CHAR_HEAD_P (ptr[0]) | 1991 | && (CHAR_HEAD_P (ptr[0]) |
| 1993 | /* Check if this is the last byte of a | 1992 | /* Check if this is the last byte of a |
| 1994 | translable character. */ | 1993 | translatable character. */ |
| 1995 | || (translate_prev_byte1 == ptr[-1] | 1994 | || (translate_prev_byte1 == ptr[-1] |
| 1996 | && (CHAR_HEAD_P (translate_prev_byte1) | 1995 | && (CHAR_HEAD_P (translate_prev_byte1) |
| 1997 | || (translate_prev_byte2 == ptr[-2] | 1996 | || (translate_prev_byte2 == ptr[-2] |
| @@ -2078,13 +2077,16 @@ set_search_regs (EMACS_INT beg_byte, EMACS_INT nbytes) | |||
| 2078 | XSETBUFFER (last_thing_searched, current_buffer); | 2077 | XSETBUFFER (last_thing_searched, current_buffer); |
| 2079 | } | 2078 | } |
| 2080 | 2079 | ||
| 2081 | /* Given STRING, a string of words separated by word delimiters, | 2080 | DEFUN ("word-search-regexp", Fword_search_regexp, Sword_search_regexp, 1, 2, 0, |
| 2082 | compute a regexp that matches those exact words separated by | 2081 | doc: /* Return a regexp which matches words, ignoring punctuation. |
| 2083 | arbitrary punctuation. If LAX is nonzero, the end of the string | 2082 | Given STRING, a string of words separated by word delimiters, |
| 2084 | need not match a word boundary unless it ends in whitespace. */ | 2083 | compute a regexp that matches those exact words separated by |
| 2085 | 2084 | arbitrary punctuation. If LAX is non-nil, the end of the string | |
| 2086 | static Lisp_Object | 2085 | need not match a word boundary unless it ends in whitespace. |
| 2087 | wordify (Lisp_Object string, int lax) | 2086 | |
| 2087 | Used in `word-search-forward', `word-search-backward', | ||
| 2088 | `word-search-forward-lax', `word-search-backward-lax'. */) | ||
| 2089 | (Lisp_Object string, Lisp_Object lax) | ||
| 2088 | { | 2090 | { |
| 2089 | register unsigned char *o; | 2091 | register unsigned char *o; |
| 2090 | register EMACS_INT i, i_byte, len, punct_count = 0, word_count = 0; | 2092 | register EMACS_INT i, i_byte, len, punct_count = 0, word_count = 0; |
| @@ -2125,7 +2127,7 @@ wordify (Lisp_Object string, int lax) | |||
| 2125 | } | 2127 | } |
| 2126 | 2128 | ||
| 2127 | adjust = - punct_count + 5 * (word_count - 1) | 2129 | adjust = - punct_count + 5 * (word_count - 1) |
| 2128 | + ((lax && !whitespace_at_end) ? 2 : 4); | 2130 | + ((!NILP (lax) && !whitespace_at_end) ? 2 : 4); |
| 2129 | if (STRING_MULTIBYTE (string)) | 2131 | if (STRING_MULTIBYTE (string)) |
| 2130 | val = make_uninit_multibyte_string (len + adjust, | 2132 | val = make_uninit_multibyte_string (len + adjust, |
| 2131 | SBYTES (string) | 2133 | SBYTES (string) |
| @@ -2162,7 +2164,7 @@ wordify (Lisp_Object string, int lax) | |||
| 2162 | prev_c = c; | 2164 | prev_c = c; |
| 2163 | } | 2165 | } |
| 2164 | 2166 | ||
| 2165 | if (!lax || whitespace_at_end) | 2167 | if (NILP (lax) || whitespace_at_end) |
| 2166 | { | 2168 | { |
| 2167 | *o++ = '\\'; | 2169 | *o++ = '\\'; |
| 2168 | *o++ = 'b'; | 2170 | *o++ = 'b'; |
| @@ -2179,7 +2181,9 @@ An optional second argument bounds the search; it is a buffer position. | |||
| 2179 | The match found must not extend before that position. | 2181 | The match found must not extend before that position. |
| 2180 | Optional third argument, if t, means if fail just return nil (no error). | 2182 | Optional third argument, if t, means if fail just return nil (no error). |
| 2181 | If not nil and not t, position at limit of search and return nil. | 2183 | If not nil and not t, position at limit of search and return nil. |
| 2182 | Optional fourth argument is repeat count--search for successive occurrences. | 2184 | Optional fourth argument COUNT, if non-nil, means to search for COUNT |
| 2185 | successive occurrences. If COUNT is negative, search forward, | ||
| 2186 | instead of backward, for -COUNT occurrences. | ||
| 2183 | 2187 | ||
| 2184 | Search case-sensitivity is determined by the value of the variable | 2188 | Search case-sensitivity is determined by the value of the variable |
| 2185 | `case-fold-search', which see. | 2189 | `case-fold-search', which see. |
| @@ -2198,7 +2202,9 @@ The match found must not extend after that position. A value of nil is | |||
| 2198 | equivalent to (point-max). | 2202 | equivalent to (point-max). |
| 2199 | Optional third argument, if t, means if fail just return nil (no error). | 2203 | Optional third argument, if t, means if fail just return nil (no error). |
| 2200 | If not nil and not t, move to limit of search and return nil. | 2204 | If not nil and not t, move to limit of search and return nil. |
| 2201 | Optional fourth argument is repeat count--search for successive occurrences. | 2205 | Optional fourth argument COUNT, if non-nil, means to search for COUNT |
| 2206 | successive occurrences. If COUNT is negative, search backward, | ||
| 2207 | instead of forward, for -COUNT occurrences. | ||
| 2202 | 2208 | ||
| 2203 | Search case-sensitivity is determined by the value of the variable | 2209 | Search case-sensitivity is determined by the value of the variable |
| 2204 | `case-fold-search', which see. | 2210 | `case-fold-search', which see. |
| @@ -2217,10 +2223,14 @@ An optional second argument bounds the search; it is a buffer position. | |||
| 2217 | The match found must not extend before that position. | 2223 | The match found must not extend before that position. |
| 2218 | Optional third argument, if t, means if fail just return nil (no error). | 2224 | Optional third argument, if t, means if fail just return nil (no error). |
| 2219 | If not nil and not t, move to limit of search and return nil. | 2225 | If not nil and not t, move to limit of search and return nil. |
| 2220 | Optional fourth argument is repeat count--search for successive occurrences. */) | 2226 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2227 | |||
| 2228 | Relies on the function `word-search-regexp' to convert a sequence | ||
| 2229 | of words in STRING to a regexp used to search words without regard | ||
| 2230 | to punctuation. */) | ||
| 2221 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2231 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| 2222 | { | 2232 | { |
| 2223 | return search_command (wordify (string, 0), bound, noerror, count, -1, 1, 0); | 2233 | return search_command (Fword_search_regexp (string, Qnil), bound, noerror, count, -1, 1, 0); |
| 2224 | } | 2234 | } |
| 2225 | 2235 | ||
| 2226 | DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4, | 2236 | DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4, |
| @@ -2231,10 +2241,14 @@ An optional second argument bounds the search; it is a buffer position. | |||
| 2231 | The match found must not extend after that position. | 2241 | The match found must not extend after that position. |
| 2232 | Optional third argument, if t, means if fail just return nil (no error). | 2242 | Optional third argument, if t, means if fail just return nil (no error). |
| 2233 | If not nil and not t, move to limit of search and return nil. | 2243 | If not nil and not t, move to limit of search and return nil. |
| 2234 | Optional fourth argument is repeat count--search for successive occurrences. */) | 2244 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2245 | |||
| 2246 | Relies on the function `word-search-regexp' to convert a sequence | ||
| 2247 | of words in STRING to a regexp used to search words without regard | ||
| 2248 | to punctuation. */) | ||
| 2235 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2249 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| 2236 | { | 2250 | { |
| 2237 | return search_command (wordify (string, 0), bound, noerror, count, 1, 1, 0); | 2251 | return search_command (Fword_search_regexp (string, Qnil), bound, noerror, count, 1, 1, 0); |
| 2238 | } | 2252 | } |
| 2239 | 2253 | ||
| 2240 | DEFUN ("word-search-backward-lax", Fword_search_backward_lax, Sword_search_backward_lax, 1, 4, | 2254 | DEFUN ("word-search-backward-lax", Fword_search_backward_lax, Sword_search_backward_lax, 1, 4, |
| @@ -2249,10 +2263,14 @@ An optional second argument bounds the search; it is a buffer position. | |||
| 2249 | The match found must not extend before that position. | 2263 | The match found must not extend before that position. |
| 2250 | Optional third argument, if t, means if fail just return nil (no error). | 2264 | Optional third argument, if t, means if fail just return nil (no error). |
| 2251 | If not nil and not t, move to limit of search and return nil. | 2265 | If not nil and not t, move to limit of search and return nil. |
| 2252 | Optional fourth argument is repeat count--search for successive occurrences. */) | 2266 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2267 | |||
| 2268 | Relies on the function `word-search-regexp' to convert a sequence | ||
| 2269 | of words in STRING to a regexp used to search words without regard | ||
| 2270 | to punctuation. */) | ||
| 2253 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2271 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| 2254 | { | 2272 | { |
| 2255 | return search_command (wordify (string, 1), bound, noerror, count, -1, 1, 0); | 2273 | return search_command (Fword_search_regexp (string, Qt), bound, noerror, count, -1, 1, 0); |
| 2256 | } | 2274 | } |
| 2257 | 2275 | ||
| 2258 | DEFUN ("word-search-forward-lax", Fword_search_forward_lax, Sword_search_forward_lax, 1, 4, | 2276 | DEFUN ("word-search-forward-lax", Fword_search_forward_lax, Sword_search_forward_lax, 1, 4, |
| @@ -2267,10 +2285,14 @@ An optional second argument bounds the search; it is a buffer position. | |||
| 2267 | The match found must not extend after that position. | 2285 | The match found must not extend after that position. |
| 2268 | Optional third argument, if t, means if fail just return nil (no error). | 2286 | Optional third argument, if t, means if fail just return nil (no error). |
| 2269 | If not nil and not t, move to limit of search and return nil. | 2287 | If not nil and not t, move to limit of search and return nil. |
| 2270 | Optional fourth argument is repeat count--search for successive occurrences. */) | 2288 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2289 | |||
| 2290 | Relies on the function `word-search-regexp' to convert a sequence | ||
| 2291 | of words in STRING to a regexp used to search words without regard | ||
| 2292 | to punctuation. */) | ||
| 2271 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2293 | (Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| 2272 | { | 2294 | { |
| 2273 | return search_command (wordify (string, 1), bound, noerror, count, 1, 1, 0); | 2295 | return search_command (Fword_search_regexp (string, Qt), bound, noerror, count, 1, 1, 0); |
| 2274 | } | 2296 | } |
| 2275 | 2297 | ||
| 2276 | DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4, | 2298 | DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4, |
| @@ -2284,6 +2306,10 @@ The match found must start at or after that position. | |||
| 2284 | Optional third argument, if t, means if fail just return nil (no error). | 2306 | Optional third argument, if t, means if fail just return nil (no error). |
| 2285 | If not nil and not t, move to limit of search and return nil. | 2307 | If not nil and not t, move to limit of search and return nil. |
| 2286 | Optional fourth argument is repeat count--search for successive occurrences. | 2308 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2309 | |||
| 2310 | Search case-sensitivity is determined by the value of the variable | ||
| 2311 | `case-fold-search', which see. | ||
| 2312 | |||
| 2287 | See also the functions `match-beginning', `match-end', `match-string', | 2313 | See also the functions `match-beginning', `match-end', `match-string', |
| 2288 | and `replace-match'. */) | 2314 | and `replace-match'. */) |
| 2289 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2315 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| @@ -2300,6 +2326,10 @@ The match found must not extend after that position. | |||
| 2300 | Optional third argument, if t, means if fail just return nil (no error). | 2326 | Optional third argument, if t, means if fail just return nil (no error). |
| 2301 | If not nil and not t, move to limit of search and return nil. | 2327 | If not nil and not t, move to limit of search and return nil. |
| 2302 | Optional fourth argument is repeat count--search for successive occurrences. | 2328 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2329 | |||
| 2330 | Search case-sensitivity is determined by the value of the variable | ||
| 2331 | `case-fold-search', which see. | ||
| 2332 | |||
| 2303 | See also the functions `match-beginning', `match-end', `match-string', | 2333 | See also the functions `match-beginning', `match-end', `match-string', |
| 2304 | and `replace-match'. */) | 2334 | and `replace-match'. */) |
| 2305 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2335 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| @@ -2319,6 +2349,10 @@ The match found must start at or after that position. | |||
| 2319 | Optional third argument, if t, means if fail just return nil (no error). | 2349 | Optional third argument, if t, means if fail just return nil (no error). |
| 2320 | If not nil and not t, move to limit of search and return nil. | 2350 | If not nil and not t, move to limit of search and return nil. |
| 2321 | Optional fourth argument is repeat count--search for successive occurrences. | 2351 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2352 | |||
| 2353 | Search case-sensitivity is determined by the value of the variable | ||
| 2354 | `case-fold-search', which see. | ||
| 2355 | |||
| 2322 | See also the functions `match-beginning', `match-end', `match-string', | 2356 | See also the functions `match-beginning', `match-end', `match-string', |
| 2323 | and `replace-match'. */) | 2357 | and `replace-match'. */) |
| 2324 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2358 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| @@ -2336,6 +2370,10 @@ The match found must not extend after that position. | |||
| 2336 | Optional third argument, if t, means if fail just return nil (no error). | 2370 | Optional third argument, if t, means if fail just return nil (no error). |
| 2337 | If not nil and not t, move to limit of search and return nil. | 2371 | If not nil and not t, move to limit of search and return nil. |
| 2338 | Optional fourth argument is repeat count--search for successive occurrences. | 2372 | Optional fourth argument is repeat count--search for successive occurrences. |
| 2373 | |||
| 2374 | Search case-sensitivity is determined by the value of the variable | ||
| 2375 | `case-fold-search', which see. | ||
| 2376 | |||
| 2339 | See also the functions `match-beginning', `match-end', `match-string', | 2377 | See also the functions `match-beginning', `match-end', `match-string', |
| 2340 | and `replace-match'. */) | 2378 | and `replace-match'. */) |
| 2341 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) | 2379 | (Lisp_Object regexp, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count) |
| @@ -2388,7 +2426,7 @@ since only regular expressions have distinguished subexpressions. */) | |||
| 2388 | int some_uppercase; | 2426 | int some_uppercase; |
| 2389 | int some_nonuppercase_initial; | 2427 | int some_nonuppercase_initial; |
| 2390 | register int c, prevc; | 2428 | register int c, prevc; |
| 2391 | int sub; | 2429 | ptrdiff_t sub; |
| 2392 | EMACS_INT opoint, newpoint; | 2430 | EMACS_INT opoint, newpoint; |
| 2393 | 2431 | ||
| 2394 | CHECK_STRING (newtext); | 2432 | CHECK_STRING (newtext); |
| @@ -2407,9 +2445,9 @@ since only regular expressions have distinguished subexpressions. */) | |||
| 2407 | else | 2445 | else |
| 2408 | { | 2446 | { |
| 2409 | CHECK_NUMBER (subexp); | 2447 | CHECK_NUMBER (subexp); |
| 2410 | sub = XINT (subexp); | 2448 | if (! (0 <= XINT (subexp) && XINT (subexp) < search_regs.num_regs)) |
| 2411 | if (sub < 0 || sub >= search_regs.num_regs) | ||
| 2412 | args_out_of_range (subexp, make_number (search_regs.num_regs)); | 2449 | args_out_of_range (subexp, make_number (search_regs.num_regs)); |
| 2450 | sub = XINT (subexp); | ||
| 2413 | } | 2451 | } |
| 2414 | 2452 | ||
| 2415 | if (NILP (string)) | 2453 | if (NILP (string)) |
| @@ -2624,15 +2662,17 @@ since only regular expressions have distinguished subexpressions. */) | |||
| 2624 | perform substitution on the replacement string. */ | 2662 | perform substitution on the replacement string. */ |
| 2625 | if (NILP (literal)) | 2663 | if (NILP (literal)) |
| 2626 | { | 2664 | { |
| 2627 | EMACS_INT length = SBYTES (newtext); | 2665 | ptrdiff_t length = SBYTES (newtext); |
| 2628 | unsigned char *substed; | 2666 | unsigned char *substed; |
| 2629 | EMACS_INT substed_alloc_size, substed_len; | 2667 | ptrdiff_t substed_alloc_size, substed_len; |
| 2630 | int buf_multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); | 2668 | int buf_multibyte = !NILP (BVAR (current_buffer, enable_multibyte_characters)); |
| 2631 | int str_multibyte = STRING_MULTIBYTE (newtext); | 2669 | int str_multibyte = STRING_MULTIBYTE (newtext); |
| 2632 | int really_changed = 0; | 2670 | int really_changed = 0; |
| 2633 | 2671 | ||
| 2634 | substed_alloc_size = length * 2 + 100; | 2672 | substed_alloc_size = ((STRING_BYTES_BOUND - 100) / 2 < length |
| 2635 | substed = (unsigned char *) xmalloc (substed_alloc_size + 1); | 2673 | ? STRING_BYTES_BOUND |
| 2674 | : length * 2 + 100); | ||
| 2675 | substed = (unsigned char *) xmalloc (substed_alloc_size); | ||
| 2636 | substed_len = 0; | 2676 | substed_len = 0; |
| 2637 | 2677 | ||
| 2638 | /* Go thru NEWTEXT, producing the actual text to insert in | 2678 | /* Go thru NEWTEXT, producing the actual text to insert in |
| @@ -2643,8 +2683,8 @@ since only regular expressions have distinguished subexpressions. */) | |||
| 2643 | { | 2683 | { |
| 2644 | unsigned char str[MAX_MULTIBYTE_LENGTH]; | 2684 | unsigned char str[MAX_MULTIBYTE_LENGTH]; |
| 2645 | const unsigned char *add_stuff = NULL; | 2685 | const unsigned char *add_stuff = NULL; |
| 2646 | EMACS_INT add_len = 0; | 2686 | ptrdiff_t add_len = 0; |
| 2647 | int idx = -1; | 2687 | ptrdiff_t idx = -1; |
| 2648 | 2688 | ||
| 2649 | if (str_multibyte) | 2689 | if (str_multibyte) |
| 2650 | { | 2690 | { |
| @@ -2707,7 +2747,7 @@ since only regular expressions have distinguished subexpressions. */) | |||
| 2707 | set up ADD_STUFF and ADD_LEN to point to it. */ | 2747 | set up ADD_STUFF and ADD_LEN to point to it. */ |
| 2708 | if (idx >= 0) | 2748 | if (idx >= 0) |
| 2709 | { | 2749 | { |
| 2710 | EMACS_INT begbyte = CHAR_TO_BYTE (search_regs.start[idx]); | 2750 | ptrdiff_t begbyte = CHAR_TO_BYTE (search_regs.start[idx]); |
| 2711 | add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte; | 2751 | add_len = CHAR_TO_BYTE (search_regs.end[idx]) - begbyte; |
| 2712 | if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx]) | 2752 | if (search_regs.start[idx] < GPT && GPT < search_regs.end[idx]) |
| 2713 | move_gap (search_regs.start[idx]); | 2753 | move_gap (search_regs.start[idx]); |
| @@ -2718,12 +2758,11 @@ since only regular expressions have distinguished subexpressions. */) | |||
| 2718 | is invariably ADD_LEN bytes starting at ADD_STUFF. */ | 2758 | is invariably ADD_LEN bytes starting at ADD_STUFF. */ |
| 2719 | 2759 | ||
| 2720 | /* Make sure SUBSTED is big enough. */ | 2760 | /* Make sure SUBSTED is big enough. */ |
| 2721 | if (substed_len + add_len >= substed_alloc_size) | 2761 | if (substed_alloc_size - substed_len < add_len) |
| 2722 | { | 2762 | substed = |
| 2723 | substed_alloc_size = substed_len + add_len + 500; | 2763 | xpalloc (substed, &substed_alloc_size, |
| 2724 | substed = (unsigned char *) xrealloc (substed, | 2764 | add_len - (substed_alloc_size - substed_len), |
| 2725 | substed_alloc_size + 1); | 2765 | STRING_BYTES_BOUND, 1); |
| 2726 | } | ||
| 2727 | 2766 | ||
| 2728 | /* Now add to the end of SUBSTED. */ | 2767 | /* Now add to the end of SUBSTED. */ |
| 2729 | if (add_stuff) | 2768 | if (add_stuff) |
| @@ -2796,7 +2835,7 @@ since only regular expressions have distinguished subexpressions. */) | |||
| 2796 | static Lisp_Object | 2835 | static Lisp_Object |
| 2797 | match_limit (Lisp_Object num, int beginningp) | 2836 | match_limit (Lisp_Object num, int beginningp) |
| 2798 | { | 2837 | { |
| 2799 | register int n; | 2838 | EMACS_INT n; |
| 2800 | 2839 | ||
| 2801 | CHECK_NUMBER (num); | 2840 | CHECK_NUMBER (num); |
| 2802 | n = XINT (num); | 2841 | n = XINT (num); |
| @@ -2957,7 +2996,7 @@ LIST should have been created by calling `match-data' previously. | |||
| 2957 | If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */) | 2996 | If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */) |
| 2958 | (register Lisp_Object list, Lisp_Object reseat) | 2997 | (register Lisp_Object list, Lisp_Object reseat) |
| 2959 | { | 2998 | { |
| 2960 | register int i; | 2999 | ptrdiff_t i; |
| 2961 | register Lisp_Object marker; | 3000 | register Lisp_Object marker; |
| 2962 | 3001 | ||
| 2963 | if (running_asynch_code) | 3002 | if (running_asynch_code) |
| @@ -2971,31 +3010,21 @@ If optional arg RESEAT is non-nil, make markers on LIST point nowhere. */) | |||
| 2971 | 3010 | ||
| 2972 | /* Allocate registers if they don't already exist. */ | 3011 | /* Allocate registers if they don't already exist. */ |
| 2973 | { | 3012 | { |
| 2974 | int length = XFASTINT (Flength (list)) / 2; | 3013 | ptrdiff_t length = XFASTINT (Flength (list)) / 2; |
| 2975 | 3014 | ||
| 2976 | if (length > search_regs.num_regs) | 3015 | if (length > search_regs.num_regs) |
| 2977 | { | 3016 | { |
| 2978 | if (search_regs.num_regs == 0) | 3017 | ptrdiff_t num_regs = search_regs.num_regs; |
| 2979 | { | 3018 | search_regs.start = |
| 2980 | search_regs.start | 3019 | xpalloc (search_regs.start, &num_regs, length - num_regs, |
| 2981 | = (regoff_t *) xmalloc (length * sizeof (regoff_t)); | 3020 | min (PTRDIFF_MAX, UINT_MAX), sizeof (regoff_t)); |
| 2982 | search_regs.end | 3021 | search_regs.end = |
| 2983 | = (regoff_t *) xmalloc (length * sizeof (regoff_t)); | 3022 | xrealloc (search_regs.end, num_regs * sizeof (regoff_t)); |
| 2984 | } | 3023 | |
| 2985 | else | 3024 | for (i = search_regs.num_regs; i < num_regs; i++) |
| 2986 | { | ||
| 2987 | search_regs.start | ||
| 2988 | = (regoff_t *) xrealloc (search_regs.start, | ||
| 2989 | length * sizeof (regoff_t)); | ||
| 2990 | search_regs.end | ||
| 2991 | = (regoff_t *) xrealloc (search_regs.end, | ||
| 2992 | length * sizeof (regoff_t)); | ||
| 2993 | } | ||
| 2994 | |||
| 2995 | for (i = search_regs.num_regs; i < length; i++) | ||
| 2996 | search_regs.start[i] = -1; | 3025 | search_regs.start[i] = -1; |
| 2997 | 3026 | ||
| 2998 | search_regs.num_regs = length; | 3027 | search_regs.num_regs = num_regs; |
| 2999 | } | 3028 | } |
| 3000 | 3029 | ||
| 3001 | for (i = 0; CONSP (list); i++) | 3030 | for (i = 0; CONSP (list); i++) |
| @@ -3125,7 +3154,7 @@ record_unwind_save_match_data (void) | |||
| 3125 | Fmatch_data (Qnil, Qnil, Qnil)); | 3154 | Fmatch_data (Qnil, Qnil, Qnil)); |
| 3126 | } | 3155 | } |
| 3127 | 3156 | ||
| 3128 | /* Quote a string to inactivate reg-expr chars */ | 3157 | /* Quote a string to deactivate reg-expr chars */ |
| 3129 | 3158 | ||
| 3130 | DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0, | 3159 | DEFUN ("regexp-quote", Fregexp_quote, Sregexp_quote, 1, 1, 0, |
| 3131 | doc: /* Return a regexp string which matches exactly STRING and nothing else. */) | 3160 | doc: /* Return a regexp string which matches exactly STRING and nothing else. */) |
| @@ -3222,6 +3251,7 @@ is to bind it with `let' around a small expression. */); | |||
| 3222 | defsubr (&Sposix_string_match); | 3251 | defsubr (&Sposix_string_match); |
| 3223 | defsubr (&Ssearch_forward); | 3252 | defsubr (&Ssearch_forward); |
| 3224 | defsubr (&Ssearch_backward); | 3253 | defsubr (&Ssearch_backward); |
| 3254 | defsubr (&Sword_search_regexp); | ||
| 3225 | defsubr (&Sword_search_forward); | 3255 | defsubr (&Sword_search_forward); |
| 3226 | defsubr (&Sword_search_backward); | 3256 | defsubr (&Sword_search_backward); |
| 3227 | defsubr (&Sword_search_forward_lax); | 3257 | defsubr (&Sword_search_forward_lax); |