new version
[emacs.git] / lisp / delsel.el
blob0aea9cf221ab92d52833843939992c5dcd7d03a5
1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Author: Matthieu Devin <devin@lucid.com>
6 ;; Maintainer: FSF
7 ;; Created: 14 Jul 92
8 ;; Last change 18-Feb-93, devin.
10 ;; This file is part of GNU Emacs.
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.
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.
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.
27 ;;; Commentary:
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.
33 ;;; Code:
35 (eval-when-compile
36 (require 'cl))
38 ;;;###autoload
39 (defalias 'pending-delete-mode 'delete-selection-mode)
41 ;;;###autoload
42 (defun delete-selection-mode (&optional arg)
43 "Toggle Delete Selection mode.
44 With prefix ARG, turn Delete Selection mode on if and only if ARG is positive.
46 When Delete Selection mode is enabled, Transient Mark mode is also enabled and
47 typed text replaces the selection if the selection is active. Otherwise, typed
48 text is just inserted at point regardless of any selection."
49 (interactive "P")
50 (setq delete-selection-mode (if arg
51 (> (prefix-numeric-value arg) 0)
52 (not delete-selection-mode)))
53 (if (not delete-selection-mode)
54 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
55 (add-hook 'pre-command-hook 'delete-selection-pre-hook)
56 (transient-mark-mode t)))
58 ;;;###autoload
59 (defcustom delete-selection-mode nil
60 "Toggle Delete Selection mode.
61 When Delete Selection mode is enabled, Transient Mark mode is also enabled and
62 typed text replaces the selection if the selection is active.
63 You must modify via \\[customize] for this variable to have an effect."
64 :set (lambda (symbol value)
65 (delete-selection-mode (or value 0)))
66 :initialize 'custom-initialize-default
67 :type 'boolean
68 :group 'editing-basics
69 :require 'delsel)
70 ;; Force loading of this file in order to customize delete-selection-mode.
71 (put 'delete-selection-mode 'custom-loads '(delsel))
73 ;; This is the standard way mechanism to put the mode into effect
74 ;; if delete-selection-mode has already been set to t
75 ;; when this file is loaded.
76 (when delete-selection-mode
77 (delete-selection-mode t))
79 (defun delete-active-region (&optional killp)
80 (if killp
81 (kill-region (point) (mark))
82 (delete-region (point) (mark)))
83 (setq mark-active nil)
84 (run-hooks 'deactivate-mark-hook)
87 (defun delete-selection-pre-hook ()
88 (when (and delete-selection-mode transient-mark-mode mark-active
89 (not buffer-read-only))
90 (let ((type (and (symbolp this-command)
91 (get this-command 'delete-selection))))
92 (cond ((eq type 'kill)
93 (delete-active-region t))
94 ((eq type 'yank)
95 ;; Before a yank command,
96 ;; make sure we don't yank the same region
97 ;; that we are going to delete.
98 ;; That would make yank a no-op.
99 (when (string= (buffer-substring-no-properties (point) (mark))
100 (car kill-ring))
101 (current-kill 1))
102 (delete-active-region nil))
103 ((eq type 'supersede)
104 (when (delete-active-region nil)
105 (setq this-command '(lambda () (interactive)))))
106 (type
107 (delete-active-region nil))))))
109 (put 'self-insert-command 'delete-selection t)
110 (put 'self-insert-iso 'delete-selection t)
112 (put 'yank 'delete-selection 'yank)
113 (put 'clipboard-yank 'delete-selection 'yank)
114 (put 'insert-register 'delete-selection t)
116 (put 'delete-backward-char 'delete-selection 'supersede)
117 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
118 (put 'delete-char 'delete-selection 'supersede)
120 (put 'newline-and-indent 'delete-selection 't)
121 (put 'newline 'delete-selection t)
122 (put 'open-line 'delete-selection t)
124 ;; This is very useful for cancelling a selection in the minibuffer without
125 ;; aborting the minibuffer.
126 (defun minibuffer-keyboard-quit ()
127 "Abort recursive edit.
128 In Delete Selection mode mode, if the mark is active, just deactivate it;
129 then it takes a second C-g to abort the minibuffer."
130 (interactive)
131 (if (and delete-selection-mode transient-mark-mode mark-active)
132 (setq deactivate-mark t)
133 (abort-recursive-edit)))
135 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
136 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
137 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
138 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
139 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
141 (provide 'delsel)
143 ;;; delsel.el ends here