Subject: bug#18886: 24.4; M-v no longer works in CUA-mode.

From: Kim Storm <[email-masked]>
Date: Sun, 02 Nov 2014 18:04:35 +0100

On 2014-10-30 21:39, Kim Storm wrote:
> On 2014-10-30 20:23, Stefan Monnier wrote:
>> BTW, I think that instead of scavenging the undo log we should place an
>> overlay at the last delete so we can directly grab the "replacement 
>> text".
> Yes - I would like to do that, as the current code is deficient in the 
> sense
> that any movement or editing will terminate the "replacement text".
> I will try it when I have some time to improve on it.

Hi Stefan,

I have made some progress on this:

The following patch eliminates the undo-list hack. It works much better
than before,  as it is now possible to edit the replacement text before
repeating the substitution using M-v.

This has always been annoying for me, so I really appreciate your 
suggestion.

Since there is only one active replacement text across all buffers, it makes
more sense for me to use an overlay to keep track of the replacement text.

If you could install this, I would appreciate it very much.

Kim

|2014-11-02  Kim F. Storm  <[email-masked]>

	* delsel.el (|||delsel--replace-text-or-position): Delete var.
|||||	(delete-selection-replacement-face): New defcustom.
|||	(delsel--replace-text, delsel--replace-overlay): New vars.
|	(delete-active-region, delete-selection-repeat-replace-region):
	Use overlay to track replacement text instead of abusing the
	undo-list; this allows the text to be edited before it is
	used for substitutions. Add highlight to the replacement text.
|

--- tmp/delsel.el    2014-11-02 17:45:25.440201850 +0100
+++ delsel.el    2014-11-02 17:49:49.000000000 +0100
@@ -54,9 +54,23 @@

  ;;; Code:

-(defvar delete-selection-save-to-register nil
+(defcustom delete-selection-save-to-register nil
    "If non-nil, deleted region text is stored in this register.
-Value must be the register (key) to use.")
+Value must be the register (key) to use."
+  :type '(choice
+      (const :tag "None" nil)
+      (character :tag "Register (Key)"))
+  :group 'editing-basics)
+
+(defcustom delete-selection-replacement-face 'highlight
+  "If non-nil, active region replacement text is shown in this face.
+The highlighted text is the text that will be inserted by
+the `delete-selection-repeat-replace-region' command."
+  :type 'face
+  :group 'editing-basics
+  :set (lambda (symbol value)
+     (if delsel--replace-overlay
+         (overlay-put delsel--replace-overlay 'face value))))

  ;;;###autoload
  (defalias 'pending-delete-mode 'delete-selection-mode)
@@ -76,7 +90,8 @@
        (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
      (add-hook 'pre-command-hook 'delete-selection-pre-hook)))

-(defvar delsel--replace-text-or-position nil)
+(defvar delsel--replace-overlay nil)    ;overlay
+(defvar delsel--replace-text nil)    ;text from overlay

  (defun delete-active-region (&optional killp)
    "Delete the active region.
@@ -89,9 +104,14 @@
     (delete-selection-save-to-register
      (set-register delete-selection-save-to-register
                    (funcall region-extract-function t))
-    (setq delsel--replace-text-or-position
-          (cons (current-buffer)
-                (and (consp buffer-undo-list) (car buffer-undo-list)))))
+    (if delsel--replace-overlay
+    (move-overlay delsel--replace-overlay (point) (point) (current-buffer))
+      (setq delsel--replace-overlay
+        (make-overlay (point) (point) (current-buffer) nil t))
+      (if delete-selection-replacement-face
+      (overlay-put delsel--replace-overlay 'face
+               delete-selection-replacement-face)))
+    (setq delsel--replace-text nil))
     (t
      (funcall region-extract-function 'delete-only)))
    t)
@@ -106,47 +126,30 @@
                         (get-register delete-selection-save-to-register)))
          (count (if (consp arg) (point-max)
                   (prefix-numeric-value current-prefix-arg))))
-    (if (not (and old-text
-                  (> (length old-text) 0)
-                  (or (stringp delsel--replace-text-or-position)
-                      (buffer-live-p (car 
delsel--replace-text-or-position)))))
+    (if (not (and old-text (> (length old-text) 0)))
          (message "No known previous replacement")
        ;; If this is the first use after overwriting regions,
        ;; find the replacement text by looking at the undo list.
-      (when (consp delsel--replace-text-or-position)
-        (let ((buffer (car delsel--replace-text-or-position))
-              (elt (cdr delsel--replace-text-or-position)))
-          (setq delsel--replace-text-or-position nil)
-          (with-current-buffer buffer
-            (save-restriction
-              (widen)
-              ;; Find the text that replaced the region via the undo list.
-              (let ((ul buffer-undo-list) u s e)
-                (when elt
-                  (while (consp ul)
-                    (setq u (car ul) ul (cdr ul))
-                    (cond
-                     ((eq u elt) ;; got it
-                      (setq ul nil))
-                     ((and (consp u) (integerp (car u)) (integerp (cdr u)))
-                      (if (and s (= (cdr u) s))
-                          (setq s (car u))
-                        (setq s (car u) e (cdr u)))))))
-                (cond ((and s e (<= s e) (= s (mark t)))
-                       (setq delsel--replace-text-or-position
-                             (filter-buffer-substring s e))
-                       (set-text-properties
-                        0 (length delsel--replace-text-or-position)
-                        nil delsel--replace-text-or-position))
-                      ((and (null s) (eq u elt)) ;; Nothing inserted.
-                       (setq delsel--replace-text-or-position ""))
-                      (t
-                       (message "Cannot locate replacement text"))))))))
-      (while (and (> count 0)
-                  delsel--replace-text-or-position
-                  (search-forward old-text nil t))
-        (replace-match delsel--replace-text-or-position nil t)
-        (setq count (1- count))))))
+      (when (and (null delsel--replace-text)
+         delsel--replace-overlay
+         (buffer-live-p (overlay-buffer delsel--replace-overlay)))
+    (with-current-buffer (overlay-buffer delsel--replace-overlay)
+      (let ((s (overlay-start delsel--replace-overlay))
+        (e (overlay-end delsel--replace-overlay)))
+        (when (/= s e)
+          (setq delsel--replace-text
+            (filter-buffer-substring s e))
+          (set-text-properties
+           0 (length delsel--replace-text)
+           nil delsel--replace-text))))
+    (delete-overlay delsel--replace-overlay))
+      (if delsel--replace-text
+      (while (and (> count 0)
+              delsel--replace-text
+              (search-forward old-text nil t))
+        (replace-match delsel--replace-text nil t)
+        (setq count (1- count)))
+    (message "Cannot locate replacement text")))))

  (defun delete-selection-helper (type)
    "Delete selection according to TYPE: