]> code.delx.au - gnu-emacs/blob - lisp/delsel.el
Merge from emacs-24; up to 2012-05-04T19:17:01Z!monnier@iro.umontreal.ca
[gnu-emacs] / lisp / delsel.el
1 ;;; delsel.el --- delete selection if you insert
2
3 ;; Copyright (C) 1992, 1997-1998, 2001-2012 Free Software Foundation, Inc.
4
5 ;; Author: Matthieu Devin <devin@lucid.com>
6 ;; Maintainer: FSF
7 ;; Created: 14 Jul 92
8 ;; Keywords: convenience emulations
9
10 ;; This file is part of GNU Emacs.
11
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 3 of the License, or
15 ;; (at your option) any later version.
16
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.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
24
25 ;;; Commentary:
26
27 ;; This file makes the active region be pending delete, meaning that
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
31 ;; Interface:
32
33 ;; Commands which will delete the selection need a 'delete-selection
34 ;; property on their symbols; commands which insert text but don't
35 ;; have this property won't delete the selection. It can be one of
36 ;; the values:
37 ;; 'yank
38 ;; For commands which do a yank; ensures the region about to be
39 ;; deleted isn't yanked.
40 ;; 'supersede
41 ;; Delete the active region and ignore the current command,
42 ;; i.e. the command will just delete the region.
43 ;; 'kill
44 ;; `kill-region' is used on the selection, rather than
45 ;; `delete-region'. (Text selected with the mouse will typically
46 ;; be yankable anyhow.)
47 ;; non-nil
48 ;; The normal case: delete the active region prior to executing
49 ;; the command which will insert replacement text.
50
51 ;;; Code:
52
53 ;;;###autoload
54 (defalias 'pending-delete-mode 'delete-selection-mode)
55
56 ;;;###autoload
57 (define-minor-mode delete-selection-mode
58 "Toggle Delete Selection mode.
59 With a prefix argument ARG, enable Delete Selection mode if ARG
60 is positive, and disable it otherwise. If called from Lisp,
61 enable the mode if ARG is omitted or nil.
62
63 When Delete Selection mode is enabled, Transient Mark mode is also
64 enabled and typed text replaces the selection if the selection is
65 active. Otherwise, typed text is just inserted at point regardless of
66 any selection."
67 :global t :group 'editing-basics
68 (if (not delete-selection-mode)
69 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
70 (add-hook 'pre-command-hook 'delete-selection-pre-hook)
71 (transient-mark-mode t)))
72
73 (defun delete-active-region (&optional killp)
74 (if killp
75 (kill-region (point) (mark))
76 (delete-region (point) (mark)))
77 t)
78
79 (defun delete-selection-pre-hook ()
80 (when (and delete-selection-mode transient-mark-mode mark-active
81 (not buffer-read-only))
82 (let ((type (and (symbolp this-command)
83 (get this-command 'delete-selection))))
84 (condition-case data
85 (cond ((eq type 'kill)
86 (delete-active-region t))
87 ((eq type 'yank)
88 ;; Before a yank command, make sure we don't yank the
89 ;; head of the kill-ring that really comes from the
90 ;; currently active region we are going to delete.
91 ;; That would make yank a no-op.
92 (when (and (string= (buffer-substring-no-properties
93 (point) (mark))
94 (car kill-ring))
95 (fboundp 'mouse-region-match)
96 (mouse-region-match))
97 (current-kill 1))
98 (delete-active-region))
99 ((eq type 'supersede)
100 (let ((empty-region (= (point) (mark))))
101 (delete-active-region)
102 (unless empty-region
103 (setq this-command 'ignore))))
104 (type
105 (delete-active-region)
106 (if (and overwrite-mode
107 (eq this-command 'self-insert-command))
108 (let ((overwrite-mode nil))
109 (self-insert-command
110 (prefix-numeric-value current-prefix-arg))
111 (setq this-command 'ignore)))))
112 ;; If ask-user-about-supersession-threat signals an error,
113 ;; stop safe_run_hooks from clearing out pre-command-hook.
114 (file-supersession (message "%s" (cadr data)) (ding))
115 (text-read-only
116 ;; This signal may come either from `delete-active-region' or
117 ;; `self-insert-command' (when `overwrite-mode' is non-nil).
118 ;; To avoid clearing out `pre-command-hook' we handle this case
119 ;; by issuing a simple message. Note, however, that we do not
120 ;; handle all related problems: When read-only text ends before
121 ;; the end of the region, the latter is not deleted but any
122 ;; subsequent insertion will succeed. We could avoid this case
123 ;; by doing a (setq this-command 'ignore) here. This would,
124 ;; however, still not handle the case where read-only text ends
125 ;; precisely where the region starts: In that case the deletion
126 ;; would succeed but the subsequent insertion would fail with a
127 ;; text-read-only error. To handle that case we would have to
128 ;; investigate text properties at both ends of the region and
129 ;; skip the deletion when inserting text is forbidden there.
130 (message "Text is read-only") (ding))))))
131
132 (put 'self-insert-command 'delete-selection t)
133 (put 'self-insert-iso 'delete-selection t)
134
135 (put 'yank 'delete-selection 'yank)
136 (put 'clipboard-yank 'delete-selection 'yank)
137 (put 'insert-register 'delete-selection t)
138
139 (put 'delete-backward-char 'delete-selection 'supersede)
140 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
141 (put 'delete-char 'delete-selection 'supersede)
142
143 (put 'newline-and-indent 'delete-selection t)
144 (put 'newline 'delete-selection t)
145 (put 'open-line 'delete-selection 'kill)
146
147 ;; This is very useful for canceling a selection in the minibuffer without
148 ;; aborting the minibuffer.
149 (defun minibuffer-keyboard-quit ()
150 "Abort recursive edit.
151 In Delete Selection mode, if the mark is active, just deactivate it;
152 then it takes a second \\[keyboard-quit] to abort the minibuffer."
153 (interactive)
154 (if (and delete-selection-mode transient-mark-mode mark-active)
155 (setq deactivate-mark t)
156 (abort-recursive-edit)))
157
158 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
159 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
160 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
161 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
162 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
163
164 (defun delsel-unload-function ()
165 "Unload the Delete Selection library."
166 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
167 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
168 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
169 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
170 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit)
171 (dolist (sym '(self-insert-command self-insert-iso yank clipboard-yank
172 insert-register delete-backward-char backward-delete-char-untabify
173 delete-char newline-and-indent newline open-line))
174 (put sym 'delete-selection nil))
175 ;; continue standard unloading
176 nil)
177
178 (provide 'delsel)
179
180 ;;; delsel.el ends here