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