(Group Definitions): Replace `...' with `@dots{}' in `@defmac' and `@defspec'.
[emacs.git] / lisp / delsel.el
blobd7a3d90e96921b7abc76f985b3edfb07edbbd2df
1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992, 1997, 1998, 2001, 2002, 2003, 2004,
4 ;; 2005 Free Software Foundation, Inc.
6 ;; Author: Matthieu Devin <devin@lucid.com>
7 ;; Maintainer: FSF
8 ;; Created: 14 Jul 92
9 ;; Keywords: convenience emulations
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; any later version.
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;; This file makes the active region be pending delete, meaning that
31 ;; text inserted while the region is active will replace the region contents.
32 ;; This is a popular behavior of personal computers text editors.
34 ;; Interface:
36 ;; Commands which will delete the selection need a 'delete-selection
37 ;; property on their symbols; commands which insert text but don't
38 ;; have this property won't delete the selction. It can be one of
39 ;; the values:
40 ;; 'yank
41 ;; For commands which do a yank; ensures the region about to be
42 ;; deleted isn't yanked.
43 ;; 'supersede
44 ;; Delete the active region and ignore the current command,
45 ;; i.e. the command will just delete the region.
46 ;; 'kill
47 ;; `kill-region' is used on the selection, rather than
48 ;; `delete-region'. (Text selected with the mouse will typically
49 ;; be yankable anyhow.)
50 ;; non-nil
51 ;; The normal case: delete the active region prior to executing
52 ;; the command which will insert replacement text.
54 ;;; Code:
56 ;;;###autoload
57 (defalias 'pending-delete-mode 'delete-selection-mode)
59 ;;;###autoload
60 (define-minor-mode delete-selection-mode
61 "Toggle Delete Selection mode.
62 With prefix ARG, turn Delete Selection mode on if and only if ARG is
63 positive.
65 When Delete Selection mode is enabled, Transient Mark mode is also
66 enabled and typed text replaces the selection if the selection is
67 active. Otherwise, typed text is just inserted at point regardless of
68 any selection."
69 :global t :group 'editing-basics
70 (if (not delete-selection-mode)
71 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
72 (add-hook 'pre-command-hook 'delete-selection-pre-hook)
73 (transient-mark-mode t)))
75 (defun delete-active-region (&optional killp)
76 (if killp
77 (kill-region (point) (mark))
78 (delete-region (point) (mark)))
81 (defun delete-selection-pre-hook ()
82 (when (and delete-selection-mode transient-mark-mode mark-active
83 (not buffer-read-only))
84 (let ((type (and (symbolp this-command)
85 (get this-command 'delete-selection))))
86 (condition-case data
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))
95 (car kill-ring))
96 (current-kill 1))
97 (delete-active-region))
98 ((eq type 'supersede)
99 (let ((empty-region (= (point) (mark))))
100 (delete-active-region)
101 (unless empty-region
102 (setq this-command 'ignore))))
103 (type
104 (delete-active-region)
105 (if (and overwrite-mode (eq this-command 'self-insert-command))
106 (let ((overwrite-mode nil))
107 (self-insert-command (prefix-numeric-value current-prefix-arg))
108 (setq this-command 'ignore)))))
109 (file-supersession
110 ;; If ask-user-about-supersession-threat signals an error,
111 ;; stop safe_run_hooks from clearing out pre-command-hook.
112 (and (eq inhibit-quit 'pre-command-hook)
113 (setq inhibit-quit 'delete-selection-dummy))
114 (signal 'file-supersession (cdr data)))))))
116 (put 'self-insert-command 'delete-selection t)
117 (put 'self-insert-iso 'delete-selection t)
119 (put 'yank 'delete-selection 'yank)
120 (put 'clipboard-yank 'delete-selection 'yank)
121 (put 'insert-register 'delete-selection t)
123 (put 'delete-backward-char 'delete-selection 'supersede)
124 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
125 (put 'delete-char 'delete-selection 'supersede)
127 (put 'newline-and-indent 'delete-selection t)
128 (put 'newline 'delete-selection t)
129 (put 'open-line 'delete-selection 'kill)
131 ;; This is very useful for cancelling a selection in the minibuffer without
132 ;; aborting the minibuffer.
133 (defun minibuffer-keyboard-quit ()
134 "Abort recursive edit.
135 In Delete Selection mode, if the mark is active, just deactivate it;
136 then it takes a second \\[keyboard-quit] to abort the minibuffer."
137 (interactive)
138 (if (and delete-selection-mode transient-mark-mode mark-active)
139 (setq deactivate-mark t)
140 (abort-recursive-edit)))
142 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
143 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
144 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
145 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
146 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
148 (defun delsel-unload-hook ()
149 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
150 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
151 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
152 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
153 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit))
155 (add-hook 'delsel-unload-hook 'delsel-unload-hook)
157 (provide 'delsel)
159 ;;; arch-tag: 1e388890-1b50-4ed0-9347-763b1343b6ed
160 ;;; delsel.el ends here