[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: query-replace-regexp: Can't use \0 in TO-STRING
From: |
zhanghj |
Subject: |
Re: query-replace-regexp: Can't use \0 in TO-STRING |
Date: |
Sun, 07 May 2017 11:55:15 +0800 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/25.2 (gnu/linux) |
Tino Calancha <address@hidden> writes:
>> Why not \0? I think \0 is more intuitive and also used in vim.
> I agree is more intuitive, and it works in `replace-match', or instance:
> (mapcar
> (lambda (group)
> (let ((str "foo123"))
> (when (string-match "[a-z]+\\([1-9]+\\)" str)
> (replace-match "bar" nil nil str group))))
> (list 0 1))
> => ("bar" "foobar")
>
> Are you willing to write a patch to implement it?
The following patch works on my machine for regexp replacing. But I
don't known if \0 should be supported in other places such as
sort-regexp-fields, occur.
diff --git a/lisp/subr.el b/lisp/subr.el
index 02e7993..c182128 100644
--- a/lisp/subr.el
+++ b/lisp/subr.el
@@ -3634,7 +3634,7 @@ match-string-no-properties
(defun match-substitute-replacement (replacement
&optional fixedcase literal string subexp)
"Return REPLACEMENT as it will be inserted by `replace-match'.
-In other words, all back-references in the form `\\&' and `\\N'
+In other words, all back-references in the form `\\&', `\\0' and `\\N'
are substituted with actual strings matched by the last search.
Optional FIXEDCASE, LITERAL, STRING and SUBEXP have the same
meaning as for `replace-match'."
diff --git a/src/search.c b/src/search.c
index 1223cbf..86255f0 100644
--- a/src/search.c
+++ b/src/search.c
@@ -2339,7 +2339,7 @@ in the replaced text, capitalize each word in NEWTEXT.
If optional third arg LITERAL is non-nil, insert NEWTEXT literally.
Otherwise treat `\\' as special:
- `\\&' in NEWTEXT means substitute original matched text.
+ `\\&' or `\\0' in NEWTEXT means substitute original matched text.
`\\N' means substitute what matched the Nth `\\(...\\)'.
If Nth parens didn't match, substitute nothing.
`\\\\' means insert one `\\'.
@@ -2523,7 +2523,7 @@ since only regular expressions have distinguished
subexpressions. */)
{
FETCH_STRING_CHAR_ADVANCE (c, newtext, pos, pos_byte);
- if (c == '&')
+ if (c == '&' || c == '0')
{
substart = search_regs.start[sub];
subend = search_regs.end[sub];
@@ -2668,7 +2668,7 @@ since only regular expressions have distinguished
subexpressions. */)
MAKE_CHAR_MULTIBYTE (c);
}
- if (c == '&')
+ if (c == '&' || c == '0')
idx = sub;
else if (c >= '1' && c <= '9' && c - '0' < search_regs.num_regs)
{