summaryrefslogtreecommitdiff
path: root/lisp/delsel.el
diff options
context:
space:
mode:
authorKim F. Storm <storm@cua.dk>2014-10-30 15:19:49 -0400
committerStefan Monnier <monnier@iro.umontreal.ca>2014-10-30 15:19:49 -0400
commit71477684db6d781a07edda5060a0530da3623d96 (patch)
tree96592d49b85f67dffcd6f90522e3d8e01152365d /lisp/delsel.el
parentc465f1c27f538c0ef2afa01f179e1f472bd9f097 (diff)
downloademacs-71477684db6d781a07edda5060a0530da3623d96.tar.gz
Restore cua-delete-copy-to-register-0 and M-v command.
* lisp/delsel.el (delete-selection-save-to-register) (delsel--replace-text-or-position): New vars. (delete-active-region): Use them. (delete-selection-repeat-replace-region): New command, moved from cua-base.el. * lisp/emulation/cua-base.el (cua--repeat-replace-text): Remove var. (cua-repeat-replace-region): Move command to delsel.el. (cua--init-keymaps): Update binding accordingly. (cua-mode): Set delete-selection-save-to-register. Fixes: debbugs:18886
Diffstat (limited to 'lisp/delsel.el')
-rw-r--r--lisp/delsel.el76
1 files changed, 71 insertions, 5 deletions
diff --git a/lisp/delsel.el b/lisp/delsel.el
index 1ada02705fe..96794fce7aa 100644
--- a/lisp/delsel.el
+++ b/lisp/delsel.el
@@ -54,6 +54,10 @@
;;; Code:
+(defvar 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.")
+
;;;###autoload
(defalias 'pending-delete-mode 'delete-selection-mode)
@@ -72,16 +76,78 @@ point regardless of any selection."
(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)
+
(defun delete-active-region (&optional killp)
"Delete the active region.
If KILLP in not-nil, the active region is killed instead of deleted."
- (if killp
- ;; Don't allow `kill-region' to change the value of `this-command'.
- (let (this-command)
- (kill-region (point) (mark) t))
- (funcall region-extract-function 'delete-only))
+ (cond
+ (killp
+ ;; Don't allow `kill-region' to change the value of `this-command'.
+ (let (this-command)
+ (kill-region (point) (mark) t)))
+ (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)))))
+ (t
+ (funcall region-extract-function 'delete-only)))
t)
+(defun delete-selection-repeat-replace-region (arg)
+ "Repeat replacing text of highlighted region with typed text.
+Search for the next stretch of text identical to the region last replaced
+by typing text over it and replaces it with the same stretch of text.
+With ARG, repeat that many times. `C-u' means until end of buffer."
+ (interactive "P")
+ (let ((old-text (and delete-selection-save-to-register
+ (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)))))
+ (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))))))
+
(defun delete-selection-helper (type)
"Delete selection according to TYPE:
`yank'