all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: Kim Storm <storm@cua.dk>
To: Stefan Monnier <monnier@IRO.UMontreal.CA>
Cc: 18886@debbugs.gnu.org
Subject: bug#18886: 24.4; M-v no longer works in CUA-mode.
Date: Sun, 02 Nov 2014 18:04:35 +0100	[thread overview]
Message-ID: <54566423.6050105@cua.dk> (raw)
In-Reply-To: <5452A1F6.2070303@cua.dk>

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  <storm@cua.dk>

	* 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:






  reply	other threads:[~2014-11-02 17:04 UTC|newest]

Thread overview: 7+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2014-10-29 16:54 bug#18886: 24.4; M-v no longer works in CUA-mode Kim F. Storm
2014-10-30  0:47 ` Stefan Monnier
2014-10-30 16:46   ` Kim Storm
2014-10-30 19:23     ` Stefan Monnier
2014-10-30 20:39       ` Kim Storm
2014-11-02 17:04         ` Kim Storm [this message]
2016-06-26 17:55 ` Noam Postavsky

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=54566423.6050105@cua.dk \
    --to=storm@cua.dk \
    --cc=18886@debbugs.gnu.org \
    --cc=monnier@IRO.UMontreal.CA \
    /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.