(kill-comment): Fixed by rewriting it with syntax-tables rather than regexps
[emacs.git] / lisp / delsel.el
blob0d7a57d8ab9f8bc57a717ce14d26a0408f4aa9e8
1 ;;; delsel.el --- delete selection if you insert
3 ;; Copyright (C) 1992, 1997, 1998 Free Software Foundation, Inc.
5 ;; Author: Matthieu Devin <devin@lucid.com>
6 ;; Maintainer: FSF
7 ;; Created: 14 Jul 92
9 ;; This file is part of GNU Emacs.
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.
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.
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.
26 ;;; Commentary:
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.
32 ;; Interface:
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.
52 ;;; Code:
54 ;;;###autoload
55 (defalias 'pending-delete-mode 'delete-selection-mode)
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.
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)))
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)
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)
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))))))
119 (put 'self-insert-command 'delete-selection t)
120 (put 'self-insert-iso 'delete-selection t)
122 (put 'yank 'delete-selection 'yank)
123 (put 'clipboard-yank 'delete-selection 'yank)
124 (put 'insert-register 'delete-selection t)
126 (put 'delete-backward-char 'delete-selection 'supersede)
127 (put 'backward-delete-char-untabify 'delete-selection 'supersede)
128 (put 'delete-char 'delete-selection 'supersede)
130 (put 'newline-and-indent 'delete-selection t)
131 (put 'newline 'delete-selection t)
132 (put 'open-line 'delete-selection 'kill)
134 (put 'insert-parentheses 'delete-selection t)
136 ;; This is very useful for cancelling a selection in the minibuffer without
137 ;; aborting the minibuffer.
138 (defun minibuffer-keyboard-quit ()
139 "Abort recursive edit.
140 In Delete Selection mode, if the mark is active, just deactivate it;
141 then it takes a second \\[keyboard-quit] to abort the minibuffer."
142 (interactive)
143 (if (and delete-selection-mode transient-mark-mode mark-active)
144 (setq deactivate-mark t)
145 (abort-recursive-edit)))
147 (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
148 (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
149 (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
150 (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
151 (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
153 (provide 'delsel)
155 ;; This is the standard way mechanism to put the mode into effect
156 ;; if delete-selection-mode has already been set to t
157 ;; when this file is loaded.
158 (when delete-selection-mode
159 (delete-selection-mode t))
161 ;;; delsel.el ends here