[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: [patch] Re: regexp repacement, how to present replacement to user?
From: |
Paul Pogonyshev |
Subject: |
Re: [patch] Re: regexp repacement, how to present replacement to user? |
Date: |
Sun, 21 Oct 2007 22:00:38 +0300 |
User-agent: |
KMail/1.7.2 |
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 <address@hidden>
* subr.el (match-substitute-replacement)
(match-substitute-replacement-no-properties): New functions.
doc/lispref ChangeLog entry:
2007-10-21 Paul Pogonyshev <address@hidden>
* 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
address@hidden 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 @address@hidden or @samp{\&} substituted with
+matched groups. Arguments @var{replacement} and optional
address@hidden and @var{subexp} have the same meaning as for
address@hidden
address@hidden defun
+
address@hidden 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.
address@hidden Properties}.
address@hidden 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.
- regexp repacement, how to present replacement to user?, Paul Pogonyshev, 2007/10/13
- Re: regexp repacement, how to present replacement to user?, Juri Linkov, 2007/10/14
- Re: regexp repacement, how to present replacement to user?, Stefan Monnier, 2007/10/14
- Re: regexp repacement, how to present replacement to user?, David Kastrup, 2007/10/15
- Re: regexp repacement, how to present replacement to user?, Paul Pogonyshev, 2007/10/15
- Re: regexp repacement, how to present replacement to user?, Juri Linkov, 2007/10/16
- Re: regexp repacement, how to present replacement to user?, David Kastrup, 2007/10/17
- Re: regexp repacement, how to present replacement to user?, Paul Pogonyshev, 2007/10/17
- [patch] Re: regexp repacement, how to present replacement to user?, Paul Pogonyshev, 2007/10/20
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Richard Stallman, 2007/10/21
- Re: [patch] Re: regexp repacement, how to present replacement to user?,
Paul Pogonyshev <=
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Juri Linkov, 2007/10/21
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Paul Pogonyshev, 2007/10/21
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Stefan Monnier, 2007/10/21
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Richard Stallman, 2007/10/25
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Paul Pogonyshev, 2007/10/28
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Richard Stallman, 2007/10/29
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Juri Linkov, 2007/10/30
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Richard Stallman, 2007/10/31
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Paul Pogonyshev, 2007/10/31
- Re: [patch] Re: regexp repacement, how to present replacement to user?, Richard Stallman, 2007/10/23