From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Paul Pogonyshev Newsgroups: gmane.emacs.devel Subject: [patch] Re: regexp repacement, how to present replacement to user? Date: Sat, 20 Oct 2007 16:53:57 +0300 Message-ID: <200710201653.57778.pogonyshev@gmx.net> References: <200710131700.42100.pogonyshev@gmx.net> <87lka2d3id.fsf@jurta.org> <200710172225.33511.pogonyshev@gmx.net> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-1" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1192919224 23181 80.91.229.12 (20 Oct 2007 22:27:04 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 20 Oct 2007 22:27:04 +0000 (UTC) Cc: Juri Linkov , monnier@iro.umontreal.ca To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sun Oct 21 00:27:04 2007 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([199.232.76.165]) by lo.gmane.org with esmtp (Exim 4.50) id 1IjMmd-0005J7-S6 for ged-emacs-devel@m.gmane.org; Sun, 21 Oct 2007 00:27:04 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IjMmW-0005Ur-CV for ged-emacs-devel@m.gmane.org; Sat, 20 Oct 2007 18:26:56 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1IjMlt-0005GP-Ia for emacs-devel@gnu.org; Sat, 20 Oct 2007 18:26:17 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1IjMlr-0005Fh-TC for emacs-devel@gnu.org; Sat, 20 Oct 2007 18:26:17 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1IjMlr-0005FZ-Hc for emacs-devel@gnu.org; Sat, 20 Oct 2007 18:26:15 -0400 Original-Received: from mail.gmx.net ([213.165.64.20]) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1IjMlq-0007v0-6M for emacs-devel@gnu.org; Sat, 20 Oct 2007 18:26:14 -0400 Original-Received: (qmail invoked by alias); 20 Oct 2007 22:26:12 -0000 Original-Received: from unknown (EHLO [80.94.230.24]) [80.94.230.24] by mail.gmx.net (mp051) with SMTP; 21 Oct 2007 00:26:12 +0200 X-Authenticated: #16844820 X-Provags-ID: V01U2FsdGVkX1+VS5o4M/WVI19H14rNzDQIoq6Y4AB53HCnSJ6tGn 61KHlahL7A/HXv User-Agent: KMail/1.7.2 In-Reply-To: <200710172225.33511.pogonyshev@gmx.net> Content-Disposition: inline X-Y-GMX-Trusted: 0 X-detected-kernel: by monty-python.gnu.org: Linux 2.6, seldom 2.4 (older, 4) X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:81317 Archived-At: 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 * 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.