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