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