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