Merge branch 'master' into comment-cache
[emacs.git] / lisp / delsel.el
blobd5f4736fddb8eafe6d2e0cde1b225f3fa27c6aa7
1 ;;; delsel.el --- delete selection if you insert -*- lexical-binding:t -*-
3 ;; Copyright (C) 1992, 1997-1998, 2001-2017 Free Software Foundation,
4 ;; Inc.
6 ;; Author: Matthieu Devin <devin@lucid.com>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Created: 14 Jul 92
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/>.
26 ;;; Commentary:
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.
32 ;; Interface:
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 immediately yanked back, which would make the
41 ;; command a no-op.
42 ;; `supersede'
43 ;; Delete the active region and ignore the current command,
44 ;; i.e. the command will just delete the region. This is for
45 ;; commands that normally delete small amounts of text, like
46 ;; a single character -- they will instead delete the whole
47 ;; active region.
48 ;; `kill'
49 ;; `kill-region' is used on the selection, rather than
50 ;; `delete-region'. (Text selected with the mouse will typically
51 ;; be yankable anyhow.)
52 ;; t
53 ;; The normal case: delete the active region prior to executing
54 ;; the command which will insert replacement text.
55 ;; FUNCTION
56 ;; For commands which need to dynamically determine this behavior.
57 ;; FUNCTION should take no argument and return one of the above
58 ;; values, or nil. In the latter case, FUNCTION should itself
59 ;; do with the active region whatever is appropriate."
61 ;;; Code:
63 (defvar delete-selection-save-to-register nil
64 "If non-nil, deleted region text is stored in this register.
65 Value must be the register (key) to use.")
67 ;;;###autoload
68 (defalias 'pending-delete-mode 'delete-selection-mode)
70 ;;;###autoload
71 (define-minor-mode delete-selection-mode
72 "Toggle Delete Selection mode.
73 With a prefix argument ARG, enable Delete Selection mode if ARG
74 is positive, and disable it otherwise. If called from Lisp,
75 enable the mode if ARG is omitted or nil.
77 When Delete Selection mode is enabled, typed text replaces the selection
78 if the selection is active. Otherwise, typed text is just inserted at
79 point regardless of any selection. Also, commands that normally delete
80 just one character will delete the entire selection instead.
82 See `delete-selection-helper' and `delete-selection-pre-hook' for
83 information on adapting behavior of commands in Delete Selection mode."
84 :global t :group 'editing-basics
85 (if (not delete-selection-mode)
86 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
87 (add-hook 'pre-command-hook 'delete-selection-pre-hook)))
89 (defvar delsel--replace-text-or-position nil)
91 (defun delete-active-region (&optional killp)
92 "Delete the active region.
93 If KILLP in not-nil, the active region is killed instead of deleted."
94 (cond
95 (killp
96 ;; Don't allow `kill-region' to change the value of `this-command'.
97 (let (this-command)
98 (kill-region (point) (mark) t)))
99 (delete-selection-save-to-register
100 (set-register delete-selection-save-to-register
101 (funcall region-extract-function t))
102 (setq delsel--replace-text-or-position
103 (cons (current-buffer)
104 (and (consp buffer-undo-list) (car buffer-undo-list)))))
106 (funcall region-extract-function 'delete-only))))
108 (defun delete-selection-repeat-replace-region (arg)
109 "Repeat replacing text of highlighted region with typed text.
110 Search for the next stretch of text identical to the region last replaced
111 by typing text over it and replaces it with the same stretch of text.
112 With ARG, repeat that many times. `C-u' means until end of buffer."
113 (interactive "P")
114 (let ((old-text (and delete-selection-save-to-register
115 (get-register delete-selection-save-to-register)))
116 (count (if (consp arg) (point-max)
117 (prefix-numeric-value current-prefix-arg))))
118 (if (not (and old-text
119 (> (length old-text) 0)
120 (or (stringp delsel--replace-text-or-position)
121 (buffer-live-p (car delsel--replace-text-or-position)))))
122 (message "No known previous replacement")
123 ;; If this is the first use after overwriting regions,
124 ;; find the replacement text by looking at the undo list.
125 (when (consp delsel--replace-text-or-position)
126 (let ((buffer (car delsel--replace-text-or-position))
127 (elt (cdr delsel--replace-text-or-position)))
128 (setq delsel--replace-text-or-position nil)
129 (with-current-buffer buffer
130 (save-restriction
131 (widen)
132 ;; Find the text that replaced the region via the undo list.
133 (let ((ul buffer-undo-list) u s e)
134 (when elt
135 (while (consp ul)
136 (setq u (car ul) ul (cdr ul))
137 (cond
138 ((eq u elt) ;; got it
139 (setq ul nil))
140 ((and (consp u) (integerp (car u)) (integerp (cdr u)))
141 (if (and s (= (cdr u) s))
142 (setq s (car u))
143 (setq s (car u) e (cdr u)))))))
144 (cond ((and s e (<= s e) (= s (mark t)))
145 (setq delsel--replace-text-or-position
146 (filter-buffer-substring s e))
147 (set-text-properties
148 0 (length delsel--replace-text-or-position)
149 nil delsel--replace-text-or-position))
150 ((and (null s) (eq u elt)) ;; Nothing inserted.
151 (setq delsel--replace-text-or-position ""))
153 (message "Cannot locate replacement text"))))))))
154 (while (and (> count 0)
155 delsel--replace-text-or-position
156 (search-forward old-text nil t))
157 (replace-match delsel--replace-text-or-position nil t)
158 (setq count (1- count))))))
160 (defun delete-selection-helper (type)
161 "Delete selection according to TYPE:
162 `yank'
163 For commands which do a yank; ensures the region about to be
164 deleted isn't immediately yanked back, which would make the
165 command a no-op.
166 `supersede'
167 Delete the active region and ignore the current command,
168 i.e. the command will just delete the region. This is for
169 commands that normally delete small amounts of text, like
170 a single character -- they will instead delete the whole
171 active region.
172 `kill'
173 `kill-region' is used on the selection, rather than
174 `delete-region'. (Text selected with the mouse will
175 typically be yankable anyhow.)
176 FUNCTION
177 For commands which need to dynamically determine this
178 behavior. FUNCTION should take no argument and return a
179 value acceptable as TYPE, or nil. In the latter case,
180 FUNCTION should itself do with the active region whatever is
181 appropriate.
182 Other non-nil values
183 The normal case: delete the active region prior to executing
184 the command which will insert replacement text."
185 (condition-case data
186 (cond ((eq type 'kill) ;Deprecated, backward compatibility.
187 (delete-active-region t)
188 (if (and overwrite-mode
189 (eq this-command 'self-insert-command))
190 (let ((overwrite-mode nil))
191 (self-insert-command
192 (prefix-numeric-value current-prefix-arg))
193 (setq this-command 'ignore))))
194 ((eq type 'yank)
195 ;; Before a yank command, make sure we don't yank the
196 ;; head of the kill-ring that really comes from the
197 ;; currently active region we are going to delete.
198 ;; That would make yank a no-op.
199 (when (and (string= (buffer-substring-no-properties
200 (point) (mark))
201 (car kill-ring))
202 (fboundp 'mouse-region-match)
203 (mouse-region-match))
204 (current-kill 1))
205 (let ((pos (copy-marker (region-beginning))))
206 (delete-active-region)
207 ;; If the region was, say, rectangular, make sure we yank
208 ;; from the top, to "replace".
209 (goto-char pos)))
210 ((eq type 'supersede)
211 (let ((empty-region (= (point) (mark))))
212 (delete-active-region)
213 (unless empty-region
214 (setq this-command 'ignore))))
215 ((functionp type) (delete-selection-helper (funcall type)))
216 (type
217 (delete-active-region)
218 (if (and overwrite-mode
219 (eq this-command 'self-insert-command))
220 (let ((overwrite-mode nil))
221 (self-insert-command
222 (prefix-numeric-value current-prefix-arg))
223 (setq this-command 'ignore)))))
224 ;; If ask-user-about-supersession-threat signals an error,
225 ;; stop safe_run_hooks from clearing out pre-command-hook.
226 (file-supersession (message "%s" (cadr data)) (ding))
227 (text-read-only
228 ;; This signal may come either from `delete-active-region' or
229 ;; `self-insert-command' (when `overwrite-mode' is non-nil).
230 ;; To avoid clearing out `pre-command-hook' we handle this case
231 ;; by issuing a simple message. Note, however, that we do not
232 ;; handle all related problems: When read-only text ends before
233 ;; the end of the region, the latter is not deleted but any
234 ;; subsequent insertion will succeed. We could avoid this case
235 ;; by doing a (setq this-command 'ignore) here. This would,
236 ;; however, still not handle the case where read-only text ends
237 ;; precisely where the region starts: In that case the deletion
238 ;; would succeed but the subsequent insertion would fail with a
239 ;; text-read-only error. To handle that case we would have to
240 ;; investigate text properties at both ends of the region and
241 ;; skip the deletion when inserting text is forbidden there.
242 (message "Text is read-only") (ding))))
244 (defun delete-selection-pre-hook ()
245 "Function run before commands that delete selections are executed.
246 Commands which will delete the selection need a `delete-selection'
247 property on their symbol; commands which insert text but don't
248 have this property won't delete the selection.
249 See `delete-selection-helper'."
250 (when (and delete-selection-mode (use-region-p)
251 (not buffer-read-only))
252 (delete-selection-helper (and (symbolp this-command)
253 (get this-command 'delete-selection)))))
255 (defun delete-selection-uses-region-p ()
256 "Return t when the current command will be using the region
257 rather than having `delete-selection' delete it, nil otherwise.
259 This function is intended for use as the value of the
260 `delete-selection' property of a command, and shouldn't be used
261 for anything else."
262 (not (run-hook-with-args-until-success
263 'self-insert-uses-region-functions)))
265 (put 'self-insert-command 'delete-selection 'delete-selection-uses-region-p)
267 (put 'insert-char 'delete-selection t)
268 (put 'quoted-insert 'delete-selection t)
270 (put 'yank 'delete-selection 'yank)
271 (put 'clipboard-yank 'delete-selection 'yank)
272 (put 'insert-register 'delete-selection t)
273 ;; delete-backward-char and delete-forward-char already delete the selection by
274 ;; default, but not delete-char.
275 (put 'delete-char 'delete-selection 'supersede)
277 (put 'reindent-then-newline-and-indent 'delete-selection t)
278 (put 'newline-and-indent 'delete-selection t)
279 (put 'newline 'delete-selection t)
280 (put 'electric-newline-and-maybe-indent 'delete-selection t)
281 (put 'open-line 'delete-selection t)
283 ;; This is very useful for canceling a selection in the minibuffer without
284 ;; aborting the minibuffer.
285 (defun minibuffer-keyboard-quit ()
286 "Abort recursive edit.
287 In Delete Selection mode, if the mark is active, just deactivate it;
288 then it takes a second \\[keyboard-quit] to abort the minibuffer."
289 (interactive)
290 (if (and delete-selection-mode (region-active-p))
291 (setq deactivate-mark t)
292 (abort-recursive-edit)))
294 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
295 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
296 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
297 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
298 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
300 (defun delsel-unload-function ()
301 "Unload the Delete Selection library."
302 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
303 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
304 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
305 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
306 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit)
307 (dolist (sym '(self-insert-command insert-char quoted-insert yank
308 clipboard-yank insert-register newline-and-indent
309 reindent-then-newline-and-indent newline open-line))
310 (put sym 'delete-selection nil))
311 ;; continue standard unloading
312 nil)
314 (provide 'delsel)
316 ;;; delsel.el ends here