emacs-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[patch] Re: regexp repacement, how to present replacement to user?


From: Paul Pogonyshev
Subject: [patch] Re: regexp repacement, how to present replacement to user?
Date: Sat, 20 Oct 2007 16:53:57 +0300
User-agent: KMail/1.7.2

I wrote:
> That's why I think this (replacement expansion with back-references) must
> be a function inside Emacs and not written in a package.

Here is a patch.  It adds two (actually one, other is just sugar) function.
Thanks to David Kastrup for the code.


2007-10-20  Paul Pogonyshev  <address@hidden>

        * subr.el (match-substitute-replacement)
        (match-substitute-replacement-no-properties): New
        functions (code mostly by David Kastrup).


*** lisp/subr.el.~1.564.~       2007-08-30 22:17:43.000000000 +0300
--- lisp/subr.el        2007-10-20 16:48:21.709573376 +0300
***************
*** 2710,2715 ****
--- 2710,2745 ----
        (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 via email to

[Prev in Thread] Current Thread [Next in Thread]