all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Juri Linkov <juri@jurta.org>
Cc: emacs-devel@gnu.org
Subject: Re: improving query-replace and query-replace-regexp
Date: Sat, 03 Jul 2004 12:45:05 +0300	[thread overview]
Message-ID: <87r7rto2by.fsf@mail.jurta.org> (raw)
In-Reply-To: jwvk6ywfbf6.fsf-monnier+emacs@gnu.org

Stefan Monnier <monnier@iro.umontreal.ca> writes:
>> What do you think of making M-% and C-M-% b behave similar to C-s and
>> C-u C-s?  This is, typing it a second time continues a previous
>> query-and-replace action.  This would save a lot of time.
> [...]
> +	(setq from (read-from-minibuffer (if (null lastto)
> +					     (format "%s: " string)
> +					   (format "%s [%s -> %s]: " string

When I looked more at the arrow in the prompt in the Stefan's patch,
I thought: why not to use that arrow in the input string instead of the
prompt.  This means to use a special user-defined separator to split
one input string into two parts: from-string and to-string.  When
using this, the prompt and a sample input string might look like this:

Query replace (sep=" -> "): x -> y

The separator value in the prompt reminds the user what string is
currently used as the separator in the input string.  Users should
choose such separators which don't appear in the text to replace.

This has several benefits: it's much easier to repeat previous
replacements by moving through the history list with both from-string
and to-string inserted into the minibuffer at a time; it's possible
to change from-string value during entering the value of to-string
in the same input string...

In the following patch a new variable `query-replace-args-separator'
is nil by default which means that the current behavior with two
separate prompts is still preserved.  But if this option is set
to a string, it is used to split the input string.  It the input
string doesn't contain a separator, then the string is considered
as the from-string only, and the second prompt asks for the to-string.
 
Index: lisp/replace.el
===================================================================
RCS file: /cvsroot/emacs/emacs/lisp/replace.el,v
retrieving revision 1.180
diff -u -r1.180 replace.el
--- lisp/replace.el	3 Jul 2004 05:18:38 -0000	1.180
+++ lisp/replace.el	3 Jul 2004 09:31:43 -0000
@@ -70,6 +70,17 @@
   :group 'matching
   :version "21.4")
 
+(defcustom query-replace-args-separator nil
+  "*Non-nil value separates from-string and to-string in one input string.
+This means that after `query-replace-read-args' reads the first
+argument it splits it using the value of this variable and
+assignes the first half of the input string to the from-string
+and the second half to the to-string.  This works provided the
+separator does not appear in the text you want to replace."
+  :type 'regexp
+  :group 'matching
+  :version "21.4")
+
 (defun query-replace-read-args (string regexp-flag &optional noerror)
   (unless noerror
     (barf-if-buffer-read-only))
@@ -82,12 +93,19 @@
       ;; That should not clobber the region for the query-replace itself.
       (save-excursion
         (setq from (read-from-minibuffer
-                    (format "%s: " string)
+                    (if query-replace-args-separator
+                        (format "%s (sep=\"%s\"): "
+                                string query-replace-args-separator)
+                      (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)))
+      (if (and query-replace-args-separator
+               (string-match query-replace-args-separator from))
+          (setq to   (substring from (match-end 0))
+                from (substring from 0 (match-beginning 0))))
       ;; Warn if user types \n or \t, but don't reject the input.
       (and regexp-flag
 	   (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\(\\\\[nt]\\)" from)
@@ -99,11 +117,12 @@
 	       (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)
-                nil nil nil
-                query-replace-to-history-variable from t)))
+    (unless to
+      (save-excursion
+        (setq to (read-from-minibuffer
+                  (format "%s %s with: " string from)
+                  nil nil nil
+                  query-replace-to-history-variable from t))))
     (when (and regexp-flag
 	       (string-match "\\(\\`\\|[^\\]\\)\\(\\\\\\\\\\)*\\\\[,#]" to))
       (let (pos list char)

-- 
Juri Linkov
http://www.jurta.org/emacs/

  parent reply	other threads:[~2004-07-03  9:45 UTC|newest]

Thread overview: 61+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2004-05-28 16:20 improving query-replace and query-replace-regexp Werner LEMBERG
2004-05-28 22:20 ` Stefan Monnier
2004-05-29 17:02   ` Richard Stallman
2004-05-29 17:05     ` Werner LEMBERG
2004-05-29 17:50       ` Miles Bader
2004-05-29 20:45         ` Werner LEMBERG
2004-05-29 21:15     ` Juri Linkov
2004-05-29 21:31       ` Miles Bader
2004-05-30 20:59         ` Juri Linkov
2004-06-01 23:48           ` Stefan Monnier
     [not found]             ` < 8765aadbgb.fsf@mail.jurta.org>
2004-06-02  0:04             ` Miles Bader
2004-06-02  0:10               ` Stefan Monnier
2004-06-02  0:17                 ` Miles Bader
2004-06-02 17:37                 ` Richard Stallman
2004-06-02 17:37               ` Richard Stallman
2004-06-02  0:56             ` Juri Linkov
2004-06-02  1:48               ` Miles Bader
2004-06-02  1:58                 ` minibuffer-eldef (was: improving query-replace and query-replace-regexp) Stefan Monnier
2004-06-02  2:15                   ` minibuffer-eldef Miles Bader
2004-06-02  3:25                     ` minibuffer-eldef Stefan Monnier
2004-06-02  3:42                       ` minibuffer-eldef Miles Bader
2004-06-02  7:01                         ` minibuffer-eldef David Kastrup
2004-06-02  7:15                           ` minibuffer-eldef Miles Bader
2004-06-02 22:55                             ` minibuffer-eldef Richard Stallman
2004-06-03  7:19                               ` minibuffer-eldef David Kastrup
2004-06-03  7:34                                 ` minibuffer-eldef Miles Bader
2004-06-03  8:13                                   ` minibuffer-eldef David Kastrup
2004-06-03 22:40                                     ` minibuffer-eldef Miles Bader
2004-06-03  7:39                                 ` minibuffer-eldef Stephan Stahl
2004-06-03  8:06                                   ` minibuffer-eldef David Kastrup
2004-06-03  8:43                                     ` minibuffer-eldef Stephan Stahl
2004-06-03 12:21                                 ` minibuffer-eldef Stefan Monnier
2004-06-03 12:35                                   ` minibuffer-eldef David Kastrup
2004-06-04  1:35                                 ` minibuffer-eldef Juri Linkov
2004-06-02  8:04                       ` minibuffer-eldef Kim F. Storm
2004-06-02  9:50                         ` minibuffer-eldef Miles Bader
2004-06-02  8:32                       ` minibuffer-eldef Werner LEMBERG
2004-06-02 17:37                 ` improving query-replace and query-replace-regexp Richard Stallman
2004-06-02 17:52                   ` David Kastrup
2004-06-03  1:28                   ` Miles Bader
2004-06-03  2:29                     ` Stefan Monnier
2004-06-03  4:52                       ` Miles Bader
2004-06-03  7:22                     ` David Kastrup
2004-06-04  2:03                       ` Richard Stallman
2004-06-07  4:28                         ` Miles Bader
2004-06-02  0:16         ` Kevin Rodgers
2004-06-02  0:32           ` Miles Bader
2004-06-03 18:35             ` Kevin Rodgers
2004-06-03 18:57               ` Stefan Monnier
2004-06-03 22:20                 ` Miles Bader
2004-06-04 17:33                   ` Richard Stallman
2004-06-02  0:59           ` Juri Linkov
2004-07-03  9:45   ` Juri Linkov [this message]
2004-07-04 17:03     ` Richard Stallman
2004-07-05 19:43       ` David Kastrup
2004-05-29 10:57 ` Miles Bader
2004-05-29 11:58   ` Kai Grossjohann
2004-05-29 12:03     ` Miles Bader
2004-05-30 14:30       ` Richard Stallman
2004-05-29 12:21     ` Werner LEMBERG
2004-05-29 15:51 ` Stefan Daschek

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=87r7rto2by.fsf@mail.jurta.org \
    --to=juri@jurta.org \
    --cc=emacs-devel@gnu.org \
    /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.