]> code.delx.au - gnu-emacs/blob - lisp/delsel.el
Grok skeleton-pair-insert-maybe.
[gnu-emacs] / lisp / delsel.el
1 ;;; delsel.el --- delete selection if you insert
2
3 ;; Copyright (C) 1992, 1997, 1998 Free Software Foundation, Inc.
4
5 ;; Author: Matthieu Devin <devin@lucid.com>
6 ;; Maintainer: FSF
7 ;; Created: 14 Jul 92
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
25
26 ;;; Commentary:
27
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.
31
32 ;; Interface:
33
34 ;; Commands which will delete the selection need a 'delete-selection
35 ;; property on their symbols; commands which insert text but don't
36 ;; have this property won't delete the selction. It can be one of
37 ;; the values:
38 ;; 'yank
39 ;; For commands which do a yank; ensures the region about to be
40 ;; deleted isn't yanked.
41 ;; 'supersede
42 ;; Delete the active region and ignore the current command,
43 ;; i.e. the command will just delete the region.
44 ;; 'kill
45 ;; `kill-region' is used on the selection, rather than
46 ;; `delete-region'. (Text selected with the mouse will typically
47 ;; be yankable anyhow.)
48 ;; non-nil
49 ;; The normal case: delete the active region prior to executing
50 ;; the command which will insert replacement text.
51
52 ;;; Code:
53
54 ;;;###autoload
55 (defalias 'pending-delete-mode 'delete-selection-mode)
56
57 ;;;###autoload
58 (defun delete-selection-mode (&optional arg)
59 "Toggle Delete Selection mode.
60 With prefix ARG, turn Delete Selection mode on if and only if ARG is
61 positive.
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 (interactive "P")
68 (setq delete-selection-mode (if arg
69 (> (prefix-numeric-value arg) 0)
70 (not delete-selection-mode)))
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)))
75
76 ;;;###autoload
77 (defcustom delete-selection-mode nil
78 "Toggle Delete Selection mode.
79 See command `delete-selection-mode'.
80 Setting this variable directly does not take effect;
81 use either \\[customize] or the function `delete-selection-mode'."
82 :set (lambda (symbol value)
83 (delete-selection-mode (or value 0)))
84 :initialize 'custom-initialize-default
85 :type 'boolean
86 :group 'editing-basics
87 :require 'delsel)
88
89 (defun delete-active-region (&optional killp)
90 (if killp
91 (kill-region (point) (mark))
92 (delete-region (point) (mark)))
93 (setq mark-active nil)
94 (run-hooks 'deactivate-mark-hook)
95 t)
96
97 (defun delete-selection-pre-hook ()
98 (when (and delete-selection-mode transient-mark-mode mark-active
99 (not buffer-read-only))
100 (let ((type (and (symbolp this-command)
101 (get this-command 'delete-selection))))
102 (cond ((eq type 'kill)
103 (delete-active-region t))
104 ((eq type 'yank)
105 ;; Before a yank command,
106 ;; make sure we don't yank the same region
107 ;; that we are going to delete.
108 ;; That would make yank a no-op.
109 (when (string= (buffer-substring-no-properties (point) (mark))
110 (car kill-ring))
111 (current-kill 1))
112 (delete-active-region))
113 ((eq type 'supersede)
114 (delete-active-region)
115 (setq this-command 'ignore))
116 (type
117 (delete-active-region))))))
118
119 (put 'self-insert-command 'delete-selection t)
120 (put 'self-insert-iso 'delete-selection t)
121
122 (put 'yank 'delete-selection 'yank)
123 (put 'clipboard-yank 'delete-selection 'yank)
124 (put 'insert-register 'delete-selection t)
125
126 (put 'delete-backward-char 'delete-selection 'supersede)
127 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
128 (put 'delete-char 'delete-selection 'supersede)
129
130 (put 'newline-and-indent 'delete-selection t)
131 (put 'newline 'delete-selection t)
132 (put 'open-line 'delete-selection 'kill)
133
134 (put 'insert-parentheses 'delete-selection t)
135
136 (put 'skeleton-pair-insert-maybe 'delete-selection t)
137
138 ;; This is very useful for cancelling a selection in the minibuffer without
139 ;; aborting the minibuffer.
140 (defun minibuffer-keyboard-quit ()
141 "Abort recursive edit.
142 In Delete Selection mode, if the mark is active, just deactivate it;
143 then it takes a second \\[keyboard-quit] to abort the minibuffer."
144 (interactive)
145 (if (and delete-selection-mode transient-mark-mode mark-active)
146 (setq deactivate-mark t)
147 (abort-recursive-edit)))
148
149 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
150 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
151 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
152 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
153 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
154
155 (provide 'delsel)
156
157 ;; This is the standard way mechanism to put the mode into effect
158 ;; if delete-selection-mode has already been set to t
159 ;; when this file is loaded.
160 (when delete-selection-mode
161 (delete-selection-mode t))
162
163 ;;; delsel.el ends here