[Top][All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: query-replace-interactive not documented

From: Juri Linkov
Subject: Re: query-replace-interactive not documented
Date: Tue, 08 Jun 2004 09:55:07 +0300
User-agent: Gnus/5.110002 (No Gnus v0.2) Emacs/21.3.50 (gnu/linux)

Richard Stallman <address@hidden> writes:
>     `query-replace-interactive' is not a user option.  It is defined using
>     defvar in replace.el.  Its docstring does not start with a `*' either.
>     Of course, we can not document every single defvar either.
>       Is it something one would want to set permanently to t?
> Some people might.

I doubt that people may want to set it permanently to t.
And toggling this variable on/off is very inconvenient.

However, this variable might be useful in the following situation:
it can be set temporarily to t by typing M-% in isearch mode, which
can exit isearch mode and start query-replace with isearch string.
It's natural to assume that if the user types M-% or C-M-% in isearch
mode he wants to use the current search string as FROM-STRING for
query-replace and query-replace-regexp.

I also added a new value `initial' to `query-replace-interactive' to
insert the last search string into FROM-STRING prompt as an initial
value to allow the user to modify it before performing replacements.

Index: lisp/isearch.el
RCS file: /cvsroot/emacs/emacs/lisp/isearch.el,v
retrieving revision 1.228
diff -u -w -b -r1.228 isearch.el
--- lisp/isearch.el     6 Jun 2004 13:57:39 -0000       1.228
+++ lisp/isearch.el     8 Jun 2004 03:18:36 -0000
@@ -336,6 +338,9 @@
     (define-key map "\M-r" 'isearch-toggle-regexp)
     (define-key map "\M-e" 'isearch-edit-string)
+    (define-key map    [?\M-%] 'isearch-query-replace)
+    (define-key map [?\C-\M-%] 'isearch-query-replace-regexp)
   "Keymap for `isearch-mode'.")
@@ -1047,6 +1054,23 @@
   (sit-for 1)
+(defun isearch-query-replace ()
+  "Start query-replace with string to replace from last search string."
+  (interactive)
+  (let ((query-replace-interactive 'initial))
+    (isearch-exit)
+    (if isearch-forward (goto-char isearch-other-end))
+    (call-interactively 'query-replace)))
+(defun isearch-query-replace-regexp ()
+  "Start query-replace-regexp with string to replace from last search string."
+  (interactive)
+  (let ((query-replace-interactive 'initial))
+    (isearch-exit)
+    (if isearch-forward (goto-char isearch-other-end))
+    (call-interactively 'query-replace-regexp)))
 (defun isearch-delete-char ()
   "Discard last input item and move point back.
 If no previous match was done, just beep."

Index: lisp/replace.el
RCS file: /cvsroot/emacs/emacs/lisp/replace.el,v
retrieving revision 1.171
diff -u -r1.171 replace.el
--- lisp/replace.el     30 May 2004 21:50:35 -0000      1.171
+++ lisp/replace.el     8 Jun 2004 02:58:37 -0000
@@ -38,8 +38,12 @@
 (defcustom query-replace-interactive nil
   "Non-nil means `query-replace' uses the last search string.
-That becomes the \"string to replace\"."
-  :type 'boolean
+That becomes the \"string to replace\".
+If value is `initial', the last search string is inserted into
+the minibuffer as an initial value for \"string to replace\"."
+  :type '(choice (const :tag "Off" nil)
+                 (const :tag "Initial content" initial)
+                 (other :tag "Default value" t))
   :group 'matching)
 (defcustom query-replace-from-history-variable 'query-replace-history
@@ -70,16 +74,20 @@
   (unless noerror
   (let (from to)
-    (if query-replace-interactive
+    (if (and query-replace-interactive
+             (not (eq query-replace-interactive 'initial)))
        (setq from (car (if regexp-flag regexp-search-ring search-ring)))
       ;; The save-excursion here is in case the user marks and copies
       ;; a region in order to specify the minibuffer input.
       ;; That should not clobber the region for the query-replace itself.
-       (setq from (read-from-minibuffer (format "%s: " string)
-                                        nil nil nil
-                                        query-replace-from-history-variable
-                                        nil t)))
+       (setq from (read-from-minibuffer
+                    (format "%s: " string)
+                    (if (eq query-replace-interactive 'initial)
+                        (car (if regexp-flag regexp-search-ring search-ring)))
+                    nil nil
+                    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)))

Juri Linkov

reply via email to

[Prev in Thread] Current Thread [Next in Thread]