(command-line): Set `temporary-file-directory' based
[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'.
7853929f
SM
80You must modify via \\[customize] for this variable to have an effect."
81 :set (lambda (symbol value)
82 (delete-selection-mode (or value 0)))
16be0abe 83 :initialize 'custom-initialize-default
7853929f
SM
84 :type 'boolean
85 :group 'editing-basics
86 :require 'delsel)
af9157b9 87
b0dbaa21 88(defun delete-active-region (&optional killp)
af9157b9
RS
89 (if killp
90 (kill-region (point) (mark))
91 (delete-region (point) (mark)))
92 (setq mark-active nil)
93 (run-hooks 'deactivate-mark-hook)
94 t)
b0dbaa21 95
28d3ed91 96(defun delete-selection-pre-hook ()
7853929f
SM
97 (when (and delete-selection-mode transient-mark-mode mark-active
98 (not buffer-read-only))
99 (let ((type (and (symbolp this-command)
100 (get this-command 'delete-selection))))
101 (cond ((eq type 'kill)
102 (delete-active-region t))
103 ((eq type 'yank)
104 ;; Before a yank command,
105 ;; make sure we don't yank the same region
106 ;; that we are going to delete.
107 ;; That would make yank a no-op.
108 (when (string= (buffer-substring-no-properties (point) (mark))
6b214411 109 (car kill-ring))
7853929f 110 (current-kill 1))
69b3c6c7 111 (delete-active-region))
7853929f 112 ((eq type 'supersede)
69b3c6c7
DL
113 (delete-active-region)
114 (setq this-command 'ignore))
7853929f 115 (type
69b3c6c7 116 (delete-active-region))))))
b0dbaa21 117
28d3ed91 118(put 'self-insert-command 'delete-selection t)
cc5ac2c6 119(put 'self-insert-iso 'delete-selection t)
b0dbaa21 120
6b214411 121(put 'yank 'delete-selection 'yank)
b708f0ad 122(put 'clipboard-yank 'delete-selection 'yank)
d4df3279 123(put 'insert-register 'delete-selection t)
b0dbaa21 124
28d3ed91
RS
125(put 'delete-backward-char 'delete-selection 'supersede)
126(put 'backward-delete-char-untabify 'delete-selection 'supersede)
127(put 'delete-char 'delete-selection 'supersede)
b0dbaa21 128
69b3c6c7 129(put 'newline-and-indent 'delete-selection t)
28d3ed91 130(put 'newline 'delete-selection t)
69b3c6c7
DL
131(put 'open-line 'delete-selection 'kill)
132
133(put 'insert-parentheses 'delete-selection t)
b0dbaa21 134
b0dbaa21
RS
135;; This is very useful for cancelling a selection in the minibuffer without
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
146(define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
28d3ed91
RS
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)
b0dbaa21 151
d4df3279 152(provide 'delsel)
b0dbaa21 153
16be0abe
RS
154;; This is the standard way mechanism to put the mode into effect
155;; if delete-selection-mode has already been set to t
156;; when this file is loaded.
157(when delete-selection-mode
158 (delete-selection-mode t))
159
d4df3279 160;;; delsel.el ends here