1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992, 1997-1998, 2001-2012 Free Software Foundation, Inc.
5 ;; Author: Matthieu Devin <devin@lucid.com>
8 ;; Keywords: convenience emulations
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 3 of the License, or
15 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
27 ;; This file makes the active region be pending delete, meaning that
28 ;; text inserted while the region is active will replace the region contents.
29 ;; This is a popular behavior of personal computers text editors.
33 ;; Commands which will delete the selection need a 'delete-selection
34 ;; property on their symbols; commands which insert text but don't
35 ;; have this property won't delete the selection. It can be one of
38 ;; For commands which do a yank; ensures the region about to be
39 ;; deleted isn't yanked.
41 ;; Delete the active region and ignore the current command,
42 ;; i.e. the command will just delete the region.
44 ;; `kill-region' is used on the selection, rather than
45 ;; `delete-region'. (Text selected with the mouse will typically
46 ;; be yankable anyhow.)
48 ;; The normal case: delete the active region prior to executing
49 ;; the command which will insert replacement text.
51 ;; For commands which need to dynamically determine this behaviour.
52 ;; Each hook should return one of the above values or nil.
57 (defalias 'pending-delete-mode
'delete-selection-mode
)
60 (define-minor-mode delete-selection-mode
61 "Toggle Delete Selection mode.
62 With a prefix argument ARG, enable Delete Selection mode if ARG
63 is positive, and disable it otherwise. If called from Lisp,
64 enable the mode if ARG is omitted or nil.
66 When Delete Selection mode is enabled, Transient Mark mode is also
67 enabled and typed text replaces the selection if the selection is
68 active. Otherwise, typed text is just inserted at point regardless of
70 :global t
:group
'editing-basics
71 (if (not delete-selection-mode
)
72 (remove-hook 'pre-command-hook
'delete-selection-pre-hook
)
73 (add-hook 'pre-command-hook
'delete-selection-pre-hook
)
74 (transient-mark-mode t
)))
76 (defun delete-active-region (&optional killp
)
77 "Delete the active region.
78 If KILLP in not-nil, the active region is killed instead of deleted."
80 (kill-region (point) (mark))
81 (delete-region (point) (mark)))
84 (defun delete-selection-helper (type)
85 "Deletes selection according to TYPE:
87 For commands which do a yank; ensures the region about to be
90 Delete the active region and ignore the current command,
91 i.e. the command will just delete the region.
93 `kill-region' is used on the selection, rather than
94 `delete-region'. (Text selected with the mouse will typically
97 The normal case: delete the active region prior to executing
98 the command which will insert replacement text.
100 For commands which need to dynamically determine this behaviour.
101 Each hook should return one of the above values or nil."
103 (cond ((eq type
'kill
)
104 (delete-active-region t
))
106 ;; Before a yank command, make sure we don't yank the
107 ;; head of the kill-ring that really comes from the
108 ;; currently active region we are going to delete.
109 ;; That would make yank a no-op.
110 (when (and (string= (buffer-substring-no-properties
113 (fboundp 'mouse-region-match
)
114 (mouse-region-match))
116 (delete-active-region))
117 ((eq type
'supersede
)
118 (let ((empty-region (= (point) (mark))))
119 (delete-active-region)
121 (setq this-command
'ignore
))))
122 ((and (symbolp type
) (not (booleanp type
)))
123 (delete-selection-helper
124 (run-hook-with-args-until-success type
)))
126 (delete-active-region)
127 (if (and overwrite-mode
128 (eq this-command
'self-insert-command
))
129 (let ((overwrite-mode nil
))
131 (prefix-numeric-value current-prefix-arg
))
132 (setq this-command
'ignore
)))))
133 ;; If ask-user-about-supersession-threat signals an error,
134 ;; stop safe_run_hooks from clearing out pre-command-hook.
135 (file-supersession (message "%s" (cadr data
)) (ding))
137 ;; This signal may come either from `delete-active-region' or
138 ;; `self-insert-command' (when `overwrite-mode' is non-nil).
139 ;; To avoid clearing out `pre-command-hook' we handle this case
140 ;; by issuing a simple message. Note, however, that we do not
141 ;; handle all related problems: When read-only text ends before
142 ;; the end of the region, the latter is not deleted but any
143 ;; subsequent insertion will succeed. We could avoid this case
144 ;; by doing a (setq this-command 'ignore) here. This would,
145 ;; however, still not handle the case where read-only text ends
146 ;; precisely where the region starts: In that case the deletion
147 ;; would succeed but the subsequent insertion would fail with a
148 ;; text-read-only error. To handle that case we would have to
149 ;; investigate text properties at both ends of the region and
150 ;; skip the deletion when inserting text is forbidden there.
151 (message "Text is read-only") (ding))))
153 (defun delete-selection-pre-hook ()
154 "Normal hook run before commands that delete selections are executed.
155 Commands which will delete the selection need a 'delete-selection
156 property on their symbols; commands which insert text but don't
157 have this property won't delete the selection.
159 See `delete-selection-helper'.
161 (when (and delete-selection-mode transient-mark-mode mark-active
162 (not buffer-read-only
))
163 (let ((type (and (symbolp this-command
)
164 (get this-command
'delete-selection
))))
165 (delete-selection-helper type
))))
167 (defun delete-selection-self-insert-function ()
170 (defvar delete-selection-self-insert-hooks
171 '(delete-selection-self-insert-function)
172 "Abnormal hook run before commands that insert characters.
173 This hook should return a TYPE that `delete-selection-helper' understands.")
175 (put 'self-insert-command
'delete-selection
'delete-selection-self-insert-hooks
)
176 (put 'self-insert-iso
'delete-selection
'delete-selection-self-insert-hooks
)
178 (put 'yank
'delete-selection
'yank
)
179 (put 'clipboard-yank
'delete-selection
'yank
)
180 (put 'insert-register
'delete-selection t
)
182 (put 'delete-backward-char
'delete-selection
'supersede
)
183 (put 'backward-delete-char-untabify
'delete-selection
'supersede
)
184 (put 'delete-char
'delete-selection
'supersede
)
186 (put 'newline-and-indent
'delete-selection t
)
187 (put 'newline
'delete-selection t
)
188 (put 'open-line
'delete-selection
'kill
)
190 ;; This is very useful for canceling a selection in the minibuffer without
191 ;; aborting the minibuffer.
192 (defun minibuffer-keyboard-quit ()
193 "Abort recursive edit.
194 In Delete Selection mode, if the mark is active, just deactivate it;
195 then it takes a second \\[keyboard-quit] to abort the minibuffer."
197 (if (and delete-selection-mode transient-mark-mode mark-active
)
198 (setq deactivate-mark t
)
199 (abort-recursive-edit)))
201 (define-key minibuffer-local-map
"\C-g" 'minibuffer-keyboard-quit
)
202 (define-key minibuffer-local-ns-map
"\C-g" 'minibuffer-keyboard-quit
)
203 (define-key minibuffer-local-completion-map
"\C-g" 'minibuffer-keyboard-quit
)
204 (define-key minibuffer-local-must-match-map
"\C-g" 'minibuffer-keyboard-quit
)
205 (define-key minibuffer-local-isearch-map
"\C-g" 'minibuffer-keyboard-quit
)
207 (defun delsel-unload-function ()
208 "Unload the Delete Selection library."
209 (define-key minibuffer-local-map
"\C-g" 'abort-recursive-edit
)
210 (define-key minibuffer-local-ns-map
"\C-g" 'abort-recursive-edit
)
211 (define-key minibuffer-local-completion-map
"\C-g" 'abort-recursive-edit
)
212 (define-key minibuffer-local-must-match-map
"\C-g" 'abort-recursive-edit
)
213 (define-key minibuffer-local-isearch-map
"\C-g" 'abort-recursive-edit
)
214 (dolist (sym '(self-insert-command self-insert-iso yank clipboard-yank
215 insert-register delete-backward-char backward-delete-char-untabify
216 delete-char newline-and-indent newline open-line
))
217 (put sym
'delete-selection nil
))
218 ;; continue standard unloading
223 ;;; delsel.el ends here