(read_key_sequence): Reexamine this_command
[bpt/emacs.git] / lisp / delsel.el
CommitLineData
28d3ed91 1;;; delsel.el --- delete selection if you insert
76550a57 2
b0dbaa21 3;;; Copyright (C) 1992 Free Software Foundation, Inc.
76550a57
ER
4
5;; Author: Matthieu Devin <devin@lucid.com>
6;; Created: 14 Jul 92
7;; Last change 18-Feb-93, devin.
b0dbaa21
RS
8
9;;; This file is part of GNU Emacs.
10
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.
15
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.
20
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
23;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
24
76550a57 25;;; Commentary:
b0dbaa21 26
76550a57 27;;; This file makes the active region be pending delete, meaning that
b0dbaa21
RS
28;;; text inserted while the region is active will replace the region contents.
29;;; This is a popular behavior of personal computers text editors.
30
76550a57
ER
31;;; Code:
32
28d3ed91
RS
33(defvar delete-selection-mode t
34 "*Non-nil means Delete Selection mode is enabled.
35In Delete Selection mode, when a region is highlighted,
af9157b9
RS
36insertion commands first delete the region and then insert.")
37
b0dbaa21 38(defun delete-active-region (&optional killp)
af9157b9
RS
39 (if killp
40 (kill-region (point) (mark))
41 (delete-region (point) (mark)))
42 (setq mark-active nil)
43 (run-hooks 'deactivate-mark-hook)
44 t)
b0dbaa21 45
28d3ed91
RS
46(defun delete-selection-pre-hook ()
47 (if (and delete-selection-mode
af9157b9
RS
48 (not buffer-read-only)
49 transient-mark-mode mark-active)
50 (let ((type (and (symbolp this-command)
28d3ed91 51 (get this-command 'delete-selection))))
af9157b9
RS
52 (cond ((eq type 'kill)
53 (delete-active-region t))
54 ((eq type 'supersede)
55 (if (delete-active-region ())
56 (setq this-command '(lambda () (interactive)))))
57 (type
58 (delete-active-region ()))))))
59
28d3ed91 60(add-hook 'pre-command-hook 'delete-selection-pre-hook)
b0dbaa21 61
28d3ed91 62(put 'self-insert-command 'delete-selection t)
b0dbaa21 63
28d3ed91
RS
64(put 'yank 'delete-selection t)
65(put 'x-yank-clipboard-selection 'delete-selection t)
b0dbaa21 66
28d3ed91
RS
67(put 'delete-backward-char 'delete-selection 'supersede)
68(put 'backward-delete-char-untabify 'delete-selection 'supersede)
69(put 'delete-char 'delete-selection 'supersede)
b0dbaa21 70
28d3ed91
RS
71(put 'newline-and-indent 'delete-selection 't)
72(put 'newline 'delete-selection t)
73(put 'open-line 'delete-selection t)
b0dbaa21 74
28d3ed91
RS
75(defalias 'pending-delete-mode 'delete-selection-mode)
76(defun delete-selection-mode (arg)
77 "Toggle Delete Selection mode.
b0dbaa21
RS
78When ON, typed text replaces the selection if the selection is active.
79When OFF, typed text is just inserted at point."
af9157b9 80 (interactive "P")
28d3ed91
RS
81 (setq delete-selection-mode
82 (if (null arg) (not delete-selection-mode)
af9157b9
RS
83 (> (prefix-numeric-value arg) 0)))
84 (set-buffer-modified-p (buffer-modified-p))) ;No-op, but updates mode line.
b0dbaa21
RS
85
86;; This new definition of control-G makes the first control-G disown the
87;; selection and the second one signal a QUIT.
88;; This is very useful for cancelling a selection in the minibuffer without
89;; aborting the minibuffer.
28d3ed91 90;; It has actually nothing to do with delete-selection but its more necessary
b0dbaa21
RS
91;; with pending delete because pending delete users use the selection more.
92(defun keyboard-quit ()
93 "Signal a `quit' condition.
af9157b9
RS
94During execution of Lisp code, this character causes a quit directly.
95At top-level, as an editor command, this simply beeps.
b0dbaa21
RS
96In Transient Mark mode, if the mark is active, just deactivate it."
97 (interactive)
98 (if (and transient-mark-mode mark-active)
99 (progn
100 ;; Don't beep if just deactivating the region.
101 (setq mark-active nil)
102 (run-hooks 'deactivate-mark-hook))
103 (signal 'quit nil)))
104
105(defun minibuffer-keyboard-quit ()
106 "Abort recursive edit.
107In Transient Mark mode, if the mark is active, just deactivate it."
108 (interactive)
109 (if (and transient-mark-mode mark-active)
110 (progn
111 ;; Don't beep if just deactivating the region.
112 (setq mark-active nil)
113 (run-hooks 'deactivate-mark-hook))
114 (abort-recursive-edit)))
115
116(define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
28d3ed91
RS
117(define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
118(define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
119(define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
120(define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
b0dbaa21
RS
121
122(provide 'pending-del)
123
76550a57 124;;; pending-del.el ends here