From 14683e6ad63bd1f6312fd609d80bd89035ee0372 Mon Sep 17 00:00:00 2001
From: Duncan Findlay <duncf@google.com>
Date: Fri, 10 Jun 2022 18:46:49 -0700
Subject: [PATCH] Support `select-active-regions' with xterm

This allows Emacs to save the active region to the user's
primary selection on supported terminals.  The behavior follows
the existing `select-active-regions' variable and requires
`xterm-select-active-regions' to be non-nil.

* src/keyboard.c (command_loop_1):
* lisp/simple.el (deactivate-mark): On text terminals, check
terminal parameter `xterm--set-selections' and variable
`xterm-select-active-regions' when deciding whether to update
primary selection. (bug#55883)
* lisp/term/xterm.el (xterm-select-active-regions): New
defcustom.
---
 etc/NEWS           | 6 ++++++
 lisp/simple.el     | 7 ++++++-
 lisp/term/xterm.el | 8 ++++++++
 src/keyboard.c     | 6 +++++-
 4 files changed, 25 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 1b8560a923..5da9b4a7ea 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -502,6 +502,12 @@ 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".
 
+*** Select active regions with xterm selection support.
+On terminals with xterm setSelection support, the active region may be
+saved to the X primary selection, following the
+'select-active-regions' variable.  This support is enabled with
+'xterm-select-active-regions'.
+
 ** ERT
 
 +++
diff --git a/lisp/simple.el b/lisp/simple.el
index 05a0855a96..0be4cbb22e 100644
--- a/lisp/simple.el
+++ b/lisp/simple.el
@@ -6661,6 +6661,9 @@ mark
 
 ;; Behind display-selections-p.
 
+;; From term/xterm.el
+(defvar xterm-select-active-regions)
+
 (defun deactivate-mark (&optional force)
   "Deactivate the mark.
 If Transient Mark mode is disabled, this function normally does
@@ -6679,7 +6682,9 @@ deactivate-mark
 		   (eq (car-safe transient-mark-mode) 'only)
 		 select-active-regions)
 	       (region-active-p)
-	       (display-selections-p))
+               (or (display-selections-p)
+                   (and xterm-select-active-regions
+                        (terminal-parameter nil 'xterm--set-selection))))
       ;; The var `saved-region-selection', if non-nil, is the text in
       ;; the region prior to the last command modifying the buffer.
       ;; Set the selection to that, or to the current region.
diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el
index a7e257f41c..0791780d40 100644
--- a/lisp/term/xterm.el
+++ b/lisp/term/xterm.el
@@ -80,6 +80,14 @@ xterm-store-paste-on-kill-ring
   :version "28.1"
   :type 'boolean)
 
+(defcustom xterm-select-active-regions nil
+  "If non-nil, update PRIMARY X selection on text-mode frames.
+On a text-mode terminal that supports setSelection command, if
+this variable is non-nil, Emacs will set the PRIMARY selection
+from the active region, according to `select-active-regions'."
+  :version "29.1"
+  :type 'boolean)
+
 (defconst xterm-paste-ending-sequence "\e[201~"
   "Characters sent by the terminal to end a bracketed paste.")
 
diff --git a/src/keyboard.c b/src/keyboard.c
index 55d710ed62..76b9e3bd13 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -1569,7 +1569,9 @@ 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 (Fwindow_system (Qnil))
+		   || (!NILP (SYMBOL_VAL (XSYMBOL (Qxterm_select_active_regions)))
+		       && !NILP (Fterminal_parameter (Qnil, Qxterm__set_selection))))
 		  /* 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
@@ -12163,6 +12165,8 @@ syms_of_keyboard (void)
   DEFSYM (Qpolling_period, "polling-period");
 
   DEFSYM (Qgui_set_selection, "gui-set-selection");
+  DEFSYM (Qxterm__set_selection, "xterm--set-selection");
+  DEFSYM (Qxterm_select_active_regions, "xterm-select-active-regions");
 
   /* The primary selection.  */
   DEFSYM (QPRIMARY, "PRIMARY");
-- 
2.36.1.476.g0c4daa206d-goog

