summaryrefslogtreecommitdiff
path: root/lisp/select.el
diff options
context:
space:
mode:
authorPo Lu <luangruo@yahoo.com>2022-07-09 09:07:07 +0800
committerPo Lu <luangruo@yahoo.com>2022-07-09 09:07:22 +0800
commit35ae8d9f3b18c34a6e6c594afcc442e7aaa5fe29 (patch)
tree1334f0af09634f84f320ddd45d47a0ab893fa8b9 /lisp/select.el
parent3d3aaf3af3e497e5ed1aa5924c73fadf45ea3eef (diff)
downloademacs-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.
Diffstat (limited to 'lisp/select.el')
-rw-r--r--lisp/select.el39
1 files changed, 39 insertions, 0 deletions
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.