emacs-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

master 35ae8d9f3b: Add new minor mode to deactivate the region once PRIM


From: Po Lu
Subject: master 35ae8d9f3b: Add new minor mode to deactivate the region once PRIMARY is lost
Date: Fri, 8 Jul 2022 21:07:32 -0400 (EDT)

branch: master
commit 35ae8d9f3b18c34a6e6c594afcc442e7aaa5fe29
Author: Po Lu <luangruo@yahoo.com>
Commit: Po Lu <luangruo@yahoo.com>

    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.
---
 doc/emacs/killing.texi | 23 +++++++++++++++--------
 etc/NEWS               |  5 +++++
 lisp/select.el         | 39 +++++++++++++++++++++++++++++++++++++++
 3 files changed, 59 insertions(+), 8 deletions(-)

diff --git a/doc/emacs/killing.texi b/doc/emacs/killing.texi
index 4435f6e494..bb8d51158a 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.
diff --git a/etc/NEWS b/etc/NEWS
index 925bd9a212..5831bbefd4 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -2421,6 +2421,11 @@ This is meant to be used in modes that have a header 
line that should
 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 d977a8714b..6002b2615e 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.



reply via email to

[Prev in Thread] Current Thread [Next in Thread]