From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Glenn Morris Newsgroups: gmane.emacs.devel Subject: Re: master 2b2fd39: xterm.el: Implement OSC-52 functionality for setting the X selection Date: Fri, 17 Apr 2015 12:04:53 -0400 Message-ID: <2yvbguvize.fsf@fencepost.gnu.org> References: <20150413144903.4697.38336@vcs.savannah.gnu.org> NNTP-Posting-Host: plane.gmane.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii X-Trace: ger.gmane.org 1429286732 8711 80.91.229.3 (17 Apr 2015 16:05:32 GMT) X-Complaints-To: usenet@ger.gmane.org NNTP-Posting-Date: Fri, 17 Apr 2015 16:05:32 +0000 (UTC) Cc: Stefan Monnier To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Fri Apr 17 18:05:31 2015 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by plane.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1Yj8lj-0004bT-K3 for ged-emacs-devel@m.gmane.org; Fri, 17 Apr 2015 18:05:27 +0200 Original-Received: from localhost ([::1]:42466 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yj8li-0005sq-Vk for ged-emacs-devel@m.gmane.org; Fri, 17 Apr 2015 12:05:26 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:60704) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yj8lL-0005Hq-2y for emacs-devel@gnu.org; Fri, 17 Apr 2015 12:05:09 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1Yj8lF-0000jM-2h for emacs-devel@gnu.org; Fri, 17 Apr 2015 12:05:02 -0400 Original-Received: from fencepost.gnu.org ([2001:4830:134:3::e]:39547) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1Yj8lE-0000jA-Rc for emacs-devel@gnu.org; Fri, 17 Apr 2015 12:04:56 -0400 Original-Received: from rgm by fencepost.gnu.org with local (Exim 4.71) (envelope-from ) id 1Yj8lB-0006LS-NZ; Fri, 17 Apr 2015 12:04:54 -0400 X-Spook: DRM Ansar al-Islam MD2 Defcon BCCI NATO HAMASMOIS Clinton X-Ran: *"3w{+.+N!0&|3!0q`0?0VuFKZHA'"Y1CPJ`:vxVc!pRv}d{"[>'Sh;rP+692]G~)tv}xw X-Hue: cyan X-Attribution: GM In-Reply-To: (Stefan Monnier's message of "Mon, 13 Apr 2015 14:49:03 +0000") User-Agent: Gnus (www.gnus.org), GNU Emacs (www.gnu.org/software/emacs/) X-detected-operating-system: by eggs.gnu.org: Error: Malformed IPv6 address (bad octet value). X-Received-From: 2001:4830:134:3::e X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:185532 Archived-At: This is not a small change, but I see no corresponding entry in copyright.list. Stefan Monnier wrote: > branch: master > commit 2b2fd3965f8c096b39de7e0418d37433269a1dce > Author: Philipp Stephani > Commit: Stefan Monnier > > xterm.el: Implement OSC-52 functionality for setting the X selection > > * lisp/term/xterm.el (xterm-max-cut-length): New var. > (xterm--set-selection, terminal-init-xterm-activate-set-selection): New funs. > (terminal-init-xterm, xterm--version-handler): Use them. > --- > lisp/term/xterm.el | 93 +++++++++++++++++++++++++++++++++++++++++++++++++-- > 1 files changed, 89 insertions(+), 4 deletions(-) > > diff --git a/lisp/term/xterm.el b/lisp/term/xterm.el > index 519f691..f4d1a84 100644 > --- a/lisp/term/xterm.el > +++ b/lisp/term/xterm.el > @@ -37,7 +37,8 @@ If a list, assume that the listed features are supported, without checking. > > The relevant features are: > modifyOtherKeys -- if supported, more key bindings work (e.g., \"\\C-,\") > - reportBackground -- if supported, Xterm reports its background color" > + reportBackground -- if supported, Xterm reports its background color > + setSelection -- if supported, Xterm saves yanked text to the X selection" > :version "24.1" > :group 'xterm > :type '(choice (const :tag "No" nil) > @@ -45,7 +46,24 @@ The relevant features are: > ;; NOTE: If you add entries here, make sure to update > ;; `terminal-init-xterm' as well. > (set (const :tag "modifyOtherKeys support" modifyOtherKeys) > - (const :tag "report background" reportBackground)))) > + (const :tag "report background" reportBackground) > + (const :tag "set X selection" setSelection)))) > + > +(defcustom xterm-max-cut-length 100000 > + "Maximum number of bytes to cut into xterm using the OSC 52 sequence. > + > +The OSC 52 sequence requires a terminator byte. Some terminals will ignore or > +mistreat a terminated sequence that is longer than a certain size, usually to > +protect users from runaway sequences. > + > +This variable allows you to tweak the maximum number of bytes that will be sent > +using the OSC 52 sequence. > + > +If you select a region larger than this size, it won't be copied to your system > +clipboard. Since clipboard data is base 64 encoded, the actual number of > +string bytes that can be copied is 3/4 of this value." > + :group 'xterm > + :type 'integer) > > (defconst xterm-paste-ending-sequence "\e[201~" > "Characters send by the terminal to end a bracketed paste.") > @@ -620,7 +638,13 @@ The relevant features are: > ;; introduced) or higher, initialize the > ;; modifyOtherKeys support. > (when (>= version 216) > - (terminal-init-xterm-modify-other-keys)))))) > + (terminal-init-xterm-modify-other-keys)) > + ;; In version 203 support for accessing the X selection was > + ;; added. Hterm reports itself as version 256 and supports it > + ;; as well. gnome-terminal doesn't and is excluded by this > + ;; test. > + (when (>= version 203) > + (terminal-init-xterm-activate-set-selection)))))) > > (defun xterm--query (query handlers) > "Send QUERY string to the terminal and watch for a response. > @@ -699,7 +723,10 @@ We run the first FUNCTION whose STRING matches the input events." > '(("\e]11;" . xterm--report-background-handler)))) > > (when (memq 'modifyOtherKeys xterm-extra-capabilities) > - (terminal-init-xterm-modify-other-keys))) > + (terminal-init-xterm-modify-other-keys)) > + > + (when (memq 'setSelection xterm-extra-capabilities) > + (terminal-init-xterm-activate-set-selection))) > > ;; Unconditionally enable bracketed paste mode: terminals that don't > ;; support it just ignore the sequence. > @@ -719,6 +746,64 @@ We run the first FUNCTION whose STRING matches the input events." > (push "\e[?2004l" (terminal-parameter nil 'tty-mode-reset-strings)) > (push "\e[?2004h" (terminal-parameter nil 'tty-mode-set-strings))) > > +(defun terminal-init-xterm-activate-set-selection () > + "Terminal initialization for `gui-set-selection'." > + ;; All text terminals are represented by the nil GUI type. We need > + ;; to detect XTerm again in `xterm--set-selection' using the > + ;; terminal parameters. > + (gui-method-define gui-set-selection nil #'xterm--set-selection)) > + > +(defun xterm--set-selection (type data) > + "Copy DATA to the X selection using the OSC 52 escape sequence. > + > +TYPE specifies which selection to set; it must be either > +`PRIMARY' or `CLIPBOARD'. DATA must be a string. > + > +This can be used as a `gui-set-selection' method for > +xterm-compatible terminal emulators. Then your system clipboard > +will be updated whenever you copy a region of text in Emacs. > + > +If the resulting OSC 52 sequence would be longer than > +`xterm-max-cut-length', then the TEXT is not sent to the system > +clipboard. > + > +This function either sends a raw OSC 52 sequence or wraps the OSC > +52 in a Device Control String sequence. This way, it will work > +on a bare terminal emulators as well as inside the screen > +program. When inside the screen program, this function also > +chops long DCS sequences into multiple smaller ones to avoid > +hitting screen's max DCS length." > + (let* ((init-function (terminal-parameter nil 'terminal-initted)) > + (xterm (eq init-function 'terminal-init-xterm)) > + (screen (eq init-function 'terminal-init-screen))) > + ;; Only do something if the current terminal is actually an XTerm > + ;; or screen. > + (when (or xterm screen) > + (let* ((bytes (encode-coding-string data 'utf-8-unix)) > + (base-64 (if screen > + (replace-regexp-in-string > + "\n" "\e\\\eP" > + (base64-encode-string bytes) > + :fixedcase :literal) > + (base64-encode-string bytes :no-line-break))) > + (length (string-bytes base-64))) > + (if (> length xterm-max-cut-length) > + (progn > + (warn "Selection too long to send to terminal: %d bytes" length) > + (sit-for 2)) > + (send-string-to-terminal > + (concat > + (when screen "\eP") > + "\e]52;" > + (cond > + ((eq type 'PRIMARY) "p") > + ((eq type 'CLIPBOARD) "c") > + (t (error "Invalid type %S" type))) > + ";" > + base-64 > + "\a" > + (when screen "\e\\")))))))) > + > ;; Set up colors, for those versions of xterm that support it. > (defvar xterm-standard-colors > ;; The names in the comments taken from XTerm-col.ad in the xterm > > _______________________________________________ > Emacs-diffs mailing list > Emacs-diffs@gnu.org > https://lists.gnu.org/mailman/listinfo/emacs-diffs