--- replace.el 10 Jun 2004 09:36:09 +0200 1.172 +++ replace.el 16 Jun 2004 03:01:16 +0200 @@ -165,16 +165,44 @@ (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 (match-end 0)) to-expr) + (while pos + (when (> pos 2) + (push (substring to-string 0 (- pos 2)) to-expr)) + (setq pos (read-from-string to-string pos)) + (push `(replace-requote ,(pop pos)) to-expr) + (setq to-string (and (< pos (length to-string)) + (substring to-string + (if (eq (aref to-string pos) ?') + (1+ pos) + pos))) + pos (and + to-string + (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\`" + to-string) + (match-end 0)))) + (unless (zerop (length to-string)) + (push to-string to-expr)) + (replace-match-string-symbols to-expr) + (cons 'replace-eval-replacement + (if (> (length to-expr) 1) + (cons 'concat (nreverse 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) @@ -1040,6 +1068,14 @@ replacement (prin1-to-string replacement t)))) +(defun replace-requote (replacement) + (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