1 ;;; xterm.el --- define function key sequences and standard colors for xterm -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995, 2001-2016 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
32 (defconst xterm--extra-capabilities-type
33 ;; NOTE: If you add entries here, make sure to update
34 ;; `terminal-init-xterm' as well.
35 '(set (const :tag
"modifyOtherKeys support" modifyOtherKeys
)
36 (const :tag
"report background" reportBackground
)
37 (const :tag
"get X selection" getSelection
)
38 (const :tag
"set X selection" setSelection
)))
40 (defcustom xterm-extra-capabilities
'check
41 "Whether Xterm supports some additional, more modern, features.
42 If nil, just assume that it does not.
43 If `check', try to check if it does.
44 If a list, assume that the listed features are supported, without checking.
46 The relevant features are:
47 modifyOtherKeys -- if supported, more key bindings work (e.g., \"\\C-,\")
48 reportBackground -- if supported, Xterm reports its background color
49 getSelection -- if supported, Xterm yanks text from the X selection
50 setSelection -- if supported, Xterm saves killed text to the X selection"
52 :type
`(choice (const :tag
"Check" check
)
53 ,xterm--extra-capabilities-type
))
55 (defcustom xterm-max-cut-length
100000
56 "Maximum number of bytes to cut into xterm using the OSC 52 sequence.
58 The OSC 52 sequence requires a terminator byte. Some terminals will ignore or
59 mistreat a terminated sequence that is longer than a certain size, usually to
60 protect users from runaway sequences.
62 This variable allows you to tweak the maximum number of bytes that will be sent
63 using the OSC 52 sequence.
65 If you select a region larger than this size, it won't be copied to your system
66 clipboard. Since clipboard data is base 64 encoded, the actual number of
67 string bytes that can be copied is 3/4 of this value."
71 (defconst xterm-paste-ending-sequence
"\e[201~"
72 "Characters send by the terminal to end a bracketed paste.")
74 (defun xterm--pasted-text ()
75 "Handle the rest of a terminal paste operation.
76 Return the pasted text as a string."
77 (let ((end-marker-length (length xterm-paste-ending-sequence
)))
79 (set-buffer-multibyte nil
)
80 (while (not (search-backward xterm-paste-ending-sequence
81 (- (point) end-marker-length
) t
))
82 (let ((event (read-event nil nil
83 ;; Use finite timeout to avoid glomming the
84 ;; event onto this-command-keys.
85 most-positive-fixnum
)))
89 (let ((last-coding-system-used))
90 (decode-coding-region (point-min) (point) (keyboard-coding-system)
94 "Handle the start of a terminal paste operation."
96 (let* ((pasted-text (xterm--pasted-text))
97 (interprogram-paste-function (lambda () pasted-text
)))
100 (define-key global-map
[xterm-paste
] #'xterm-paste
)
102 (defvar xterm-rxvt-function-map
103 (let ((map (make-sparse-keymap)))
104 (define-key map
"\e[2~" [insert])
105 (define-key map "\e[3~" [delete])
106 (define-key map "\e[4~" [select])
107 (define-key map "\e[5~" [prior])
108 (define-key map "\e[6~" [next])
110 (define-key map "\e[15~" [f5])
111 (define-key map "\e[17~" [f6])
112 (define-key map "\e[18~" [f7])
113 (define-key map "\e[19~" [f8])
114 (define-key map "\e[20~" [f9])
115 (define-key map "\e[21~" [f10])
117 (define-key map "\e[2;2~" [S-insert])
119 ;; Other versions of xterm might emit these.
120 (define-key map "\e[A" [up])
121 (define-key map "\e[B" [down])
122 (define-key map "\e[C" [right])
123 (define-key map "\e[D" [left])
125 (define-key map "\e[11~" [f1])
126 (define-key map "\e[12~" [f2])
127 (define-key map "\e[13~" [f3])
128 (define-key map "\e[14~" [f4])
130 ;; Recognize the start of a bracketed paste sequence. The handler
131 ;; internally recognizes the end.
132 (define-key map "\e[200~" [xterm-paste])
135 "Keymap of escape sequences, shared between xterm and rxvt support.")
137 (defvar xterm-function-map
138 (let ((map (make-sparse-keymap)))
139 (set-keymap-parent map xterm-rxvt-function-map)
141 ;; xterm from X.org 6.8.2 uses these key definitions.
142 (define-key map "\eOP" [f1])
143 (define-key map "\eOQ" [f2])
144 (define-key map "\eOR" [f3])
145 (define-key map "\eOS" [f4])
146 (define-key map "\e[23~" [f11])
147 (define-key map "\e[24~" [f12])
149 (define-key map "\eO2P" [S-f1])
150 (define-key map "\eO2Q" [S-f2])
151 (define-key map "\eO2R" [S-f3])
152 (define-key map "\eO2S" [S-f4])
153 (define-key map "\e[1;2P" [S-f1])
154 (define-key map "\e[1;2Q" [S-f2])
155 (define-key map "\e[1;2R" [S-f3])
156 (define-key map "\e[1;2S" [S-f4])
157 (define-key map "\e[15;2~" [S-f5])
158 (define-key map "\e[17;2~" [S-f6])
159 (define-key map "\e[18;2~" [S-f7])
160 (define-key map "\e[19;2~" [S-f8])
161 (define-key map "\e[20;2~" [S-f9])
162 (define-key map "\e[21;2~" [S-f10])
163 (define-key map "\e[23;2~" [S-f11])
164 (define-key map "\e[24;2~" [S-f12])
166 (define-key map "\eO5P" [C-f1])
167 (define-key map "\eO5Q" [C-f2])
168 (define-key map "\eO5R" [C-f3])
169 (define-key map "\eO5S" [C-f4])
170 (define-key map "\e[15;5~" [C-f5])
171 (define-key map "\e[17;5~" [C-f6])
172 (define-key map "\e[18;5~" [C-f7])
173 (define-key map "\e[19;5~" [C-f8])
174 (define-key map "\e[20;5~" [C-f9])
175 (define-key map "\e[21;5~" [C-f10])
176 (define-key map "\e[23;5~" [C-f11])
177 (define-key map "\e[24;5~" [C-f12])
179 (define-key map "\eO6P" [C-S-f1])
180 (define-key map "\eO6Q" [C-S-f2])
181 (define-key map "\eO6R" [C-S-f3])
182 (define-key map "\eO6S" [C-S-f4])
183 (define-key map "\e[15;6~" [C-S-f5])
184 (define-key map "\e[17;6~" [C-S-f6])
185 (define-key map "\e[18;6~" [C-S-f7])
186 (define-key map "\e[19;6~" [C-S-f8])
187 (define-key map "\e[20;6~" [C-S-f9])
188 (define-key map "\e[21;6~" [C-S-f10])
189 (define-key map "\e[23;6~" [C-S-f11])
190 (define-key map "\e[24;6~" [C-S-f12])
192 (define-key map "\eO3P" [M-f1])
193 (define-key map "\eO3Q" [M-f2])
194 (define-key map "\eO3R" [M-f3])
195 (define-key map "\eO3S" [M-f4])
196 (define-key map "\e[15;3~" [M-f5])
197 (define-key map "\e[17;3~" [M-f6])
198 (define-key map "\e[18;3~" [M-f7])
199 (define-key map "\e[19;3~" [M-f8])
200 (define-key map "\e[20;3~" [M-f9])
201 (define-key map "\e[21;3~" [M-f10])
202 (define-key map "\e[23;3~" [M-f11])
203 (define-key map "\e[24;3~" [M-f12])
205 (define-key map "\eO4P" [M-S-f1])
206 (define-key map "\eO4Q" [M-S-f2])
207 (define-key map "\eO4R" [M-S-f3])
208 (define-key map "\eO4S" [M-S-f4])
209 (define-key map "\e[15;4~" [M-S-f5])
210 (define-key map "\e[17;4~" [M-S-f6])
211 (define-key map "\e[18;4~" [M-S-f7])
212 (define-key map "\e[19;4~" [M-S-f8])
213 (define-key map "\e[20;4~" [M-S-f9])
214 (define-key map "\e[21;4~" [M-S-f10])
215 (define-key map "\e[23;4~" [M-S-f11])
216 (define-key map "\e[24;4~" [M-S-f12])
218 (define-key map "\eOA" [up])
219 (define-key map "\eOB" [down])
220 (define-key map "\eOC" [right])
221 (define-key map "\eOD" [left])
222 (define-key map "\eOF" [end])
223 (define-key map "\eOH" [home])
225 (define-key map "\e[1;2A" [S-up])
226 (define-key map "\e[1;2B" [S-down])
227 (define-key map "\e[1;2C" [S-right])
228 (define-key map "\e[1;2D" [S-left])
229 (define-key map "\e[1;2F" [S-end])
230 (define-key map "\e[1;2H" [S-home])
232 (define-key map "\e[1;4A" [M-S-up])
233 (define-key map "\e[1;4B" [M-S-down])
234 (define-key map "\e[1;4C" [M-S-right])
235 (define-key map "\e[1;4D" [M-S-left])
236 (define-key map "\e[1;4F" [M-S-end])
237 (define-key map "\e[1;4H" [M-S-home])
239 (define-key map "\e[1;5A" [C-up])
240 (define-key map "\e[1;5B" [C-down])
241 (define-key map "\e[1;5C" [C-right])
242 (define-key map "\e[1;5D" [C-left])
243 (define-key map "\e[1;5F" [C-end])
244 (define-key map "\e[1;5H" [C-home])
246 (define-key map "\e[1;6A" [C-S-up])
247 (define-key map "\e[1;6B" [C-S-down])
248 (define-key map "\e[1;6C" [C-S-right])
249 (define-key map "\e[1;6D" [C-S-left])
250 (define-key map "\e[1;6F" [C-S-end])
251 (define-key map "\e[1;6H" [C-S-home])
253 (define-key map "\e[1;7A" [C-M-up])
254 (define-key map "\e[1;7B" [C-M-down])
255 (define-key map "\e[1;7C" [C-M-right])
256 (define-key map "\e[1;7D" [C-M-left])
257 (define-key map "\e[1;7F" [C-M-end])
258 (define-key map "\e[1;7H" [C-M-home])
260 (define-key map "\e[1;8A" [C-M-S-up])
261 (define-key map "\e[1;8B" [C-M-S-down])
262 (define-key map "\e[1;8C" [C-M-S-right])
263 (define-key map "\e[1;8D" [C-M-S-left])
264 (define-key map "\e[1;8F" [C-M-S-end])
265 (define-key map "\e[1;8H" [C-M-S-home])
267 (define-key map "\e[1;3A" [M-up])
268 (define-key map "\e[1;3B" [M-down])
269 (define-key map "\e[1;3C" [M-right])
270 (define-key map "\e[1;3D" [M-left])
271 (define-key map "\e[1;3F" [M-end])
272 (define-key map "\e[1;3H" [M-home])
274 (define-key map "\e[3;2~" [S-delete])
275 (define-key map "\e[5;2~" [S-prior])
276 (define-key map "\e[6;2~" [S-next])
278 (define-key map "\e[2;4~" [M-S-insert])
279 (define-key map "\e[3;4~" [M-S-delete])
280 (define-key map "\e[5;4~" [M-S-prior])
281 (define-key map "\e[6;4~" [M-S-next])
283 (define-key map "\e[2;5~" [C-insert])
284 (define-key map "\e[3;5~" [C-delete])
285 (define-key map "\e[5;5~" [C-prior])
286 (define-key map "\e[6;5~" [C-next])
288 (define-key map "\e[2;6~" [C-S-insert])
289 (define-key map "\e[3;6~" [C-S-delete])
290 (define-key map "\e[5;6~" [C-S-prior])
291 (define-key map "\e[6;6~" [C-S-next])
293 (define-key map "\e[2;7~" [C-M-insert])
294 (define-key map "\e[3;7~" [C-M-delete])
295 (define-key map "\e[5;7~" [C-M-prior])
296 (define-key map "\e[6;7~" [C-M-next])
298 (define-key map "\e[2;8~" [C-M-S-insert])
299 (define-key map "\e[3;8~" [C-M-S-delete])
300 (define-key map "\e[5;8~" [C-M-S-prior])
301 (define-key map "\e[6;8~" [C-M-S-next])
303 (define-key map "\e[2;3~" [M-insert])
304 (define-key map "\e[3;3~" [M-delete])
305 (define-key map "\e[5;3~" [M-prior])
306 (define-key map "\e[6;3~" [M-next])
308 (define-key map "\e[29~" [print])
310 (define-key map "\eOj" [kp-multiply])
311 (define-key map "\eOk" [kp-add])
312 (define-key map "\eOl" [kp-separator])
313 (define-key map "\eOm" [kp-subtract])
314 (define-key map "\eOo" [kp-divide])
315 (define-key map "\eOp" [kp-0])
316 (define-key map "\eOq" [kp-1])
317 (define-key map "\eOr" [kp-2])
318 (define-key map "\eOs" [kp-3])
319 (define-key map "\eOt" [kp-4])
320 (define-key map "\eOu" [kp-5])
321 (define-key map "\eOv" [kp-6])
322 (define-key map "\eOw" [kp-7])
323 (define-key map "\eOx" [kp-8])
324 (define-key map "\eOy" [kp-9])
326 (define-key map "\eO2j" [S-kp-multiply])
327 (define-key map "\eO2k" [S-kp-add])
328 (define-key map "\eO2l" [S-kp-separator])
329 (define-key map "\eO2m" [S-kp-subtract])
330 (define-key map "\eO2o" [S-kp-divide])
331 (define-key map "\eO2p" [S-kp-0])
332 (define-key map "\eO2q" [S-kp-1])
333 (define-key map "\eO2r" [S-kp-2])
334 (define-key map "\eO2s" [S-kp-3])
335 (define-key map "\eO2t" [S-kp-4])
336 (define-key map "\eO2u" [S-kp-5])
337 (define-key map "\eO2v" [S-kp-6])
338 (define-key map "\eO2w" [S-kp-7])
339 (define-key map "\eO2x" [S-kp-8])
340 (define-key map "\eO2y" [S-kp-9])
342 (define-key map "\eO4j" [M-S-kp-multiply])
343 (define-key map "\eO4k" [M-S-kp-add])
344 (define-key map "\eO4l" [M-S-kp-separator])
345 (define-key map "\eO4m" [M-S-kp-subtract])
346 (define-key map "\eO4o" [M-S-kp-divide])
347 (define-key map "\eO4p" [M-S-kp-0])
348 (define-key map "\eO4q" [M-S-kp-1])
349 (define-key map "\eO4r" [M-S-kp-2])
350 (define-key map "\eO4s" [M-S-kp-3])
351 (define-key map "\eO4t" [M-S-kp-4])
352 (define-key map "\eO4u" [M-S-kp-5])
353 (define-key map "\eO4v" [M-S-kp-6])
354 (define-key map "\eO4w" [M-S-kp-7])
355 (define-key map "\eO4x" [M-S-kp-8])
356 (define-key map "\eO4y" [M-S-kp-9])
358 (define-key map "\eO6j" [C-S-kp-multiply])
359 (define-key map "\eO6k" [C-S-kp-add])
360 (define-key map "\eO6l" [C-S-kp-separator])
361 (define-key map "\eO6m" [C-S-kp-subtract])
362 (define-key map "\eO6o" [C-S-kp-divide])
363 (define-key map "\eO6p" [C-S-kp-0])
364 (define-key map "\eO6q" [C-S-kp-1])
365 (define-key map "\eO6r" [C-S-kp-2])
366 (define-key map "\eO6s" [C-S-kp-3])
367 (define-key map "\eO6t" [C-S-kp-4])
368 (define-key map "\eO6u" [C-S-kp-5])
369 (define-key map "\eO6v" [C-S-kp-6])
370 (define-key map "\eO6w" [C-S-kp-7])
371 (define-key map "\eO6x" [C-S-kp-8])
372 (define-key map "\eO6y" [C-S-kp-9])
374 (define-key map "\eO8j" [C-M-S-kp-multiply])
375 (define-key map "\eO8k" [C-M-S-kp-add])
376 (define-key map "\eO8l" [C-M-S-kp-separator])
377 (define-key map "\eO8m" [C-M-S-kp-subtract])
378 (define-key map "\eO8o" [C-M-S-kp-divide])
379 (define-key map "\eO8p" [C-M-S-kp-0])
380 (define-key map "\eO8q" [C-M-S-kp-1])
381 (define-key map "\eO8r" [C-M-S-kp-2])
382 (define-key map "\eO8s" [C-M-S-kp-3])
383 (define-key map "\eO8t" [C-M-S-kp-4])
384 (define-key map "\eO8u" [C-M-S-kp-5])
385 (define-key map "\eO8v" [C-M-S-kp-6])
386 (define-key map "\eO8w" [C-M-S-kp-7])
387 (define-key map "\eO8x" [C-M-S-kp-8])
388 (define-key map "\eO8y" [C-M-S-kp-9])
390 ;; These keys are available in xterm starting from version 216
391 ;; if the modifyOtherKeys resource is set to 1.
392 (dolist (bind '((5 9 [C-tab])
401 ;; Not all C-DIGIT keys have a distinct binding.
420 (6 63 [(control ??)])
422 ;; These are the strings emitted for various C-M-
423 ;; combinations for keyboards whose Meta and Alt
424 ;; modifiers are on the same key (usually labeled "Alt").
460 (14 63 [(control meta ??)])
498 (8 63 [(control meta ??)])
504 (6 13 [C-S-return])))
506 (format "\e[27;%d;%d~" (nth 0 bind) (nth 1 bind)) (nth 2 bind))
507 ;; For formatOtherKeys=1, the sequence is a bit shorter (bug#13839).
509 (format "\e[%d;%du" (nth 1 bind) (nth 0 bind)) (nth 2 bind)))
511 ;; Other versions of xterm might emit these.
512 (define-key map "\e[1~" [home])
514 (define-key map "\eO2A" [S-up])
515 (define-key map "\eO2B" [S-down])
516 (define-key map "\eO2C" [S-right])
517 (define-key map "\eO2D" [S-left])
518 (define-key map "\eO2F" [S-end])
519 (define-key map "\eO2H" [S-home])
521 (define-key map "\eO5A" [C-up])
522 (define-key map "\eO5B" [C-down])
523 (define-key map "\eO5C" [C-right])
524 (define-key map "\eO5D" [C-left])
525 (define-key map "\eO5F" [C-end])
526 (define-key map "\eO5H" [C-home])
529 "Function key map overrides for xterm.")
531 (defvar xterm-alternatives-map
532 (let ((map (make-sparse-keymap)))
533 ;; The terminal initialization C code file might have initialized
534 ;; function keys F13->F60 from the termcap/terminfo information.
535 ;; On a PC-style keyboard these keys correspond to
536 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The code
537 ;; here substitutes the corresponding definitions in function-key-map.
538 ;; The mapping from escape sequences to Fn is done in input-decode-map
539 ;; whereas this here mapping is done in local-function-key-map so that
540 ;; bindings to f45 still work, in case your keyboard really has an f45
541 ;; key rather than C-S-f9.
542 (define-key map [f13] [S-f1])
543 (define-key map [f14] [S-f2])
544 (define-key map [f15] [S-f3])
545 (define-key map [f16] [S-f4])
546 (define-key map [f17] [S-f5])
547 (define-key map [f18] [S-f6])
548 (define-key map [f19] [S-f7])
549 (define-key map [f20] [S-f8])
550 (define-key map [f21] [S-f9])
551 (define-key map [f22] [S-f10])
552 (define-key map [f23] [S-f11])
553 (define-key map [f24] [S-f12])
555 (define-key map [f25] [C-f1])
556 (define-key map [f26] [C-f2])
557 (define-key map [f27] [C-f3])
558 (define-key map [f28] [C-f4])
559 (define-key map [f29] [C-f5])
560 (define-key map [f30] [C-f6])
561 (define-key map [f31] [C-f7])
562 (define-key map [f32] [C-f8])
563 (define-key map [f33] [C-f9])
564 (define-key map [f34] [C-f10])
565 (define-key map [f35] [C-f11])
566 (define-key map [f36] [C-f12])
568 (define-key map [f37] [C-S-f1])
569 (define-key map [f38] [C-S-f2])
570 (define-key map [f39] [C-S-f3])
571 (define-key map [f40] [C-S-f4])
572 (define-key map [f41] [C-S-f5])
573 (define-key map [f42] [C-S-f6])
574 (define-key map [f43] [C-S-f7])
575 (define-key map [f44] [C-S-f8])
576 (define-key map [f45] [C-S-f9])
577 (define-key map [f46] [C-S-f10])
578 (define-key map [f47] [C-S-f11])
579 (define-key map [f48] [C-S-f12])
581 (define-key map [f49] [M-f1])
582 (define-key map [f50] [M-f2])
583 (define-key map [f51] [M-f3])
584 (define-key map [f52] [M-f4])
585 (define-key map [f53] [M-f5])
586 (define-key map [f54] [M-f6])
587 (define-key map [f55] [M-f7])
588 (define-key map [f56] [M-f8])
589 (define-key map [f57] [M-f9])
590 (define-key map [f58] [M-f10])
591 (define-key map [f59] [M-f11])
592 (define-key map [f60] [M-f12])
594 (define-key map [f61] [M-S-f1])
595 (define-key map [f62] [M-S-f2])
596 (define-key map [f63] [M-S-f3])
597 (define-key map [f64] [M-S-f4])
598 (define-key map [f65] [M-S-f5])
599 (define-key map [f66] [M-S-f6])
600 (define-key map [f67] [M-S-f7])
601 (define-key map [f68] [M-S-f8])
602 (define-key map [f69] [M-S-f9])
603 (define-key map [f70] [M-S-f10])
604 (define-key map [f71] [M-S-f11])
605 (define-key map [f72] [M-S-f12])
608 "Keymap of possible alternative meanings for some keys.")
610 ;; Set up colors, for those versions of xterm that support it.
611 (defvar xterm-standard-colors
612 ;; The names in the comments taken from XTerm-col.ad in the xterm
613 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
615 '(("black" 0 ( 0 0 0)) ; black
616 ("red" 1 (205 0 0)) ; red3
617 ("green" 2 ( 0 205 0)) ; green3
618 ("yellow" 3 (205 205 0)) ; yellow3
619 ("blue" 4 ( 0 0 238)) ; blue2
620 ("magenta" 5 (205 0 205)) ; magenta3
621 ("cyan" 6 ( 0 205 205)) ; cyan3
622 ("white" 7 (229 229 229)) ; gray90
623 ("brightblack" 8 (127 127 127)) ; gray50
624 ("brightred" 9 (255 0 0)) ; red
625 ("brightgreen" 10 ( 0 255 0)) ; green
626 ("brightyellow" 11 (255 255 0)) ; yellow
627 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
628 ("brightmagenta" 13 (255 0 255)) ; magenta
629 ("brightcyan" 14 ( 0 255 255)) ; cyan
630 ("brightwhite" 15 (255 255 255))) ; white
631 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
633 (defun xterm--report-background-handler ()
636 ;; The reply should be: \e ] 11 ; rgb: NUMBER1 / NUMBER2 / NUMBER3 \e \\
637 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?\\)))
638 (setq str (concat str (string chr))))
640 "rgb:\\([a-f0-9]+\\)/\\([a-f0-9]+\\)/\\([a-f0-9]+\\)" str)
641 (let ((recompute-faces
642 (xterm-maybe-set-dark-background-mode
643 (string-to-number (match-string 1 str) 16)
644 (string-to-number (match-string 2 str) 16)
645 (string-to-number (match-string 3 str) 16))))
647 ;; Recompute faces here in case the background mode was
648 ;; set to dark. We used to call
649 ;; `tty-set-up-initial-frame-faces' only once, but that
650 ;; caused the light background faces to be computed
652 ;; http://permalink.gmane.org/gmane.emacs.devel/119627
653 (when recompute-faces
654 (tty-set-up-initial-frame-faces))))))
656 (defun xterm--version-handler ()
659 ;; The reply should be: \e [ > NUMBER1 ; NUMBER2 ; NUMBER3 c
660 ;; If the timeout is completely removed for read-event, this
661 ;; might hang for terminals that pretend to be xterm, but don't
662 ;; respond to this escape sequence. RMS' opinion was to remove
663 ;; it completely. That might be right, but let's first try to
664 ;; see if by using a longer timeout we get rid of most issues.
665 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?c)))
666 (setq str (concat str (string chr))))
667 ;; Since xterm-280, the terminal type (NUMBER1) is now 41 instead of 0.
668 (when (string-match "\\([0-9]+\\);\\([0-9]+\\);0" str)
669 (let ((version (string-to-number (match-string 2 str))))
670 (when (and (> version 2000) (equal (match-string 1 str) "1"))
671 ;; Hack attack! bug#16988: gnome-terminal reports "1;NNNN;0"
672 ;; with a large NNNN but is based on a rather old xterm code.
673 ;; Gnome terminal 3.6.1 reports 1;3406;0
674 ;; Gnome terminal 2.32.1 reports 1;2802;0
676 (when (equal (match-string 1 str) "83")
677 ;; `screen' (which returns 83;40003;0) seems to also lack support for
678 ;; some of these (bug#17607, bug#20356).
679 ;; Note: this code path should normally not be used any more
680 ;; since term/screen.el now binds xterm-extra-capabilities
681 ;; to a fixed value, rather than using the dynamic checking.
683 ;; If version is 242 or higher, assume the xterm supports
684 ;; reporting the background color (TODO: maybe earlier
685 ;; versions do too...)
686 (when (>= version 242)
687 (xterm--query "\e]11;?\e\\"
688 '(("\e]11;" . xterm--report-background-handler))))
690 ;; If version is 216 (the version when modifyOtherKeys was
691 ;; introduced) or higher, initialize the
692 ;; modifyOtherKeys support.
693 (when (>= version 216)
694 (xterm--init-modify-other-keys))
695 ;; In version 203 support for accessing the X selection was
696 ;; added. Hterm reports itself as version 256 and supports it
697 ;; as well. gnome-terminal doesn't and is excluded by this
699 (when (>= version 203)
700 ;; Most xterms seem to have it disabled by default, and if it's
701 ;; disabled, C-y will incur a timeout, so we only use it if the user
702 ;; explicitly requests it.
703 ;;(xterm--init-activate-get-selection)
704 (xterm--init-activate-set-selection))))))
706 (defvar xterm-query-timeout 2
707 "Seconds to wait for an answer from the terminal.
708 Can be nil to mean \"no timeout\".")
710 (defun xterm--query (query handlers &optional no-async)
711 "Send QUERY string to the terminal and watch for a response.
712 HANDLERS is an alist with elements of the form (STRING . FUNCTION).
713 We run the first FUNCTION whose STRING matches the input events."
714 ;; We used to query synchronously, but the need to use `discard-input' is
715 ;; rather annoying (bug#6758). Maybe we could always use the asynchronous
716 ;; approach, but it's less tested.
717 ;; FIXME: Merge the two branches.
720 (dolist (handler handlers)
721 (define-key input-decode-map (car handler)
722 (lambda (&optional _prompt)
723 ;; Unregister the handler, since we don't expect
725 (dolist (handler handlers)
726 (define-key input-decode-map (car handler) nil))
727 (funcall (cdr handler))
729 (if (and (or (null xterm-query-timeout) (input-pending-p))
732 (funcall register handlers)
733 (send-string-to-terminal query))
734 ;; Pending input can be mistakenly returned by the calls to
735 ;; read-event below: discard it.
737 (send-string-to-terminal query)
739 (let ((handler (pop handlers))
741 (while (and (< i (length (car handler)))
742 (let ((evt (read-event nil nil xterm-query-timeout)))
743 (if (and (null evt) (= i 0) (not no-async))
744 ;; Timeout on the first event: fallback on async.
746 (funcall register (cons handler handlers))
749 (or (eq evt (aref (car handler) i))
750 (progn (if evt (push evt unread-command-events))
753 (if (= i (length (car handler)))
754 (progn (setq handlers nil)
755 (funcall (cdr handler)))
757 (push (aref (car handler) (setq i (1- i)))
758 unread-command-events))))))))
760 (defun xterm--push-map (map basemap)
761 ;; Use inheritance to let the main keymaps override those defaults.
762 ;; This way we don't override terminfo-derived settings or settings
763 ;; made in the init file.
766 (make-composed-keymap map (keymap-parent basemap))))
768 (defun terminal-init-xterm ()
769 "Terminal initialization function for xterm."
770 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
771 ;; rxvt's keybindings are incompatible with xterm's. It is
772 ;; better in that case to use rxvt's initialization function.
773 (if (and (getenv "COLORTERM" (selected-frame))
774 (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
775 (tty-run-terminal-initialization (selected-frame) "rxvt")
777 (xterm--push-map xterm-alternatives-map local-function-key-map)
778 (xterm--push-map xterm-function-map input-decode-map))
780 (xterm-register-default-colors xterm-standard-colors)
781 (tty-set-up-initial-frame-faces)
783 (if (eq xterm-extra-capabilities 'check)
784 ;; Try to find out the type of terminal by sending a "Secondary
785 ;; Device Attributes (DA)" query.
786 (xterm--query "\e[>0c"
787 ;; Some terminals (like OS X's Terminal.app) respond to
788 ;; this query as if it were a "Primary Device Attributes"
789 ;; query instead, so we should handle that too.
790 '(("\e[?" . xterm--version-handler)
791 ("\e[>" . xterm--version-handler)))
793 (when (memq 'reportBackground xterm-extra-capabilities)
794 (xterm--query "\e]11;?\e\\"
795 '(("\e]11;" . xterm--report-background-handler))))
797 (when (memq 'modifyOtherKeys xterm-extra-capabilities)
798 (xterm--init-modify-other-keys))
800 (when (memq 'getSelection xterm-extra-capabilities)
801 (xterm--init-activate-get-selection))
802 (when (memq 'setSelection xterm-extra-capabilities)
803 (xterm--init-activate-set-selection)))
805 ;; Unconditionally enable bracketed paste mode: terminals that don't
806 ;; support it just ignore the sequence.
807 (xterm--init-bracketed-paste-mode)
809 (run-hooks 'terminal-init-xterm-hook))
811 (defun xterm--init-modify-other-keys ()
812 "Terminal initialization for xterm's modifyOtherKeys support."
813 (send-string-to-terminal "\e[>4;1m")
814 (push "\e[>4m" (terminal-parameter nil 'tty-mode-reset-strings))
815 (push "\e[>4;1m" (terminal-parameter nil 'tty-mode-set-strings)))
817 (defun xterm--init-bracketed-paste-mode ()
818 "Terminal initialization for bracketed paste mode."
819 (send-string-to-terminal "\e[?2004h")
820 (push "\e[?2004l" (terminal-parameter nil 'tty-mode-reset-strings))
821 (push "\e[?2004h" (terminal-parameter nil 'tty-mode-set-strings)))
823 (defun xterm--init-activate-get-selection ()
824 "Terminal initialization for `gui-get-selection'."
825 (set-terminal-parameter nil 'xterm--get-selection t))
827 (defun xterm--init-activate-set-selection ()
828 "Terminal initialization for `gui-set-selection'."
829 (set-terminal-parameter nil 'xterm--set-selection t))
831 (defun xterm--selection-char (type)
835 (_ (error "Invalid selection type: %S" type))))
837 (cl-defmethod gui-backend-get-selection
839 &context (window-system nil)
840 ;; Only applies to terminals which have it enabled.
841 ((terminal-parameter nil 'xterm--get-selection) (eql t)))
842 (unless (eq data-type 'STRING)
843 (error "Unsupported data type %S" data-type))
844 (let* ((screen (eq (terminal-parameter nil 'terminal-initted)
845 'terminal-init-screen))
846 (query (concat "\e]52;" (xterm--selection-char type) ";")))
848 (set-buffer-multibyte nil)
850 (concat (when screen "\eP") query "?\a" (when screen "\e\\"))
851 (list (cons query (lambda ()
852 (while (let ((char (read-char)))
853 (unless (eq char ?\a)
857 (base64-decode-region (point-min) (point-max))
858 (decode-coding-region (point-min) (point-max) 'utf-8-unix t))))
860 (cl-defmethod gui-backend-set-selection
862 &context (window-system nil)
863 ;; Only applies to terminals which have it enabled.
864 ((terminal-parameter nil 'xterm--set-selection) (eql t)))
865 "Copy DATA to the X selection using the OSC 52 escape sequence.
867 TYPE specifies which selection to set; it must be either
868 `PRIMARY' or `CLIPBOARD'. DATA must be a string.
870 This can be used as a `gui-set-selection' method for
871 xterm-compatible terminal emulators. Then your system clipboard
872 will be updated whenever you copy a region of text in Emacs.
874 If the resulting OSC 52 sequence would be longer than
875 `xterm-max-cut-length', then the TEXT is not sent to the system
878 This function either sends a raw OSC 52 sequence or wraps the OSC
879 52 in a Device Control String sequence. This way, it will work
880 on a bare terminal emulators as well as inside the screen
881 program. When inside the screen program, this function also
882 chops long DCS sequences into multiple smaller ones to avoid
883 hitting screen's max DCS length."
884 (let* ((screen (eq (terminal-parameter nil 'terminal-initted)
885 'terminal-init-screen))
886 (bytes (encode-coding-string data 'utf-8-unix))
888 (replace-regexp-in-string
890 (base64-encode-string bytes)
892 (base64-encode-string bytes :no-line-break)))
893 (length (length base-64)))
894 (if (> length xterm-max-cut-length)
896 (warn "Selection too long to send to terminal: %d bytes" length)
898 (send-string-to-terminal
901 "\e]52;" (xterm--selection-char type) ";" base-64 "\a"
902 (when screen "\e\\"))))))
904 (defun xterm-rgb-convert-to-16bit (prim)
905 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
906 (logior prim (lsh prim 8)))
908 (defun xterm-register-default-colors (colors)
909 "Register the default set of colors for xterm or compatible emulator.
911 This function registers the number of colors returned by `display-color-cells'
912 for the currently selected frame. The first (16) colors are taken from
913 COLORS, which see, while the rest are computed assuming
914 either the 88- or 256-color standard color scheme supported by latest
916 (let* ((ncolors (display-color-cells))
917 (color (car colors)))
919 ;; Clear the 8 default tty colors registered by startup.el
921 ;; Only register as many colors as are supported by the display.
922 (while (and (> ncolors 0) colors)
923 (tty-color-define (car color) (cadr color)
924 (mapcar #'xterm-rgb-convert-to-16bit
926 (setq colors (cdr colors)
928 ncolors (1- ncolors)))
929 ;; We've exhausted the colors from `colors'. If there
930 ;; are more colors to support, compute them now.
933 ((= ncolors 240) ; 256-color xterm
934 ;; 216 non-gray colors first
935 (let ((r 0) (g 0) (b 0))
936 (while (> ncolors 24)
937 ;; This and other formulas taken from 256colres.pl and
938 ;; 88colres.pl in the xterm distribution.
939 (tty-color-define (format "color-%d" (- 256 ncolors))
941 (mapcar #'xterm-rgb-convert-to-16bit
942 (list (if (zerop r) 0 (+ (* r 40) 55))
943 (if (zerop g) 0 (+ (* g 40) 55))
944 (if (zerop b) 0 (+ (* b 40) 55)))))
953 (setq ncolors (1- ncolors))))
954 ;; Now the 24 gray colors
956 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
957 (tty-color-define (format "color-%d" (- 256 ncolors))
959 (list color color color))
960 (setq ncolors (1- ncolors))))
961 ((= ncolors 72) ; 88-color xterm
962 ;; 64 non-gray colors
963 (let ((levels '(0 139 205 255))
966 (tty-color-define (format "color-%d" (- 88 ncolors))
968 (mapcar #'xterm-rgb-convert-to-16bit
979 (setq ncolors (1- ncolors))))
980 ;; Now the 8 gray colors
982 (setq color (xterm-rgb-convert-to-16bit
986 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
987 (tty-color-define (format "color-%d" (- 88 ncolors))
989 (list color color color))
990 (setq ncolors (1- ncolors))))
991 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
992 ;; Modifying color mappings means realized faces don't use the
993 ;; right colors, so clear them.
996 (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
997 ;; Use the heuristic in `frame-set-background-mode' to decide if a
999 (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
1000 (set-terminal-parameter nil 'background-mode 'dark)
1003 (provide 'xterm) ;Backward compatibility.
1004 (provide 'term/xterm)
1005 ;;; xterm.el ends here