Replace gui-method macros with cl-generic with &context
[emacs.git] / lisp / term / xterm.el
blob4e48e80e4e9553255129c5bb1787332b08d73e9e
1 ;;; xterm.el --- define function key sequences and standard colors for xterm -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995, 2001-2015 Free Software Foundation, Inc.
5 ;; Author: FSF
6 ;; Keywords: terminals
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/>.
23 ;;; Commentary:
25 ;;; Code:
27 (defgroup xterm nil
28 "XTerm support."
29 :version "24.1"
30 :group 'terminals)
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 "set X selection" setSelection)))
39 (defcustom xterm-extra-capabilities 'check
40 "Whether Xterm supports some additional, more modern, features.
41 If nil, just assume that it does not.
42 If `check', try to check if it does.
43 If a list, assume that the listed features are supported, without checking.
45 The relevant features are:
46 modifyOtherKeys -- if supported, more key bindings work (e.g., \"\\C-,\")
47 reportBackground -- if supported, Xterm reports its background color
48 setSelection -- if supported, Xterm saves yanked text to the X selection"
49 :version "24.1"
50 :type `(choice (const :tag "Check" check)
51 ,xterm--extra-capabilities-type))
53 (defcustom xterm-max-cut-length 100000
54 "Maximum number of bytes to cut into xterm using the OSC 52 sequence.
56 The OSC 52 sequence requires a terminator byte. Some terminals will ignore or
57 mistreat a terminated sequence that is longer than a certain size, usually to
58 protect users from runaway sequences.
60 This variable allows you to tweak the maximum number of bytes that will be sent
61 using the OSC 52 sequence.
63 If you select a region larger than this size, it won't be copied to your system
64 clipboard. Since clipboard data is base 64 encoded, the actual number of
65 string bytes that can be copied is 3/4 of this value."
66 :type 'integer)
68 (defconst xterm-paste-ending-sequence "\e[201~"
69 "Characters send by the terminal to end a bracketed paste.")
71 (defun xterm-paste ()
72 "Handle the start of a terminal paste operation."
73 (interactive)
74 (let* ((end-marker-length (length xterm-paste-ending-sequence))
75 (pasted-text (with-temp-buffer
76 (set-buffer-multibyte nil)
77 (while (not (search-backward
78 xterm-paste-ending-sequence
79 (- (point) end-marker-length) t))
80 (let ((event (read-event
81 nil nil
82 ;; Use finite timeout to avoid
83 ;; glomming the event onto
84 ;; this-command-keys.
85 most-positive-fixnum)))
86 (when (eql event ?\r)
87 (setf event ?\n))
88 (insert event)))
89 (let ((last-coding-system-used))
90 (decode-coding-region
91 (point-min) (point)
92 (keyboard-coding-system) t))))
93 (interprogram-paste-function (lambda () pasted-text)))
94 (yank)))
96 (define-key global-map [xterm-paste] #'xterm-paste)
98 (defvar xterm-rxvt-function-map
99 (let ((map (make-sparse-keymap)))
100 (define-key map "\e[2~" [insert])
101 (define-key map "\e[3~" [delete])
102 (define-key map "\e[4~" [select])
103 (define-key map "\e[5~" [prior])
104 (define-key map "\e[6~" [next])
106 (define-key map "\e[15~" [f5])
107 (define-key map "\e[17~" [f6])
108 (define-key map "\e[18~" [f7])
109 (define-key map "\e[19~" [f8])
110 (define-key map "\e[20~" [f9])
111 (define-key map "\e[21~" [f10])
113 (define-key map "\e[2;2~" [S-insert])
115 ;; Other versions of xterm might emit these.
116 (define-key map "\e[A" [up])
117 (define-key map "\e[B" [down])
118 (define-key map "\e[C" [right])
119 (define-key map "\e[D" [left])
121 (define-key map "\e[11~" [f1])
122 (define-key map "\e[12~" [f2])
123 (define-key map "\e[13~" [f3])
124 (define-key map "\e[14~" [f4])
126 ;; Recognize the start of a bracketed paste sequence. The handler
127 ;; internally recognizes the end.
128 (define-key map "\e[200~" [xterm-paste])
130 map)
131 "Keymap of escape sequences, shared between xterm and rxvt support.")
133 (defvar xterm-function-map
134 (let ((map (make-sparse-keymap)))
135 (set-keymap-parent map xterm-rxvt-function-map)
137 ;; xterm from X.org 6.8.2 uses these key definitions.
138 (define-key map "\eOP" [f1])
139 (define-key map "\eOQ" [f2])
140 (define-key map "\eOR" [f3])
141 (define-key map "\eOS" [f4])
142 (define-key map "\e[23~" [f11])
143 (define-key map "\e[24~" [f12])
145 (define-key map "\eO2P" [S-f1])
146 (define-key map "\eO2Q" [S-f2])
147 (define-key map "\eO2R" [S-f3])
148 (define-key map "\eO2S" [S-f4])
149 (define-key map "\e[1;2P" [S-f1])
150 (define-key map "\e[1;2Q" [S-f2])
151 (define-key map "\e[1;2R" [S-f3])
152 (define-key map "\e[1;2S" [S-f4])
153 (define-key map "\e[15;2~" [S-f5])
154 (define-key map "\e[17;2~" [S-f6])
155 (define-key map "\e[18;2~" [S-f7])
156 (define-key map "\e[19;2~" [S-f8])
157 (define-key map "\e[20;2~" [S-f9])
158 (define-key map "\e[21;2~" [S-f10])
159 (define-key map "\e[23;2~" [S-f11])
160 (define-key map "\e[24;2~" [S-f12])
162 (define-key map "\eO5P" [C-f1])
163 (define-key map "\eO5Q" [C-f2])
164 (define-key map "\eO5R" [C-f3])
165 (define-key map "\eO5S" [C-f4])
166 (define-key map "\e[15;5~" [C-f5])
167 (define-key map "\e[17;5~" [C-f6])
168 (define-key map "\e[18;5~" [C-f7])
169 (define-key map "\e[19;5~" [C-f8])
170 (define-key map "\e[20;5~" [C-f9])
171 (define-key map "\e[21;5~" [C-f10])
172 (define-key map "\e[23;5~" [C-f11])
173 (define-key map "\e[24;5~" [C-f12])
175 (define-key map "\eO6P" [C-S-f1])
176 (define-key map "\eO6Q" [C-S-f2])
177 (define-key map "\eO6R" [C-S-f3])
178 (define-key map "\eO6S" [C-S-f4])
179 (define-key map "\e[15;6~" [C-S-f5])
180 (define-key map "\e[17;6~" [C-S-f6])
181 (define-key map "\e[18;6~" [C-S-f7])
182 (define-key map "\e[19;6~" [C-S-f8])
183 (define-key map "\e[20;6~" [C-S-f9])
184 (define-key map "\e[21;6~" [C-S-f10])
185 (define-key map "\e[23;6~" [C-S-f11])
186 (define-key map "\e[24;6~" [C-S-f12])
188 (define-key map "\eO3P" [M-f1])
189 (define-key map "\eO3Q" [M-f2])
190 (define-key map "\eO3R" [M-f3])
191 (define-key map "\eO3S" [M-f4])
192 (define-key map "\e[15;3~" [M-f5])
193 (define-key map "\e[17;3~" [M-f6])
194 (define-key map "\e[18;3~" [M-f7])
195 (define-key map "\e[19;3~" [M-f8])
196 (define-key map "\e[20;3~" [M-f9])
197 (define-key map "\e[21;3~" [M-f10])
198 (define-key map "\e[23;3~" [M-f11])
199 (define-key map "\e[24;3~" [M-f12])
201 (define-key map "\eO4P" [M-S-f1])
202 (define-key map "\eO4Q" [M-S-f2])
203 (define-key map "\eO4R" [M-S-f3])
204 (define-key map "\eO4S" [M-S-f4])
205 (define-key map "\e[15;4~" [M-S-f5])
206 (define-key map "\e[17;4~" [M-S-f6])
207 (define-key map "\e[18;4~" [M-S-f7])
208 (define-key map "\e[19;4~" [M-S-f8])
209 (define-key map "\e[20;4~" [M-S-f9])
210 (define-key map "\e[21;4~" [M-S-f10])
211 (define-key map "\e[23;4~" [M-S-f11])
212 (define-key map "\e[24;4~" [M-S-f12])
214 (define-key map "\eOA" [up])
215 (define-key map "\eOB" [down])
216 (define-key map "\eOC" [right])
217 (define-key map "\eOD" [left])
218 (define-key map "\eOF" [end])
219 (define-key map "\eOH" [home])
221 (define-key map "\e[1;2A" [S-up])
222 (define-key map "\e[1;2B" [S-down])
223 (define-key map "\e[1;2C" [S-right])
224 (define-key map "\e[1;2D" [S-left])
225 (define-key map "\e[1;2F" [S-end])
226 (define-key map "\e[1;2H" [S-home])
228 (define-key map "\e[1;4A" [M-S-up])
229 (define-key map "\e[1;4B" [M-S-down])
230 (define-key map "\e[1;4C" [M-S-right])
231 (define-key map "\e[1;4D" [M-S-left])
232 (define-key map "\e[1;4F" [M-S-end])
233 (define-key map "\e[1;4H" [M-S-home])
235 (define-key map "\e[1;5A" [C-up])
236 (define-key map "\e[1;5B" [C-down])
237 (define-key map "\e[1;5C" [C-right])
238 (define-key map "\e[1;5D" [C-left])
239 (define-key map "\e[1;5F" [C-end])
240 (define-key map "\e[1;5H" [C-home])
242 (define-key map "\e[1;6A" [C-S-up])
243 (define-key map "\e[1;6B" [C-S-down])
244 (define-key map "\e[1;6C" [C-S-right])
245 (define-key map "\e[1;6D" [C-S-left])
246 (define-key map "\e[1;6F" [C-S-end])
247 (define-key map "\e[1;6H" [C-S-home])
249 (define-key map "\e[1;7A" [C-M-up])
250 (define-key map "\e[1;7B" [C-M-down])
251 (define-key map "\e[1;7C" [C-M-right])
252 (define-key map "\e[1;7D" [C-M-left])
253 (define-key map "\e[1;7F" [C-M-end])
254 (define-key map "\e[1;7H" [C-M-home])
256 (define-key map "\e[1;8A" [C-M-S-up])
257 (define-key map "\e[1;8B" [C-M-S-down])
258 (define-key map "\e[1;8C" [C-M-S-right])
259 (define-key map "\e[1;8D" [C-M-S-left])
260 (define-key map "\e[1;8F" [C-M-S-end])
261 (define-key map "\e[1;8H" [C-M-S-home])
263 (define-key map "\e[1;3A" [M-up])
264 (define-key map "\e[1;3B" [M-down])
265 (define-key map "\e[1;3C" [M-right])
266 (define-key map "\e[1;3D" [M-left])
267 (define-key map "\e[1;3F" [M-end])
268 (define-key map "\e[1;3H" [M-home])
270 (define-key map "\e[3;2~" [S-delete])
271 (define-key map "\e[5;2~" [S-prior])
272 (define-key map "\e[6;2~" [S-next])
274 (define-key map "\e[2;4~" [M-S-insert])
275 (define-key map "\e[3;4~" [M-S-delete])
276 (define-key map "\e[5;4~" [M-S-prior])
277 (define-key map "\e[6;4~" [M-S-next])
279 (define-key map "\e[2;5~" [C-insert])
280 (define-key map "\e[3;5~" [C-delete])
281 (define-key map "\e[5;5~" [C-prior])
282 (define-key map "\e[6;5~" [C-next])
284 (define-key map "\e[2;6~" [C-S-insert])
285 (define-key map "\e[3;6~" [C-S-delete])
286 (define-key map "\e[5;6~" [C-S-prior])
287 (define-key map "\e[6;6~" [C-S-next])
289 (define-key map "\e[2;7~" [C-M-insert])
290 (define-key map "\e[3;7~" [C-M-delete])
291 (define-key map "\e[5;7~" [C-M-prior])
292 (define-key map "\e[6;7~" [C-M-next])
294 (define-key map "\e[2;8~" [C-M-S-insert])
295 (define-key map "\e[3;8~" [C-M-S-delete])
296 (define-key map "\e[5;8~" [C-M-S-prior])
297 (define-key map "\e[6;8~" [C-M-S-next])
299 (define-key map "\e[2;3~" [M-insert])
300 (define-key map "\e[3;3~" [M-delete])
301 (define-key map "\e[5;3~" [M-prior])
302 (define-key map "\e[6;3~" [M-next])
304 (define-key map "\e[29~" [print])
306 (define-key map "\eOj" [kp-multiply])
307 (define-key map "\eOk" [kp-add])
308 (define-key map "\eOl" [kp-separator])
309 (define-key map "\eOm" [kp-subtract])
310 (define-key map "\eOo" [kp-divide])
311 (define-key map "\eOp" [kp-0])
312 (define-key map "\eOq" [kp-1])
313 (define-key map "\eOr" [kp-2])
314 (define-key map "\eOs" [kp-3])
315 (define-key map "\eOt" [kp-4])
316 (define-key map "\eOu" [kp-5])
317 (define-key map "\eOv" [kp-6])
318 (define-key map "\eOw" [kp-7])
319 (define-key map "\eOx" [kp-8])
320 (define-key map "\eOy" [kp-9])
322 (define-key map "\eO2j" [S-kp-multiply])
323 (define-key map "\eO2k" [S-kp-add])
324 (define-key map "\eO2l" [S-kp-separator])
325 (define-key map "\eO2m" [S-kp-subtract])
326 (define-key map "\eO2o" [S-kp-divide])
327 (define-key map "\eO2p" [S-kp-0])
328 (define-key map "\eO2q" [S-kp-1])
329 (define-key map "\eO2r" [S-kp-2])
330 (define-key map "\eO2s" [S-kp-3])
331 (define-key map "\eO2t" [S-kp-4])
332 (define-key map "\eO2u" [S-kp-5])
333 (define-key map "\eO2v" [S-kp-6])
334 (define-key map "\eO2w" [S-kp-7])
335 (define-key map "\eO2x" [S-kp-8])
336 (define-key map "\eO2y" [S-kp-9])
338 (define-key map "\eO4j" [M-S-kp-multiply])
339 (define-key map "\eO4k" [M-S-kp-add])
340 (define-key map "\eO4l" [M-S-kp-separator])
341 (define-key map "\eO4m" [M-S-kp-subtract])
342 (define-key map "\eO4o" [M-S-kp-divide])
343 (define-key map "\eO4p" [M-S-kp-0])
344 (define-key map "\eO4q" [M-S-kp-1])
345 (define-key map "\eO4r" [M-S-kp-2])
346 (define-key map "\eO4s" [M-S-kp-3])
347 (define-key map "\eO4t" [M-S-kp-4])
348 (define-key map "\eO4u" [M-S-kp-5])
349 (define-key map "\eO4v" [M-S-kp-6])
350 (define-key map "\eO4w" [M-S-kp-7])
351 (define-key map "\eO4x" [M-S-kp-8])
352 (define-key map "\eO4y" [M-S-kp-9])
354 (define-key map "\eO6j" [C-S-kp-multiply])
355 (define-key map "\eO6k" [C-S-kp-add])
356 (define-key map "\eO6l" [C-S-kp-separator])
357 (define-key map "\eO6m" [C-S-kp-subtract])
358 (define-key map "\eO6o" [C-S-kp-divide])
359 (define-key map "\eO6p" [C-S-kp-0])
360 (define-key map "\eO6q" [C-S-kp-1])
361 (define-key map "\eO6r" [C-S-kp-2])
362 (define-key map "\eO6s" [C-S-kp-3])
363 (define-key map "\eO6t" [C-S-kp-4])
364 (define-key map "\eO6u" [C-S-kp-5])
365 (define-key map "\eO6v" [C-S-kp-6])
366 (define-key map "\eO6w" [C-S-kp-7])
367 (define-key map "\eO6x" [C-S-kp-8])
368 (define-key map "\eO6y" [C-S-kp-9])
370 (define-key map "\eO8j" [C-M-S-kp-multiply])
371 (define-key map "\eO8k" [C-M-S-kp-add])
372 (define-key map "\eO8l" [C-M-S-kp-separator])
373 (define-key map "\eO8m" [C-M-S-kp-subtract])
374 (define-key map "\eO8o" [C-M-S-kp-divide])
375 (define-key map "\eO8p" [C-M-S-kp-0])
376 (define-key map "\eO8q" [C-M-S-kp-1])
377 (define-key map "\eO8r" [C-M-S-kp-2])
378 (define-key map "\eO8s" [C-M-S-kp-3])
379 (define-key map "\eO8t" [C-M-S-kp-4])
380 (define-key map "\eO8u" [C-M-S-kp-5])
381 (define-key map "\eO8v" [C-M-S-kp-6])
382 (define-key map "\eO8w" [C-M-S-kp-7])
383 (define-key map "\eO8x" [C-M-S-kp-8])
384 (define-key map "\eO8y" [C-M-S-kp-9])
386 ;; These keys are available in xterm starting from version 216
387 ;; if the modifyOtherKeys resource is set to 1.
388 (dolist (bind '((5 9 [C-tab])
389 (5 13 [C-return])
390 (5 39 [?\C-\'])
391 (5 44 [?\C-,])
392 (5 45 [?\C--])
393 (5 46 [?\C-.])
394 (5 47 [?\C-/])
395 (5 48 [?\C-0])
396 (5 49 [?\C-1])
397 ;; Not all C-DIGIT keys have a distinct binding.
398 (5 57 [?\C-9])
399 (5 59 [?\C-\;])
400 (5 61 [?\C-=])
401 (5 92 [?\C-\\])
403 (6 33 [?\C-!])
404 (6 34 [?\C-\"])
405 (6 35 [?\C-#])
406 (6 36 [?\C-$])
407 (6 37 [?\C-%])
408 (6 38 [?\C-&])
409 (6 40 [?\C-\(])
410 (6 41 [?\C-\)])
411 (6 42 [?\C-*])
412 (6 43 [?\C-+])
413 (6 58 [?\C-:])
414 (6 60 [?\C-<])
415 (6 62 [?\C->])
416 (6 63 [(control ??)])
418 ;; These are the strings emitted for various C-M-
419 ;; combinations for keyboards whose Meta and Alt
420 ;; modifiers are on the same key (usually labeled "Alt").
421 (13 9 [C-M-tab])
422 (13 13 [C-M-return])
424 (13 39 [?\C-\M-\'])
425 (13 44 [?\C-\M-,])
426 (13 45 [?\C-\M--])
427 (13 46 [?\C-\M-.])
428 (13 47 [?\C-\M-/])
429 (13 48 [?\C-\M-0])
430 (13 49 [?\C-\M-1])
431 (13 50 [?\C-\M-2])
432 (13 51 [?\C-\M-3])
433 (13 52 [?\C-\M-4])
434 (13 53 [?\C-\M-5])
435 (13 54 [?\C-\M-6])
436 (13 55 [?\C-\M-7])
437 (13 56 [?\C-\M-8])
438 (13 57 [?\C-\M-9])
439 (13 59 [?\C-\M-\;])
440 (13 61 [?\C-\M-=])
441 (13 92 [?\C-\M-\\])
443 (14 33 [?\C-\M-!])
444 (14 34 [?\C-\M-\"])
445 (14 35 [?\C-\M-#])
446 (14 36 [?\C-\M-$])
447 (14 37 [?\C-\M-%])
448 (14 38 [?\C-\M-&])
449 (14 40 [?\C-\M-\(])
450 (14 41 [?\C-\M-\)])
451 (14 42 [?\C-\M-*])
452 (14 43 [?\C-\M-+])
453 (14 58 [?\C-\M-:])
454 (14 60 [?\C-\M-<])
455 (14 62 [?\C-\M->])
456 (14 63 [(control meta ??)])
458 (7 9 [C-M-tab])
459 (7 13 [C-M-return])
461 (7 32 [?\C-\M-\s])
462 (7 39 [?\C-\M-\'])
463 (7 44 [?\C-\M-,])
464 (7 45 [?\C-\M--])
465 (7 46 [?\C-\M-.])
466 (7 47 [?\C-\M-/])
467 (7 48 [?\C-\M-0])
468 (7 49 [?\C-\M-1])
469 (7 50 [?\C-\M-2])
470 (7 51 [?\C-\M-3])
471 (7 52 [?\C-\M-4])
472 (7 53 [?\C-\M-5])
473 (7 54 [?\C-\M-6])
474 (7 55 [?\C-\M-7])
475 (7 56 [?\C-\M-8])
476 (7 57 [?\C-\M-9])
477 (7 59 [?\C-\M-\;])
478 (7 61 [?\C-\M-=])
479 (7 92 [?\C-\M-\\])
481 (8 33 [?\C-\M-!])
482 (8 34 [?\C-\M-\"])
483 (8 35 [?\C-\M-#])
484 (8 36 [?\C-\M-$])
485 (8 37 [?\C-\M-%])
486 (8 38 [?\C-\M-&])
487 (8 40 [?\C-\M-\(])
488 (8 41 [?\C-\M-\)])
489 (8 42 [?\C-\M-*])
490 (8 43 [?\C-\M-+])
491 (8 58 [?\C-\M-:])
492 (8 60 [?\C-\M-<])
493 (8 62 [?\C-\M->])
494 (8 63 [(control meta ??)])
496 (2 9 [S-tab])
497 (2 13 [S-return])
499 (6 9 [C-S-tab])
500 (6 13 [C-S-return])))
501 (define-key map
502 (format "\e[27;%d;%d~" (nth 0 bind) (nth 1 bind)) (nth 2 bind))
503 ;; For formatOtherKeys=1, the sequence is a bit shorter (bug#13839).
504 (define-key map
505 (format "\e[%d;%du" (nth 1 bind) (nth 0 bind)) (nth 2 bind)))
507 ;; Other versions of xterm might emit these.
508 (define-key map "\e[1~" [home])
510 (define-key map "\eO2A" [S-up])
511 (define-key map "\eO2B" [S-down])
512 (define-key map "\eO2C" [S-right])
513 (define-key map "\eO2D" [S-left])
514 (define-key map "\eO2F" [S-end])
515 (define-key map "\eO2H" [S-home])
517 (define-key map "\eO5A" [C-up])
518 (define-key map "\eO5B" [C-down])
519 (define-key map "\eO5C" [C-right])
520 (define-key map "\eO5D" [C-left])
521 (define-key map "\eO5F" [C-end])
522 (define-key map "\eO5H" [C-home])
524 map)
525 "Function key map overrides for xterm.")
527 (defvar xterm-alternatives-map
528 (let ((map (make-sparse-keymap)))
529 ;; The terminal initialization C code file might have initialized
530 ;; function keys F13->F60 from the termcap/terminfo information.
531 ;; On a PC-style keyboard these keys correspond to
532 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The code
533 ;; here substitutes the corresponding definitions in function-key-map.
534 ;; The mapping from escape sequences to Fn is done in input-decode-map
535 ;; whereas this here mapping is done in local-function-key-map so that
536 ;; bindings to f45 still work, in case your keyboard really has an f45
537 ;; key rather than C-S-f9.
538 (define-key map [f13] [S-f1])
539 (define-key map [f14] [S-f2])
540 (define-key map [f15] [S-f3])
541 (define-key map [f16] [S-f4])
542 (define-key map [f17] [S-f5])
543 (define-key map [f18] [S-f6])
544 (define-key map [f19] [S-f7])
545 (define-key map [f20] [S-f8])
546 (define-key map [f21] [S-f9])
547 (define-key map [f22] [S-f10])
548 (define-key map [f23] [S-f11])
549 (define-key map [f24] [S-f12])
551 (define-key map [f25] [C-f1])
552 (define-key map [f26] [C-f2])
553 (define-key map [f27] [C-f3])
554 (define-key map [f28] [C-f4])
555 (define-key map [f29] [C-f5])
556 (define-key map [f30] [C-f6])
557 (define-key map [f31] [C-f7])
558 (define-key map [f32] [C-f8])
559 (define-key map [f33] [C-f9])
560 (define-key map [f34] [C-f10])
561 (define-key map [f35] [C-f11])
562 (define-key map [f36] [C-f12])
564 (define-key map [f37] [C-S-f1])
565 (define-key map [f38] [C-S-f2])
566 (define-key map [f39] [C-S-f3])
567 (define-key map [f40] [C-S-f4])
568 (define-key map [f41] [C-S-f5])
569 (define-key map [f42] [C-S-f6])
570 (define-key map [f43] [C-S-f7])
571 (define-key map [f44] [C-S-f8])
572 (define-key map [f45] [C-S-f9])
573 (define-key map [f46] [C-S-f10])
574 (define-key map [f47] [C-S-f11])
575 (define-key map [f48] [C-S-f12])
577 (define-key map [f49] [M-f1])
578 (define-key map [f50] [M-f2])
579 (define-key map [f51] [M-f3])
580 (define-key map [f52] [M-f4])
581 (define-key map [f53] [M-f5])
582 (define-key map [f54] [M-f6])
583 (define-key map [f55] [M-f7])
584 (define-key map [f56] [M-f8])
585 (define-key map [f57] [M-f9])
586 (define-key map [f58] [M-f10])
587 (define-key map [f59] [M-f11])
588 (define-key map [f60] [M-f12])
590 map)
591 "Keymap of possible alternative meanings for some keys.")
593 ;; Set up colors, for those versions of xterm that support it.
594 (defvar xterm-standard-colors
595 ;; The names in the comments taken from XTerm-col.ad in the xterm
596 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
597 ;; from rgb.txt.
598 '(("black" 0 ( 0 0 0)) ; black
599 ("red" 1 (205 0 0)) ; red3
600 ("green" 2 ( 0 205 0)) ; green3
601 ("yellow" 3 (205 205 0)) ; yellow3
602 ("blue" 4 ( 0 0 238)) ; blue2
603 ("magenta" 5 (205 0 205)) ; magenta3
604 ("cyan" 6 ( 0 205 205)) ; cyan3
605 ("white" 7 (229 229 229)) ; gray90
606 ("brightblack" 8 (127 127 127)) ; gray50
607 ("brightred" 9 (255 0 0)) ; red
608 ("brightgreen" 10 ( 0 255 0)) ; green
609 ("brightyellow" 11 (255 255 0)) ; yellow
610 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
611 ("brightmagenta" 13 (255 0 255)) ; magenta
612 ("brightcyan" 14 ( 0 255 255)) ; cyan
613 ("brightwhite" 15 (255 255 255))) ; white
614 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
616 (defun xterm--report-background-handler ()
617 (let ((str "")
618 chr)
619 ;; The reply should be: \e ] 11 ; rgb: NUMBER1 / NUMBER2 / NUMBER3 \e \\
620 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?\\)))
621 (setq str (concat str (string chr))))
622 (when (string-match
623 "rgb:\\([a-f0-9]+\\)/\\([a-f0-9]+\\)/\\([a-f0-9]+\\)" str)
624 (let ((recompute-faces
625 (xterm-maybe-set-dark-background-mode
626 (string-to-number (match-string 1 str) 16)
627 (string-to-number (match-string 2 str) 16)
628 (string-to-number (match-string 3 str) 16))))
630 ;; Recompute faces here in case the background mode was
631 ;; set to dark. We used to call
632 ;; `tty-set-up-initial-frame-faces' only once, but that
633 ;; caused the light background faces to be computed
634 ;; incorrectly. See:
635 ;; http://permalink.gmane.org/gmane.emacs.devel/119627
636 (when recompute-faces
637 (tty-set-up-initial-frame-faces))))))
639 (defun xterm--version-handler ()
640 (let ((str "")
641 chr)
642 ;; The reply should be: \e [ > NUMBER1 ; NUMBER2 ; NUMBER3 c
643 ;; If the timeout is completely removed for read-event, this
644 ;; might hang for terminals that pretend to be xterm, but don't
645 ;; respond to this escape sequence. RMS' opinion was to remove
646 ;; it completely. That might be right, but let's first try to
647 ;; see if by using a longer timeout we get rid of most issues.
648 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?c)))
649 (setq str (concat str (string chr))))
650 ;; Since xterm-280, the terminal type (NUMBER1) is now 41 instead of 0.
651 (when (string-match "\\([0-9]+\\);\\([0-9]+\\);0" str)
652 (let ((version (string-to-number (match-string 2 str))))
653 (when (and (> version 2000) (equal (match-string 1 str) "1"))
654 ;; Hack attack! bug#16988: gnome-terminal reports "1;NNNN;0"
655 ;; with a large NNNN but is based on a rather old xterm code.
656 ;; Gnome terminal 3.6.1 reports 1;3406;0
657 ;; Gnome terminal 2.32.1 reports 1;2802;0
658 (setq version 200))
659 (when (equal (match-string 1 str) "83")
660 ;; `screen' (which returns 83;40003;0) seems to also lack support for
661 ;; some of these (bug#17607, bug#20356).
662 ;; Note: this code path should normally not be used any more
663 ;; since term/screen.el now binds xterm-extra-capabilities
664 ;; to a fixed value, rather than using the dynamic checking.
665 (setq version 200))
666 ;; If version is 242 or higher, assume the xterm supports
667 ;; reporting the background color (TODO: maybe earlier
668 ;; versions do too...)
669 (when (>= version 242)
670 (xterm--query "\e]11;?\e\\"
671 '(("\e]11;" . xterm--report-background-handler))))
673 ;; If version is 216 (the version when modifyOtherKeys was
674 ;; introduced) or higher, initialize the
675 ;; modifyOtherKeys support.
676 (when (>= version 216)
677 (terminal-init-xterm-modify-other-keys))
678 ;; In version 203 support for accessing the X selection was
679 ;; added. Hterm reports itself as version 256 and supports it
680 ;; as well. gnome-terminal doesn't and is excluded by this
681 ;; test.
682 (when (>= version 203)
683 (terminal-init-xterm-activate-set-selection))))))
685 (defun xterm--query (query handlers)
686 "Send QUERY string to the terminal and watch for a response.
687 HANDLERS is an alist with elements of the form (STRING . FUNCTION).
688 We run the first FUNCTION whose STRING matches the input events."
689 ;; We used to query synchronously, but the need to use `discard-input' is
690 ;; rather annoying (bug#6758). Maybe we could always use the asynchronous
691 ;; approach, but it's less tested.
692 ;; FIXME: Merge the two branches.
693 (if (input-pending-p)
694 (progn
695 (dolist (handler handlers)
696 (define-key input-decode-map (car handler)
697 (lambda (&optional _prompt)
698 ;; Unregister the handler, since we don't expect further answers.
699 (dolist (handler handlers)
700 (define-key input-decode-map (car handler) nil))
701 (funcall (cdr handler))
702 [])))
703 (send-string-to-terminal query))
704 ;; Pending input can be mistakenly returned by the calls to
705 ;; read-event below. Discard it.
706 (send-string-to-terminal query)
707 (while handlers
708 (let ((handler (pop handlers))
709 (i 0))
710 (while (and (< i (length (car handler)))
711 (let ((evt (read-event nil nil 2)))
712 (or (eq evt (aref (car handler) i))
713 (progn (if evt (push evt unread-command-events))
714 nil))))
715 (setq i (1+ i)))
716 (if (= i (length (car handler)))
717 (progn (setq handlers nil)
718 (funcall (cdr handler)))
719 (while (> i 0)
720 (push (aref (car handler) (setq i (1- i)))
721 unread-command-events)))))))
723 (defun xterm--push-map (map basemap)
724 ;; Use inheritance to let the main keymaps override those defaults.
725 ;; This way we don't override terminfo-derived settings or settings
726 ;; made in the init file.
727 (set-keymap-parent
728 basemap
729 (make-composed-keymap map (keymap-parent basemap))))
731 (defun terminal-init-xterm ()
732 "Terminal initialization function for xterm."
733 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
734 ;; rxvt's keybindings are incompatible with xterm's. It is
735 ;; better in that case to use rxvt's initialization function.
736 (if (and (getenv "COLORTERM" (selected-frame))
737 (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
738 (tty-run-terminal-initialization (selected-frame) "rxvt")
740 (xterm--push-map xterm-alternatives-map local-function-key-map)
741 (xterm--push-map xterm-function-map input-decode-map))
743 (xterm-register-default-colors xterm-standard-colors)
744 (tty-set-up-initial-frame-faces)
746 (if (eq xterm-extra-capabilities 'check)
747 ;; Try to find out the type of terminal by sending a "Secondary
748 ;; Device Attributes (DA)" query.
749 (xterm--query "\e[>0c"
750 ;; Some terminals (like OS X's Terminal.app) respond to
751 ;; this query as if it were a "Primary Device Attributes"
752 ;; query instead, so we should handle that too.
753 '(("\e[?" . xterm--version-handler)
754 ("\e[>" . xterm--version-handler)))
756 (when (memq 'reportBackground xterm-extra-capabilities)
757 (xterm--query "\e]11;?\e\\"
758 '(("\e]11;" . xterm--report-background-handler))))
760 (when (memq 'modifyOtherKeys xterm-extra-capabilities)
761 (terminal-init-xterm-modify-other-keys))
763 (when (memq 'setSelection xterm-extra-capabilities)
764 (terminal-init-xterm-activate-set-selection)))
766 ;; Unconditionally enable bracketed paste mode: terminals that don't
767 ;; support it just ignore the sequence.
768 (terminal-init-xterm-bracketed-paste-mode)
770 (run-hooks 'terminal-init-xterm-hook))
772 (defun terminal-init-xterm-modify-other-keys ()
773 "Terminal initialization for xterm's modifyOtherKeys support."
774 (send-string-to-terminal "\e[>4;1m")
775 (push "\e[>4m" (terminal-parameter nil 'tty-mode-reset-strings))
776 (push "\e[>4;1m" (terminal-parameter nil 'tty-mode-set-strings)))
778 (defun terminal-init-xterm-bracketed-paste-mode ()
779 "Terminal initialization for bracketed paste mode."
780 (send-string-to-terminal "\e[?2004h")
781 (push "\e[?2004l" (terminal-parameter nil 'tty-mode-reset-strings))
782 (push "\e[?2004h" (terminal-parameter nil 'tty-mode-set-strings)))
784 (defun terminal-init-xterm-activate-set-selection ()
785 "Terminal initialization for `gui-set-selection'."
786 (set-terminal-parameter nil 'xterm--set-selection t))
788 ;; FIXME: This defines the gui method for all terminals, even tho it only
789 ;; supports a subset of them.
790 (cl-defmethod gui-backend-set-selection (type data &context (window-system (eql nil)))
791 "Copy DATA to the X selection using the OSC 52 escape sequence.
793 TYPE specifies which selection to set; it must be either
794 `PRIMARY' or `CLIPBOARD'. DATA must be a string.
796 This can be used as a `gui-set-selection' method for
797 xterm-compatible terminal emulators. Then your system clipboard
798 will be updated whenever you copy a region of text in Emacs.
800 If the resulting OSC 52 sequence would be longer than
801 `xterm-max-cut-length', then the TEXT is not sent to the system
802 clipboard.
804 This function either sends a raw OSC 52 sequence or wraps the OSC
805 52 in a Device Control String sequence. This way, it will work
806 on a bare terminal emulators as well as inside the screen
807 program. When inside the screen program, this function also
808 chops long DCS sequences into multiple smaller ones to avoid
809 hitting screen's max DCS length."
810 (let* ((screen (eq (terminal-parameter nil 'terminal-initted)
811 'terminal-init-screen)))
812 ;; Only do something if the current terminal is actually an XTerm
813 ;; or screen.
814 (when (terminal-parameter nil 'xterm--set-selection)
815 (let* ((bytes (encode-coding-string data 'utf-8-unix))
816 (base-64 (if screen
817 (replace-regexp-in-string
818 "\n" "\e\\\eP"
819 (base64-encode-string bytes)
820 :fixedcase :literal)
821 (base64-encode-string bytes :no-line-break)))
822 (length (length base-64)))
823 (if (> length xterm-max-cut-length)
824 (progn
825 (warn "Selection too long to send to terminal: %d bytes" length)
826 (sit-for 2))
827 (send-string-to-terminal
828 (concat
829 (when screen "\eP")
830 "\e]52;"
831 (pcase type
832 ('PRIMARY "p")
833 ('CLIPBOARD "c")
834 (_ (error "Invalid selection type: %S" type)))
836 base-64
837 "\a"
838 (when screen "\e\\"))))))))
840 (defun xterm-rgb-convert-to-16bit (prim)
841 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
842 (logior prim (lsh prim 8)))
844 (defun xterm-register-default-colors (colors)
845 "Register the default set of colors for xterm or compatible emulator.
847 This function registers the number of colors returned by `display-color-cells'
848 for the currently selected frame. The first (16) colors are taken from
849 COLORS, which see, while the rest are computed assuming
850 either the 88- or 256-color standard color scheme supported by latest
851 versions of xterm."
852 (let* ((ncolors (display-color-cells))
853 (color (car colors)))
854 (if (> ncolors 0)
855 ;; Clear the 8 default tty colors registered by startup.el
856 (tty-color-clear))
857 ;; Only register as many colors as are supported by the display.
858 (while (and (> ncolors 0) colors)
859 (tty-color-define (car color) (cadr color)
860 (mapcar #'xterm-rgb-convert-to-16bit
861 (car (cddr color))))
862 (setq colors (cdr colors)
863 color (car colors)
864 ncolors (1- ncolors)))
865 ;; We've exhausted the colors from `colors'. If there
866 ;; are more colors to support, compute them now.
867 (when (> ncolors 0)
868 (cond
869 ((= ncolors 240) ; 256-color xterm
870 ;; 216 non-gray colors first
871 (let ((r 0) (g 0) (b 0))
872 (while (> ncolors 24)
873 ;; This and other formulas taken from 256colres.pl and
874 ;; 88colres.pl in the xterm distribution.
875 (tty-color-define (format "color-%d" (- 256 ncolors))
876 (- 256 ncolors)
877 (mapcar #'xterm-rgb-convert-to-16bit
878 (list (if (zerop r) 0 (+ (* r 40) 55))
879 (if (zerop g) 0 (+ (* g 40) 55))
880 (if (zerop b) 0 (+ (* b 40) 55)))))
882 (setq b (1+ b))
883 (if (> b 5)
884 (setq g (1+ g)
885 b 0))
886 (if (> g 5)
887 (setq r (1+ r)
888 g 0))
889 (setq ncolors (1- ncolors))))
890 ;; Now the 24 gray colors
891 (while (> ncolors 0)
892 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
893 (tty-color-define (format "color-%d" (- 256 ncolors))
894 (- 256 ncolors)
895 (list color color color))
896 (setq ncolors (1- ncolors))))
897 ((= ncolors 72) ; 88-color xterm
898 ;; 64 non-gray colors
899 (let ((levels '(0 139 205 255))
900 (r 0) (g 0) (b 0))
901 (while (> ncolors 8)
902 (tty-color-define (format "color-%d" (- 88 ncolors))
903 (- 88 ncolors)
904 (mapcar #'xterm-rgb-convert-to-16bit
905 (list (nth r levels)
906 (nth g levels)
907 (nth b levels))))
908 (setq b (1+ b))
909 (if (> b 3)
910 (setq g (1+ g)
911 b 0))
912 (if (> g 3)
913 (setq r (1+ r)
914 g 0))
915 (setq ncolors (1- ncolors))))
916 ;; Now the 8 gray colors
917 (while (> ncolors 0)
918 (setq color (xterm-rgb-convert-to-16bit
919 (floor
920 (if (= ncolors 8)
921 46.36363636
922 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
923 (tty-color-define (format "color-%d" (- 88 ncolors))
924 (- 88 ncolors)
925 (list color color color))
926 (setq ncolors (1- ncolors))))
927 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
928 ;; Modifying color mappings means realized faces don't use the
929 ;; right colors, so clear them.
930 (clear-face-cache)))
932 (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
933 ;; Use the heuristic in `frame-set-background-mode' to decide if a
934 ;; frame is dark.
935 (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
936 (set-terminal-parameter nil 'background-mode 'dark)
939 (provide 'xterm) ;Backward compatibility.
940 (provide 'term/xterm)
941 ;;; xterm.el ends here