Subject: bug#55883: [PATCH] Update X Primary Selection with active regions

From: Duncan Findlay <[email-masked]>
Date: Thu, 9 Jun 2022 23:18:01 -0700

Severity: wishlist
Tags: patch

Attached are two patches to allow users that use xterm's OSC 52
clipboard support to have their primary selection updated with the
active region, to better match the behavior of running Emacs in X
locally.

This behavior is enabled automatically for supported terminals, and is
controlled by the existing `select-active-regions' variable.

Previous discussion:
https://lists.gnu.org/archive/html/emacs-devel/2022-06/msg00126.html

This should be covered by a corporate copyright assignment on file.
From 5dee5a44fd0d3ad57ba7e40fd5b0542d70160c14 Mon Sep 17 00:00:00 2001
From: Duncan Findlay <[email-masked]>
Date: Fri, 20 May 2022 00:17:51 -0700
Subject: [PATCH 1/2] Use `display-selections-p' for selecting active region

`display-selections-p' is documented as the preferred way to determine
capabilities, over `window-system'.  This allows supported text
terminals to indicate support for these selections.

* src/keyboard.c (command_loop_1): Replace call to `window-system'
with `display-selections-p' when deciding whether to update primary
selection.
---
 src/keyboard.c | 3 ++-
 1 file changed, 2 insertions(+), 1 deletion(-)

diff --git a/src/keyboard.c b/src/keyboard.c
index 55d710ed62..dc3c37e4d8 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -1569,7 +1569,7 @@ command_loop_1 (void)
 	    {
 	      /* Even if not deactivating the mark, set PRIMARY if
 		 `select-active-regions' is non-nil.  */
-	      if (!NILP (Fwindow_system (Qnil))
+	      if (!NILP (call0 (Qdisplay_selections_p))
 		  /* Even if mark_active is non-nil, the actual buffer
 		     marker may not have been set yet (Bug#7044).  */
 		  && XMARKER (BVAR (current_buffer, mark))->buffer
@@ -12162,6 +12162,7 @@ syms_of_keyboard (void)
 
   DEFSYM (Qpolling_period, "polling-period");
 
+  DEFSYM (Qdisplay_selections_p, "display-selections-p");
   DEFSYM (Qgui_set_selection, "gui-set-selection");
 
   /* The primary selection.  */
-- 
2.36.1.476.g0c4daa206d-goog

From 25bb187911e0444c5e1618650a061f0be317bee7 Mon Sep 17 00:00:00 2001
From: Duncan Findlay <[email-masked]>
Date: Thu, 2 Jun 2022 20:23:44 -0700
Subject: [PATCH 2/2] Support `select-active-regions' with xterm

This allows Emacs to save the active region to the user's primary
selection on supported terminals.  This behavior is controlled by the
existing `select-active-regions' variable.

* lisp/frame.el (display-selections-p): For text terminals, return
terminal parameter `display-selections-p` to indicate selection
support.
* lisp/term/xterm.el (xterm--init-activate-set-selection): Set the
`display-selections-p` terminal parameter.
---
 etc/NEWS           | 5 +++++
 lisp/frame.el      | 2 ++
 lisp/term/xterm.el | 3 ++-
 3 files changed, 9 insertions(+), 1 deletion(-)

diff --git a/etc/NEWS b/etc/NEWS
index cd4b1b06ec..17c98b772c 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -477,6 +477,11 @@ This is in addition to previously-supported ways of discovering 24-bit
 color support: either via the "RGB" or "setf24" capabilities, or if
 the 'COLORTERM' environment variable is set to the value "truecolor".
 
+*** Primary selection of active regions with xterm setSelection support.
+On supported terminals, the active region may be saved to the X
+primary selection.  This behavior is controlled by the
+'select-active-regions' variable.
+
 ** ERT
 
 +++
diff --git a/lisp/frame.el b/lisp/frame.el
index 27f99fb7d2..e926dff201 100644
--- a/lisp/frame.el
+++ b/lisp/frame.el
@@ -2164,6 +2164,8 @@ display-selections-p
        (not (null dos-windows-version))))
      ((memq frame-type '(x w32 ns pgtk))
       t)
+     ((eq frame-type t)
+      (terminal-parameter display 'display-selections-p))
      (t
       nil))))
 
diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el
index a7e257f41c..f6add0fbf2 100644
--- a/lisp/term/xterm.el
+++ b/lisp/term/xterm.el
@@ -946,7 +946,8 @@ xterm--init-activate-get-selection
 
 (defun xterm--init-activate-set-selection ()
   "Terminal initialization for `gui-set-selection'."
-  (set-terminal-parameter nil 'xterm--set-selection t))
+  (set-terminal-parameter nil 'xterm--set-selection t)
+  (set-terminal-parameter nil 'display-selections-p t))
 
 (defun xterm--init-frame-title ()
   "Terminal initialization for XTerm frame titles."
-- 
2.36.1.476.g0c4daa206d-goog