[Top][All Lists]

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

Set X primary selection with Emacs in xterm

From: Duncan Findlay
Subject: Set X primary selection with Emacs in xterm
Date: Thu, 2 Jun 2022 21:03:49 -0700

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.


Attachment: 0001-Use-display-selections-p-when-deciding-to-update-pri.patch
Description: Text Data

Attachment: 0002-Support-setting-primary-selection-with-xterm-setSele.patch
Description: Text Data

reply via email to

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