(Qcenter): New variable.
[bpt/emacs.git] / lisp / delsel.el
CommitLineData
28d3ed91 1;;; delsel.el --- delete selection if you insert
76550a57 2
69b3c6c7 3;; Copyright (C) 1992, 1997, 1998 Free Software Foundation, Inc.
76550a57
ER
4
5;; Author: Matthieu Devin <devin@lucid.com>
4228277d 6;; Maintainer: FSF
76550a57 7;; Created: 14 Jul 92
b0dbaa21 8
b578f267 9;; This file is part of GNU Emacs.
b0dbaa21 10
b578f267
EN
11;; GNU Emacs is free software; you can redistribute it and/or modify
12;; it under the terms of the GNU General Public License as published by
13;; the Free Software Foundation; either version 2, or (at your option)
14;; any later version.
b0dbaa21 15
b578f267
EN
16;; GNU Emacs is distributed in the hope that it will be useful,
17;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19;; GNU General Public License for more details.
b0dbaa21 20
b578f267
EN
21;; You should have received a copy of the GNU General Public License
22;; along with GNU Emacs; see the file COPYING. If not, write to the
23;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24;; Boston, MA 02111-1307, USA.
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
36;; have this property won't delete the selction. 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;; 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
58(defun delete-selection-mode (&optional arg)
59 "Toggle Delete Selection mode.
69b3c6c7
DL
60With prefix ARG, turn Delete Selection mode on if and only if ARG is
61positive.
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."
7853929f
SM
67 (interactive "P")
68 (setq delete-selection-mode (if arg
69 (> (prefix-numeric-value arg) 0)
70 (not delete-selection-mode)))
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)
74 (transient-mark-mode t)))
75
76;;;###autoload
77(defcustom delete-selection-mode nil
78 "Toggle Delete Selection mode.
69b3c6c7 79See command `delete-selection-mode'.
735cb9a3
DL
80Setting this variable directly does not take effect;
81use either \\[customize] or the function `delete-selection-mode'."
7853929f
SM
82 :set (lambda (symbol value)
83 (delete-selection-mode (or value 0)))
16be0abe 84 :initialize 'custom-initialize-default
7853929f
SM
85 :type 'boolean
86 :group 'editing-basics
87 :require 'delsel)
af9157b9 88
b0dbaa21 89(defun delete-active-region (&optional killp)
af9157b9
RS
90 (if killp
91 (kill-region (point) (mark))
92 (delete-region (point) (mark)))
93 (setq mark-active nil)
94 (run-hooks 'deactivate-mark-hook)
95 t)
b0dbaa21 96
28d3ed91 97(defun delete-selection-pre-hook ()
7853929f
SM
98 (when (and delete-selection-mode transient-mark-mode mark-active
99 (not buffer-read-only))
100 (let ((type (and (symbolp this-command)
101 (get this-command 'delete-selection))))
102 (cond ((eq type 'kill)
103 (delete-active-region t))
104 ((eq type 'yank)
105 ;; Before a yank command,
106 ;; make sure we don't yank the same region
107 ;; that we are going to delete.
108 ;; That would make yank a no-op.
109 (when (string= (buffer-substring-no-properties (point) (mark))
6b214411 110 (car kill-ring))
7853929f 111 (current-kill 1))
69b3c6c7 112 (delete-active-region))
7853929f 113 ((eq type 'supersede)
69b3c6c7
DL
114 (delete-active-region)
115 (setq this-command 'ignore))
7853929f 116 (type
69b3c6c7 117 (delete-active-region))))))
b0dbaa21 118
28d3ed91 119(put 'self-insert-command 'delete-selection t)
cc5ac2c6 120(put 'self-insert-iso 'delete-selection t)
b0dbaa21 121
6b214411 122(put 'yank 'delete-selection 'yank)
b708f0ad 123(put 'clipboard-yank 'delete-selection 'yank)
d4df3279 124(put 'insert-register 'delete-selection t)
b0dbaa21 125
28d3ed91
RS
126(put 'delete-backward-char 'delete-selection 'supersede)
127(put 'backward-delete-char-untabify 'delete-selection 'supersede)
128(put 'delete-char 'delete-selection 'supersede)
b0dbaa21 129
69b3c6c7 130(put 'newline-and-indent 'delete-selection t)
28d3ed91 131(put 'newline 'delete-selection t)
69b3c6c7
DL
132(put 'open-line 'delete-selection 'kill)
133
134(put 'insert-parentheses 'delete-selection t)
b0dbaa21 135
b0dbaa21
RS
136;; This is very useful for cancelling a selection in the minibuffer without
137;; aborting the minibuffer.
b0dbaa21
RS
138(defun minibuffer-keyboard-quit ()
139 "Abort recursive edit.
69b3c6c7
DL
140In Delete Selection mode, if the mark is active, just deactivate it;
141then it takes a second \\[keyboard-quit] to abort the minibuffer."
b0dbaa21 142 (interactive)
d4df3279
RS
143 (if (and delete-selection-mode transient-mark-mode mark-active)
144 (setq deactivate-mark t)
b0dbaa21
RS
145 (abort-recursive-edit)))
146
147(define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
28d3ed91
RS
148(define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
149(define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
150(define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
151(define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
b0dbaa21 152
d4df3279 153(provide 'delsel)
b0dbaa21 154
16be0abe
RS
155;; This is the standard way mechanism to put the mode into effect
156;; if delete-selection-mode has already been set to t
157;; when this file is loaded.
158(when delete-selection-mode
159 (delete-selection-mode t))
160
d4df3279 161;;; delsel.el ends here