Provide a way for applications in terminal to detect whether they can access the clipboard
- iTerm2 version: 3.3.9
- OS version: macOS Catalina 10.15.4
This came up specifically in the context of Emacs. Emacs is capable of sending OSC 52 control sequences to add text to the clipboard, but it needs to detect whether the terminal it's running on has this capability. By default, Emacs current implements this detection by sending a Secondary Device Attributes query and examining the version number in the response. The code is here: https://github.com/emacs-mirror/emacs/blob/f18af6cd5cb7dbbf7420ec2d3efed4e202c4f0dd/lisp/term/xterm.el#L713
However, this doesn't work for iTerm2, because iTerm2 responds to the Secondary Device Attributes query by claiming to be a very old version of xterm (0;95;0c): https://gitlab.com/gnachman/iterm2/-/blob/master/sources/VT100Output.m#L99
Because of this, the Emacs detection code assumes that iTerm2 doesn't support clipboard access, so it doesn't generate OSC 52 sequences and Emacs commands don't copy to the system clipboard.
The Emacs feature detection code already has hacks for other cases (GNOME terminal and screen), so it would probably be fine to do something similar here, but we would need some way to detect that we're running in iTerm2.
Best-case: A way to detect (from within Emacs) "I'm running within iTerm2, and the 'Applications in terminal may access clipboard'" feature is enabled.
Second-best: A way to detect (from within Emacs) "I'm running within iTerm2"
It's possible that we could add "version 95 == iTerm2" to the Emacs terminal feature detection code, but I'm doubtful that this would be accepted since it could conflict with other terminals that have similar behavior. Ideally it could be something relatively unique to iTerm2.