(defalias 'pending-delete-mode 'delete-selection-mode)
(defun delete-selection-mode (&optional arg)
"Toggle Delete Selection mode.
With prefix ARG, turn Delete Selection mode on if and only if ARG is
positive.
When Delete Selection mode is enabled, Transient Mark mode is also
enabled and typed text replaces the selection if the selection is
active. Otherwise, typed text is just inserted at point regardless of
any selection."
(interactive "P")
(setq delete-selection-mode (if arg
(> (prefix-numeric-value arg) 0)
(not delete-selection-mode)))
(if (not delete-selection-mode)
(remove-hook 'pre-command-hook 'delete-selection-pre-hook)
(add-hook 'pre-command-hook 'delete-selection-pre-hook)
(transient-mark-mode t)))
(defcustom delete-selection-mode nil
"Toggle Delete Selection mode.
See command `delete-selection-mode'.
Setting this variable directly does not take effect;
use either \\[customize] or the function `delete-selection-mode'."
:set (lambda (symbol value)
(delete-selection-mode (or value 0)))
:initialize 'custom-initialize-default
:type 'boolean
:group 'editing-basics
:require 'delsel)
(defun delete-active-region (&optional killp)
(if killp
(kill-region (point) (mark))
(delete-region (point) (mark)))
(setq mark-active nil)
(run-hooks 'deactivate-mark-hook)
t)
(defun delete-selection-pre-hook ()
(when (and delete-selection-mode transient-mark-mode mark-active
(not buffer-read-only))
(let ((type (and (symbolp this-command)
(get this-command 'delete-selection))))
(cond ((eq type 'kill)
(delete-active-region t))
((eq type 'yank)
(when (string= (buffer-substring-no-properties (point) (mark))
(car kill-ring))
(current-kill 1))
(delete-active-region))
((eq type 'supersede)
(delete-active-region)
(setq this-command 'ignore))
(type
(delete-active-region))))))
(put 'self-insert-command 'delete-selection t)
(put 'self-insert-iso 'delete-selection t)
(put 'yank 'delete-selection 'yank)
(put 'clipboard-yank 'delete-selection 'yank)
(put 'insert-register 'delete-selection t)
(put 'delete-backward-char 'delete-selection 'supersede)
(put 'backward-delete-char-untabify 'delete-selection 'supersede)
(put 'delete-char 'delete-selection 'supersede)
(put 'newline-and-indent 'delete-selection t)
(put 'newline 'delete-selection t)
(put 'open-line 'delete-selection 'kill)
(put 'insert-parentheses 'delete-selection t)
(defun minibuffer-keyboard-quit ()
"Abort recursive edit.
In Delete Selection mode, if the mark is active, just deactivate it;
then it takes a second \\[keyboard-quit] to abort the minibuffer."
(interactive)
(if (and delete-selection-mode transient-mark-mode mark-active)
(setq deactivate-mark t)
(abort-recursive-edit)))
(define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
(define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
(define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
(define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
(define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
(provide 'delsel)
(when delete-selection-mode
(delete-selection-mode t))