unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Paul Pogonyshev <pogonyshev@gmx.net>
To: emacs-devel@gnu.org, rms@gnu.org
Cc: juri@jurta.org
Subject: Re: [patch] Re: regexp repacement, how to present replacement to user?
Date: Fri, 2 Nov 2007 22:58:54 +0200	[thread overview]
Message-ID: <200711022258.55262.pogonyshev@gmx.net> (raw)
In-Reply-To: <E1InUXH-0006WP-Fb@fencepost.gnu.org>

Richard Stallman wrote:
> 	    * replace.el (query-replace-substitute-replacement): New
> 	    defcustom.
> 
> That name is rather long, so please call it
> `query-replace-show-replacement'.  (That is also a clearer
> description of what the feature does.)
> 
> Other than that, it looks good to me.  But please write the 
> changes for NEWS and the manuals before installing this.

Here is a patch.  I don't have CVS write access, so I'd be grateful
if someone installs this.

Paul


/etc/NEWS:
** C-M-% now shows replacement as it would look in the buffer, with
`\N' and `\&' substituted according to the match.  Old behavior can be
restored by customizing `query-replace-show-replacement'.

** New function `match-substitute-replacement' returns the result of
`replace-match' without actually using it in the buffer.

doc/lispref/ChangeLog:
2007-11-02  Paul Pogonyshev  <pogonyshev@gmx.net>

	* searching.texi (Replacing Match): Describe new
	`match-substitute-replacement' and
	`match-substitute-replacement-no-properties'.

doc/emacs/ChangeLog:
2007-11-02  Paul Pogonyshev  <pogonyshev@gmx.net>

	* search.texi (Query Replace): Mention
	`query-replace-show-replacement'.

lisp/ChangeLog:
2007-11-02  Paul Pogonyshev  <pogonyshev@gmx.net>

	* replace.el (query-replace-show-replacement): New defcustom.
	(perform-replace): Use `match-substitute-replacement' if
	`query-replace-show-replacement' is non-nil.

2007-11-02  David Kastrup  <dak@gnu.org>

	* subr.el (match-substitute-replacement): New functions.


Index: doc/lispref/searching.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/doc/lispref/searching.texi,v
retrieving revision 1.2
diff -u -r1.2 searching.texi
--- doc/lispref/searching.texi	6 Sep 2007 04:27:40 -0000	1.2
+++ doc/lispref/searching.texi	2 Nov 2007 20:32:36 -0000
@@ -1260,6 +1260,16 @@
 just the text that matched @samp{\(ba*r\)}.
 @end defun
 
+@defun match-substitute-replacement replacement &optional fixedcase literal string subexp
+This function returns the text that would be inserted into the buffer
+by @code{replace-match}, but without modifying the buffer.  It is
+useful if you want to present the user with actual replacement result,
+with constructs like @samp{\@var{n}} or @samp{\&} substituted with
+matched groups.  Arguments @var{replacement} and optional
+@var{fixedcase}, @var{literal}, @var{string} and @var{subexp} have the
+same meaning as for @code{replace-match}.
+@end defun
+
 @node Simple Match Data
 @subsection Simple Match Data Access
 
Index: doc/emacs/search.texi
===================================================================
RCS file: /cvsroot/emacs/emacs/doc/emacs/search.texi,v
retrieving revision 1.1
diff -u -r1.1 search.texi
--- doc/emacs/search.texi	6 Sep 2007 04:48:32 -0000	1.1
+++ doc/emacs/search.texi	2 Nov 2007 20:32:36 -0000
@@ -1172,7 +1172,11 @@
   These commands highlight the current match using the face
 @code{query-replace}.  They highlight other matches using
 @code{lazy-highlight} just like incremental search (@pxref{Incremental
-Search}).
+Search}).  By default, @code{query-replace-regexp} will show
+substituted replacement string for the current match in the
+minibuffer.  If you want to keep special sequences @samp{\&} and
+@samp{\@var{n}} unexpanded, customize
+@code{query-replace-show-replacement} variable.
 
   The characters you can type when you are shown a match for the string
 or regexp are:
Index: lisp/replace.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/replace.el,v
retrieving revision 1.259
diff -u -r1.259 replace.el
--- lisp/replace.el	26 Jul 2007 05:26:32 -0000	1.259
+++ lisp/replace.el	2 Nov 2007 20:32:36 -0000
@@ -69,6 +69,12 @@
   :group 'matching
   :version "22.1")
 
