From mboxrd@z Thu Jan 1 00:00:00 1970 Path: main.gmane.org!not-for-mail From: David Kastrup Newsgroups: gmane.emacs.devel Subject: Re: query-replace-interactive not documented Date: 16 Jun 2004 16:35:18 +0200 Sender: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Message-ID: References: <20040528.181649.25475113.wl@gnu.org> <200405291737.i4THbPJ06689@raven.dms.auburn.edu> <873c5jug73.fsf@mail.jurta.org> <87oenqa4lu.fsf@mail.jurta.org> <873c51w5rq.fsf@mail.jurta.org> <87659snbd8.fsf@mail.jurta.org> <87llin98sc.fsf@mail.jurta.org> NNTP-Posting-Host: deer.gmane.org Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" X-Trace: sea.gmane.org 1087396565 25028 80.91.224.253 (16 Jun 2004 14:36:05 GMT) X-Complaints-To: usenet@sea.gmane.org NNTP-Posting-Date: Wed, 16 Jun 2004 14:36:05 +0000 (UTC) Cc: Juri Linkov , schwab@suse.de, emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Wed Jun 16 16:35:54 2004 Return-path: Original-Received: from quimby.gnus.org ([80.91.224.244]) by deer.gmane.org with esmtp (Exim 3.35 #1 (Debian)) id 1BabW6-0003Ok-00 for ; Wed, 16 Jun 2004 16:35:54 +0200 Original-Received: from lists.gnu.org ([199.232.76.165]) by quimby.gnus.org with esmtp (Exim 3.35 #1 (Debian)) id 1BabW6-0003TJ-00 for ; Wed, 16 Jun 2004 16:35:54 +0200 Original-Received: from localhost ([127.0.0.1] helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BabX5-000813-K5 for emacs-devel@quimby.gnus.org; Wed, 16 Jun 2004 10:36:55 -0400 Original-Received: from mailman by lists.gnu.org with tmda-scanned (Exim 4.33) id 1BabWx-0007yO-Nl for emacs-devel@gnu.org; Wed, 16 Jun 2004 10:36:47 -0400 Original-Received: from exim by lists.gnu.org with spam-scanned (Exim 4.33) id 1BabWw-0007xv-TV for emacs-devel@gnu.org; Wed, 16 Jun 2004 10:36:47 -0400 Original-Received: from [199.232.76.173] (helo=monty-python.gnu.org) by lists.gnu.org with esmtp (Exim 4.33) id 1BabWw-0007xW-Oc for emacs-devel@gnu.org; Wed, 16 Jun 2004 10:36:46 -0400 Original-Received: from [199.232.76.164] (helo=fencepost.gnu.org) by monty-python.gnu.org with esmtp (Exim 4.34) id 1BabVZ-0008TE-Ne for emacs-devel@gnu.org; Wed, 16 Jun 2004 10:35:21 -0400 Original-Received: from localhost ([127.0.0.1] helo=lola.goethe.zz) by fencepost.gnu.org with esmtp (Exim 4.34) id 1BabVY-0000Fg-GR; Wed, 16 Jun 2004 10:35:21 -0400 Original-To: storm@cua.dk (Kim F. Storm) In-Reply-To: Original-Lines: 40 User-Agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.4 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+emacs-devel=quimby.gnus.org@gnu.org Xref: main.gmane.org gmane.emacs.devel:25033 X-Report-Spam: http://spam.gmane.org/gmane.emacs.devel:25033 --=-=-= David Kastrup writes: > Just working on a newer version right now. Ok, here is my current version (apart from documentation which needs to get added to several places, of course). I've stolen some improvements from Juri in the course, but the pleasure's mutual. I have not added the \? proposal yet as I feel it of dubious utility: without a proper prompt string, it will probably feel unconvenient to use in particular where several \? occur, and I don't see a good syntax for prompt strings: mandating them to be LF-ended would require C-q C-j even when you don't want them after all, and you can easily do them with \,(read-string "My prompt") anyway. Maybe a fixed space-terminated (not-escapable) prompt would do, like \? xxx to get no particular prompt string and \?veracity xxx to get "veracity" as a prompt. But I really don't like that too much. --=-=-= Content-Type: text/x-patch Content-Disposition: attachment --- replace.el 10 Jun 2004 09:36:09 +0200 1.172 +++ replace.el 16 Jun 2004 15:26:16 +0200 @@ -165,16 +165,46 @@ (interactive (let ((common (query-replace-read-args "Query replace regexp" t))) - (list (nth 0 common) (nth 1 common) (nth 2 common) - ;; These are done separately here - ;; so that command-history will record these expressions - ;; rather than the values they had this time. - (if (and transient-mark-mode mark-active) - (region-beginning)) - (if (and transient-mark-mode mark-active) - (region-end))))) - + (list + (nth 0 common) + (if (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" + (nth 1 common)) + (let ((to-string (nth 1 common)) pos to-expr char) + (while (string-match + "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" + to-string) + (setq pos (match-end 0)) + (push (substring to-string 0 (- pos 2)) to-expr) + (setq char (aref to-string (1- pos)) + to-string (substring to-string pos)) + (cond ((eq char ?\#) + (push '(number-to-string replace-count) to-expr)) + ((eq char ?\,) + (setq pos (read-from-string to-string)) + (push `(replace-quote ,(pop pos)) to-expr) + (setq to-string + (substring to-string + (if (and (< pos (length to-string)) + (eq (aref to-string pos) ?\ )) + (1+ pos) + pos)))))) + (setq to-expr (delete "" (nreverse (cons to-string to-expr)))) + (replace-match-string-symbols to-expr) + (cons 'replace-eval-replacement + (if (> (length to-expr) 1) + (cons 'concat to-expr) + (car to-expr)))) + (nth 1 common)) + (nth 2 common) + ;; These are done separately here + ;; so that command-history will record these expressions + ;; rather than the values they had this time. + (if (and transient-mark-mode mark-active) + (region-beginning)) + (if (and transient-mark-mode mark-active) + (region-end))))) (perform-replace regexp to-string t t delimited nil nil start end)) + (define-key esc-map [?\C-%] 'query-replace-regexp) (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end) @@ -1012,6 +1042,7 @@ #N (string-to-number (match-string N)) & (match-string 0) #& (string-to-number (match-string 0)) +# replace-count Note that these symbols must be preceeded by a backslash in order to type them." @@ -1031,7 +1062,9 @@ ((string= "&" name) (setcar n '(match-string 0))) ((string= "#&" name) - (setcar n '(string-to-number (match-string 0)))))))) + (setcar n '(string-to-number (match-string 0)))) + ((string= "#" name) + (setcar n 'replace-count)))))) (setq n (cdr n)))) (defun replace-eval-replacement (expression replace-count) @@ -1040,6 +1073,21 @@ replacement (prin1-to-string replacement t)))) +(defun replace-quote (replacement) + "Quote a replacement string. +This just doubles all backslashes in REPLACEMENT and +returns the resulting string. If REPLACEMENT is not +a string, it is first passed through `prin1-to-string' +with the `noescape' argument set. + +`match-data' is preserved across the call." + (save-match-data + (replace-regexp-in-string "\\\\" "\\\\" + (if (stringp replacement) + replacement + (prin1-to-string replacement t)) + t t))) + (defun replace-loop-through-replacements (data replace-count) ;; DATA is a vector contaning the following values: ;; 0 next-rotate-count --=-=-= Since the change involved only the interactive usage, and apart from \# as a symbol in replacement expressions only makes hitherto illegal input legal, it should have no effect whatsoever on code stability. I think that the functionality is user-accessible to a degree by now that it would make sense mentioning it also in the Emacs manual, in case people agree it is a worthwhile addition. Personally, I think this functionality is the greatest thing since sliced bread, but then I am obviously prejudiced. -- David Kastrup, Kriemhildstr. 15, 44793 Bochum --=-=-= Content-Type: text/plain; charset="us-ascii" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit Content-Disposition: inline _______________________________________________ Emacs-devel mailing list Emacs-devel@gnu.org http://lists.gnu.org/mailman/listinfo/emacs-devel --=-=-=--