* lisp/delsel.el (delete-active-region): Let-bind `this-command'
[bpt/emacs.git] / lisp / delsel.el
1 ;;; delsel.el --- delete selection if you insert
2
3 ;; Copyright (C) 1992, 1997-1998, 2001-2013 Free Software Foundation,
4 ;; Inc.
5
6 ;; Author: Matthieu Devin <devin@lucid.com>
7 ;; Maintainer: FSF
8 ;; Created: 14 Jul 92
9 ;; Keywords: convenience emulations
10
11 ;; This file is part of GNU Emacs.
12
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.
17
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.
22
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/>.
25
26 ;;; Commentary:
27
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.
31
32 ;; Interface:
33
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
37 ;; the values:
38 ;; 'yank
39 ;; For commands which do a yank; ensures the region about to be
40 ;; deleted isn't yanked.
41 ;; 'supersede
42 ;; Delete the active region and ignore the current command,
43 ;; i.e. the command will just delete the region.
44 ;; 'kill
45 ;; `kill-region' is used on the selection, rather than
46 ;; `delete-region'. (Text selected with the mouse will typically
47 ;; be yankable anyhow.)
48 ;; t
49 ;; The normal case: delete the active region prior to executing
50 ;; the command which will insert replacement text.
51 ;; <function>
52 ;; For commands which need to dynamically determine this behaviour.
53 ;; The function should return one of the above values or nil.
54
55 ;;; Code:
56
57 ;;;###autoload
58 (defalias 'pending-delete-mode 'delete-selection-mode)
59
60 ;;;###autoload
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.
66
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
70 any selection."
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)
75 (transient-mark-mode t)))
76
77 (defun delete-active-region (&optional killp)
78 "Delete the active region.
79 If KILLP in not-nil, the active region is killed instead of deleted."
80 (if killp
81 ;; Don't allow `kill-region' to change the value of `this-command'.
82 (let (this-command)
83 (kill-region (point) (mark) t))
84 (funcall region-extract-function 'delete-only))
85 t)
86
87 (defun delete-selection-helper (type)
88 "Delete selection according to TYPE:
89 `yank'
90 For commands which do a yank; ensures the region about to be
91 deleted isn't yanked.
92 `supersede'
93 Delete the active region and ignore the current command,
94 i.e. the command will just delete the region.
95 `kill'
96 `kill-region' is used on the selection, rather than
97 `delete-region'. (Text selected with the mouse will typically
98 be yankable anyhow.)
99 t
100 The normal case: delete the active region prior to executing
101 the command which will insert replacement text.
102 FUNCTION
103 For commands which need to dynamically determine this behaviour.
104 FUNCTION should take no argument and return one of the above values or nil."
105 (condition-case data
106 (cond ((eq type 'kill)
107 (delete-active-region t)
108 (if (and overwrite-mode
109 (eq this-command 'self-insert-command))
110 (let ((overwrite-mode nil))
111 (self-insert-command
112 (prefix-numeric-value current-prefix-arg))
113 (setq this-command 'ignore))))
114 ((eq type 'yank)
115 ;; Before a yank command, make sure we don't yank the
116 ;; head of the kill-ring that really comes from the
117 ;; currently active region we are going to delete.
118 ;; That would make yank a no-op.
119 (when (and (string= (buffer-substring-no-properties
120 (point) (mark))
121 (car kill-ring))
122 (fboundp 'mouse-region-match)
123 (mouse-region-match))
124 (current-kill 1))
125 (delete-active-region))
126 ((eq type 'supersede)
127 (let ((empty-region (= (point) (mark))))
128 (delete-active-region)
129 (unless empty-region
130 (setq this-command 'ignore))))
131 ((functionp type) (delete-selection-helper (funcall type)))
132 (type
133 (delete-active-region)
134 (if (and overwrite-mode
135 (eq this-command 'self-insert-command))
136 (let ((overwrite-mode nil))
137 (self-insert-command
138 (prefix-numeric-value current-prefix-arg))
139 (setq this-command 'ignore)))))
140 ;; If ask-user-about-supersession-threat signals an error,
141 ;; stop safe_run_hooks from clearing out pre-command-hook.
142 (file-supersession (message "%s" (cadr data)) (ding))
143 (text-read-only
144 ;; This signal may come either from `delete-active-region' or
145 ;; `self-insert-command' (when `overwrite-mode' is non-nil).
146 ;; To avoid clearing out `pre-command-hook' we handle this case
147 ;; by issuing a simple message. Note, however, that we do not
148 ;; handle all related problems: When read-only text ends before
149 ;; the end of the region, the latter is not deleted but any
150 ;; subsequent insertion will succeed. We could avoid this case
151 ;; by doing a (setq this-command 'ignore) here. This would,
152 ;; however, still not handle the case where read-only text ends
153 ;; precisely where the region starts: In that case the deletion
154 ;; would succeed but the subsequent insertion would fail with a
155 ;; text-read-only error. To handle that case we would have to
156 ;; investigate text properties at both ends of the region and
157 ;; skip the deletion when inserting text is forbidden there.
158 (message "Text is read-only") (ding))))
159
160 (defun delete-selection-pre-hook ()
161 "Function run before commands that delete selections are executed.
162 Commands which will delete the selection need a `delete-selection'
163 property on their symbol; commands which insert text but don't
164 have this property won't delete the selection.
165 See `delete-selection-helper'."
166 (when (and delete-selection-mode (use-region-p)
167 (not buffer-read-only))
168 (delete-selection-helper (and (symbolp this-command)
169 (get this-command 'delete-selection)))))
170
171 (put 'self-insert-command 'delete-selection
172 (lambda ()
173 (not (run-hook-with-args-until-success
174 'self-insert-uses-region-functions))))
175
176 (put 'insert-char 'delete-selection t)
177 (put 'quoted-insert 'delete-selection t)
178
179 (put 'yank 'delete-selection 'yank)
180 (put 'clipboard-yank 'delete-selection 'yank)
181 (put 'insert-register 'delete-selection t)
182
183 (put 'reindent-then-newline-and-indent 'delete-selection t)
184 (put 'newline-and-indent 'delete-selection t)
185 (put 'newline 'delete-selection t)
186 (put 'open-line 'delete-selection 'kill)
187
188 ;; This is very useful for canceling a selection in the minibuffer without
189 ;; aborting the minibuffer.
190 (defun minibuffer-keyboard-quit ()
191 "Abort recursive edit.
192 In Delete Selection mode, if the mark is active, just deactivate it;
193 then it takes a second \\[keyboard-quit] to abort the minibuffer."
194 (interactive)
195 (if (and delete-selection-mode transient-mark-mode mark-active)
196 (setq deactivate-mark t)
197 (abort-recursive-edit)))
198
199 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
200 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
201 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
202 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
203 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
204
205 (defun delsel-unload-function ()
206 "Unload the Delete Selection library."
207 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
208 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
209 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
210 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
211 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit)
212 (dolist (sym '(self-insert-command insert-char quoted-insert yank clipboard-yank
213 insert-register
214 reindent-then-newline-and-indent newline-and-indent newline open-line))
215 (put sym 'delete-selection nil))
216 ;; continue standard unloading
217 nil)
218
219 (provide 'delsel)
220
221 ;;; delsel.el ends here