+(defcustom query-replace-show-replacement t
+  "*Non-nil means to show what actual replacement text will be."
+  :type 'boolean
+  :group 'matching
+  :version "23.1")
+
 (defcustom query-replace-highlight t
   "*Non-nil means to highlight matches during query replacement."
   :type 'boolean
@@ -1570,10 +1576,17 @@
 		   (or delimited-flag regexp-flag) case-fold-search)
 		  ;; Bind message-log-max so we don't fill up the message log
 		  ;; with a bunch of identical messages.
-		  (let ((message-log-max nil))
+		  (let ((message-log-max nil)
+			(replacement-presentation
+			 (if query-replace-show-replacement
+			     (save-match-data
+			       (set-match-data real-match-data)
+			       (match-substitute-replacement next-replacement
+							     nocasify literal))
+			   next-replacement)))
 		    (message message
                              (query-replace-descr from-string)
-                             (query-replace-descr next-replacement)))
+                             (query-replace-descr replacement-presentation)))
 		  (setq key (read-event))
 		  ;; Necessary in case something happens during read-event
 		  ;; that clobbers the match data.
Index: lisp/subr.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/subr.el,v
retrieving revision 1.565
diff -u -r1.565 subr.el
--- lisp/subr.el	27 Oct 2007 09:07:15 -0000	1.565
+++ lisp/subr.el	2 Nov 2007 20:32:39 -0000
@@ -2709,6 +2709,24 @@
 	(buffer-substring-no-properties (match-beginning num)
 					(match-end num)))))
 
+
+(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'
+are substituted with actual strings matched by the last search.
+Optional FIXEDCASE, LITERAL, STRING and SUBEXP have the same
+meaning as for `replace-match'."
+  (let ((match (match-string 0 string)))
+    (save-match-data
+      (set-match-data (mapcar (lambda (x)
+				(if (numberp x)
+				    (- x (match-beginning 0))
+				  x))
+			      (match-data t)))
+      (replace-match replacement fixedcase literal match subexp))))
+
+
 (defun looking-back (regexp &optional limit greedy)
   "Return non-nil if text before point matches regular expression REGEXP.
 Like `looking-at' except matches before point, and is slower.

  reply	other threads:[~2007-11-02 20:58 UTC|newest]

Thread overview: 30+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2007-10-13 14:00 regexp repacement, how to present replacement to user? Paul Pogonyshev
2007-10-14 22:50 ` Juri Linkov
2007-10-15  3:20   ` Stefan Monnier
2007-10-15  5:57     ` David Kastrup
2007-10-15 20:28       ` Paul Pogonyshev
2007-10-16 23:54         ` Juri Linkov
2007-10-17  6:06           ` David Kastrup
2007-10-17 19:25           ` Paul Pogonyshev
2007-10-20 13:53             ` [patch] " Paul Pogonyshev
2007-10-21 16:26               ` Richard Stallman
2007-10-21 19:00                 ` Paul Pogonyshev
2007-10-21 20:43                   ` Juri Linkov
2007-10-21 21:15                     ` Paul Pogonyshev
2007-10-22  1:22                   ` Stefan Monnier
2007-10-26  3:48                     ` Richard Stallman
2007-10-28 14:41                       ` Paul Pogonyshev
2007-10-29  9:22                         ` Richard Stallman
2007-10-30 14:16                           ` Juri Linkov
2007-10-31  7:46                             ` Richard Stallman
2007-10-31 21:39                               ` Paul Pogonyshev
2007-11-01  7:32                                 ` Richard Stallman
2007-11-02 20:58                                   ` Paul Pogonyshev [this message]
2007-11-10 15:01                                     ` Paul Pogonyshev
2007-11-11  5:21                                       ` Richard Stallman
2007-11-10 21:54                                     ` Juri Linkov
2007-10-23  7:12                   ` Richard Stallman
2007-10-23  8:16                     ` David Kastrup
2007-10-23 17:53                       ` Richard Stallman
2007-10-23 19:03                         ` Lennart Borgman (gmail)
2007-10-24  8:32                           ` Richard Stallman

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

  List information: https://www.gnu.org/software/emacs/

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

  git send-email \
    --in-reply-to=200711022258.55262.pogonyshev@gmx.net \
    --to=pogonyshev@gmx.net \
    --cc=emacs-devel@gnu.org \
    --cc=juri@jurta.org \
    --cc=rms@gnu.org \
    /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 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).