1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992, 1997 Free Software Foundation, Inc.
5 ;; Author: Matthieu Devin <devin@lucid.com>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
28 ;; This file makes the active region be pending delete, meaning that
29 ;; text inserted while the region is active will replace the region contents.
30 ;; This is a popular behavior of personal computers text editors.
38 (defalias 'pending-delete-mode
'delete-selection-mode
)
41 (defun delete-selection-mode (&optional arg
)
42 "Toggle Delete Selection mode.
43 With prefix ARG, turn Delete Selection mode on if and only if ARG is positive.
45 When Delete Selection mode is enabled, Transient Mark mode is also enabled and
46 typed text replaces the selection if the selection is active. Otherwise, typed
47 text is just inserted at point regardless of any selection."
49 (setq delete-selection-mode
(if arg
50 (> (prefix-numeric-value arg
) 0)
51 (not delete-selection-mode
)))
52 (if (not delete-selection-mode
)
53 (remove-hook 'pre-command-hook
'delete-selection-pre-hook
)
54 (add-hook 'pre-command-hook
'delete-selection-pre-hook
)
55 (transient-mark-mode t
)))
58 (defcustom delete-selection-mode nil
59 "Toggle Delete Selection mode.
60 When Delete Selection mode is enabled, Transient Mark mode is also enabled and
61 typed text replaces the selection if the selection is active.
62 You must modify via \\[customize] for this variable to have an effect."
63 :set
(lambda (symbol value
)
64 (delete-selection-mode (or value
0)))
66 :group
'editing-basics
69 (defun delete-active-region (&optional killp
)
71 (kill-region (point) (mark))
72 (delete-region (point) (mark)))
73 (setq mark-active nil
)
74 (run-hooks 'deactivate-mark-hook
)
77 (defun delete-selection-pre-hook ()
78 (when (and delete-selection-mode transient-mark-mode mark-active
79 (not buffer-read-only
))
80 (let ((type (and (symbolp this-command
)
81 (get this-command
'delete-selection
))))
82 (cond ((eq type
'kill
)
83 (delete-active-region t
))
85 ;; Before a yank command,
86 ;; make sure we don't yank the same region
87 ;; that we are going to delete.
88 ;; That would make yank a no-op.
89 (when (string= (buffer-substring-no-properties (point) (mark))
92 (delete-active-region nil
))
94 (when (delete-active-region nil
)
95 (setq this-command
'(lambda () (interactive)))))
97 (delete-active-region nil
))))))
99 (put 'self-insert-command
'delete-selection t
)
100 (put 'self-insert-iso
'delete-selection t
)
102 (put 'yank
'delete-selection
'yank
)
103 (put 'clipboard-yank
'delete-selection
'yank
)
104 (put 'insert-register
'delete-selection t
)
106 (put 'delete-backward-char
'delete-selection
'supersede
)
107 (put 'backward-delete-char-untabify
'delete-selection
'supersede
)
108 (put 'delete-char
'delete-selection
'supersede
)
110 (put 'newline-and-indent
'delete-selection
't
)
111 (put 'newline
'delete-selection t
)
112 (put 'open-line
'delete-selection t
)
114 ;; This is very useful for cancelling a selection in the minibuffer without
115 ;; aborting the minibuffer.
116 (defun minibuffer-keyboard-quit ()
117 "Abort recursive edit.
118 In Delete Selection mode mode, if the mark is active, just deactivate it;
119 then it takes a second C-g to abort the minibuffer."
121 (if (and delete-selection-mode transient-mark-mode mark-active
)
122 (setq deactivate-mark t
)
123 (abort-recursive-edit)))
125 (define-key minibuffer-local-map
"\C-g" 'minibuffer-keyboard-quit
)
126 (define-key minibuffer-local-ns-map
"\C-g" 'minibuffer-keyboard-quit
)
127 (define-key minibuffer-local-completion-map
"\C-g" 'minibuffer-keyboard-quit
)
128 (define-key minibuffer-local-must-match-map
"\C-g" 'minibuffer-keyboard-quit
)
129 (define-key minibuffer-local-isearch-map
"\C-g" 'minibuffer-keyboard-quit
)
133 ;;; delsel.el ends here