all messages for Emacs-related lists mirrored at yhetil.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, monnier@iro.umontreal.ca
Subject: Re: [patch] Re: regexp repacement, how to present replacement to user?
Date: Sun, 21 Oct 2007 22:00:38 +0300	[thread overview]
Message-ID: <200710212200.38685.pogonyshev@gmx.net> (raw)
In-Reply-To: <E1Ijdd8-0002Yq-MF@fencepost.gnu.org>

Richard Stallman wrote:
> This code makes sense, but why do we want it?

These functions basically perform part of what `replace-match' does.
The latter substitutes replacement group references (`\N' and `\&')
and replaces match in the buffer with the result text.  Proposed
functions only generate and return that text, without modyfing the
buffer.

Currently, Emacs doesn't use this or similar functionality to my
knowledge.  However, this can be used in UI to present the user.
Especially useful if the user doesn't write regular expression and
replacement himself, but instead invokes some command that uses a
predefined regexp written by someone else.  E.g., for instance, it
would be quite cryptic if a user saw

    Replace \\([[:digit:]]+\\)\\.\\([[:digit:]]+\\) with \1,\2 (decimal comma)?

since he doesn't really care how this works internally at all.
Instead, seeing

    Replace 12.345 with 12,345 (decimal comma)?

is more understandable.  (This is an example of partly hypotetical
Elisp package standardizing numbers to use locale-specific comma
separator.)

> If David Kastrup wrote the code, please put his name in the header.

Done.

> Before installing this code, if we want to install it, we would need a
> NEWS entry and changes for the Lisp manual.

Done.


lisp ChangeLog entry:
2007-10-21  David Kastrup  <dak@gnu.org>

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

doc/lispref ChangeLog entry:
2007-10-21  Paul Pogonyshev  <pogonyshev@gmx.net>

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

etc/NEWS entry:
** Two new functions `match-substitute-replacement' and
`match-substitute-replacement-no-properties' return the result of
`replace-match' without actually using it in the buffer.


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	21 Oct 2007 18:27:50 -0000
@@ -1260,6 +1260,22 @@
 just the text that matched @samp{\(ba*r\)}.
 @end defun
 
+@defun match-substitute-replacement replacement &optional fixedcase 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} and @var{subexp} have the same meaning as for
+@code{replace-match}.
+@end defun
+
+@defun match-substitute-replacement-no-properties replacement &optional fixedcase subexp
+This is like @code{match-substitute-replacement}, except that it
+returns text without any properties, just the characters themselves.
+@xref{Text Properties}.
+@end defun
+
 @node Simple Match Data
 @subsection Simple Match Data Access
 
Index: lisp/subr.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/subr.el,v
retrieving revision 1.564
diff -u -r1.564 subr.el
--- lisp/subr.el	29 Aug 2007 05:28:07 -0000	1.564
+++ lisp/subr.el	21 Oct 2007 18:27:54 -0000
@@ -2710,6 +2710,36 @@
 	(buffer-substring-no-properties (match-beginning num)
 					(match-end num)))))
 
+
+(defun match-substitute-replacement (replacement &optional fixedcase 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 abd SUBEXP have the same meaning as for
+`replace-match'."
+  (let ((match (match-string 0)))
+    (save-match-data
+      (set-match-data (mapcar (lambda (x)
+				(if (numberp x)
+				    (- x (match-beginning 0))
+				  x))
+			      (match-data t)))
+      (replace-match replacement fixedcase nil match subexp))))
+
+(defun match-substitute-replacement-no-properties (replacement
+						   &optional fixedcase subexp)
+  "Return REPLACEMENT as it will be inserted by `replace-match', without text properties.
+See `match-substitute-replacement' for details."
+  (let ((match (match-string-no-properties 0)))
+    (save-match-data
+      (set-match-data (mapcar (lambda (x)
+				(if (numberp x)
+				    (- x (match-beginning 0))
+				  x))
+			      (match-data t)))
+      (replace-match replacement fixedcase nil 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-10-21 19:00 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 [this message]
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
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

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

  git send-email \
    --in-reply-to=200710212200.38685.pogonyshev@gmx.net \
    --to=pogonyshev@gmx.net \
    --cc=emacs-devel@gnu.org \
    --cc=juri@jurta.org \
    --cc=monnier@iro.umontreal.ca \
    --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 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.