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