Move word search functions from search.c to isearch.el (bug#10145, bug#11381).

* lisp/isearch.el (word-search-regexp, word-search-backward)
(word-search-forward, word-search-backward-lax)
(word-search-forward-lax): Move functions from search.c.

* src/search.c (Fword_search_regexp, Fword_search_backward)
(Fword_search_forward, Fword_search_backward_lax)
(Fword_search_forward_lax): Move functions to isearch.el.
This commit is contained in:
Juri Linkov 2012-05-17 03:03:49 +03:00
parent 5ec546086a
commit a0a79cde7c
4 changed files with 102 additions and 181 deletions

View file

@ -1,3 +1,10 @@
2012-05-17 Juri Linkov <juri@jurta.org>
* isearch.el (word-search-regexp, word-search-backward)
(word-search-forward, word-search-backward-lax)
(word-search-forward-lax): Move functions from search.c
(bug#10145, bug#11381).
2012-05-16 Agustín Martín Domingo <agustin.martin@hispalinux.es>
* flyspell.el (flyspell-check-pre-word-p, flyspell-check-word-p)

View file

@ -1381,6 +1381,94 @@ Use `isearch-exit' to quit without signaling."
(sit-for 1)
(isearch-update))
;; Word search
(defun word-search-regexp (string &optional lax)
"Return a regexp which matches words, ignoring punctuation.
Given STRING, a string of words separated by word delimiters,
compute a regexp that matches those exact words separated by
arbitrary punctuation. If LAX is non-nil, the end of the string
need not match a word boundary unless it ends in whitespace.
Used in `word-search-forward', `word-search-backward',
`word-search-forward-lax', `word-search-backward-lax'."
(if (string-match-p "^\\W*$" string)
""
(concat
"\\b"
(mapconcat 'identity (split-string string "\\W+" t) "\\W+")
(if (or (not lax) (string-match-p "\\W$" string)) "\\b"))))
(defun word-search-backward (string &optional bound noerror count)
"Search backward from point for STRING, ignoring differences in punctuation.
Set point to the beginning of the occurrence found, and return point.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend before that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation."
(interactive "sWord search backward: ")
(re-search-backward (word-search-regexp string nil) bound noerror count))
(defun word-search-forward (string &optional bound noerror count)
"Search forward from point for STRING, ignoring differences in punctuation.
Set point to the end of the occurrence found, and return point.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend after that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation."
(interactive "sWord search: ")
(re-search-forward (word-search-regexp string nil) bound noerror count))
(defun word-search-backward-lax (string &optional bound noerror count)
"Search backward from point for STRING, ignoring differences in punctuation.
Set point to the beginning of the occurrence found, and return point.
Unlike `word-search-backward', the end of STRING need not match a word
boundary, unless STRING ends in whitespace.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend before that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation."
(interactive "sWord search backward: ")
(re-search-backward (word-search-regexp string t) bound noerror count))
(defun word-search-forward-lax (string &optional bound noerror count)
"Search forward from point for STRING, ignoring differences in punctuation.
Set point to the end of the occurrence found, and return point.
Unlike `word-search-forward', the end of STRING need not match a word
boundary, unless STRING ends in whitespace.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend after that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation."
(interactive "sWord search: ")
(re-search-forward (word-search-regexp string t) bound noerror count))
(defun isearch-query-replace (&optional delimited regexp-flag)
"Start `query-replace' with string to replace from last search string.
The arg DELIMITED (prefix arg if interactive), if non-nil, means replace

View file

@ -1,3 +1,10 @@
2012-05-17 Juri Linkov <juri@jurta.org>
* search.c (Fword_search_regexp, Fword_search_backward)
(Fword_search_forward, Fword_search_backward_lax)
(Fword_search_forward_lax): Move functions to isearch.el
(bug#10145, bug#11381).
2012-05-16 Paul Eggert <eggert@cs.ucla.edu>
* xgselect.c (xg_select): Just invoke 'select' if -nw (Bug#9754).

View file

@ -2078,102 +2078,6 @@ set_search_regs (EMACS_INT beg_byte, EMACS_INT nbytes)
XSETBUFFER (last_thing_searched, current_buffer);
}
DEFUN ("word-search-regexp", Fword_search_regexp, Sword_search_regexp, 1, 2, 0,
doc: /* Return a regexp which matches words, ignoring punctuation.
Given STRING, a string of words separated by word delimiters,
compute a regexp that matches those exact words separated by
arbitrary punctuation. If LAX is non-nil, the end of the string
need not match a word boundary unless it ends in whitespace.
Used in `word-search-forward', `word-search-backward',
`word-search-forward-lax', `word-search-backward-lax'. */)
(Lisp_Object string, Lisp_Object lax)
{
register unsigned char *o;
register EMACS_INT i, i_byte, len, punct_count = 0, word_count = 0;
Lisp_Object val;
int prev_c = 0;
EMACS_INT adjust;
int whitespace_at_end;
CHECK_STRING (string);
len = SCHARS (string);
for (i = 0, i_byte = 0; i < len; )
{
int c;
FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c, string, i, i_byte);
if (SYNTAX (c) != Sword)
{
punct_count++;
if (SYNTAX (prev_c) == Sword)
word_count++;
}
prev_c = c;
}
if (SYNTAX (prev_c) == Sword)
{
word_count++;
whitespace_at_end = 0;
}
else
{
whitespace_at_end = 1;
if (!word_count)
return empty_unibyte_string;
}
adjust = - punct_count + 5 * (word_count - 1)
+ ((!NILP (lax) && !whitespace_at_end) ? 2 : 4);
if (STRING_MULTIBYTE (string))
val = make_uninit_multibyte_string (len + adjust,
SBYTES (string)
+ adjust);
else
val = make_uninit_string (len + adjust);
o = SDATA (val);
*o++ = '\\';
*o++ = 'b';
prev_c = 0;
for (i = 0, i_byte = 0; i < len; )
{
int c;
EMACS_INT i_byte_orig = i_byte;
FETCH_STRING_CHAR_AS_MULTIBYTE_ADVANCE (c, string, i, i_byte);
if (SYNTAX (c) == Sword)
{
memcpy (o, SDATA (string) + i_byte_orig, i_byte - i_byte_orig);
o += i_byte - i_byte_orig;
}
else if (SYNTAX (prev_c) == Sword && --word_count)
{
*o++ = '\\';
*o++ = 'W';
*o++ = '\\';
*o++ = 'W';
*o++ = '*';
}
prev_c = c;
}
if (NILP (lax) || whitespace_at_end)
{
*o++ = '\\';
*o++ = 'b';
}
return val;
}
DEFUN ("search-backward", Fsearch_backward, Ssearch_backward, 1, 4,
"MSearch backward: ",
doc: /* Search backward from point for STRING.
@ -2216,86 +2120,6 @@ See also the functions `match-beginning', `match-end' and `replace-match'. */)
return search_command (string, bound, noerror, count, 1, 0, 0);
}
DEFUN ("word-search-backward", Fword_search_backward, Sword_search_backward, 1, 4,
"sWord search backward: ",
doc: /* Search backward from point for STRING, ignoring differences in punctuation.
Set point to the beginning of the occurrence found, and return point.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend before that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation. */)
(Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
{
return search_command (Fword_search_regexp (string, Qnil), bound, noerror, count, -1, 1, 0);
}
DEFUN ("word-search-forward", Fword_search_forward, Sword_search_forward, 1, 4,
"sWord search: ",
doc: /* Search forward from point for STRING, ignoring differences in punctuation.
Set point to the end of the occurrence found, and return point.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend after that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation. */)
(Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
{
return search_command (Fword_search_regexp (string, Qnil), bound, noerror, count, 1, 1, 0);
}
DEFUN ("word-search-backward-lax", Fword_search_backward_lax, Sword_search_backward_lax, 1, 4,
"sWord search backward: ",
doc: /* Search backward from point for STRING, ignoring differences in punctuation.
Set point to the beginning of the occurrence found, and return point.
Unlike `word-search-backward', the end of STRING need not match a word
boundary, unless STRING ends in whitespace.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend before that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation. */)
(Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
{
return search_command (Fword_search_regexp (string, Qt), bound, noerror, count, -1, 1, 0);
}
DEFUN ("word-search-forward-lax", Fword_search_forward_lax, Sword_search_forward_lax, 1, 4,
"sWord search: ",
doc: /* Search forward from point for STRING, ignoring differences in punctuation.
Set point to the end of the occurrence found, and return point.
Unlike `word-search-forward', the end of STRING need not match a word
boundary, unless STRING ends in whitespace.
An optional second argument bounds the search; it is a buffer position.
The match found must not extend after that position.
Optional third argument, if t, means if fail just return nil (no error).
If not nil and not t, move to limit of search and return nil.
Optional fourth argument is repeat count--search for successive occurrences.
Relies on the function `word-search-regexp' to convert a sequence
of words in STRING to a regexp used to search words without regard
to punctuation. */)
(Lisp_Object string, Lisp_Object bound, Lisp_Object noerror, Lisp_Object count)
{
return search_command (Fword_search_regexp (string, Qt), bound, noerror, count, 1, 1, 0);
}
DEFUN ("re-search-backward", Fre_search_backward, Sre_search_backward, 1, 4,
"sRE search backward: ",
doc: /* Search backward from point for match for regular expression REGEXP.
@ -3252,11 +3076,6 @@ is to bind it with `let' around a small expression. */);
defsubr (&Sposix_string_match);
defsubr (&Ssearch_forward);
defsubr (&Ssearch_backward);
defsubr (&Sword_search_regexp);
defsubr (&Sword_search_forward);
defsubr (&Sword_search_backward);
defsubr (&Sword_search_forward_lax);
defsubr (&Sword_search_backward_lax);
defsubr (&Sre_search_forward);
defsubr (&Sre_search_backward);
defsubr (&Sposix_search_forward);