all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: zhanghj <netjunegg@gmail.com>
To: Tino Calancha <tino.calancha@gmail.com>
Cc: netjune@qq.com, Emacs developers <emacs-devel@gnu.org>
Subject: Re: query-replace-regexp: Can't use \0 in TO-STRING
Date: Sun, 07 May 2017 11:55:15 +0800	[thread overview]
Message-ID: <87bmr5micc.fsf@gmail.com> (raw)
In-Reply-To: <alpine.DEB.2.20.1705062134410.29811@calancha-pc> (Tino Calancha's message of "Sat, 6 May 2017 21:39:25 +0900 (JST)")

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)
 		{



  reply	other threads:[~2017-05-07  3:55 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
2017-05-07 14:48         ` Eli Zaretskii
2017-05-09 22:02           ` Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87bmr5micc.fsf@gmail.com \
    --to=netjunegg@gmail.com \
    --cc=emacs-devel@gnu.org \
    --cc=netjune@qq.com \
    --cc=tino.calancha@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

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

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.