From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: "Drew Adams" Newsgroups: gmane.emacs.devel Subject: RE: general perform-replace REPLACEMENTS arg for regexpquery-replacement? Date: Sat, 15 Nov 2008 14:31:30 -0800 Message-ID: <005b01c94771$e8129be0$0200a8c0@us.oracle.com> References: <005901c94752$06ca6e70$0200a8c0@us.oracle.com> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset="us-ascii" Content-Transfer-Encoding: 7bit X-Trace: ger.gmane.org 1226788309 4904 80.91.229.12 (15 Nov 2008 22:31:49 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Sat, 15 Nov 2008 22:31:49 +0000 (UTC) To: "'Drew Adams'" , Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Sat Nov 15 23:32:51 2008 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 1L1Th7-0005Rm-IK for ged-emacs-devel@m.gmane.org; Sat, 15 Nov 2008 23:32:45 +0100 Original-Received: from localhost ([127.0.0.1]:56407 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L1Tfz-000601-9s for ged-emacs-devel@m.gmane.org; Sat, 15 Nov 2008 17:31:35 -0500 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.43) id 1L1Tfu-0005zd-EI for emacs-devel@gnu.org; Sat, 15 Nov 2008 17:31:30 -0500 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.43) id 1L1Tfr-0005zR-Rz for emacs-devel@gnu.org; Sat, 15 Nov 2008 17:31:28 -0500 Original-Received: from [199.232.76.173] (port=56393 helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.43) id 1L1Tfr-0005zO-KK for emacs-devel@gnu.org; Sat, 15 Nov 2008 17:31:27 -0500 Original-Received: from rcsinet13.oracle.com ([148.87.113.125]:24575 helo=rgminet13.oracle.com) by monty-python.gnu.org with esmtps (TLS-1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.60) (envelope-from ) id 1L1Tfr-0005cx-AP for emacs-devel@gnu.org; Sat, 15 Nov 2008 17:31:27 -0500 Original-Received: from acsinet13.oracle.com (acsinet13.oracle.com [141.146.126.235]) by rgminet13.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id mAFMVcEg005679 (version=TLSv1/SSLv3 cipher=DHE-RSA-AES256-SHA bits=256 verify=OK) for ; Sat, 15 Nov 2008 22:31:40 GMT Original-Received: from acsmt702.oracle.com (acsmt702.oracle.com [141.146.40.80]) by acsinet13.oracle.com (Switch-3.3.1/Switch-3.3.1) with ESMTP id mAFMVYUY031395 for ; Sat, 15 Nov 2008 22:31:36 GMT Original-Received: from dradamslap1 (/24.23.165.218) by default (Oracle Beehive Gateway v4.0) with ESMTP ; Sat, 15 Nov 2008 14:31:16 -0800 X-Mailer: Microsoft Office Outlook 11 In-Reply-To: <005901c94752$06ca6e70$0200a8c0@us.oracle.com> Thread-Index: AclHUgaffUr+N2kfSzuWjkMvFwmuWAAGdMxQ X-MimeOLE: Produced By Microsoft MimeOLE V6.00.2900.3350 X-Source-IP: acsmt702.oracle.com [141.146.40.80] X-Auth-Type: Internal IP X-CT-RefId: str=0001.0A090208.491F4DBA.0063:SCFSTAT928724,ss=1,fgs=0 X-detected-operating-system: by monty-python.gnu.org: GNU/Linux 2.6 (newer, 1) 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:105709 Archived-At: > M-: (query-replace-regexp "\\$\\$" '("\\\\[" "\\\\]")) > > But AFAICT, you cannot use `query-replace-regexp' > interactively to do this. You > cannot, for instance, do this to get the same effect: > > C-M-% RET \$\$ RET \, '("\\\\[" "\\\\]") > > That just replaces "$$" by the string "(\\[ \\])". What's needed, I think, is some new char sequnce, e.g. `\@', that signifies that what follows it should be taken as a list of replacement strings, not converted to a single parenthesized replacement string as `\,' does. Whether such a construct should be available at all levels recursively (as is the case for `\,') or just at the top level, I don't know. But I suspect only top level makes sense, given what `perform-replace' expects. Whether such a construct should be robust enough to treat not just a literal list of regexp strings but an arbitrary sexp that evals to such a list, I don't know. Here is a naive definition that lets `C-M-%' work for a literal list of strings at top level, such as in the example above (with `\,' replaced by `\@'): C-M-% RET \$\$ RET \@("\\\\[" "\\\\]") The only change to the existing code is to add the outermost `if' and the first `if' clause. The rest is unchanged. (defun query-replace-compile-replacement (to regexp-flag) "Maybe convert a regexp replacement TO to Lisp. Returns a list suitable for `perform-replace' if necessary, the original string if not." (if (and regexp-flag (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\\ @\\((\"[^\"].*\"\\s-*\"[^\"].*\")\\)" to)) (let ((lst (car (read-from-string (substring to (match-beginning 3) (match-end 3)))))) (mapcar (lambda (x) (query-replace-compile-replacement x regexp-flag)) lst)) (if (and regexp-flag (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to)) (let (pos list char) (while (progn (setq pos (match-end 0)) (push (substring to 0 (- pos 2)) list) (setq char (aref to (1- pos)) to (substring to pos)) (cond ((eq char ?\#) (push '(number-to-string replace-count) list)) ((eq char ?\,) (setq pos (read-from-string to)) (push `(replace-quote ,(car pos)) list) (let ((end ;; Swallow a space after a symbol ;; if there is a space. (if (and (or (symbolp (car pos)) ;; Swallow a space after 'foo ;; but not after (quote foo). (and (eq (car-safe (car pos)) 'quote) (not (= ?\( (aref to 0))))) (eq (string-match " " to (cdr pos)) (cdr pos))) (1+ (cdr pos)) (cdr pos)))) (setq to (substring to end))))) (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))) (setq to (nreverse (delete "" (cons to list)))) (replace-match-string-symbols to) (cons 'replace-eval-replacement (if (cdr to) (cons 'concat to) (car to))))) to)) Perhaps this is good enough as is? Or perhaps someone more familiar with the existing code can improve it. Some possible improvements were mentioned above. If people try this and think it is OK as is, I can submit a patch.