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

From: Duncan Findlay <[email-masked]>
Date: Fri, 10 Jun 2022 18:59:47 -0700

Attached is an updated version that I believe addresses all the
concerns raised on the mailing list.

Given the changes don't split so cleanly into two any more, I've
combined them into a single patch.

Thanks!

On Thu, Jun 9, 2022 at 11:52 PM Eli Zaretskii <[email-masked]> wrote:
>
> > Date: Thu, 9 Jun 2022 23:18:01 -0700
> > From:  Duncan Findlay via "Bug reports for GNU Emacs,
> >  the Swiss army knife of text editors" <[email-masked]>
> >
> > 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
>
> Thanks.
>
> However, the patches you posted seem to ignore the comments I made in
> the above-mentioned discussions, specifically here:
>
>   https://lists.gnu.org/archive/html/emacs-devel/2022-06/msg00134.html
>
> Did you send a wrong version of the patches, perhaps?
From bc906a8a0c67125880497c4729d7940fa4ba8bf6 Mon Sep 17 00:00:00 2001
From: Duncan Findlay <[email-masked]>
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): Check terminal parameter
`display-selections-p' for text terminals when deciding whether
to update primary selection.
* lisp/frame.el (display-selections-p): Return terminal
parameter `display-selections-p' to indicate selection support.
* lisp/term/xterm.el (xterm-select-active-regions): New
defcustom.  (xterm--init-activate-set-selection): Set
the `display-selections-p' terminal parameter.
---
 etc/NEWS           |  6 ++++++
 lisp/frame.el      |  2 ++
 lisp/term/xterm.el | 11 ++++++++++-
 src/keyboard.c     |  5 ++++-
 4 files changed, 22 insertions(+), 2 deletions(-)

diff --git a/etc/NEWS b/etc/NEWS
index 1789d47351..57b5d06e78 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -492,6 +492,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/frame.el b/lisp/frame.el
index 27f99fb7d2..e530afb387 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)
+     ((terminal-parameter display 'display-selections-p)
+      t)
      (t
       nil))))
 
diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el
index a7e257f41c..671d757c11 100644
--- a/lisp/term/xterm.el
+++ b/lisp/term/xterm.el
@@ -80,6 +80,13 @@ xterm-store-paste-on-kill-ring
   :version "28.1"
   :type 'boolean)
 
+(defcustom xterm-select-active-regions nil
+  "If non-nil, on a terminal with setSelection support, Emacs will
+also update the primary selection with the active region, based
+on the value of `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.")
 
@@ -946,7 +953,9 @@ 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)
+  (when xterm-select-active-regions
+    (set-terminal-parameter nil 'display-selections-p t)))
 
 (defun xterm--init-frame-title ()
   "Terminal initialization for XTerm frame titles."
diff --git a/src/keyboard.c b/src/keyboard.c
index 55d710ed62..8e3738b4c7 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -1,3 +1,4 @@
+
 /* Keyboard and mouse input; editor command loop.
 
 Copyright (C) 1985-1989, 1993-1997, 1999-2022 Free Software Foundation,
@@ -1569,7 +1570,8 @@ 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 (Fterminal_parameter (Qnil, 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 +12164,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