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