diff options
| author | Simon Marshall | 1997-07-15 07:45:25 +0000 |
|---|---|---|
| committer | Simon Marshall | 1997-07-15 07:45:25 +0000 |
| commit | 7853929fc92c8fdee65da53d1deaca782b97484d (patch) | |
| tree | ff21a57d8e200bdc7511c33614b9aaa6acd5f77d | |
| parent | ad3f1e65bde1bc50b3d92c8126e10ebd0f410994 (diff) | |
| download | emacs-7853929fc92c8fdee65da53d1deaca782b97484d.tar.gz emacs-7853929fc92c8fdee65da53d1deaca782b97484d.zip | |
Customise. Don't modify pre-command-hook on file load; do in command.
| -rw-r--r-- | lisp/delsel.el | 113 |
1 files changed, 68 insertions, 45 deletions
diff --git a/lisp/delsel.el b/lisp/delsel.el index 28cc758c31e..bc6bea59871 100644 --- a/lisp/delsel.el +++ b/lisp/delsel.el | |||
| @@ -32,10 +32,55 @@ | |||
| 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 |
| @@ -46,29 +91,26 @@ insertion commands first delete the region and then insert.") | |||
| 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) |
| @@ -85,25 +127,6 @@ insertion commands first delete the region and then insert.") | |||
| 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 | 129 | ||
| 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 | |||
| 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 () |