Commit | Line | Data |
---|---|---|
28d3ed91 | 1 | ;;; delsel.el --- delete selection if you insert |
76550a57 | 2 | |
b578f267 | 3 | ;; Copyright (C) 1992 Free Software Foundation, Inc. |
76550a57 ER |
4 | |
5 | ;; Author: Matthieu Devin <devin@lucid.com> | |
6 | ;; Created: 14 Jul 92 | |
7 | ;; Last change 18-Feb-93, devin. | |
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 | ||
28d3ed91 RS |
34 | (defvar delete-selection-mode t |
35 | "*Non-nil means Delete Selection mode is enabled. | |
36 | In Delete Selection mode, when a region is highlighted, | |
af9157b9 RS |
37 | insertion commands first delete the region and then insert.") |
38 | ||
b0dbaa21 | 39 | (defun delete-active-region (&optional killp) |
af9157b9 RS |
40 | (if killp |
41 | (kill-region (point) (mark)) | |
42 | (delete-region (point) (mark))) | |
43 | (setq mark-active nil) | |
44 | (run-hooks 'deactivate-mark-hook) | |
45 | t) | |
b0dbaa21 | 46 | |
28d3ed91 RS |
47 | (defun delete-selection-pre-hook () |
48 | (if (and delete-selection-mode | |
af9157b9 RS |
49 | (not buffer-read-only) |
50 | transient-mark-mode mark-active) | |
51 | (let ((type (and (symbolp this-command) | |
28d3ed91 | 52 | (get this-command 'delete-selection)))) |
af9157b9 RS |
53 | (cond ((eq type 'kill) |
54 | (delete-active-region t)) | |
6b214411 RS |
55 | ((eq type 'yank) |
56 | ;; Before a yank command, | |
57 | ;; make sure we don't yank the same region | |
58 | ;; that we are going to delete. | |
59 | ;; That would make yank a no-op. | |
60 | (if (string= (buffer-substring (point) (mark)) | |
61 | (car kill-ring)) | |
62 | (current-kill 1)) | |
63 | (delete-active-region nil)) | |
af9157b9 | 64 | ((eq type 'supersede) |
d4df3279 | 65 | (if (delete-active-region nil) |
af9157b9 RS |
66 | (setq this-command '(lambda () (interactive))))) |
67 | (type | |
d4df3279 | 68 | (delete-active-region nil)))))) |
af9157b9 | 69 | |
28d3ed91 | 70 | (add-hook 'pre-command-hook 'delete-selection-pre-hook) |
b0dbaa21 | 71 | |
28d3ed91 | 72 | (put 'self-insert-command 'delete-selection t) |
cc5ac2c6 | 73 | (put 'self-insert-iso 'delete-selection t) |
b0dbaa21 | 74 | |
6b214411 | 75 | (put 'yank 'delete-selection 'yank) |
b708f0ad | 76 | (put 'clipboard-yank 'delete-selection 'yank) |
d4df3279 | 77 | (put 'insert-register 'delete-selection t) |
b0dbaa21 | 78 | |
28d3ed91 RS |
79 | (put 'delete-backward-char 'delete-selection 'supersede) |
80 | (put 'backward-delete-char-untabify 'delete-selection 'supersede) | |
81 | (put 'delete-char 'delete-selection 'supersede) | |
b0dbaa21 | 82 | |
28d3ed91 RS |
83 | (put 'newline-and-indent 'delete-selection 't) |
84 | (put 'newline 'delete-selection t) | |
85 | (put 'open-line 'delete-selection t) | |
b0dbaa21 | 86 | |
d4df3279 | 87 | ;;;###autoload |
28d3ed91 | 88 | (defalias 'pending-delete-mode 'delete-selection-mode) |
d4df3279 | 89 | ;;;###autoload |
28d3ed91 RS |
90 | (defun delete-selection-mode (arg) |
91 | "Toggle Delete Selection mode. | |
b0dbaa21 RS |
92 | When ON, typed text replaces the selection if the selection is active. |
93 | When OFF, typed text is just inserted at point." | |
af9157b9 | 94 | (interactive "P") |
28d3ed91 RS |
95 | (setq delete-selection-mode |
96 | (if (null arg) (not delete-selection-mode) | |
af9157b9 | 97 | (> (prefix-numeric-value arg) 0))) |
2fc86734 | 98 | (force-mode-line-update)) |
b0dbaa21 | 99 | |
b0dbaa21 RS |
100 | ;; This is very useful for cancelling a selection in the minibuffer without |
101 | ;; aborting the minibuffer. | |
b0dbaa21 RS |
102 | (defun minibuffer-keyboard-quit () |
103 | "Abort recursive edit. | |
d4df3279 RS |
104 | In Delete Selection mode mode, if the mark is active, just deactivate it; |
105 | then it takes a second C-g to abort the minibuffer." | |
b0dbaa21 | 106 | (interactive) |
d4df3279 RS |
107 | (if (and delete-selection-mode transient-mark-mode mark-active) |
108 | (setq deactivate-mark t) | |
b0dbaa21 RS |
109 | (abort-recursive-edit))) |
110 | ||
111 | (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit) | |
28d3ed91 RS |
112 | (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit) |
113 | (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit) | |
114 | (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit) | |
115 | (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit) | |
b0dbaa21 | 116 | |
d4df3279 | 117 | (provide 'delsel) |
b0dbaa21 | 118 | |
d4df3279 | 119 | ;;; delsel.el ends here |