Add ibuffer-filter-by-derived-mode to ibuffer menu and doc
[emacs.git] / lisp / delsel.el
blob2ed826761898959e8818856359d984443f613fca
1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992, 1997-1998, 2001-2012 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 3 of the License, or
15 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
25 ;;; Commentary:
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.
31 ;; Interface:
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 ;; t
48 ;; The normal case: delete the active region prior to executing
49 ;; the command which will insert replacement text.
50 ;; <function>
51 ;; For commands which need to dynamically determine this behaviour.
52 ;; The function should return one of the above values or nil.
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 a prefix argument ARG, enable Delete Selection mode if ARG
63 is positive, and disable it otherwise. If called from Lisp,
64 enable the mode if ARG is omitted or nil.
66 When Delete Selection mode is enabled, Transient Mark mode is also
67 enabled and typed text replaces the selection if the selection is
68 active. Otherwise, typed text is just inserted at point regardless of
69 any selection."
70 :global t :group 'editing-basics
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)))
76 (defun delete-active-region (&optional killp)
77 "Delete the active region.
78 If KILLP in not-nil, the active region is killed instead of deleted."
79 (if killp
80 (kill-region (point) (mark))
81 (delete-region (point) (mark)))
84 (defun delete-selection-helper (type)
85 "Delete selection according to TYPE:
86 `yank'
87 For commands which do a yank; ensures the region about to be
88 deleted isn't yanked.
89 `supersede'
90 Delete the active region and ignore the current command,
91 i.e. the command will just delete the region.
92 `kill'
93 `kill-region' is used on the selection, rather than
94 `delete-region'. (Text selected with the mouse will typically
95 be yankable anyhow.)
97 The normal case: delete the active region prior to executing
98 the command which will insert replacement text.
99 FUNCTION
100 For commands which need to dynamically determine this behaviour.
101 FUNCTION should take no argument and return one of the above values or nil."
102 (condition-case data
103 (cond ((eq type 'kill)
104 (delete-active-region t))
105 ((eq type 'yank)
106 ;; Before a yank command, make sure we don't yank the
107 ;; head of the kill-ring that really comes from the
108 ;; currently active region we are going to delete.
109 ;; That would make yank a no-op.
110 (when (and (string= (buffer-substring-no-properties
111 (point) (mark))
112 (car kill-ring))
113 (fboundp 'mouse-region-match)
114 (mouse-region-match))
115 (current-kill 1))
116 (delete-active-region))
117 ((eq type 'supersede)
118 (let ((empty-region (= (point) (mark))))
119 (delete-active-region)
120 (unless empty-region
121 (setq this-command 'ignore))))
122 ((functionp type) (delete-selection-helper (funcall type)))
123 (type
124 (delete-active-region)
125 (if (and overwrite-mode
126 (eq this-command 'self-insert-command))
127 (let ((overwrite-mode nil))
128 (self-insert-command
129 (prefix-numeric-value current-prefix-arg))
130 (setq this-command 'ignore)))))
131 ;; If ask-user-about-supersession-threat signals an error,
132 ;; stop safe_run_hooks from clearing out pre-command-hook.
133 (file-supersession (message "%s" (cadr data)) (ding))
134 (text-read-only
135 ;; This signal may come either from `delete-active-region' or
136 ;; `self-insert-command' (when `overwrite-mode' is non-nil).
137 ;; To avoid clearing out `pre-command-hook' we handle this case
138 ;; by issuing a simple message. Note, however, that we do not
139 ;; handle all related problems: When read-only text ends before
140 ;; the end of the region, the latter is not deleted but any
141 ;; subsequent insertion will succeed. We could avoid this case
142 ;; by doing a (setq this-command 'ignore) here. This would,
143 ;; however, still not handle the case where read-only text ends
144 ;; precisely where the region starts: In that case the deletion
145 ;; would succeed but the subsequent insertion would fail with a
146 ;; text-read-only error. To handle that case we would have to
147 ;; investigate text properties at both ends of the region and
148 ;; skip the deletion when inserting text is forbidden there.
149 (message "Text is read-only") (ding))))
151 (defun delete-selection-pre-hook ()
152 "Function run before commands that delete selections are executed.
153 Commands which will delete the selection need a `delete-selection'
154 property on their symbol; commands which insert text but don't
155 have this property won't delete the selection.
156 See `delete-selection-helper'."
157 (when (and delete-selection-mode (use-region-p)
158 (not buffer-read-only))
159 (delete-selection-helper (and (symbolp this-command)
160 (get this-command 'delete-selection)))))
162 (put 'self-insert-command 'delete-selection
163 (lambda ()
164 (not (run-hook-with-args-until-success
165 'self-insert-uses-region-functions))))
167 (put 'self-insert-iso 'delete-selection t)
169 (put 'yank 'delete-selection 'yank)
170 (put 'clipboard-yank 'delete-selection 'yank)
171 (put 'insert-register 'delete-selection t)
173 (put 'delete-backward-char 'delete-selection 'supersede)
174 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
175 (put 'delete-char 'delete-selection 'supersede)
177 (put 'newline-and-indent 'delete-selection t)
178 (put 'newline 'delete-selection t)
179 (put 'open-line 'delete-selection 'kill)
181 ;; This is very useful for canceling a selection in the minibuffer without
182 ;; aborting the minibuffer.
183 (defun minibuffer-keyboard-quit ()
184 "Abort recursive edit.
185 In Delete Selection mode, if the mark is active, just deactivate it;
186 then it takes a second \\[keyboard-quit] to abort the minibuffer."
187 (interactive)
188 (if (and delete-selection-mode transient-mark-mode mark-active)
189 (setq deactivate-mark t)
190 (abort-recursive-edit)))
192 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
193 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
194 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
195 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
196 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
198 (defun delsel-unload-function ()
199 "Unload the Delete Selection library."
200 (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
201 (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
202 (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
203 (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
204 (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit)
205 (dolist (sym '(self-insert-command self-insert-iso yank clipboard-yank
206 insert-register delete-backward-char backward-delete-char-untabify
207 delete-char newline-and-indent newline open-line))
208 (put sym 'delete-selection nil))
209 ;; continue standard unloading
210 nil)
212 (provide 'delsel)
214 ;;; delsel.el ends here