Merge from emacs--rel--22
[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,
d7a0267c 4;; 2005, 2006, 2007 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
b578f267
EN
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 2, or (at your option)
16;; 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
EN
23;; You should have received a copy of the GNU General Public License
24;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
25;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26;; Boston, MA 02110-1301, USA.
b0dbaa21 27
76550a57 28;;; Commentary:
b0dbaa21 29
b578f267
EN
30;; This file makes the active region be pending delete, meaning that
31;; text inserted while the region is active will replace the region contents.
32;; This is a popular behavior of personal computers text editors.
b0dbaa21 33
69b3c6c7
DL
34;; Interface:
35
36;; Commands which will delete the selection need a 'delete-selection
37;; property on their symbols; commands which insert text but don't
38;; have this property won't delete the selction. It can be one of
39;; the values:
40;; 'yank
41;; For commands which do a yank; ensures the region about to be
42;; deleted isn't yanked.
43;; 'supersede
44;; Delete the active region and ignore the current command,
45;; i.e. the command will just delete the region.
46;; 'kill
47;; `kill-region' is used on the selection, rather than
48;; `delete-region'. (Text selected with the mouse will typically
49;; be yankable anyhow.)
50;; non-nil
51;; The normal case: delete the active region prior to executing
52;; the command which will insert replacement text.
76550a57 53
69b3c6c7 54;;; Code:
7853929f
SM
55
56;;;###autoload
57(defalias 'pending-delete-mode 'delete-selection-mode)
58
59;;;###autoload
ad03cafc 60(define-minor-mode delete-selection-mode
7853929f 61 "Toggle Delete Selection mode.
69b3c6c7
DL
62With prefix ARG, turn Delete Selection mode on if and only if ARG is
63positive.
7853929f 64
69b3c6c7
DL
65When Delete Selection mode is enabled, Transient Mark mode is also
66enabled and typed text replaces the selection if the selection is
67active. Otherwise, typed text is just inserted at point regardless of
68any selection."
82fdafde 69 :global t :group 'editing-basics
7853929f
SM
70 (if (not delete-selection-mode)
71 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
72 (add-hook 'pre-command-hook 'delete-selection-pre-hook)
73 (transient-mark-mode t)))
74
b0dbaa21 75(defun delete-active-region (&optional killp)
af9157b9
RS
76 (if killp
77 (kill-region (point) (mark))
78 (delete-region (point) (mark)))
af9157b9 79 t)
b0dbaa21 80
28d3ed91 81(defun delete-selection-pre-hook ()
7853929f
SM
82 (when (and delete-selection-mode transient-mark-mode mark-active
83 (not buffer-read-only))
84 (let ((type (and (symbolp this-command)
85 (get this-command 'delete-selection))))
f13e84fa
RS
86 (condition-case data
87 (cond ((eq type 'kill)
88 (delete-active-region t))
89 ((eq type 'yank)
195d88f4
JL
90 ;; Before a yank command, make sure we don't yank the
91 ;; head of the kill-ring that really comes from the
92 ;; currently active region we are going to delete
93 ;; (when last-command is one that uses copy-region-as-kill
94 ;; or kill-new). That would make yank a no-op.
95 (when (and (string= (buffer-substring-no-properties (point) (mark))
96 (car kill-ring))
97 (memq last-command
98 '(mouse-set-region mouse-drag-region
99 mouse-save-then-kill mouse-secondary-save-then-kill)))
f13e84fa
RS
100 (current-kill 1))
101 (delete-active-region))
102 ((eq type 'supersede)
103 (let ((empty-region (= (point) (mark))))
104 (delete-active-region)
105 (unless empty-region
106 (setq this-command 'ignore))))
107 (type
ec08e2f4
JL
108 (delete-active-region)
109 (if (and overwrite-mode (eq this-command 'self-insert-command))
110 (let ((overwrite-mode nil))
111 (self-insert-command (prefix-numeric-value current-prefix-arg))
112 (setq this-command 'ignore)))))
f13e84fa
RS
113 (file-supersession
114 ;; If ask-user-about-supersession-threat signals an error,
115 ;; stop safe_run_hooks from clearing out pre-command-hook.
116 (and (eq inhibit-quit 'pre-command-hook)
117 (setq inhibit-quit 'delete-selection-dummy))
118 (signal 'file-supersession (cdr data)))))))
b0dbaa21 119
28d3ed91 120(put 'self-insert-command 'delete-selection t)
cc5ac2c6 121(put 'self-insert-iso 'delete-selection t)
b0dbaa21 122
6b214411 123(put 'yank 'delete-selection 'yank)
b708f0ad 124(put 'clipboard-yank 'delete-selection 'yank)
d4df3279 125(put 'insert-register 'delete-selection t)
b0dbaa21 126
28d3ed91
RS
127(put 'delete-backward-char 'delete-selection 'supersede)
128(put 'backward-delete-char-untabify 'delete-selection 'supersede)
129(put 'delete-char 'delete-selection 'supersede)
b0dbaa21 130
69b3c6c7 131(put 'newline-and-indent 'delete-selection t)
28d3ed91 132(put 'newline 'delete-selection t)
69b3c6c7
DL
133(put 'open-line 'delete-selection 'kill)
134
23652376 135;; This is very useful for cancelling a selection in the minibuffer without
b0dbaa21 136;; aborting the minibuffer.
b0dbaa21
RS
137(defun minibuffer-keyboard-quit ()
138 "Abort recursive edit.
69b3c6c7
DL
139In Delete Selection mode, if the mark is active, just deactivate it;
140then it takes a second \\[keyboard-quit] to abort the minibuffer."
b0dbaa21 141 (interactive)
d4df3279
RS
142 (if (and delete-selection-mode transient-mark-mode mark-active)
143 (setq deactivate-mark t)
b0dbaa21
RS
144 (abort-recursive-edit)))
145
23652376
DL
146(define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
147(define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
148(define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
149(define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
150(define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
151
152(defun delsel-unload-hook ()
153 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
154 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
155 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
156 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
157 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit))
b0dbaa21 158
87f14b12
RS
159(add-hook 'delsel-unload-hook 'delsel-unload-hook)
160
d4df3279 161(provide 'delsel)
b0dbaa21 162
ab5796a9 163;;; arch-tag: 1e388890-1b50-4ed0-9347-763b1343b6ed
d4df3279 164;;; delsel.el ends here