1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992, 1997-1998, 2001-2013 Free Software Foundation,
6 ;; Author: Matthieu Devin <devin@lucid.com>
9 ;; Keywords: convenience emulations
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software: you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation, either version 3 of the License, or
16 ;; (at your option) any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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.
34 ;; Commands which will delete the selection need a 'delete-selection
35 ;; property on their symbols; commands which insert text but don't
36 ;; have this property won't delete the selection. It can be one of
39 ;; For commands which do a yank; ensures the region about to be
40 ;; deleted isn't yanked.
42 ;; Delete the active region and ignore the current command,
43 ;; i.e. the command will just delete the region.
45 ;; `kill-region' is used on the selection, rather than
46 ;; `delete-region'. (Text selected with the mouse will typically
47 ;; be yankable anyhow.)
49 ;; The normal case: delete the active region prior to executing
50 ;; the command which will insert replacement text.
52 ;; For commands which need to dynamically determine this behaviour.
53 ;; The function should return one of the above values or nil.
58 (defalias 'pending-delete-mode
'delete-selection-mode
)
61 (define-minor-mode delete-selection-mode
62 "Toggle Delete Selection mode.
63 With a prefix argument ARG, enable Delete Selection mode if ARG
64 is positive, and disable it otherwise. If called from Lisp,
65 enable the mode if ARG is omitted or nil.
67 When Delete Selection mode is enabled, Transient Mark mode is also
68 enabled and typed text replaces the selection if the selection is
69 active. Otherwise, typed text is just inserted at point regardless of
71 :global t
:group
'editing-basics
72 (if (not delete-selection-mode
)
73 (remove-hook 'pre-command-hook
'delete-selection-pre-hook
)
74 (add-hook 'pre-command-hook
'delete-selection-pre-hook
)))
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 ;; Don't allow `kill-region' to change the value of `this-command'.
82 (kill-region (point) (mark) t
))
83 (funcall region-extract-function
'delete-only
))
86 (defun delete-selection-helper (type)
87 "Delete selection according to TYPE:
89 For commands which do a yank; ensures the region about to be
92 Delete the active region and ignore the current command,
93 i.e. the command will just delete the region.
95 `kill-region' is used on the selection, rather than
96 `delete-region'. (Text selected with the mouse will typically
99 The normal case: delete the active region prior to executing
100 the command which will insert replacement text.
102 For commands which need to dynamically determine this behaviour.
103 FUNCTION should take no argument and return one of the above values or nil."
105 (cond ((eq type
'kill
)
106 (delete-active-region t
)
107 (if (and overwrite-mode
108 (eq this-command
'self-insert-command
))
109 (let ((overwrite-mode nil
))
111 (prefix-numeric-value current-prefix-arg
))
112 (setq this-command
'ignore
))))
114 ;; Before a yank command, make sure we don't yank the
115 ;; head of the kill-ring that really comes from the
116 ;; currently active region we are going to delete.
117 ;; That would make yank a no-op.
118 (when (and (string= (buffer-substring-no-properties
121 (fboundp 'mouse-region-match
)
122 (mouse-region-match))
124 (let ((pos (copy-marker (region-beginning))))
125 (delete-active-region)
126 ;; If the region was, say, rectangular, make sure we yank
127 ;; from the top, to "replace".
129 ((eq type
'supersede
)
130 (let ((empty-region (= (point) (mark))))
131 (delete-active-region)
133 (setq this-command
'ignore
))))
134 ((functionp type
) (delete-selection-helper (funcall type
)))
136 (delete-active-region)
137 (if (and overwrite-mode
138 (eq this-command
'self-insert-command
))
139 (let ((overwrite-mode nil
))
141 (prefix-numeric-value current-prefix-arg
))
142 (setq this-command
'ignore
)))))
143 ;; If ask-user-about-supersession-threat signals an error,
144 ;; stop safe_run_hooks from clearing out pre-command-hook.
145 (file-supersession (message "%s" (cadr data
)) (ding))
147 ;; This signal may come either from `delete-active-region' or
148 ;; `self-insert-command' (when `overwrite-mode' is non-nil).
149 ;; To avoid clearing out `pre-command-hook' we handle this case
150 ;; by issuing a simple message. Note, however, that we do not
151 ;; handle all related problems: When read-only text ends before
152 ;; the end of the region, the latter is not deleted but any
153 ;; subsequent insertion will succeed. We could avoid this case
154 ;; by doing a (setq this-command 'ignore) here. This would,
155 ;; however, still not handle the case where read-only text ends
156 ;; precisely where the region starts: In that case the deletion
157 ;; would succeed but the subsequent insertion would fail with a
158 ;; text-read-only error. To handle that case we would have to
159 ;; investigate text properties at both ends of the region and
160 ;; skip the deletion when inserting text is forbidden there.
161 (message "Text is read-only") (ding))))
163 (defun delete-selection-pre-hook ()
164 "Function run before commands that delete selections are executed.
165 Commands which will delete the selection need a `delete-selection'
166 property on their symbol; commands which insert text but don't
167 have this property won't delete the selection.
168 See `delete-selection-helper'."
169 (when (and delete-selection-mode
(use-region-p)
170 (not buffer-read-only
))
171 (delete-selection-helper (and (symbolp this-command
)
172 (get this-command
'delete-selection
)))))
174 (put 'self-insert-command
'delete-selection
176 (not (run-hook-with-args-until-success
177 'self-insert-uses-region-functions
))))
179 (put 'insert-char
'delete-selection t
)
180 (put 'quoted-insert
'delete-selection t
)
182 (put 'yank
'delete-selection
'yank
)
183 (put 'clipboard-yank
'delete-selection
'yank
)
184 (put 'insert-register
'delete-selection t
)
186 (put 'reindent-then-newline-and-indent
'delete-selection t
)
187 (put 'newline-and-indent
'delete-selection t
)
188 (put 'newline
'delete-selection t
)
189 (put 'open-line
'delete-selection
'kill
)
191 ;; This is very useful for canceling a selection in the minibuffer without
192 ;; aborting the minibuffer.
193 (defun minibuffer-keyboard-quit ()
194 "Abort recursive edit.
195 In Delete Selection mode, if the mark is active, just deactivate it;
196 then it takes a second \\[keyboard-quit] to abort the minibuffer."
198 (if (and delete-selection-mode
(region-active-p))
199 (setq deactivate-mark t
)
200 (abort-recursive-edit)))
202 (define-key minibuffer-local-map
"\C-g" 'minibuffer-keyboard-quit
)
203 (define-key minibuffer-local-ns-map
"\C-g" 'minibuffer-keyboard-quit
)
204 (define-key minibuffer-local-completion-map
"\C-g" 'minibuffer-keyboard-quit
)
205 (define-key minibuffer-local-must-match-map
"\C-g" 'minibuffer-keyboard-quit
)
206 (define-key minibuffer-local-isearch-map
"\C-g" 'minibuffer-keyboard-quit
)
208 (defun delsel-unload-function ()
209 "Unload the Delete Selection library."
210 (define-key minibuffer-local-map
"\C-g" 'abort-recursive-edit
)
211 (define-key minibuffer-local-ns-map
"\C-g" 'abort-recursive-edit
)
212 (define-key minibuffer-local-completion-map
"\C-g" 'abort-recursive-edit
)
213 (define-key minibuffer-local-must-match-map
"\C-g" 'abort-recursive-edit
)
214 (define-key minibuffer-local-isearch-map
"\C-g" 'abort-recursive-edit
)
215 (dolist (sym '(self-insert-command insert-char quoted-insert yank
216 clipboard-yank insert-register newline-and-indent
217 reindent-then-newline-and-indent newline open-line
))
218 (put sym
'delete-selection nil
))
219 ;; continue standard unloading
224 ;;; delsel.el ends here