*** empty log message ***
[emacs/old-mirror.git] / lisp / delsel.el
blob962fa156a07433235d3633af13fe89b405c2ed4c
1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992, 1997, 1998, 2001 Free Software Foundation, Inc.
5 ;; Author: Matthieu Devin <devin@lucid.com>
6 ;; Maintainer: FSF
7 ;; Created: 14 Jul 92
8 ;; Keywords: convenience emulations
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 ;; Interface:
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.
53 ;;; Code:
55 ;;;###autoload
56 (defalias 'pending-delete-mode 'delete-selection-mode)
58 ;;;###autoload
59 (define-minor-mode delete-selection-mode
60 "Toggle Delete Selection mode.
61 With prefix ARG, turn Delete Selection mode on if and only if ARG is
62 positive.
64 When Delete Selection mode is enabled, Transient Mark mode is also
65 enabled and typed text replaces the selection if the selection is
66 active. Otherwise, typed text is just inserted at point regardless of
67 any selection."
68 :global t :group 'editing-basics
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)))
74 (defun delete-active-region (&optional killp)
75 (if killp
76 (kill-region (point) (mark))
77 (delete-region (point) (mark)))
80 (defun delete-selection-pre-hook ()
81 (when (and delete-selection-mode transient-mark-mode mark-active
82 (not buffer-read-only))
83 (let ((type (and (symbolp this-command)
84 (get this-command 'delete-selection))))
85 (condition-case data
86 (cond ((eq type 'kill)
87 (delete-active-region t))
88 ((eq type 'yank)
89 ;; Before a yank command,
90 ;; make sure we don't yank the same region
91 ;; that we are going to delete.
92 ;; That would make yank a no-op.
93 (when (string= (buffer-substring-no-properties (point) (mark))
94 (car kill-ring))
95 (current-kill 1))
96 (delete-active-region))
97 ((eq type 'supersede)
98 (let ((empty-region (= (point) (mark))))
99 (delete-active-region)
100 (unless empty-region
101 (setq this-command 'ignore))))
102 (type
103 (delete-active-region)))
104 (file-supersession
105 ;; If ask-user-about-supersession-threat signals an error,
106 ;; stop safe_run_hooks from clearing out pre-command-hook.
107 (and (eq inhibit-quit 'pre-command-hook)
108 (setq inhibit-quit 'delete-selection-dummy))
109 (signal 'file-supersession (cdr data)))))))
111 (put 'self-insert-command 'delete-selection t)
112 (put 'self-insert-iso 'delete-selection t)
114 (put 'yank 'delete-selection 'yank)
115 (put 'clipboard-yank 'delete-selection 'yank)
116 (put 'insert-register 'delete-selection t)
118 (put 'delete-backward-char 'delete-selection 'supersede)
119 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
120 (put 'delete-char 'delete-selection 'supersede)
122 (put 'newline-and-indent 'delete-selection t)
123 (put 'newline 'delete-selection t)
124 (put 'open-line 'delete-selection 'kill)
126 ;; This is very useful for cancelling a selection in the minibuffer without
127 ;; aborting the minibuffer.
128 (defun minibuffer-keyboard-quit ()
129 "Abort recursive edit.
130 In Delete Selection mode, if the mark is active, just deactivate it;
131 then it takes a second \\[keyboard-quit] to abort the minibuffer."
132 (interactive)
133 (if (and delete-selection-mode transient-mark-mode mark-active)
134 (setq deactivate-mark t)
135 (abort-recursive-edit)))
137 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
138 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
139 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
140 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
141 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
143 (defun delsel-unload-hook ()
144 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
145 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
146 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
147 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
148 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit))
150 (add-hook 'delsel-unload-hook 'delsel-unload-hook)
152 (provide 'delsel)
154 ;;; arch-tag: 1e388890-1b50-4ed0-9347-763b1343b6ed
155 ;;; delsel.el ends here