viper -- Update and prettify package-info.in provides.
[packages] / xemacs-packages / pc / pending-del.el
1 ;; pending-del.el --- Making insertions replace any selected text.
2
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Author: Matthieu Devin <devin@lucid.com>, 14 Jul 92.
6 ;; Maintainer: Hrvoje Niksic <hniksic@xemacs.org>
7 ;; Version 2.2
8
9 ;; This file is part of XEmacs.
10
11 ;; XEmacs is free software; you can redistribute it and/or modify it
12 ;; 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 ;; XEmacs is distributed in the hope that it will be useful, but
17 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
19 ;; General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with XEmacs; 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 ;;; Synched up with: 19.34  (distributed as delsel.el in FSF)
27
28 ;;; Commentary:
29
30 ;; Much of this code was revamped by Hrvoje Niksic, July 1997, with
31 ;; version number set to 2.x.
32
33 ;; Pending-del is now a minor mode, with all the normal toggle
34 ;; functions.  It should be somewhat faster, too.
35
36 \f
37 ;;; Code:
38
39 ;;;###autoload
40 (defcustom pending-delete-mode nil
41   "Non-nil when Pending Delete mode is enabled. In Pending Delete mode, typed
42 text replaces the selected region. Normally, you shouldn't modify this
43 variable by hand, but use the function `pending-delete-mode' instead. However, 
44 you can customize the default value from the options menu (auto delete
45 selection)."
46   :type 'boolean
47   :set (lambda (symbol value)
48          (pending-delete-mode (or value 0)))
49   :initialize 'custom-initialize-default
50   :require 'pending-del
51   :group 'keyboard)
52
53 (defcustom pending-delete-modeline-string " PenDel"
54   "*String to display in the modeline when Pending Delete mode is active.
55 Set this to nil if you don't want a modeline indicator."
56   :type '(choice string
57                  (const :tag "none" nil))
58   :group 'keyboard)
59
60 (add-minor-mode 'pending-delete-mode 'pending-delete-modeline-string)
61
62 \f
63 (defun pending-delete-active-region (&optional killp)
64   (when (and (region-active-p)
65              (if zmacs-region-rectangular-p
66                  (eq (extent-object (car zmacs-region-extent))
67                      (current-buffer))
68                (eq (extent-object zmacs-region-extent) (current-buffer)))
69              (not buffer-read-only))
70     ;; Here we used to check whether the point lies between the
71     ;; beginning and end of the extent.  I don't see how it is
72     ;; necessary, as the C code makes sure that this is so; it only
73     ;; slow things down.
74     (if zmacs-region-rectangular-p
75         (if killp
76             (kill-rectangle (region-beginning) (region-end))
77           (delete-rectangle (region-beginning) (region-end)))
78       (if killp
79           (kill-region (region-beginning) (region-end))
80         (delete-region (region-beginning) (region-end))))
81     (zmacs-deactivate-region)
82     t))
83
84 (defun pending-delete-pre-hook ()
85   (condition-case e
86       (let ((type (and (symbolp this-command)
87                        (get this-command 'pending-delete))))
88         (cond ((eq type 'kill)
89                (pending-delete-active-region t))
90               ((eq type 'supersede)
91                (if (pending-delete-active-region ())
92                    (setq this-command (lambda () (interactive)))))
93               (type
94                (pending-delete-active-region ()))))
95     (error
96      (warn "Error caught in `pending-delete-pre-hook': %s"
97            (error-message-string e)))))
98
99 \f
100
101 ;; now set up the keys that delete the selection and do nothing else.
102 ;; (Typically anything that is or could be bound to `delete' or
103 ;; `backspace'.)
104 (mapcar #'(lambda (sym)
105             (put sym 'pending-delete 'supersede))
106         '(delete-backward-char
107           backward-delete-char
108           delete-char
109           backward-or-forward-delete-char
110           kill-word
111           backward-kill-word
112           backward-or-forward-kill-word
113
114           c-electric-delete
115           cperl-electric-backspace
116           cperl-electric-delete))
117
118 ;; now set up the keys that delete the selection and then do their normal
119 ;; action.
120 (mapcar #'(lambda (sym)
121             (put sym 'pending-delete t))
122         '(self-insert-command
123
124           yank
125           yank-rectangle
126           yank-clipboard-selection
127           x-yank-clipboard-selection
128           toolbar-paste
129
130           cperl-electric-paren
131           cperl-electric-rparen
132           cperl-electric-terminator
133           cperl-electric-semi
134           cperl-electric-lbrace
135           cperl-electric-brace
136
137           skeleton-pair-insert-maybe
138
139           electric-c-sharp-sign
140           electric-c-terminator
141           electric-c-semi
142           electric-c-brace))
143
144 ;; Don't delete for these.  They're more problematic than helpful.
145 ;;
146 ;; (put 'newline-and-indent 'pending-delete t)
147 ;; (put 'newline 'pending-delete t)
148 ;; (put 'open-line 'pending-delete t)
149
150 (put 'insert-register 'pending-delete t)
151
152 \f
153 ;;;###autoload
154 (defun turn-on-pending-delete (&optional ignored)
155   "Turn on pending delete minor mode unconditionally."
156   (interactive)
157   (pending-delete-mode 1))
158
159 ;;;###autoload
160 (defun turn-off-pending-delete (&optional ignored)
161   "Turn off pending delete minor mode unconditionally."
162   (interactive)
163   (pending-delete-mode 0))
164
165 ;;;###autoload
166 (defun pending-delete-mode (&optional arg)
167   "Toggle Pending Delete minor mode.
168 When the pending delete is on, typed text replaces the selection.
169 With a positive argument, turns it on.
170 With a non-positive argument, turns it off."
171   (interactive "P")
172   (setq pending-delete-mode
173         (if (null arg) (not pending-delete-mode)
174           (> (prefix-numeric-value arg) 0)))
175   (if pending-delete-mode
176       (add-hook 'pre-command-hook 'pending-delete-pre-hook)
177     (remove-hook 'pre-command-hook 'pending-delete-pre-hook))
178   (force-mode-line-update))
179
180
181 ;; Backward compatibility:
182 ;;;###autoload
183 (define-obsolete-function-alias 'pending-delete-on 'turn-on-pending-delete)
184 ;;;###autoload
185 (define-obsolete-function-alias 'pending-delete-off 'turn-off-pending-delete)
186
187 ;; FSF compatibility:
188 ;;;###autoload
189 (define-compatible-function-alias 'delete-selection-mode 'pending-delete-mode)
190 (define-compatible-variable-alias 'delete-selection-mode 'pending-delete-mode)
191
192 ;; Compatibility and convenience:
193 ;;;###autoload
194 (defalias 'pending-delete 'pending-delete-mode)
195
196 \f
197 ;; The following code used to turn the mode on unconditionally.
198 ;; However, this is a very bad idea -- since pending-del is
199 ;; autoloaded, (turn-on-pending-delete) is as easy to add to `.emacs'
200 ;; as (require 'pending-del) used to be.
201
202 ;(pending-delete-on (eq pending-delete-verbose t))
203
204 (provide 'pending-del)
205
206 ;;; pending-del.el ends here