all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: David Kastrup <dak@gnu.org>
Cc: Juri Linkov <juri@jurta.org>, schwab@suse.de, emacs-devel@gnu.org
Subject: Re: query-replace-interactive not documented
Date: 17 Jun 2004 02:56:37 +0200	[thread overview]
Message-ID: <x5pt7zatoa.fsf@lola.goethe.zz> (raw)
In-Reply-To: <m3isdrjg0l.fsf@kfs-l.imdomain.dk>

[-- Attachment #1: Type: text/plain, Size: 1329 bytes --]

storm@cua.dk (Kim F. Storm) writes:

> David Kastrup <dak@gnu.org> writes:
> 
> > Juri Linkov <juri@jurta.org> writes:
> > 
> > > It is not important what question it asks.  It would work even for
> > > two or three different \? in one replacement string.  In rare cases
> > > where the user needs more \?, he can use \,(read-string "My prompt:
> > > ").
> > > 
> > > Or to let it to automatically enumerate prompt strings, i.e. to have
> > > a counter in expression generating code in `query-replace-regexp'
> > > that will convert several \? in the same string into prompts in a
> > > fixed format with the prompt counter added to the prompt string:
> > > 
> > > (concat (read-string "Enter string 1: ") (read-string "Enter string
> > > 2: "))
> > 
> > Yes, I might do something like that.  
> 
> Maybe "Enter string: " for the first one, "Enter string 2:" etc for the
> following strings.

Ouch, I just realized that the dialog will basically be:

Enter string: xsxa RET
Query replace gfag with fdsfxsxa* (y/n)?

That's so weird that I don't really know if it should be a "feature"
with its own shortcut.  Entering the strings when they might not even
get used...

I'll just post the current untested diff (which might or might not
work) since I'm about to fall asleep.  In case it works, people have
something to play with.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 5171 bytes --]

--- replace.el	10 Jun 2004 09:36:09 +0200	1.172
+++ replace.el	17 Jun 2004 02:16:46 +0200	
@@ -81,14 +81,15 @@
 					 query-replace-from-history-variable
 					 nil t)))
       ;; Warn if user types \n or \t, but don't reject the input.
-      (if (string-match "\\\\[nt]" from)
-	  (let ((match (match-string 0 from)))
-	    (cond
-	     ((string= match "\\n")
-	      (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
-	     ((string= match "\\t")
-	      (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
-	    (sit-for 2))))
+      (and regexp-flag
+	   (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\)*\\(\\\\[nt]\\)" from)
+	   (let ((match (match-string 3 from)))
+	     (cond
+	      ((string= match "\\n")
+	       (message "Note: `\\n' here doesn't match a newline; to do that, type C-q C-j instead"))
+	      ((string= match "\\t")
+	       (message "Note: `\\t' here doesn't match a tab; to do that, just type TAB")))
+	     (sit-for 2))))
 
     (save-excursion
       (setq to (read-from-minibuffer (format "%s %s with: " string from)
@@ -165,18 +166,65 @@
   (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 prompt)
+	    (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))))
+		    ((eq char ?\?)
+		     (push `(replace-quote
+			     (replace-read-replacement
+			      ,(if prompt
+				   (progn
+				     (setq prompt (1+ prompt))
+				     (format "Replacement %d: " prompt))
+				 (setq prompt 1)
+				 "Replacement: ")))
+			   to-expr))))
+	    (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 replace-read-replacement (prompt)
+  "Read a replacement string with PROMPT."
+  (read-from-minibuffer
+   prompt
+   nil nil t query-replace-to-history-variable
+   nil t))
+
 (defun query-replace-regexp-eval (regexp to-expr &optional delimited start end)
   "Replace some things after point matching REGEXP with the result of TO-EXPR.
 As each match is found, the user must type a character saying
@@ -1012,6 +1060,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 +1080,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 +1091,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

[-- Attachment #3: Type: text/plain, Size: 52 bytes --]



-- 
David Kastrup, Kriemhildstr. 15, 44793 Bochum

[-- Attachment #4: Type: text/plain, Size: 142 bytes --]

_______________________________________________
Emacs-devel mailing list
Emacs-devel@gnu.org
http://lists.gnu.org/mailman/listinfo/emacs-devel

  reply	other threads:[~2004-06-17  0:56 UTC|newest]

Thread overview: 101+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-28 16:16 query-replace-interactive not documented Werner LEMBERG
2004-05-29 17:02 ` Richard Stallman
2004-05-29 17:37   ` Luc Teirlinck
2004-05-29 20:33     ` Juri Linkov
2004-06-11  8:19       ` Juri Linkov
2004-06-11  8:39         ` Kim F. Storm
2004-06-11  9:00         ` David Kastrup
2004-06-12  8:21           ` Juri Linkov
2004-06-12  1:50         ` Richard Stallman
2004-06-12  8:16           ` Juri Linkov
2004-06-13  0:01             ` Richard Stallman
2004-06-13  0:46               ` Miles Bader
2004-06-13  9:03               ` David Kastrup
2004-06-14 18:50                 ` Richard Stallman
2004-06-14 20:49                   ` Kim F. Storm
2004-06-14 21:20                     ` David Kastrup
2004-06-15 14:29                       ` Juri Linkov
2004-06-15 15:43                         ` David Kastrup
2004-06-15 18:17                           ` Juri Linkov
2004-06-15 20:23                             ` David Kastrup
2004-06-15 22:30                             ` Andreas Schwab
2004-06-15 22:36                               ` David Kastrup
2004-06-15 22:43                                 ` Kim F. Storm
2004-06-15 23:13                                   ` David Kastrup
2004-06-16  1:16                                     ` David Kastrup
2004-06-16  8:08                                       ` Juri Linkov
2004-06-16  9:23                                         ` David Kastrup
2004-06-16  8:02                                   ` Juri Linkov
2004-06-17  5:06                                   ` Richard Stallman
2004-06-16  8:17                               ` Juri Linkov
2004-06-16  9:01                                 ` David Kastrup
2004-06-16 17:06                                   ` Kevin Rodgers
2004-06-16  9:02                                 ` Andreas Schwab
2004-06-16  1:41                           ` Miles Bader
2004-06-16  2:01                             ` David Kastrup
2004-06-16  2:11                               ` Miles Bader
2004-06-16 16:57                           ` Richard Stallman
2004-06-15 22:25                         ` Andreas Schwab
2004-06-15 22:28                           ` Kim F. Storm
2004-06-16  9:00                             ` Juri Linkov
2004-06-16  9:25                               ` Andreas Schwab
2004-06-16  9:32                               ` David Kastrup
2004-06-16 11:30                                 ` Kim F. Storm
2004-06-16 12:15                                   ` David Kastrup
2004-06-16 14:35                                     ` David Kastrup
2004-06-16 15:23                                       ` Juri Linkov
2004-06-16 21:15                                         ` David Kastrup
2004-06-16 22:26                                           ` Kim F. Storm
2004-06-17  0:56                                             ` David Kastrup [this message]
2004-06-17 12:14                                               ` David Kastrup
2004-06-17 13:05                                                 ` Kim F. Storm
2004-06-17 13:29                                                   ` David Kastrup
2004-06-17 14:10                                                     ` Kim F. Storm
2004-06-17 14:56                                                 ` David Kastrup
2004-06-17 15:33                                                   ` Juri Linkov
2004-06-17 17:03                                                     ` David Kastrup
2004-06-18  6:43                                                       ` Juri Linkov
2004-06-18  7:13                                                         ` David Kastrup
2004-06-16 15:27                                       ` Kim F. Storm
2004-06-16 17:28                                       ` Juri Linkov
2004-06-16 21:07                                         ` David Kastrup
2004-06-17  0:47                                           ` David Kastrup
2004-06-17 23:05                                           ` Richard Stallman
2004-06-18  6:55                                             ` Juri Linkov
2004-06-19  3:19                                               ` Richard Stallman
2004-06-19  7:00                                                 ` David Kastrup
2004-06-20 19:18                                                   ` Richard Stallman
2004-06-20 21:05                                                     ` David Kastrup
2004-06-21  9:31                                                       ` Richard Stallman
2004-06-21  9:50                                                         ` David Kastrup
2004-06-22 23:16                                                           ` Richard Stallman
2004-06-25 23:12                                                         ` Juri Linkov
2004-06-26  7:34                                                           ` David Kastrup
2004-06-26 16:18                                                             ` Juri Linkov
2004-06-27 17:41                                                           ` Richard Stallman
2004-06-21  9:31                                                       ` Richard Stallman
2004-06-21  9:38                                                         ` David Kastrup
2004-06-22 23:17                                                           ` Richard Stallman
2004-06-22 23:20                                                             ` David Kastrup
2004-06-17  5:07                                 ` Richard Stallman
2004-06-14 16:59               ` Juri Linkov
2004-06-12  8:21           ` David Kastrup
2004-05-30 19:41     ` Richard Stallman
2004-05-30 22:00       ` Luc Teirlinck
2004-06-08  6:55       ` Juri Linkov
2004-06-11  8:22         ` Juri Linkov
2004-06-12  1:50           ` Richard Stallman
2004-06-18 20:00             ` isearch hooks (was: query-replace-interactive not documented) Juri Linkov
2004-06-19  1:10               ` Miles Bader
2004-06-19 18:09                 ` isearch hooks Juri Linkov
2004-06-19  3:19               ` isearch hooks (was: query-replace-interactive not documented) Richard Stallman
2004-06-19 18:36                 ` isearch hooks Juri Linkov
2004-06-20 19:18                   ` Richard Stallman
2004-06-21 21:56                     ` Juri Linkov
2004-06-22 23:17                       ` Richard Stallman
2004-06-25 18:07                         ` Juri Linkov
2004-06-27 17:41                           ` Richard Stallman
2004-06-27 22:34                             ` Juri Linkov
2004-06-28 14:57                               ` Richard Stallman
2004-06-29  0:25               ` isearch hooks (was: query-replace-interactive not documented) Stefan
2004-06-29  1:17                 ` isearch hooks Juri Linkov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=x5pt7zatoa.fsf@lola.goethe.zz \
    --to=dak@gnu.org \
    --cc=emacs-devel@gnu.org \
    --cc=juri@jurta.org \
    --cc=schwab@suse.de \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.