Mercurial > emacs
comparison lisp/delsel.el @ 18785:1a455d8dfa7c
Customise. Don't modify pre-command-hook on file load; do in command.
author | Simon Marshall <simon@gnu.org> |
---|---|
date | Tue, 15 Jul 1997 07:45:25 +0000 |
parents | 9830b54653b7 |
children | c8c67c398475 |
comparison
equal
deleted
inserted
replaced
18784:5e56fda13c2f | 18785:1a455d8dfa7c |
---|---|
30 ;; text inserted while the region is active will replace the region contents. | 30 ;; text inserted while the region is active will replace the region contents. |
31 ;; This is a popular behavior of personal computers text editors. | 31 ;; This is a popular behavior of personal computers text editors. |
32 | 32 |
33 ;;; Code: | 33 ;;; Code: |
34 | 34 |
35 (defvar delete-selection-mode t | 35 (eval-when-compile |
36 "*Non-nil means Delete Selection mode is enabled. | 36 (require 'cl)) |
37 In Delete Selection mode, when a region is highlighted, | 37 |
38 insertion commands first delete the region and then insert.") | 38 ;;;###autoload |
39 (defalias 'pending-delete-mode 'delete-selection-mode) | |
40 | |
41 ;;;###autoload | |
42 (defun delete-selection-mode (&optional arg) | |
43 "Toggle Delete Selection mode. | |
44 With prefix ARG, turn Delete Selection mode on if and only if ARG is positive. | |
45 | |
46 When Delete Selection mode is enabled, Transient Mark mode is also enabled and | |
47 typed text replaces the selection if the selection is active. Otherwise, typed | |
48 text is just inserted at point regardless of any selection." | |
49 (interactive "P") | |
50 (setq delete-selection-mode (if arg | |
51 (> (prefix-numeric-value arg) 0) | |
52 (not delete-selection-mode))) | |
53 (if (not delete-selection-mode) | |
54 (remove-hook 'pre-command-hook 'delete-selection-pre-hook) | |
55 (add-hook 'pre-command-hook 'delete-selection-pre-hook) | |
56 (transient-mark-mode t))) | |
57 | |
58 ;;;###autoload | |
59 (defcustom delete-selection-mode nil | |
60 "Toggle Delete Selection mode. | |
61 When Delete Selection mode is enabled, Transient Mark mode is also enabled and | |
62 typed text replaces the selection if the selection is active. | |
63 You must modify via \\[customize] for this variable to have an effect." | |
64 :set (lambda (symbol value) | |
65 (delete-selection-mode (or value 0))) | |
66 :initialize 'custom-initialize-default | |
67 :type 'boolean | |
68 :group 'editing-basics | |
69 :require 'delsel) | |
70 | |
71 ;; Since the above autoloaded option contains a `:set' form, this file would | |
72 ;; get loaded from loaddefs.el. We can use the above `:initialize' keyword, | |
73 ;; and the below `when' form, to the prevent automatic loading of this file, or | |
74 ;; an `:initialize' keyword of the form: | |
75 ;; | |
76 ;; :initialize (lambda (symbol value) | |
77 ;; (if value | |
78 ;; (delete-selection-mode t) | |
79 ;; (custom-initialize-default symbol nil)) | |
80 ;; | |
81 ;; We choose the former as it is the general mechanism for such toggle options. | |
82 (when delete-selection-mode | |
83 (delete-selection-mode t)) | |
39 | 84 |
40 (defun delete-active-region (&optional killp) | 85 (defun delete-active-region (&optional killp) |
41 (if killp | 86 (if killp |
42 (kill-region (point) (mark)) | 87 (kill-region (point) (mark)) |
43 (delete-region (point) (mark))) | 88 (delete-region (point) (mark))) |
44 (setq mark-active nil) | 89 (setq mark-active nil) |
45 (run-hooks 'deactivate-mark-hook) | 90 (run-hooks 'deactivate-mark-hook) |
46 t) | 91 t) |
47 | 92 |
48 (defun delete-selection-pre-hook () | 93 (defun delete-selection-pre-hook () |
49 (if (and delete-selection-mode | 94 (when (and delete-selection-mode transient-mark-mode mark-active |
50 (not buffer-read-only) | 95 (not buffer-read-only)) |
51 transient-mark-mode mark-active) | 96 (let ((type (and (symbolp this-command) |
52 (let ((type (and (symbolp this-command) | 97 (get this-command 'delete-selection)))) |
53 (get this-command 'delete-selection)))) | 98 (cond ((eq type 'kill) |
54 (cond ((eq type 'kill) | 99 (delete-active-region t)) |
55 (delete-active-region t)) | 100 ((eq type 'yank) |
56 ((eq type 'yank) | 101 ;; Before a yank command, |
57 ;; Before a yank command, | 102 ;; make sure we don't yank the same region |
58 ;; make sure we don't yank the same region | 103 ;; that we are going to delete. |
59 ;; that we are going to delete. | 104 ;; That would make yank a no-op. |
60 ;; That would make yank a no-op. | 105 (when (string= (buffer-substring-no-properties (point) (mark)) |
61 (if (string= (buffer-substring (point) (mark)) | |
62 (car kill-ring)) | 106 (car kill-ring)) |
63 (current-kill 1)) | 107 (current-kill 1)) |
64 (delete-active-region nil)) | 108 (delete-active-region nil)) |
65 ((eq type 'supersede) | 109 ((eq type 'supersede) |
66 (if (delete-active-region nil) | 110 (when (delete-active-region nil) |
67 (setq this-command '(lambda () (interactive))))) | 111 (setq this-command '(lambda () (interactive))))) |
68 (type | 112 (type |
69 (delete-active-region nil)))))) | 113 (delete-active-region nil)))))) |
70 | |
71 (add-hook 'pre-command-hook 'delete-selection-pre-hook) | |
72 | 114 |
73 (put 'self-insert-command 'delete-selection t) | 115 (put 'self-insert-command 'delete-selection t) |
74 (put 'self-insert-iso 'delete-selection t) | 116 (put 'self-insert-iso 'delete-selection t) |
75 | 117 |
76 (put 'yank 'delete-selection 'yank) | 118 (put 'yank 'delete-selection 'yank) |
82 (put 'delete-char 'delete-selection 'supersede) | 124 (put 'delete-char 'delete-selection 'supersede) |
83 | 125 |
84 (put 'newline-and-indent 'delete-selection 't) | 126 (put 'newline-and-indent 'delete-selection 't) |
85 (put 'newline 'delete-selection t) | 127 (put 'newline 'delete-selection t) |
86 (put 'open-line 'delete-selection t) | 128 (put 'open-line 'delete-selection t) |
87 | |
88 ;;;###autoload | |
89 (defalias 'pending-delete-mode 'delete-selection-mode) | |
90 ;;;###autoload | |
91 (defun delete-selection-mode (arg) | |
92 "Toggle Delete Selection mode. | |
93 When ON, typed text replaces the selection if the selection is active. | |
94 When OFF, typed text is just inserted at point. | |
95 | |
96 Delete Selection mode works only when Transient Mark mode is enabled. | |
97 Use \\[transient-mark-mode] to enable or disable Transient Mark mode. | |
98 | |
99 A positive argument turns the mode on, negative argument turns it off, | |
100 and no argument (or nil) toggles the mode." | |
101 (interactive "P") | |
102 (setq delete-selection-mode | |
103 (if (null arg) (not delete-selection-mode) | |
104 (> (prefix-numeric-value arg) 0))) | |
105 (force-mode-line-update)) | |
106 | 129 |
107 ;; This is very useful for cancelling a selection in the minibuffer without | 130 ;; This is very useful for cancelling a selection in the minibuffer without |
108 ;; aborting the minibuffer. | 131 ;; aborting the minibuffer. |
109 (defun minibuffer-keyboard-quit () | 132 (defun minibuffer-keyboard-quit () |
110 "Abort recursive edit. | 133 "Abort recursive edit. |