delsel.el 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181
  1. ;;; delsel.el --- delete selection if you insert
  2. ;; Copyright (C) 1992, 1997-1998, 2001-2012 Free Software Foundation, Inc.
  3. ;; Author: Matthieu Devin <devin@lucid.com>
  4. ;; Maintainer: FSF
  5. ;; Created: 14 Jul 92
  6. ;; Keywords: convenience emulations
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; This file makes the active region be pending delete, meaning that
  20. ;; text inserted while the region is active will replace the region contents.
  21. ;; This is a popular behavior of personal computers text editors.
  22. ;; Interface:
  23. ;; Commands which will delete the selection need a 'delete-selection
  24. ;; property on their symbols; commands which insert text but don't
  25. ;; have this property won't delete the selection. It can be one of
  26. ;; the values:
  27. ;; 'yank
  28. ;; For commands which do a yank; ensures the region about to be
  29. ;; deleted isn't yanked.
  30. ;; 'supersede
  31. ;; Delete the active region and ignore the current command,
  32. ;; i.e. the command will just delete the region.
  33. ;; 'kill
  34. ;; `kill-region' is used on the selection, rather than
  35. ;; `delete-region'. (Text selected with the mouse will typically
  36. ;; be yankable anyhow.)
  37. ;; non-nil
  38. ;; The normal case: delete the active region prior to executing
  39. ;; the command which will insert replacement text.
  40. ;;; Code:
  41. ;;;###autoload
  42. (defalias 'pending-delete-mode 'delete-selection-mode)
  43. ;;;###autoload
  44. (define-minor-mode delete-selection-mode
  45. "Toggle Delete Selection mode.
  46. With a prefix argument ARG, enable Delete Selection mode if ARG
  47. is positive, and disable it otherwise. If called from Lisp,
  48. enable the mode if ARG is omitted or nil.
  49. When Delete Selection mode is enabled, Transient Mark mode is also
  50. enabled and typed text replaces the selection if the selection is
  51. active. Otherwise, typed text is just inserted at point regardless of
  52. any selection."
  53. :global t :group 'editing-basics
  54. (if (not delete-selection-mode)
  55. (remove-hook 'pre-command-hook 'delete-selection-pre-hook)
  56. (add-hook 'pre-command-hook 'delete-selection-pre-hook)
  57. (transient-mark-mode t)))
  58. (defun delete-active-region (&optional killp)
  59. (if killp
  60. (kill-region (point) (mark))
  61. (delete-region (point) (mark)))
  62. t)
  63. (defun delete-selection-pre-hook ()
  64. (when (and delete-selection-mode transient-mark-mode mark-active
  65. (not buffer-read-only))
  66. (let ((type (and (symbolp this-command)
  67. (get this-command 'delete-selection))))
  68. (condition-case data
  69. (cond ((eq type 'kill)
  70. (delete-active-region t))
  71. ((eq type 'yank)
  72. ;; Before a yank command, make sure we don't yank the
  73. ;; head of the kill-ring that really comes from the
  74. ;; currently active region we are going to delete.
  75. ;; That would make yank a no-op.
  76. (when (and (string= (buffer-substring-no-properties (point) (mark))
  77. (car kill-ring))
  78. (fboundp 'mouse-region-match)
  79. (mouse-region-match))
  80. (current-kill 1))
  81. (delete-active-region))
  82. ((eq type 'supersede)
  83. (let ((empty-region (= (point) (mark))))
  84. (delete-active-region)
  85. (unless empty-region
  86. (setq this-command 'ignore))))
  87. (type
  88. (delete-active-region)
  89. (if (and overwrite-mode (eq this-command 'self-insert-command))
  90. (let ((overwrite-mode nil))
  91. (self-insert-command (prefix-numeric-value current-prefix-arg))
  92. (setq this-command 'ignore)))))
  93. (file-supersession
  94. ;; If ask-user-about-supersession-threat signals an error,
  95. ;; stop safe_run_hooks from clearing out pre-command-hook.
  96. (and (eq inhibit-quit 'pre-command-hook)
  97. (setq inhibit-quit 'delete-selection-dummy))
  98. (signal 'file-supersession (cdr data)))
  99. (text-read-only
  100. ;; This signal may come either from `delete-active-region' or
  101. ;; `self-insert-command' (when `overwrite-mode' is non-nil).
  102. ;; To avoid clearing out `pre-command-hook' we handle this case
  103. ;; by issuing a simple message. Note, however, that we do not
  104. ;; handle all related problems: When read-only text ends before
  105. ;; the end of the region, the latter is not deleted but any
  106. ;; subsequent insertion will succeed. We could avoid this case
  107. ;; by doing a (setq this-command 'ignore) here. This would,
  108. ;; however, still not handle the case where read-only text ends
  109. ;; precisely where the region starts: In that case the deletion
  110. ;; would succeed but the subsequent insertion would fail with a
  111. ;; text-read-only error. To handle that case we would have to
  112. ;; investigate text properties at both ends of the region and
  113. ;; skip the deletion when inserting text is forbidden there.
  114. (message "Text is read-only") (ding))))))
  115. (put 'self-insert-command 'delete-selection t)
  116. (put 'self-insert-iso 'delete-selection t)
  117. (put 'yank 'delete-selection 'yank)
  118. (put 'clipboard-yank 'delete-selection 'yank)
  119. (put 'insert-register 'delete-selection t)
  120. (put 'delete-backward-char 'delete-selection 'supersede)
  121. (put 'backward-delete-char-untabify 'delete-selection 'supersede)
  122. (put 'delete-char 'delete-selection 'supersede)
  123. (put 'newline-and-indent 'delete-selection t)
  124. (put 'newline 'delete-selection t)
  125. (put 'open-line 'delete-selection 'kill)
  126. ;; This is very useful for canceling a selection in the minibuffer without
  127. ;; aborting the minibuffer.
  128. (defun minibuffer-keyboard-quit ()
  129. "Abort recursive edit.
  130. In Delete Selection mode, if the mark is active, just deactivate it;
  131. then it takes a second \\[keyboard-quit] to abort the minibuffer."
  132. (interactive)
  133. (if (and delete-selection-mode transient-mark-mode mark-active)
  134. (setq deactivate-mark t)
  135. (abort-recursive-edit)))
  136. (define-key minibuffer-local-map "\C-g" 'minibuffer-keyboard-quit)
  137. (define-key minibuffer-local-ns-map "\C-g" 'minibuffer-keyboard-quit)
  138. (define-key minibuffer-local-completion-map "\C-g" 'minibuffer-keyboard-quit)
  139. (define-key minibuffer-local-must-match-map "\C-g" 'minibuffer-keyboard-quit)
  140. (define-key minibuffer-local-isearch-map "\C-g" 'minibuffer-keyboard-quit)
  141. (defun delsel-unload-function ()
  142. "Unload the Delete Selection library."
  143. (define-key minibuffer-local-map "\C-g" 'abort-recursive-edit)
  144. (define-key minibuffer-local-ns-map "\C-g" 'abort-recursive-edit)
  145. (define-key minibuffer-local-completion-map "\C-g" 'abort-recursive-edit)
  146. (define-key minibuffer-local-must-match-map "\C-g" 'abort-recursive-edit)
  147. (define-key minibuffer-local-isearch-map "\C-g" 'abort-recursive-edit)
  148. (dolist (sym '(self-insert-command self-insert-iso yank clipboard-yank
  149. insert-register delete-backward-char backward-delete-char-untabify
  150. delete-char newline-and-indent newline open-line))
  151. (put sym 'delete-selection nil))
  152. ;; continue standard unloading
  153. nil)
  154. (provide 'delsel)
  155. ;;; delsel.el ends here