]> code.delx.au - gnu-emacs/blob - lisp/delsel.el
(menu-bar-help-menu): Add menu item for non-English
[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 ;; Keywords: convenience emulations
9
10 ;; This file is part of GNU Emacs.
11
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 2, or (at your option)
15 ;; any later version.
16
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.
21
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs; see the file COPYING. If not, write to the
24 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
25 ;; Boston, MA 02111-1307, USA.
26
27 ;;; Commentary:
28
29 ;; This file makes the active region be pending delete, meaning that
30 ;; text inserted while the region is active will replace the region contents.
31 ;; This is a popular behavior of personal computers text editors.
32
33 ;; Interface:
34
35 ;; Commands which will delete the selection need a 'delete-selection
36 ;; property on their symbols; commands which insert text but don't
37 ;; have this property won't delete the selction. It can be one of
38 ;; the values:
39 ;; 'yank
40 ;; For commands which do a yank; ensures the region about to be
41 ;; deleted isn't yanked.
42 ;; 'supersede
43 ;; Delete the active region and ignore the current command,
44 ;; i.e. the command will just delete the region.
45 ;; 'kill
46 ;; `kill-region' is used on the selection, rather than
47 ;; `delete-region'. (Text selected with the mouse will typically
48 ;; be yankable anyhow.)
49 ;; non-nil
50 ;; The normal case: delete the active region prior to executing
51 ;; the command which will insert replacement text.
52
53 ;;; Code:
54
55 ;;;###autoload
56 (defalias 'pending-delete-mode 'delete-selection-mode)
57
58 ;;;###autoload
59 (defun delete-selection-mode (&optional arg)
60 "Toggle Delete Selection mode.
61 With prefix ARG, turn Delete Selection mode on if and only if ARG is
62 positive.
63
64 When Delete Selection mode is enabled, Transient Mark mode is also
65 enabled and typed text replaces the selection if the selection is
66 active. Otherwise, typed text is just inserted at point regardless of
67 any selection."
68 (interactive "P")
69 (setq delete-selection-mode (if arg
70 (> (prefix-numeric-value arg) 0)
71 (not delete-selection-mode)))
72 (if (not delete-selection-mode)
73 (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
74 (add-hook 'pre-command-hook 'delete-selection-pre-hook)
75 (transient-mark-mode t)))
76
77 ;;;###autoload
78 (defcustom delete-selection-mode nil
79 "Toggle Delete Selection mode.
80 See command `delete-selection-mode'.
81 Setting this variable directly does not take effect;
82 use either \\[customize] or the function `delete-selection-mode'."
83 :set (lambda (symbol value)
84 (delete-selection-mode (or value 0)))
85 :initialize 'custom-initialize-default
86 :type 'boolean
87 :group 'editing-basics
88 :require 'delsel)
89
90 (defun delete-active-region (&optional killp)
91 (if killp
92 (kill-region (point) (mark))
93 (delete-region (point) (mark)))
94 (setq mark-active nil)
95 (run-hooks 'deactivate-mark-hook)
96 t)
97
98 (defun delete-selection-pre-hook ()
99 (when (and delete-selection-mode transient-mark-mode mark-active
100 (not buffer-read-only))
101 (let ((type (and (symbolp this-command)
102 (get this-command 'delete-selection))))
103 (cond ((eq type 'kill)
104 (delete-active-region t))
105 ((eq type 'yank)
106 ;; Before a yank command,
107 ;; make sure we don't yank the same region
108 ;; that we are going to delete.
109 ;; That would make yank a no-op.
110 (when (string= (buffer-substring-no-properties (point) (mark))
111 (car kill-ring))
112 (current-kill 1))
113 (delete-active-region))
114 ((eq type 'supersede)
115 (delete-active-region)
116 (setq this-command 'ignore))
117 (type
118 (delete-active-region))))))
119
120 (put 'self-insert-command 'delete-selection t)
121 (put 'self-insert-iso 'delete-selection t)
122
123 (put 'yank 'delete-selection 'yank)
124 (put 'clipboard-yank 'delete-selection 'yank)
125 (put 'insert-register 'delete-selection t)
126
127 (put 'delete-backward-char 'delete-selection 'supersede)
128 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
129 (put 'delete-char 'delete-selection 'supersede)
130
131 (put 'newline-and-indent 'delete-selection t)
132 (put 'newline 'delete-selection t)
133 (put 'open-line 'delete-selection 'kill)
134
135 (put 'insert-parentheses 'delete-selection t)
136
137 ;; This is very useful for cancelling a selection in the minibuffer without
138 ;; aborting the minibuffer.
139 (defun minibuffer-keyboard-quit ()
140 "Abort recursive edit.
141 In Delete Selection mode, if the mark is active, just deactivate it;
142 then it takes a second \\[keyboard-quit] to abort the minibuffer."
143 (interactive)
144 (if (and delete-selection-mode transient-mark-mode mark-active)
145 (setq deactivate-mark t)
146 (abort-recursive-edit)))
147
148 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
149 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
150 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
151 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
152 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
153
154 (provide 'delsel)
155
156 ;; This is the standard way mechanism to put the mode into effect
157 ;; if delete-selection-mode has already been set to t
158 ;; when this file is loaded.
159 (when delete-selection-mode
160 (delete-selection-mode t))
161
162 ;;; delsel.el ends here