unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* query-replace-regexp: Can't use \0 in TO-STRING
@ 2017-05-06  0:48 zhanghj
  2017-05-06  5:04 ` Tino Calancha
  0 siblings, 1 reply; 11+ messages in thread
From: zhanghj @ 2017-05-06  0:48 UTC (permalink / raw)
  To: emacs-devel

The doc says:
In TO-STRING, ‘\&’ or ‘\0’ stands for whatever matched the whole of
REGEXP, and ‘\N’ (where N is a digit) stands for whatever matched
the Nth ‘\(...\)’ (1-based) in REGEXP.  The ‘\(...\)’ groups are
counted from 1.

But when using \0, emacs gives the following error message: 
match-substitute-replacement: Invalid use of ‘\’ in replacement text

Using \& is OK.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-06  0:48 query-replace-regexp: Can't use \0 in TO-STRING zhanghj
@ 2017-05-06  5:04 ` Tino Calancha
  2017-05-06  8:09   ` Eli Zaretskii
  2017-05-06 12:28   ` zhanghj
  0 siblings, 2 replies; 11+ messages in thread
From: Tino Calancha @ 2017-05-06  5:04 UTC (permalink / raw)
  To: zhanghj; +Cc: Tino Calancha, Emacs developers

[-- Attachment #1: Type: text/plain, Size: 719 bytes --]



On Sat, 6 May 2017, zhanghj wrote:

> The doc says:
> In TO-STRING, ‘\&’ or ‘\0’ stands for whatever matched the whole of
> REGEXP, and ‘\N’ (where N is a digit) stands for whatever matched
> the Nth ‘\(...\)’ (1-based) in REGEXP.  The ‘\(...\)’ groups are
> counted from 1.
>
> But when using \0, emacs gives the following error message:
> match-substitute-replacement: Invalid use of ‘\’ in replacement text
>
> Using \& is OK.
Thanks.
The docstring for `query-replace-regexp' was updated in commit 38f4b8ea615
to fix Bug#23884.
Apparently, \0 has never referred to the whole match in this command
(\& it does), so we might want to skip \0 from this docstring and the 
manual.

Regards,
Tino

^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-06  5:04 ` Tino Calancha
@ 2017-05-06  8:09   ` Eli Zaretskii
  2017-05-06 10:34     ` Tino Calancha
  2017-05-07  3:48     ` zhanghj
  2017-05-06 12:28   ` zhanghj
  1 sibling, 2 replies; 11+ messages in thread
From: Eli Zaretskii @ 2017-05-06  8:09 UTC (permalink / raw)
  To: Tino Calancha; +Cc: tino.calancha, emacs-devel, netjunegg

> From: Tino Calancha <tino.calancha@gmail.com>
> Date: Sat, 6 May 2017 14:04:46 +0900 (JST)
> Cc: Tino Calancha <tino.calancha@gmail.com>,
> 	Emacs developers <emacs-devel@gnu.org>
> 
> The docstring for `query-replace-regexp' was updated in commit 38f4b8ea615
> to fix Bug#23884.
> Apparently, \0 has never referred to the whole match in this command
> (\& it does), so we might want to skip \0 from this docstring and the 
> manual.

I fixed the doc string.  the manual doesn't need fixing, AFAICT, as it
already describes the behavior correctly.

Thanks.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-06  8:09   ` Eli Zaretskii
@ 2017-05-06 10:34     ` Tino Calancha
  2017-05-07  3:48     ` zhanghj
  1 sibling, 0 replies; 11+ messages in thread
From: Tino Calancha @ 2017-05-06 10:34 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: netjunegg, emacs-devel, Tino Calancha



On Sat, 6 May 2017, Eli Zaretskii wrote:

>> From: Tino Calancha <tino.calancha@gmail.com>
>> Date: Sat, 6 May 2017 14:04:46 +0900 (JST)
>> Cc: Tino Calancha <tino.calancha@gmail.com>,
>> 	Emacs developers <emacs-devel@gnu.org>
>>
>> The docstring for `query-replace-regexp' was updated in commit 38f4b8ea615
>> to fix Bug#23884.
>> Apparently, \0 has never referred to the whole match in this command
>> (\& it does), so we might want to skip \0 from this docstring and the
>> manual.
>
> I fixed the doc string.  the manual doesn't need fixing, AFAICT, as it
> already describes the behavior correctly.
Amen.
Thanks.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-06  5:04 ` Tino Calancha
  2017-05-06  8:09   ` Eli Zaretskii
@ 2017-05-06 12:28   ` zhanghj
  2017-05-06 12:39     ` Tino Calancha
  1 sibling, 1 reply; 11+ messages in thread
From: zhanghj @ 2017-05-06 12:28 UTC (permalink / raw)
  To: Tino Calancha; +Cc: netjune, Emacs developers

Tino Calancha <tino.calancha@gmail.com> writes:

> The docstring for `query-replace-regexp' was updated in commit 38f4b8ea615
> to fix Bug#23884.
> Apparently, \0 has never referred to the whole match in this command
> (\& it does), so we might want to skip \0 from this docstring and the 
> manual.
>
> Regards,
> Tino
Why not \0? I think \0 is more intuitive and also used in vim.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-06 12:28   ` zhanghj
@ 2017-05-06 12:39     ` Tino Calancha
  2017-05-07  3:55       ` zhanghj
  0 siblings, 1 reply; 11+ messages in thread
From: Tino Calancha @ 2017-05-06 12:39 UTC (permalink / raw)
  To: zhanghj; +Cc: netjune, Emacs developers, Tino Calancha



On Sat, 6 May 2017, zhanghj wrote:

> Tino Calancha <tino.calancha@gmail.com> writes:
>
>> The docstring for `query-replace-regexp' was updated in commit 38f4b8ea615
>> to fix Bug#23884.
>> Apparently, \0 has never referred to the whole match in this command
>> (\& it does), so we might want to skip \0 from this docstring and the
>> manual.
>>
>> Regards,
>> Tino
> 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?



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-06  8:09   ` Eli Zaretskii
  2017-05-06 10:34     ` Tino Calancha
@ 2017-05-07  3:48     ` zhanghj
  2017-05-19  8:13       ` Eli Zaretskii
  1 sibling, 1 reply; 11+ messages in thread
From: zhanghj @ 2017-05-07  3:48 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: netjune, emacs-devel, Tino Calancha

Eli Zaretskii <eliz@gnu.org> writes:

>> From: Tino Calancha <tino.calancha@gmail.com>
>> Date: Sat, 6 May 2017 14:04:46 +0900 (JST)
>> Cc: Tino Calancha <tino.calancha@gmail.com>,
>> 	Emacs developers <emacs-devel@gnu.org>
>> 
>> The docstring for `query-replace-regexp' was updated in commit 38f4b8ea615
>> to fix Bug#23884.
>> Apparently, \0 has never referred to the whole match in this command
>> (\& it does), so we might want to skip \0 from this docstring and the 
>> manual.
>
> I fixed the doc string.  the manual doesn't need fixing, AFAICT, as it
> already describes the behavior correctly.
>
> Thanks.

Maybe also the doc string of query-replace-regexp-eval?




^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-06 12:39     ` Tino Calancha
@ 2017-05-07  3:55       ` zhanghj
  2017-05-07 14:48         ` Eli Zaretskii
  0 siblings, 1 reply; 11+ messages in thread
From: zhanghj @ 2017-05-07  3:55 UTC (permalink / raw)
  To: Tino Calancha; +Cc: netjune, Emacs developers

Tino Calancha <tino.calancha@gmail.com> 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)
 		{



^ permalink raw reply related	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-07  3:55       ` zhanghj
@ 2017-05-07 14:48         ` Eli Zaretskii
  2017-05-09 22:02           ` Juri Linkov
  0 siblings, 1 reply; 11+ messages in thread
From: Eli Zaretskii @ 2017-05-07 14:48 UTC (permalink / raw)
  To: zhanghj; +Cc: netjune, emacs-devel, tino.calancha

> From: zhanghj <netjunegg@gmail.com>
> Date: Sun, 07 May 2017 11:55:15 +0800
> Cc: netjune@qq.com, Emacs developers <emacs-devel@gnu.org>
> 
> Tino Calancha <tino.calancha@gmail.com> 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.

If this is accepted (and I'm not sure we should), then the user manual
should also be updated, and NEWS should have an entry about the
change.

Thanks.



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-07 14:48         ` Eli Zaretskii
@ 2017-05-09 22:02           ` Juri Linkov
  0 siblings, 0 replies; 11+ messages in thread
From: Juri Linkov @ 2017-05-09 22:02 UTC (permalink / raw)
  To: Eli Zaretskii; +Cc: netjune, emacs-devel, tino.calancha, zhanghj

>> >> 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.
>
> If this is accepted (and I'm not sure we should), then the user manual
> should also be updated, and NEWS should have an entry about the
> change.

Some other regexp engines support the \0 format as well:
http://www.regular-expressions.info/refreplacebackref.html
What is important is to clearly differentiate the whole match from
back references in the documentation (i.e. unlike back references
\0 works only in replacements, not matches).



^ permalink raw reply	[flat|nested] 11+ messages in thread

* Re: query-replace-regexp: Can't use \0 in TO-STRING
  2017-05-07  3:48     ` zhanghj
@ 2017-05-19  8:13       ` Eli Zaretskii
  0 siblings, 0 replies; 11+ messages in thread
From: Eli Zaretskii @ 2017-05-19  8:13 UTC (permalink / raw)
  To: zhanghj; +Cc: netjune, emacs-devel, tino.calancha

> From: zhanghj <netjunegg@gmail.com>
> Cc: Tino Calancha <tino.calancha@gmail.com>,  emacs-devel@gnu.org
> CC: netjune@qq.com
> Date: Sun, 07 May 2017 11:48:11 +0800
> 
> Eli Zaretskii <eliz@gnu.org> writes:
> 
> > I fixed the doc string.  the manual doesn't need fixing, AFAICT, as it
> > already describes the behavior correctly.
> >
> > Thanks.
> 
> Maybe also the doc string of query-replace-regexp-eval?

Done.



^ permalink raw reply	[flat|nested] 11+ messages in thread

end of thread, other threads:[~2017-05-19  8:13 UTC | newest]

Thread overview: 11+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2017-05-06  0:48 query-replace-regexp: Can't use \0 in TO-STRING zhanghj
2017-05-06  5:04 ` Tino Calancha
2017-05-06  8:09   ` Eli Zaretskii
2017-05-06 10:34     ` Tino Calancha
2017-05-07  3:48     ` zhanghj
2017-05-19  8:13       ` Eli Zaretskii
2017-05-06 12:28   ` zhanghj
2017-05-06 12:39     ` Tino Calancha
2017-05-07  3:55       ` zhanghj
2017-05-07 14:48         ` Eli Zaretskii
2017-05-09 22:02           ` Juri Linkov

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).