(add-log-current-defun): Check major-mode for `cperl-mode' as well.
[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
f947a7fa 8;; Keywords: convenience emulations
b0dbaa21 9
b578f267 10;; This file is part of GNU Emacs.
b0dbaa21 11
b578f267
EN
12;; GNU Emacs is free software; you can redistribute it and/or modify
13;; it under the terms of the GNU General Public License as published by
14;; the Free Software Foundation; either version 2, or (at your option)
15;; 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
EN
22;; You should have received a copy of the GNU General Public License
23;; along with GNU Emacs; see the file COPYING. If not, write to the
24;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25;; Boston, MA 02111-1307, USA.
b0dbaa21 26
76550a57 27;;; Commentary:
b0dbaa21 28
b578f267
EN
29;; This file makes the active region be pending delete, meaning that
30;; text inserted while the region is active will replace the region contents.
31;; This is a popular behavior of personal computers text editors.
b0dbaa21 32
69b3c6c7
DL
33;; Interface:
34
35;; Commands which will delete the selection need a 'delete-selection
36;; property on their symbols; commands which insert text but don't
37;; have this property won't delete the selction. It can be one of
38;; the values:
39;; 'yank
40;; For commands which do a yank; ensures the region about to be
41;; deleted isn't yanked.
42;; 'supersede
43;; Delete the active region and ignore the current command,
44;; i.e. the command will just delete the region.
45;; 'kill
46;; `kill-region' is used on the selection, rather than
47;; `delete-region'. (Text selected with the mouse will typically
48;; be yankable anyhow.)
49;; non-nil
50;; The normal case: delete the active region prior to executing
51;; the command which will insert replacement text.
76550a57 52
69b3c6c7 53;;; Code:
7853929f
SM
54
55;;;###autoload
56(defalias 'pending-delete-mode 'delete-selection-mode)
57
58;;;###autoload
ad03cafc 59(define-minor-mode delete-selection-mode
7853929f 60 "Toggle Delete Selection mode.
69b3c6c7
DL
61With prefix ARG, turn Delete Selection mode on if and only if ARG is
62positive.
7853929f 63
69b3c6c7
DL
64When Delete Selection mode is enabled, Transient Mark mode is also
65enabled and typed text replaces the selection if the selection is
66active. Otherwise, typed text is just inserted at point regardless of
67any selection."
82fdafde 68 :global t :group 'editing-basics
7853929f
SM
69 (if (not delete-selection-mode)
70 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
71 (add-hook 'pre-command-hook 'delete-selection-pre-hook)
72 (transient-mark-mode t)))
73
b0dbaa21 74(defun delete-active-region (&optional killp)
af9157b9
RS
75 (if killp
76 (kill-region (point) (mark))
77 (delete-region (point) (mark)))
78 (setq mark-active nil)
79 (run-hooks 'deactivate-mark-hook)
80 t)
b0dbaa21 81
28d3ed91 82(defun delete-selection-pre-hook ()
7853929f
SM
83 (when (and delete-selection-mode transient-mark-mode mark-active
84 (not buffer-read-only))
85 (let ((type (and (symbolp this-command)
86 (get this-command 'delete-selection))))
87 (cond ((eq type 'kill)
88 (delete-active-region t))
89 ((eq type 'yank)
90 ;; Before a yank command,
91 ;; make sure we don't yank the same region
92 ;; that we are going to delete.
93 ;; That would make yank a no-op.
94 (when (string= (buffer-substring-no-properties (point) (mark))
6b214411 95 (car kill-ring))
7853929f 96 (current-kill 1))
69b3c6c7 97 (delete-active-region))
7853929f 98 ((eq type 'supersede)
69b3c6c7
DL
99 (delete-active-region)
100 (setq this-command 'ignore))
7853929f 101 (type
69b3c6c7 102 (delete-active-region))))))
b0dbaa21 103
28d3ed91 104(put 'self-insert-command 'delete-selection t)
cc5ac2c6 105(put 'self-insert-iso 'delete-selection t)
b0dbaa21 106
6b214411 107(put 'yank 'delete-selection 'yank)
b708f0ad 108(put 'clipboard-yank 'delete-selection 'yank)
d4df3279 109(put 'insert-register 'delete-selection t)
b0dbaa21 110
28d3ed91
RS
111(put 'delete-backward-char 'delete-selection 'supersede)
112(put 'backward-delete-char-untabify 'delete-selection 'supersede)
113(put 'delete-char 'delete-selection 'supersede)
b0dbaa21 114
69b3c6c7 115(put 'newline-and-indent 'delete-selection t)
28d3ed91 116(put 'newline 'delete-selection t)
69b3c6c7
DL
117(put 'open-line 'delete-selection 'kill)
118
119(put 'insert-parentheses 'delete-selection t)
b0dbaa21 120
23652376 121;; This is very useful for cancelling a selection in the minibuffer without
b0dbaa21 122;; aborting the minibuffer.
b0dbaa21
RS
123(defun minibuffer-keyboard-quit ()
124 "Abort recursive edit.
69b3c6c7
DL
125In Delete Selection mode, if the mark is active, just deactivate it;
126then it takes a second \\[keyboard-quit] to abort the minibuffer."
b0dbaa21 127 (interactive)
d4df3279
RS
128 (if (and delete-selection-mode transient-mark-mode mark-active)
129 (setq deactivate-mark t)
b0dbaa21
RS
130 (abort-recursive-edit)))
131
23652376
DL
132(define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
133(define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
134(define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
135(define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
136(define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
137
138(defun delsel-unload-hook ()
139 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
140 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
141 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
142 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
143 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit))
b0dbaa21 144
d4df3279 145(provide 'delsel)
b0dbaa21 146
d4df3279 147;;; delsel.el ends here