Switch to recommended form of GPLv3 permissions notice.
[bpt/emacs.git] / lisp / delsel.el
CommitLineData
28d3ed91 1;;; delsel.el --- delete selection if you insert
76550a57 2
0d30b337 3;; Copyright (C) 1992, 1997, 1998, 2001, 2002, 2003, 2004,
409cc4a3 4;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
76550a57
ER
5
6;; Author: Matthieu Devin <devin@lucid.com>
4228277d 7;; Maintainer: FSF
76550a57 8;; Created: 14 Jul 92
f947a7fa 9;; Keywords: convenience emulations
b0dbaa21 10
b578f267 11;; This file is part of GNU Emacs.
b0dbaa21 12
eb3fa2cf 13;; GNU Emacs is free software: you can redistribute it and/or modify
b578f267 14;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
15;; the Free Software Foundation, either version 3 of the License, or
16;; (at your option) any later version.
b0dbaa21 17
b578f267
EN
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.
b0dbaa21 22
b578f267 23;; You should have received a copy of the GNU General Public License
eb3fa2cf 24;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
b0dbaa21 25
76550a57 26;;; Commentary:
b0dbaa21 27
b578f267
EN
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.
b0dbaa21 31
69b3c6c7
DL
32;; Interface:
33
34;; Commands which will delete the selection need a 'delete-selection
35;; property on their symbols; commands which insert text but don't
c815b73f 36;; have this property won't delete the selection. It can be one of
69b3c6c7
DL
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;; non-nil
49;; The normal case: delete the active region prior to executing
50;; the command which will insert replacement text.
76550a57 51
69b3c6c7 52;;; Code:
7853929f
SM
53
54;;;###autoload
55(defalias 'pending-delete-mode 'delete-selection-mode)
56
57;;;###autoload
ad03cafc 58(define-minor-mode delete-selection-mode
7853929f 59 "Toggle Delete Selection mode.
f7fdcecd
RS
60With prefix ARG, turn Delete Selection mode on if ARG is
61positive, off if ARG is not positive.
7853929f 62
69b3c6c7
DL
63When Delete Selection mode is enabled, Transient Mark mode is also
64enabled and typed text replaces the selection if the selection is
65active. Otherwise, typed text is just inserted at point regardless of
66any selection."
82fdafde 67 :global t :group 'editing-basics
7853929f
SM
68 (if (not delete-selection-mode)
69 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
70 (add-hook 'pre-command-hook 'delete-selection-pre-hook)
71 (transient-mark-mode t)))
72
b0dbaa21 73(defun delete-active-region (&optional killp)
af9157b9
RS
74 (if killp
75 (kill-region (point) (mark))
76 (delete-region (point) (mark)))
af9157b9 77 t)
b0dbaa21 78
28d3ed91 79(defun delete-selection-pre-hook ()
7853929f
SM
80 (when (and delete-selection-mode transient-mark-mode mark-active
81 (not buffer-read-only))
82 (let ((type (and (symbolp this-command)
83 (get this-command 'delete-selection))))
f13e84fa
RS
84 (condition-case data
85 (cond ((eq type 'kill)
86 (delete-active-region t))
87 ((eq type 'yank)
195d88f4
JL
88 ;; Before a yank command, make sure we don't yank the
89 ;; head of the kill-ring that really comes from the
67755cc4
JL
90 ;; currently active region we are going to delete.
91 ;; That would make yank a no-op.
195d88f4
JL
92 (when (and (string= (buffer-substring-no-properties (point) (mark))
93 (car kill-ring))
614a773a 94 (fboundp 'mouse-region-match)
67755cc4 95 (mouse-region-match))
f13e84fa
RS
96 (current-kill 1))
97 (delete-active-region))
98 ((eq type 'supersede)
99 (let ((empty-region (= (point) (mark))))
100 (delete-active-region)
101 (unless empty-region
102 (setq this-command 'ignore))))
103 (type
ec08e2f4
JL
104 (delete-active-region)
105 (if (and overwrite-mode (eq this-command 'self-insert-command))
106 (let ((overwrite-mode nil))
107 (self-insert-command (prefix-numeric-value current-prefix-arg))
108 (setq this-command 'ignore)))))
f13e84fa
RS
109 (file-supersession
110 ;; If ask-user-about-supersession-threat signals an error,
111 ;; stop safe_run_hooks from clearing out pre-command-hook.
112 (and (eq inhibit-quit 'pre-command-hook)
113 (setq inhibit-quit 'delete-selection-dummy))
578877a1
MR
114 (signal 'file-supersession (cdr data)))
115 (text-read-only
116 ;; This signal may come either from `delete-active-region' or
117 ;; `self-insert-command' (when `overwrite-mode' is non-nil).
118 ;; To avoid clearing out `pre-command-hook' we handle this case
119 ;; by issuing a simple message. Note, however, that we do not
120 ;; handle all related problems: When read-only text ends before
121 ;; the end of the region, the latter is not deleted but any
122 ;; subsequent insertion will succeed. We could avoid this case
123 ;; by doing a (setq this-command 'ignore) here. This would,
124 ;; however, still not handle the case where read-only text ends
125 ;; precisely where the region starts: In that case the deletion
126 ;; would succeed but the subsequent insertion would fail with a
127 ;; text-read-only error. To handle that case we would have to
128 ;; investigate text properties at both ends of the region and
129 ;; skip the deletion when inserting text is forbidden there.
130 (message "Text is read-only") (ding))))))
b0dbaa21 131
28d3ed91 132(put 'self-insert-command 'delete-selection t)
cc5ac2c6 133(put 'self-insert-iso 'delete-selection t)
b0dbaa21 134
6b214411 135(put 'yank 'delete-selection 'yank)
b708f0ad 136(put 'clipboard-yank 'delete-selection 'yank)
d4df3279 137(put 'insert-register 'delete-selection t)
b0dbaa21 138
28d3ed91
RS
139(put 'delete-backward-char 'delete-selection 'supersede)
140(put 'backward-delete-char-untabify 'delete-selection 'supersede)
141(put 'delete-char 'delete-selection 'supersede)
b0dbaa21 142
69b3c6c7 143(put 'newline-and-indent 'delete-selection t)
28d3ed91 144(put 'newline 'delete-selection t)
69b3c6c7
DL
145(put 'open-line 'delete-selection 'kill)
146
23652376 147;; This is very useful for cancelling a selection in the minibuffer without
b0dbaa21 148;; aborting the minibuffer.
b0dbaa21
RS
149(defun minibuffer-keyboard-quit ()
150 "Abort recursive edit.
69b3c6c7
DL
151In Delete Selection mode, if the mark is active, just deactivate it;
152then it takes a second \\[keyboard-quit] to abort the minibuffer."
b0dbaa21 153 (interactive)
d4df3279
RS
154 (if (and delete-selection-mode transient-mark-mode mark-active)
155 (setq deactivate-mark t)
b0dbaa21
RS
156 (abort-recursive-edit)))
157
23652376
DL
158(define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
159(define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
160(define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
161(define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
162(define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
163
c815b73f
JB
164(defun delsel-unload-function ()
165 "Unload the Delete Selection library."
23652376
DL
166 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
167 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
168 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
169 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
c815b73f
JB
170 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit)
171 (dolist (sym '(self-insert-command self-insert-iso yank clipboard-yank
172 insert-register delete-backward-char backward-delete-char-untabify
173 delete-char newline-and-indent newline open-line))
8c8e1952 174 (put sym 'delete-selection nil))
c815b73f
JB
175 ;; continue standard unloading
176 nil)
87f14b12 177
d4df3279 178(provide 'delsel)
b0dbaa21 179
cbee283d 180;; arch-tag: 1e388890-1b50-4ed0-9347-763b1343b6ed
d4df3279 181;;; delsel.el ends here