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