1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Matthieu Devin <devin@lucid.com>
8 ;; Last change 18-Feb-93, devin.
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software; you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation; either version 2, or (at your option)
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
29 ;; This file makes the active region be pending delete, meaning that
30 ;; text inserted while the region is active will replace the region contents.
31 ;; This is a popular behavior of personal computers text editors.
35 (defvar delete-selection-mode t
36 "*Non-nil means Delete Selection mode is enabled.
37 In Delete Selection mode, when a region is highlighted,
38 insertion commands first delete the region and then insert.")
40 (defun delete-active-region (&optional killp
)
42 (kill-region (point) (mark))
43 (delete-region (point) (mark)))
44 (setq mark-active nil
)
45 (run-hooks 'deactivate-mark-hook
)
48 (defun delete-selection-pre-hook ()
49 (if (and delete-selection-mode
50 (not buffer-read-only
)
51 transient-mark-mode mark-active
)
52 (let ((type (and (symbolp this-command
)
53 (get this-command
'delete-selection
))))
54 (cond ((eq type
'kill
)
55 (delete-active-region t
))
57 ;; Before a yank command,
58 ;; make sure we don't yank the same region
59 ;; that we are going to delete.
60 ;; That would make yank a no-op.
61 (if (string= (buffer-substring (point) (mark))
64 (delete-active-region nil
))
66 (if (delete-active-region nil
)
67 (setq this-command
'(lambda () (interactive)))))
69 (delete-active-region nil
))))))
71 (add-hook 'pre-command-hook
'delete-selection-pre-hook
)
73 (put 'self-insert-command
'delete-selection t
)
74 (put 'self-insert-iso
'delete-selection t
)
76 (put 'yank
'delete-selection
'yank
)
77 (put 'clipboard-yank
'delete-selection
'yank
)
78 (put 'insert-register
'delete-selection t
)
80 (put 'delete-backward-char
'delete-selection
'supersede
)
81 (put 'backward-delete-char-untabify
'delete-selection
'supersede
)
82 (put 'delete-char
'delete-selection
'supersede
)
84 (put 'newline-and-indent
'delete-selection
't
)
85 (put 'newline
'delete-selection t
)
86 (put 'open-line
'delete-selection t
)
89 (defalias 'pending-delete-mode
'delete-selection-mode
)
91 (defun delete-selection-mode (arg)
92 "Toggle Delete Selection mode.
93 When ON, typed text replaces the selection if the selection is active.
94 When OFF, typed text is just inserted at point."
96 (setq delete-selection-mode
97 (if (null arg
) (not delete-selection-mode
)
98 (> (prefix-numeric-value arg
) 0)))
99 (force-mode-line-update))
101 ;; This is very useful for cancelling a selection in the minibuffer without
102 ;; aborting the minibuffer.
103 (defun minibuffer-keyboard-quit ()
104 "Abort recursive edit.
105 In Delete Selection mode mode, if the mark is active, just deactivate it;
106 then it takes a second C-g to abort the minibuffer."
108 (if (and delete-selection-mode transient-mark-mode mark-active
)
109 (setq deactivate-mark t
)
110 (abort-recursive-edit)))
112 (define-key minibuffer-local-map
"\C-g" 'minibuffer-keyboard-quit
)
113 (define-key minibuffer-local-ns-map
"\C-g" 'minibuffer-keyboard-quit
)
114 (define-key minibuffer-local-completion-map
"\C-g" 'minibuffer-keyboard-quit
)
115 (define-key minibuffer-local-must-match-map
"\C-g" 'minibuffer-keyboard-quit
)
116 (define-key minibuffer-local-isearch-map
"\C-g" 'minibuffer-keyboard-quit
)
120 ;;; delsel.el ends here