diff options
author | Po Lu <luangruo@yahoo.com> | 2022-07-09 09:07:07 +0800 |
---|---|---|
committer | Po Lu <luangruo@yahoo.com> | 2022-07-09 09:07:22 +0800 |
commit | 35ae8d9f3b18c34a6e6c594afcc442e7aaa5fe29 (patch) | |
tree | 1334f0af09634f84f320ddd45d47a0ab893fa8b9 | |
parent | 3d3aaf3af3e497e5ed1aa5924c73fadf45ea3eef (diff) | |
download | emacs-35ae8d9f3b18c34a6e6c594afcc442e7aaa5fe29.tar.gz |
Add new minor mode to deactivate the region once PRIMARY is lost
* doc/emacs/killing.texi (Primary Selection): Document new minor
mode `lost-selection-mode'.
* etc/NEWS: Announce new minor mode.
* lisp/select.el (lost-selection-function): New function.
(lost-selection-mode): New global minor mode.
-rw-r--r-- | doc/emacs/killing.texi | 23 | ||||
-rw-r--r-- | etc/NEWS | 5 | ||||
-rw-r--r-- | lisp/select.el | 39 |
3 files changed, 59 insertions, 8 deletions
diff --git a/doc/emacs/killing.texi b/doc/emacs/killing.texi index 4435f6e4946..bb8d51158ae 100644 --- a/doc/emacs/killing.texi +++ b/doc/emacs/killing.texi @@ -610,14 +610,14 @@ yanks the contents of the clipboard at point. @cindex primary selection @cindex selection, primary - Under the X Window System, there exists a @dfn{primary selection} -containing the last stretch of text selected in an X application -(usually by dragging the mouse). Typically, this text can be inserted -into other X applications by @kbd{mouse-2} clicks. The primary -selection is separate from the clipboard. Its contents are more -fragile; they are overwritten each time you select text with the -mouse, whereas the clipboard is only overwritten by explicit cut -or copy commands. + Under the X Window System, PGTK and Haiku, there exists a +@dfn{primary selection} containing the last stretch of text selected +in an X application (usually by dragging the mouse). Typically, this +text can be inserted into other X applications by @kbd{mouse-2} +clicks. The primary selection is separate from the clipboard. Its +contents are more fragile; they are overwritten each time you select +text with the mouse, whereas the clipboard is only overwritten by +explicit cut or copy commands. Under X, whenever the region is active (@pxref{Mark}), the text in the region is saved in the primary selection. This applies regardless @@ -639,6 +639,13 @@ regions to the primary selection entirely. (@kbd{C-y}) to insert this text if @code{select-enable-primary} is set (@pxref{Clipboard}). +@cindex lost-selection-mode + By default, Emacs keeps the region active even after text is +selected in another program; this is contrary to typical X behavior. +To make Emacs deactivate the region after another program places data +in the primary selection, enable the global minor mode +@code{lost-selection-mode}. + @cindex MS-Windows, and primary selection MS-Windows provides no primary selection, but Emacs emulates it within a single Emacs session by storing the selected text internally. @@ -2422,6 +2422,11 @@ be kept aligned with the buffer contents when the user switches 'display-line-numbers-mode' on or off. +++ +** New minor mode 'lost-selection-mode'. +This minor mode makes Emacs deactivate the mark in all buffers when +the primary selection is obtained by another program. + ++++ ** New predicate 'char-uppercase-p'. This returns non-nil if its argument its an uppercase character. diff --git a/lisp/select.el b/lisp/select.el index d977a8714b6..6002b2615e1 100644 --- a/lisp/select.el +++ b/lisp/select.el @@ -475,6 +475,45 @@ are not available to other programs." (symbolp data) (integerp data))) + +;; Minor mode to make losing ownership of PRIMARY behave more like +;; other X programs. + +(defun lost-selection-function (selection) + "Handle losing of ownership of SELECTION. +If SELECTION is `PRIMARY', deactivate the mark in every +non-temporary buffer." + (let ((select-active-regions nil)) + (when (eq selection 'PRIMARY) + (dolist (buffer (buffer-list)) + (unless (string-match-p "^ " + (buffer-name buffer)) + (with-current-buffer buffer + (deactivate-mark t))))))) + +(define-minor-mode lost-selection-mode + "Toggle `lost-selection-mode'. + +When this is enabled, selecting some text in another program will +cause the mark to be deactivated in all buffers, mimicking the +behavior of most X Windows programs." + :global t + :group 'x + (if lost-selection-mode + (cond ((featurep 'x) (add-hook 'x-lost-selection-functions + #'lost-selection-function)) + ((featurep 'pgtk) (add-hook 'pgtk-lost-selection-functions + #'lost-selection-function)) + ((featurep 'haiku) (add-hook 'haiku-lost-selection-functions + #'lost-selection-function))) + (cond ((featurep 'x) (remove-hook 'x-lost-selection-functions + #'lost-selection-function)) + ((featurep 'pgtk) (remove-hook 'pgtk-lost-selection-functions + #'lost-selection-function)) + ((featurep 'haiku) (remove-hook 'haiku-lost-selection-functions + #'lost-selection-function))))) + + ;; Functions to convert the selection into various other selection types. ;; Every selection type that Emacs handles is implemented this way, except ;; for TIMESTAMP, which is a special case. |