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: Re: [patch] Re: regexp repacement, how to present replacement to user? Date: Wed, 31 Oct 2007 23:39:07 +0200 Message-ID: <200710312339.07601.pogonyshev@gmx.net> References: <200710131700.42100.pogonyshev@gmx.net> <87sl3syctl.fsf@jurta.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="iso-8859-15" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1193865620 13118 80.91.229.12 (31 Oct 2007 21:20:20 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Wed, 31 Oct 2007 21:20:20 +0000 (UTC) Cc: Juri Linkov To: emacs-devel@gnu.org, rms@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Wed Oct 31 22:20:23 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 1InKz1-0003mD-RF for ged-emacs-devel@m.gmane.org; Wed, 31 Oct 2007 22:20:16 +0100 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1InKys-0006eJ-3a for ged-emacs-devel@m.gmane.org; Wed, 31 Oct 2007 17:20:06 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1InKyP-0006IX-CB for emacs-devel@gnu.org; Wed, 31 Oct 2007 17:19:37 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1InKyO-0006I5-QN for emacs-devel@gnu.org; Wed, 31 Oct 2007 17:19:36 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1InKyO-0006Hy-E9 for emacs-devel@gnu.org; Wed, 31 Oct 2007 17:19:36 -0400 Original-Received: from mail.gmx.net ([213.165.64.20]) by monty-python.gnu.org with smtp (Exim 4.60) (envelope-from ) id 1InKyN-00011y-Un for emacs-devel@gnu.org; Wed, 31 Oct 2007 17:19:36 -0400 Original-Received: (qmail invoked by alias); 31 Oct 2007 21:19:34 -0000 Original-Received: from unknown (EHLO [80.94.234.230]) [80.94.234.230] by mail.gmx.net (mp036) with SMTP; 31 Oct 2007 22:19:34 +0100 X-Authenticated: #16844820 X-Provags-ID: V01U2FsdGVkX18Lqw0O/bwL523uoXHU6UeJsmZDk76EJexbQI6ZhV 9O2HzS1myk6Xm+ User-Agent: KMail/1.7.2 In-Reply-To: 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:82256 Archived-At: Richard Stallman wrote: > I agree that a key to toggle between the two modes is not necessary, > but we should have an option to turn this feature off for users who > like the old behavior. > > I have no objection to that option. OK, here is a stab at making a patch. I'm ommiting doc changes, since this is most likely not going to be final version anyway. I changed the new function to accept all arguments `replace-match' does and removed the `-no-properties' variant, as Juri Linkov suggested. It probably makes sense to make the replacement text in the `query-regexp-replace' prompt stand out, by using quotes or a text property. However, I didn't implement this as current implementation doesn't. Paul 2007-10-31 Paul Pogonyshev * replace.el (query-replace-substitute-replacement): New defcustom. (perform-replace): Use `match-substitute-replacement' if `query-replace-substitute-replacement' is non-nil. 2007-10-31 David Kastrup * subr.el (match-substitute-replacement): New functions. 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 31 Oct 2007 21:17:37 -0000 @@ -69,6 +69,12 @@ :group 'matching :version "22.1") +(defcustom query-replace-substitute-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-substitute-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 31 Oct 2007 21:17:37 -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.