emacs-devel
[Top][All Lists]
Advanced

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

Re: improving query-replace and query-replace-regexp


From: Stefan Monnier
Subject: Re: improving query-replace and query-replace-regexp
Date: 01 Jun 2004 19:48:23 -0400
User-agent: Gnus/5.09 (Gnus v5.9.0) Emacs/21.3.50

> I tried Stefan's implementation and it works fine.  I have only one remark:
> if the last history item contains newlines, it may confuse users because
> it displays only the closing square bracket at the minibuffer prompt.

Good point.  How about the version below instead?


        Stefan


--- orig/lisp/replace.el
+++ mod/lisp/replace.el
@@ -1,6 +1,6 @@
 ;;; replace.el --- replace commands for Emacs
 
-;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 2002
+;; Copyright (C) 1985, 86, 87, 92, 94, 96, 1997, 2000, 2001, 02, 03, 2004
 ;;  Free Software Foundation, Inc.
 
 ;; Maintainer: FSF
@@ -69,17 +69,32 @@
 (defun query-replace-read-args (string regexp-flag &optional noerror)
   (unless noerror
     (barf-if-buffer-read-only))
-  (let (from to)
+  (let ((lastfrom (car (symbol-value query-replace-from-history-variable)))
+       (lastto (car (symbol-value query-replace-to-history-variable)))
+       from to)
     (if query-replace-interactive
        (setq from (car (if regexp-flag regexp-search-ring search-ring)))
+      (if (equal lastfrom lastto)
+         ;; Typically, this is because the two histlists are shared.
+         (setq lastfrom
+               (cadr (symbol-value query-replace-from-history-variable))))
       ;; 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.
       (save-excursion
-       (setq from (read-from-minibuffer (format "%s: " string)
+       (setq from (read-from-minibuffer
+                   (if (null lastto)
+                       (format "%s: " string)
+                     (format "%s [%s -> %s]: " string
+                             (mapconcat 'isearch-text-char-description
+                                        lastfrom "")
+                             (mapconcat 'isearch-text-char-description
+                                        lastto "")))
                                         nil nil nil
                                         query-replace-from-history-variable
                                         nil t)))
+      (if (and lastto (zerop (length from)))
+         (setq from lastfrom to lastto)
       ;; Warn if user types \n or \t, but don't reject the input.
       (if (string-match "\\\\[nt]" from)
          (let ((match (match-string 0 from)))
@@ -88,12 +103,13 @@
              (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))))
+             (sit-for 2)))))
 
+    (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)))
+                                      query-replace-to-history-variable from 
t))))
     (list from to current-prefix-arg)))
 
 (defun query-replace (from-string to-string &optional delimited start end)
@@ -842,7 +858,6 @@
          (let ((matches 0)     ;; count of matched lines
                (lines 1)       ;; line count
                (matchbeg 0)
-               (matchend 0)
                (origpt nil)
                (begpt nil)
                (endpt nil)
@@ -862,8 +877,7 @@
                  (setq origpt (point))
                  (when (setq endpt (re-search-forward regexp nil t))
                    (setq matches (1+ matches)) ;; increment match count
-                   (setq matchbeg (match-beginning 0)
-                         matchend (match-end 0))
+                   (setq matchbeg (match-beginning 0))
                    (setq begpt (save-excursion
                                  (goto-char matchbeg)
                                  (line-beginning-position)))




reply via email to

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