This repository has been archived by the owner on Aug 21, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
osc52.el
176 lines (136 loc) · 6.84 KB
/
osc52.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
;;; osc52.el --- copy text to the host terminal clipboard -*- lexical-binding: t -*-
;; Author: Jörg Thalheim <[email protected]>
;; URL: https://github.com/Mic92/osc52
;; Version: 0.01
;; Package-Requires: ((emacs "24.3"))
;; This script can be loaded during emacs initialization to automatically
;; send `kill-region' and `kill-ring-save' regions to your system clipboard.
;; The OSC 52 terminal escape sequence is used to transfer the selection from
;; emacs to the host terminal.
;; It is based off of the osc52.el copyright the Chromium OS authors, but
;; was modified to add support for tmux, graphical displays, and
;; multi-byte strings.
;; It works in hterm, xterm, and other terminal emulators which support the
;; sequence.
;; It also works under screen, via `osc52-select-text-dcs' and tmux via
;; `osc52-select-text-tmux', as long as the terminal supports OSC 52.
;; Call `osc52-set-cut-function' to activate.
;;; Commentary:
;;; Code:
(defcustom osc52-max-sequence 100000
"Maximum length of 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
characters that can be copied is 1/3 of this value.")
(defcustom osc52-multiplexer 'tmux
"Select which terminal multiplexer should be used when creating OSC 52 sequences. Device control string escape sequences are only used when the value of the environment variable TERM starts with the string \"screen\".
If set to 'tmux, a tmux DCS escape sequence will be generated, otherwise a screen DCS will be used.")
(defun osc52-select-text (string &optional replace yank-handler)
"Copy STRING to the system clipboard using the OSC 52 escape sequence.
Set `interprogram-cut-function' to this when using a compatible terminal, and
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
`osc52-max-sequence', then the STRING is not sent to the system
clipboard.
This function sends a raw OSC 52 sequence and will work on a bare terminal
emulators. It does not work on screen or tmux terminals, since they don't
natively support OSC 52."
(let ((b64-length (+ (* (length string) 3) 2)))
(if (<= b64-length osc52-max-sequence)
(send-string-to-terminal
(concat "\e]52;c;"
(base64-encode-string (encode-coding-string string 'utf-8-unix) t)
"\07"))
(message \"Selection too long to send to terminal %d\" b64-length)
(sit-for 2))))
(defun osc52-select-text-dcs (string &optional replace yank-handler)
"Copy STRING to the system clipboard using the OSC 52 escape sequence, for
screen users.
Set `interprogram-cut-function' to this when using the screen program, and 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
`osc52-max-sequence', then the STRING is not sent to the system
clipboard.
This function wraps the OSC 52 in a Device Control String sequence. This causes
screen to pass the wrapped OSC 52 sequence along to the host termianl. This
function also chops long DCS sequences into multiple smaller ones to avoid
hitting screen's max DCS length."
(let ((b64-length (+ (* (length string) 3) 2)))
(if (<= b64-length osc52-max-sequence)
(send-string-to-terminal
(concat "\eP\e]52;c;"
(replace-regexp-in-string
"\n" "\e\\\\\eP"
(base64-encode-string (encode-coding-string string 'utf-8-unix)))
"\07\e\\"))
(message "Selection too long to send to terminal %d" b64-length)
(sit-for 2))))
(defun osc52-select-text-tmux (string &optional replace yank-handler)
"Copy STRING to the system clipboard using the OSC 52 escape sequence, for
tmux users.
Set `interprogram-cut-function' to this when using the screen program, and 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
`osc52-max-sequence', then the STRING is not sent to the system
clipboard.
This function wraps the OSC 52 in a Device Control String sequence. This causes
screen to pass the wrapped OSC 52 sequence along to the host termianl. This
function also chops long DCS sequences into multiple smaller ones to avoid
hitting screen's max DCS length."
(let ((b64-length (+ (* (length string) 3) 2)))
(if (<= b64-length osc52-max-sequence)
(send-string-to-terminal
(concat "\ePtmux;\e\e]52;c;"
(base64-encode-string (encode-coding-string string 'utf-8-unix)
t)
"\a\e\\"))
(message "Selection too long to send to terminal %d" b64-length)
(sit-for 2))))
(defvar osc52-cut-function)
(defun osc52-interprogram-cut-function (string &optional replace yank-handler)
(if (display-graphic-p)
(x-select-text string)
(funcall osc52-cut-function string)))
(defun osc52-set-cut-function ()
"Initialize the `interprogram-cut-function' based on the value of
`display-graphic-p' and the TERM environment variable."
(interactive)
;; Look `initial-environment' instead of `(getenv "TERM")',
;; because emacs might set it to "dumb" internally.
;; `inital-environment' has the pure value when it started.
(let ((term
;; Make term == "" instead of nil, when no TERM environment variable
(or (ignore-errors
(replace-regexp-in-string
"^TERM=" ""
(dolist (env initial-environment)
(if (string-match "^TERM=" env) (return env)))
'fixedcase))
"")))
(setq osc52-cut-function
;; If `TERM' contains "tmux", they should use tmux.
(if (string-match "tmux" term)
'osc52-select-text-tmux
;; Otherwise, if the `TERM' starts from "screen",
;; they might actually use screen, but perhaps tmux.
;; They have to set actual terminal by `osc52-multiplexer'
(if (string-match "^screen" term)
(if (equal osc52-multiplexer 'tmux)
'osc52-select-text-tmux
'osc52-select-text-dcs)
;; No terminal multiplexer.
'osc52-select-text))))
(setq interprogram-cut-function 'osc52-interprogram-cut-function))
(defun osc52-send-region-to-clipboard (START END)
"Copy the region to the system clipboard using the OSC 52 escape sequence."
(interactive "r")
(osc52-interprogram-cut-function (buffer-substring-no-properties
START END)))
(provide 'osc52)
;;; osc52.el ends here