Commit | Line | Data |
---|---|---|
28d3ed91 | 1 | ;;; delsel.el --- delete selection if you insert |
76550a57 | 2 | |
a6b9b0c5 | 3 | ;; Copyright (C) 1992, 1997 Free Software Foundation, Inc. |
76550a57 ER |
4 | |
5 | ;; Author: Matthieu Devin <devin@lucid.com> | |
4228277d | 6 | ;; Maintainer: FSF |
76550a57 | 7 | ;; Created: 14 Jul 92 |
b0dbaa21 | 8 | |
b578f267 | 9 | ;; This file is part of GNU Emacs. |
b0dbaa21 | 10 | |
b578f267 EN |
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify |
12 | ;; it under the terms of the GNU General Public License as published by | |
13 | ;; the Free Software Foundation; either version 2, or (at your option) | |
14 | ;; any later version. | |
b0dbaa21 | 15 | |
b578f267 EN |
16 | ;; GNU Emacs is distributed in the hope that it will be useful, |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
b0dbaa21 | 20 | |
b578f267 EN |
21 | ;; You should have received a copy of the GNU General Public License |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to the | |
23 | ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330, | |
24 | ;; Boston, MA 02111-1307, USA. | |
b0dbaa21 | 25 | |
76550a57 | 26 | ;;; Commentary: |
b0dbaa21 | 27 | |
b578f267 EN |
28 | ;; This file makes the active region be pending delete, meaning that |
29 | ;; text inserted while the region is active will replace the region contents. | |
30 | ;; This is a popular behavior of personal computers text editors. | |
b0dbaa21 | 31 | |
76550a57 ER |
32 | ;;; Code: |
33 | ||
7853929f SM |
34 | (eval-when-compile |
35 | (require 'cl)) | |
36 | ||
37 | ;;;###autoload | |
38 | (defalias 'pending-delete-mode 'delete-selection-mode) | |
39 | ||
40 | ;;;###autoload | |
41 | (defun delete-selection-mode (&optional arg) | |
42 | "Toggle Delete Selection mode. | |
43 | With prefix ARG, turn Delete Selection mode on if and only if ARG is positive. | |
44 | ||
45 | When Delete Selection mode is enabled, Transient Mark mode is also enabled and | |
46 | typed text replaces the selection if the selection is active. Otherwise, typed | |
47 | text is just inserted at point regardless of any selection." | |
48 | (interactive "P") | |
49 | (setq delete-selection-mode (if arg | |
50 | (> (prefix-numeric-value arg) 0) | |
51 | (not delete-selection-mode))) | |
52 | (if (not delete-selection-mode) | |
53 | (remove-hook 'pre-command-hook 'delete-selection-pre-hook) | |
54 | (add-hook 'pre-command-hook 'delete-selection-pre-hook) | |
55 | (transient-mark-mode t))) | |
56 | ||
57 | ;;;###autoload | |
58 | (defcustom delete-selection-mode nil | |
59 | "Toggle Delete Selection mode. | |
60 | When Delete Selection mode is enabled, Transient Mark mode is also enabled and | |
61 | typed text replaces the selection if the selection is active. | |
62 | You must modify via \\[customize] for this variable to have an effect." | |
63 | :set (lambda (symbol value) | |
64 | (delete-selection-mode (or value 0))) | |
16be0abe | 65 | :initialize 'custom-initialize-default |
7853929f SM |
66 | :type 'boolean |
67 | :group 'editing-basics | |
68 | :require 'delsel) | |
af9157b9 | 69 | |
b0dbaa21 | 70 | (defun delete-active-region (&optional killp) |
af9157b9 RS |
71 | (if killp |
72 | (kill-region (point) (mark)) | |
73 | (delete-region (point) (mark))) | |
74 | (setq mark-active nil) | |
75 | (run-hooks 'deactivate-mark-hook) | |
76 | t) | |
b0dbaa21 | 77 | |
28d3ed91 | 78 | (defun delete-selection-pre-hook () |
7853929f SM |
79 | (when (and delete-selection-mode transient-mark-mode mark-active |
80 | (not buffer-read-only)) | |
81 | (let ((type (and (symbolp this-command) | |
82 | (get this-command 'delete-selection)))) | |
83 | (cond ((eq type 'kill) | |
84 | (delete-active-region t)) | |
85 | ((eq type 'yank) | |
86 | ;; Before a yank command, | |
87 | ;; make sure we don't yank the same region | |
88 | ;; that we are going to delete. | |
89 | ;; That would make yank a no-op. | |
90 | (when (string= (buffer-substring-no-properties (point) (mark)) | |
6b214411 | 91 | (car kill-ring)) |
7853929f SM |
92 | (current-kill 1)) |
93 | (delete-active-region nil)) | |
94 | ((eq type 'supersede) | |
95 | (when (delete-active-region nil) | |
96 | (setq this-command '(lambda () (interactive))))) | |
97 | (type | |
98 | (delete-active-region nil)))))) | |
b0dbaa21 | 99 | |
28d3ed91 | 100 | (put 'self-insert-command 'delete-selection t) |
cc5ac2c6 | 101 | (put 'self-insert-iso 'delete-selection t) |
b0dbaa21 | 102 | |
6b214411 | 103 | (put 'yank 'delete-selection 'yank) |
b708f0ad | 104 | (put 'clipboard-yank 'delete-selection 'yank) |
d4df3279 | 105 | (put 'insert-register 'delete-selection t) |
b0dbaa21 | 106 | |
28d3ed91 RS |
107 | (put 'delete-backward-char 'delete-selection 'supersede) |
108 | (put 'backward-delete-char-untabify 'delete-selection 'supersede) | |
109 | (put 'delete-char 'delete-selection 'supersede) | |
b0dbaa21 | 110 | |
28d3ed91 RS |
111 | (put 'newline-and-indent 'delete-selection 't) |
112 | (put 'newline 'delete-selection t) | |
113 | (put 'open-line 'delete-selection t) | |
b0dbaa21 | 114 | |
b0dbaa21 RS |
115 | ;; This is very useful for cancelling a selection in the minibuffer without |
116 | ;; aborting the minibuffer. | |
b0dbaa21 RS |
117 | (defun minibuffer-keyboard-quit () |
118 | "Abort recursive edit. | |
d4df3279 RS |
119 | In Delete Selection mode mode, if the mark is active, just deactivate it; |
120 | then it takes a second C-g to abort the minibuffer." | |
b0dbaa21 | 121 | (interactive) |
d4df3279 RS |
122 | (if (and delete-selection-mode transient-mark-mode mark-active) |
123 | (setq deactivate-mark t) | |
b0dbaa21 RS |
124 | (abort-recursive-edit))) |
125 | ||
126 | (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit) | |
28d3ed91 RS |
127 | (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit) |
128 | (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit) | |
129 | (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit) | |
130 | (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit) | |
b0dbaa21 | 131 | |
d4df3279 | 132 | (provide 'delsel) |
b0dbaa21 | 133 | |
16be0abe RS |
134 | ;; This is the standard way mechanism to put the mode into effect |
135 | ;; if delete-selection-mode has already been set to t | |
136 | ;; when this file is loaded. | |
137 | (when delete-selection-mode | |
138 | (delete-selection-mode t)) | |
139 | ||
d4df3279 | 140 | ;;; delsel.el ends here |