unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
* Set X primary selection with Emacs in xterm
@ 2022-06-03  4:03 Duncan Findlay
  2022-06-03  5:33 ` Po Lu
                   ` (2 more replies)
  0 siblings, 3 replies; 13+ messages in thread
From: Duncan Findlay @ 2022-06-03  4:03 UTC (permalink / raw)
  To: emacs-devel

[-- Attachment #1: Type: text/plain, Size: 2202 bytes --]

I frequently use Emacs over ssh and I'd really like to get both
primary and clipboard selections to work as close as possible to
running Emacs on X natively. I'd like to kill text in Emacs and have
that show up in my system clipboard so I can paste into other
applications. Similarly, if I select text with mark and keyboard (or
mouse with xterm-mouse-mode), I'd like it to update my local X's
primary selection so I can middle-click to paste it elsewhere. I have
two patches attached that got this working for me.

Without changes, with `(setq xterm-extra-capabilities
'(setSelection))', when I kill text, Emacs generates OSC 52 terminal
escape codes and xterm updates my clipboard. This works great! Emacs
also has support for updating the primary selection with this same
mechanism, e.g. `(gui-set-selection 'PRIMARY "primary")'. This, too,
works fine with xterm.

The bit that's missing is that when I select text with keyboard or
mouse (with xterm-mouse-mode), the primary selection is not updated.
It appears that the primary selection is only updated when
`(window-system)' is not nil.

I've attached a patch below to replace the `window-system' check with
`display-selections-p', as that's documented as the preferred way to
do this type of check. It also moves the check to lisp where we can
advise it.

The second patch changes  `(display-selections-p)' to return true
under xterm with the setSelection feature enabled.

I don't know if this second patch can be submitted as is. It may break
existing users. tmux, for example, removes the selection indicator
from OSC 52 codes, so if emacs writes to both CLIPBOARD and PRIMARY
selections, both updates will go to the same buffer on the user's
side. I've filed https://github.com/tmux/tmux/issues/3192 with tmux. I
haven't tested GNU screen.

This patch will also lead to extra data being sent to the user's
terminal which they may not need or want. It might be wise to only
send OSC 52 codes for primary selection if the user actually has a
primary selection buffer, but I'm not sure the best way to do that.
I'd appreciate some guidance here, or if somebody more experienced
wants to take this on, that'd be most appreciated.

Thanks
Duncan

[-- Attachment #2: 0001-Use-display-selections-p-when-deciding-to-update-pri.patch --]
[-- Type: text/x-patch, Size: 1470 bytes --]

From 5c7e11169b4043150a04623608de798e6edd821b Mon Sep 17 00:00:00 2001
From: Duncan Findlay <duncf@google.com>
Date: Fri, 20 May 2022 00:17:51 -0700
Subject: [PATCH 1/2] Use `display-selections-p' when deciding to update
 primary selection

`display-selections-p' is documented as the preferred way to determine
capabilities, over `window-system`.

This change moves the decision into lisp, where users can advise
changes.

 * 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 274c7b3fa8..fd44a1a739 100644
--- a/src/keyboard.c
+++ b/src/keyboard.c
@@ -1577,7 +1577,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
@@ -12154,6 +12154,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.255.ge46751e96f-goog


[-- Attachment #3: 0002-Support-setting-primary-selection-with-xterm-setSele.patch --]
[-- Type: text/x-patch, Size: 930 bytes --]

From 9c2ffc771838765ec9f6b1621ec9c05617668aee Mon Sep 17 00:00:00 2001
From: Duncan Findlay <duncf@google.com>
Date: Thu, 2 Jun 2022 20:23:44 -0700
Subject: [PATCH 2/2] Support setting primary selection with xterm setSelection
 mode.

This will cause the xterm user's X primary selection buffer to be
updated when a selection is made in Emacs.

 * lisp/frame.el (display-selections-p): Return `t' when xterm's
 setSelection mode is enabled.
---
 lisp/frame.el | 3 +++
 1 file changed, 3 insertions(+)

diff --git a/lisp/frame.el b/lisp/frame.el
index 27f99fb7d2..eb8ae1c27d 100644
--- a/lisp/frame.el
+++ b/lisp/frame.el
@@ -2164,6 +2164,9 @@ display-selections-p
        (not (null dos-windows-version))))
      ((memq frame-type '(x w32 ns pgtk))
       t)
+     ((and (memq frame-type '(t))
+           (eq (terminal-parameter nil 'xterm--set-selection) t))
+      t)
      (t
       nil))))
 
-- 
2.36.1.255.ge46751e96f-goog


^ permalink raw reply related	[flat|nested] 13+ messages in thread

end of thread, other threads:[~2022-06-15  2:43 UTC | newest]

Thread overview: 13+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2022-06-03  4:03 Set X primary selection with Emacs in xterm Duncan Findlay
2022-06-03  5:33 ` Po Lu
2022-06-03 12:27   ` Stefan Monnier
2022-06-10  6:36   ` Duncan Findlay
2022-06-03  6:57 ` Eli Zaretskii
2022-06-10 18:10   ` Duncan Findlay
2022-06-10 19:38     ` Eli Zaretskii
2022-06-11  2:03       ` Duncan Findlay
2022-06-03  9:55 ` Jean Louis
2022-06-10  5:49   ` Duncan Findlay
2022-06-10  8:50     ` James Cloos
2022-06-11  6:56     ` Jean Louis
2022-06-15  2:43       ` Duncan Findlay

Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).