Update copyright year to 2014 by running admin/update-copyright.
[emacs.git] / lisp / term / xterm.el
blob87f8c966deb7939a040817d1b172bda29a154e87
1 ;;; xterm.el --- define function key sequences and standard colors for xterm -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995, 2001-2014 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 :version "24.1"
42 :group 'xterm
43 :type '(choice (const :tag "No" nil)
44 (const :tag "Check" check)
45 ;; NOTE: If you add entries here, make sure to update
46 ;; `tocheck-capabilities' in `terminal-init-xterm' as well.
47 (set (const :tag "modifyOtherKeys support" modifyOtherKeys)
48 (const :tag "report background" reportBackground))))
50 (defvar xterm-function-map
51 (let ((map (make-sparse-keymap)))
53 ;; xterm from X.org 6.8.2 uses these key definitions.
54 (define-key map "\eOP" [f1])
55 (define-key map "\eOQ" [f2])
56 (define-key map "\eOR" [f3])
57 (define-key map "\eOS" [f4])
58 (define-key map "\e[15~" [f5])
59 (define-key map "\e[17~" [f6])
60 (define-key map "\e[18~" [f7])
61 (define-key map "\e[19~" [f8])
62 (define-key map "\e[20~" [f9])
63 (define-key map "\e[21~" [f10])
64 (define-key map "\e[23~" [f11])
65 (define-key map "\e[24~" [f12])
67 (define-key map "\eO2P" [S-f1])
68 (define-key map "\eO2Q" [S-f2])
69 (define-key map "\eO2R" [S-f3])
70 (define-key map "\eO2S" [S-f4])
71 (define-key map "\e[1;2P" [S-f1])
72 (define-key map "\e[1;2Q" [S-f2])
73 (define-key map "\e[1;2R" [S-f3])
74 (define-key map "\e[1;2S" [S-f4])
75 (define-key map "\e[15;2~" [S-f5])
76 (define-key map "\e[17;2~" [S-f6])
77 (define-key map "\e[18;2~" [S-f7])
78 (define-key map "\e[19;2~" [S-f8])
79 (define-key map "\e[20;2~" [S-f9])
80 (define-key map "\e[21;2~" [S-f10])
81 (define-key map "\e[23;2~" [S-f11])
82 (define-key map "\e[24;2~" [S-f12])
84 (define-key map "\eO5P" [C-f1])
85 (define-key map "\eO5Q" [C-f2])
86 (define-key map "\eO5R" [C-f3])
87 (define-key map "\eO5S" [C-f4])
88 (define-key map "\e[15;5~" [C-f5])
89 (define-key map "\e[17;5~" [C-f6])
90 (define-key map "\e[18;5~" [C-f7])
91 (define-key map "\e[19;5~" [C-f8])
92 (define-key map "\e[20;5~" [C-f9])
93 (define-key map "\e[21;5~" [C-f10])
94 (define-key map "\e[23;5~" [C-f11])
95 (define-key map "\e[24;5~" [C-f12])
97 (define-key map "\eO6P" [C-S-f1])
98 (define-key map "\eO6Q" [C-S-f2])
99 (define-key map "\eO6R" [C-S-f3])
100 (define-key map "\eO6S" [C-S-f4])
101 (define-key map "\e[15;6~" [C-S-f5])
102 (define-key map "\e[17;6~" [C-S-f6])
103 (define-key map "\e[18;6~" [C-S-f7])
104 (define-key map "\e[19;6~" [C-S-f8])
105 (define-key map "\e[20;6~" [C-S-f9])
106 (define-key map "\e[21;6~" [C-S-f10])
107 (define-key map "\e[23;6~" [C-S-f11])
108 (define-key map "\e[24;6~" [C-S-f12])
110 (define-key map "\eO3P" [M-f1])
111 (define-key map "\eO3Q" [M-f2])
112 (define-key map "\eO3R" [M-f3])
113 (define-key map "\eO3S" [M-f4])
114 (define-key map "\e[15;3~" [M-f5])
115 (define-key map "\e[17;3~" [M-f6])
116 (define-key map "\e[18;3~" [M-f7])
117 (define-key map "\e[19;3~" [M-f8])
118 (define-key map "\e[20;3~" [M-f9])
119 (define-key map "\e[21;3~" [M-f10])
120 (define-key map "\e[23;3~" [M-f11])
121 (define-key map "\e[24;3~" [M-f12])
123 (define-key map "\eO4P" [M-S-f1])
124 (define-key map "\eO4Q" [M-S-f2])
125 (define-key map "\eO4R" [M-S-f3])
126 (define-key map "\eO4S" [M-S-f4])
127 (define-key map "\e[15;4~" [M-S-f5])
128 (define-key map "\e[17;4~" [M-S-f6])
129 (define-key map "\e[18;4~" [M-S-f7])
130 (define-key map "\e[19;4~" [M-S-f8])
131 (define-key map "\e[20;4~" [M-S-f9])
132 (define-key map "\e[21;4~" [M-S-f10])
133 (define-key map "\e[23;4~" [M-S-f11])
134 (define-key map "\e[24;4~" [M-S-f12])
136 (define-key map "\eOA" [up])
137 (define-key map "\eOB" [down])
138 (define-key map "\eOC" [right])
139 (define-key map "\eOD" [left])
140 (define-key map "\eOF" [end])
141 (define-key map "\eOH" [home])
143 (define-key map "\e[1;2A" [S-up])
144 (define-key map "\e[1;2B" [S-down])
145 (define-key map "\e[1;2C" [S-right])
146 (define-key map "\e[1;2D" [S-left])
147 (define-key map "\e[1;2F" [S-end])
148 (define-key map "\e[1;2H" [S-home])
150 (define-key map "\e[1;4A" [M-S-up])
151 (define-key map "\e[1;4B" [M-S-down])
152 (define-key map "\e[1;4C" [M-S-right])
153 (define-key map "\e[1;4D" [M-S-left])
154 (define-key map "\e[1;4F" [M-S-end])
155 (define-key map "\e[1;4H" [M-S-home])
157 (define-key map "\e[1;5A" [C-up])
158 (define-key map "\e[1;5B" [C-down])
159 (define-key map "\e[1;5C" [C-right])
160 (define-key map "\e[1;5D" [C-left])
161 (define-key map "\e[1;5F" [C-end])
162 (define-key map "\e[1;5H" [C-home])
164 (define-key map "\e[1;6A" [C-S-up])
165 (define-key map "\e[1;6B" [C-S-down])
166 (define-key map "\e[1;6C" [C-S-right])
167 (define-key map "\e[1;6D" [C-S-left])
168 (define-key map "\e[1;6F" [C-S-end])
169 (define-key map "\e[1;6H" [C-S-home])
171 (define-key map "\e[1;7A" [C-M-up])
172 (define-key map "\e[1;7B" [C-M-down])
173 (define-key map "\e[1;7C" [C-M-right])
174 (define-key map "\e[1;7D" [C-M-left])
175 (define-key map "\e[1;7F" [C-M-end])
176 (define-key map "\e[1;7H" [C-M-home])
178 (define-key map "\e[1;8A" [C-M-S-up])
179 (define-key map "\e[1;8B" [C-M-S-down])
180 (define-key map "\e[1;8C" [C-M-S-right])
181 (define-key map "\e[1;8D" [C-M-S-left])
182 (define-key map "\e[1;8F" [C-M-S-end])
183 (define-key map "\e[1;8H" [C-M-S-home])
185 (define-key map "\e[1;3A" [M-up])
186 (define-key map "\e[1;3B" [M-down])
187 (define-key map "\e[1;3C" [M-right])
188 (define-key map "\e[1;3D" [M-left])
189 (define-key map "\e[1;3F" [M-end])
190 (define-key map "\e[1;3H" [M-home])
192 (define-key map "\e[2~" [insert])
193 (define-key map "\e[3~" [delete])
194 (define-key map "\e[5~" [prior])
195 (define-key map "\e[6~" [next])
197 (define-key map "\e[2;2~" [S-insert])
198 (define-key map "\e[3;2~" [S-delete])
199 (define-key map "\e[5;2~" [S-prior])
200 (define-key map "\e[6;2~" [S-next])
202 (define-key map "\e[2;4~" [M-S-insert])
203 (define-key map "\e[3;4~" [M-S-delete])
204 (define-key map "\e[5;4~" [M-S-prior])
205 (define-key map "\e[6;4~" [M-S-next])
207 (define-key map "\e[2;5~" [C-insert])
208 (define-key map "\e[3;5~" [C-delete])
209 (define-key map "\e[5;5~" [C-prior])
210 (define-key map "\e[6;5~" [C-next])
212 (define-key map "\e[2;6~" [C-S-insert])
213 (define-key map "\e[3;6~" [C-S-delete])
214 (define-key map "\e[5;6~" [C-S-prior])
215 (define-key map "\e[6;6~" [C-S-next])
217 (define-key map "\e[2;7~" [C-M-insert])
218 (define-key map "\e[3;7~" [C-M-delete])
219 (define-key map "\e[5;7~" [C-M-prior])
220 (define-key map "\e[6;7~" [C-M-next])
222 (define-key map "\e[2;8~" [C-M-S-insert])
223 (define-key map "\e[3;8~" [C-M-S-delete])
224 (define-key map "\e[5;8~" [C-M-S-prior])
225 (define-key map "\e[6;8~" [C-M-S-next])
227 (define-key map "\e[2;3~" [M-insert])
228 (define-key map "\e[3;3~" [M-delete])
229 (define-key map "\e[5;3~" [M-prior])
230 (define-key map "\e[6;3~" [M-next])
232 (define-key map "\e[4~" [select])
233 (define-key map "\e[29~" [print])
235 (define-key map "\eOj" [kp-multiply])
236 (define-key map "\eOk" [kp-add])
237 (define-key map "\eOl" [kp-separator])
238 (define-key map "\eOm" [kp-subtract])
239 (define-key map "\eOo" [kp-divide])
240 (define-key map "\eOp" [kp-0])
241 (define-key map "\eOq" [kp-1])
242 (define-key map "\eOr" [kp-2])
243 (define-key map "\eOs" [kp-3])
244 (define-key map "\eOt" [kp-4])
245 (define-key map "\eOu" [kp-5])
246 (define-key map "\eOv" [kp-6])
247 (define-key map "\eOw" [kp-7])
248 (define-key map "\eOx" [kp-8])
249 (define-key map "\eOy" [kp-9])
251 ;; These keys are available in xterm starting from version 216
252 ;; if the modifyOtherKeys resource is set to 1.
253 (dolist (bind '((5 9 [C-tab])
254 (5 13 [C-return])
255 (5 39 [?\C-\'])
256 (5 44 [?\C-,])
257 (5 45 [?\C--])
258 (5 46 [?\C-.])
259 (5 47 [?\C-/])
260 (5 48 [?\C-0])
261 (5 49 [?\C-1])
262 ;; Not all C-DIGIT keys have a distinct binding.
263 (5 57 [?\C-9])
264 (5 59 [?\C-\;])
265 (5 61 [?\C-=])
266 (5 92 [?\C-\\])
268 (6 33 [?\C-!])
269 (6 34 [?\C-\"])
270 (6 35 [?\C-#])
271 (6 36 [?\C-$])
272 (6 37 [?\C-%])
273 (6 38 [?\C-&])
274 (6 40 [?\C-\(])
275 (6 41 [?\C-\)])
276 (6 42 [?\C-*])
277 (6 43 [?\C-+])
278 (6 58 [?\C-:])
279 (6 60 [?\C-<])
280 (6 62 [?\C->])
281 (6 63 [(control ??)])
283 ;; These are the strings emitted for various C-M-
284 ;; combinations for keyboards whose Meta and Alt
285 ;; modifiers are on the same key (usually labeled "Alt").
286 (13 9 [C-M-tab])
287 (13 13 [C-M-return])
289 (13 39 [?\C-\M-\'])
290 (13 44 [?\C-\M-,])
291 (13 45 [?\C-\M--])
292 (13 46 [?\C-\M-.])
293 (13 47 [?\C-\M-/])
294 (13 48 [?\C-\M-0])
295 (13 49 [?\C-\M-1])
296 (13 50 [?\C-\M-2])
297 (13 51 [?\C-\M-3])
298 (13 52 [?\C-\M-4])
299 (13 53 [?\C-\M-5])
300 (13 54 [?\C-\M-6])
301 (13 55 [?\C-\M-7])
302 (13 56 [?\C-\M-8])
303 (13 57 [?\C-\M-9])
304 (13 59 [?\C-\M-\;])
305 (13 61 [?\C-\M-=])
306 (13 92 [?\C-\M-\\])
308 (14 33 [?\C-\M-!])
309 (14 34 [?\C-\M-\"])
310 (14 35 [?\C-\M-#])
311 (14 36 [?\C-\M-$])
312 (14 37 [?\C-\M-%])
313 (14 38 [?\C-\M-&])
314 (14 40 [?\C-\M-\(])
315 (14 41 [?\C-\M-\)])
316 (14 42 [?\C-\M-*])
317 (14 43 [?\C-\M-+])
318 (14 58 [?\C-\M-:])
319 (14 60 [?\C-\M-<])
320 (14 62 [?\C-\M->])
321 (14 63 [(control meta ??)])
323 (7 9 [C-M-tab])
324 (7 13 [C-M-return])
326 (7 32 [?\C-\M-\s])
327 (7 39 [?\C-\M-\'])
328 (7 44 [?\C-\M-,])
329 (7 45 [?\C-\M--])
330 (7 46 [?\C-\M-.])
331 (7 47 [?\C-\M-/])
332 (7 48 [?\C-\M-0])
333 (7 49 [?\C-\M-1])
334 (7 50 [?\C-\M-2])
335 (7 51 [?\C-\M-3])
336 (7 52 [?\C-\M-4])
337 (7 53 [?\C-\M-5])
338 (7 54 [?\C-\M-6])
339 (7 55 [?\C-\M-7])
340 (7 56 [?\C-\M-8])
341 (7 57 [?\C-\M-9])
342 (7 59 [?\C-\M-\;])
343 (7 61 [?\C-\M-=])
344 (7 92 [?\C-\M-\\])
346 (8 33 [?\C-\M-!])
347 (8 34 [?\C-\M-\"])
348 (8 35 [?\C-\M-#])
349 (8 36 [?\C-\M-$])
350 (8 37 [?\C-\M-%])
351 (8 38 [?\C-\M-&])
352 (8 40 [?\C-\M-\(])
353 (8 41 [?\C-\M-\)])
354 (8 42 [?\C-\M-*])
355 (8 43 [?\C-\M-+])
356 (8 58 [?\C-\M-:])
357 (8 60 [?\C-\M-<])
358 (8 62 [?\C-\M->])
359 (8 63 [(control meta ??)])
361 (2 9 [S-tab])
362 (2 13 [S-return])
364 (6 9 [C-S-tab])
365 (6 13 [C-S-return])))
366 (define-key map
367 (format "\e[27;%d;%d~" (nth 0 bind) (nth 1 bind)) (nth 2 bind))
368 ;; For formatOtherKeys=1, the sequence is a bit shorter (bug#13839).
369 (define-key map
370 (format "\e[%d;%du" (nth 1 bind) (nth 0 bind)) (nth 2 bind)))
372 ;; Other versions of xterm might emit these.
373 (define-key map "\e[A" [up])
374 (define-key map "\e[B" [down])
375 (define-key map "\e[C" [right])
376 (define-key map "\e[D" [left])
377 (define-key map "\e[1~" [home])
379 (define-key map "\eO2A" [S-up])
380 (define-key map "\eO2B" [S-down])
381 (define-key map "\eO2C" [S-right])
382 (define-key map "\eO2D" [S-left])
383 (define-key map "\eO2F" [S-end])
384 (define-key map "\eO2H" [S-home])
386 (define-key map "\eO5A" [C-up])
387 (define-key map "\eO5B" [C-down])
388 (define-key map "\eO5C" [C-right])
389 (define-key map "\eO5D" [C-left])
390 (define-key map "\eO5F" [C-end])
391 (define-key map "\eO5H" [C-home])
393 (define-key map "\e[11~" [f1])
394 (define-key map "\e[12~" [f2])
395 (define-key map "\e[13~" [f3])
396 (define-key map "\e[14~" [f4])
397 map)
398 "Function key map overrides for xterm.")
400 (defvar xterm-alternatives-map
401 (let ((map (make-sparse-keymap)))
402 ;; The terminal initialization C code file might have initialized
403 ;; function keys F13->F60 from the termcap/terminfo information.
404 ;; On a PC-style keyboard these keys correspond to
405 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The code
406 ;; here substitutes the corresponding definitions in function-key-map.
407 ;; The mapping from escape sequences to Fn is done in input-decode-map
408 ;; whereas this here mapping is done in local-function-key-map so that
409 ;; bindings to f45 still work, in case your keyboard really has an f45
410 ;; key rather than C-S-f9.
411 (define-key map [f13] [S-f1])
412 (define-key map [f14] [S-f2])
413 (define-key map [f15] [S-f3])
414 (define-key map [f16] [S-f4])
415 (define-key map [f17] [S-f5])
416 (define-key map [f18] [S-f6])
417 (define-key map [f19] [S-f7])
418 (define-key map [f20] [S-f8])
419 (define-key map [f21] [S-f9])
420 (define-key map [f22] [S-f10])
421 (define-key map [f23] [S-f11])
422 (define-key map [f24] [S-f12])
424 (define-key map [f25] [C-f1])
425 (define-key map [f26] [C-f2])
426 (define-key map [f27] [C-f3])
427 (define-key map [f28] [C-f4])
428 (define-key map [f29] [C-f5])
429 (define-key map [f30] [C-f6])
430 (define-key map [f31] [C-f7])
431 (define-key map [f32] [C-f8])
432 (define-key map [f33] [C-f9])
433 (define-key map [f34] [C-f10])
434 (define-key map [f35] [C-f11])
435 (define-key map [f36] [C-f12])
437 (define-key map [f37] [C-S-f1])
438 (define-key map [f38] [C-S-f2])
439 (define-key map [f39] [C-S-f3])
440 (define-key map [f40] [C-S-f4])
441 (define-key map [f41] [C-S-f5])
442 (define-key map [f42] [C-S-f6])
443 (define-key map [f43] [C-S-f7])
444 (define-key map [f44] [C-S-f8])
445 (define-key map [f45] [C-S-f9])
446 (define-key map [f46] [C-S-f10])
447 (define-key map [f47] [C-S-f11])
448 (define-key map [f48] [C-S-f12])
450 (define-key map [f49] [M-f1])
451 (define-key map [f50] [M-f2])
452 (define-key map [f51] [M-f3])
453 (define-key map [f52] [M-f4])
454 (define-key map [f53] [M-f5])
455 (define-key map [f54] [M-f6])
456 (define-key map [f55] [M-f7])
457 (define-key map [f56] [M-f8])
458 (define-key map [f57] [M-f9])
459 (define-key map [f58] [M-f10])
460 (define-key map [f59] [M-f11])
461 (define-key map [f60] [M-f12])
463 map)
464 "Keymap of possible alternative meanings for some keys.")
466 ;; List of terminals for which modify-other-keys has been turned on.
467 (defvar xterm-modify-other-keys-terminal-list nil)
469 (defun xterm--report-background-handler ()
470 (let ((str "")
471 chr)
472 ;; The reply should be: \e ] 11 ; rgb: NUMBER1 / NUMBER2 / NUMBER3 \e \\
473 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?\\)))
474 (setq str (concat str (string chr))))
475 (when (string-match
476 "rgb:\\([a-f0-9]+\\)/\\([a-f0-9]+\\)/\\([a-f0-9]+\\)" str)
477 (let ((recompute-faces
478 (xterm-maybe-set-dark-background-mode
479 (string-to-number (match-string 1 str) 16)
480 (string-to-number (match-string 2 str) 16)
481 (string-to-number (match-string 3 str) 16))))
483 ;; Recompute faces here in case the background mode was
484 ;; set to dark. We used to call
485 ;; `tty-set-up-initial-frame-faces' only once, but that
486 ;; caused the light background faces to be computed
487 ;; incorrectly. See:
488 ;; http://permalink.gmane.org/gmane.emacs.devel/119627
489 (when recompute-faces
490 (tty-set-up-initial-frame-faces))))))
492 (defun xterm--version-handler ()
493 (let ((str "")
494 chr)
495 ;; The reply should be: \e [ > NUMBER1 ; NUMBER2 ; NUMBER3 c
496 ;; If the timeout is completely removed for read-event, this
497 ;; might hang for terminals that pretend to be xterm, but don't
498 ;; respond to this escape sequence. RMS' opinion was to remove
499 ;; it completely. That might be right, but let's first try to
500 ;; see if by using a longer timeout we get rid of most issues.
501 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?c)))
502 (setq str (concat str (string chr))))
503 (when (string-match "0;\\([0-9]+\\);0" str)
504 (let ((version (string-to-number (match-string 1 str))))
505 ;; If version is 242 or higher, assume the xterm supports
506 ;; reporting the background color (TODO: maybe earlier
507 ;; versions do too...)
508 (when (>= version 242)
509 (xterm--query "\e]11;?\e\\"
510 '(("\e]11;" . xterm--report-background-handler))))
512 ;; If version is 216 (the version when modifyOtherKeys was
513 ;; introduced) or higher, initialize the
514 ;; modifyOtherKeys support.
515 (when (>= version 216)
516 (terminal-init-xterm-modify-other-keys))))))
518 (defun xterm--query (query handlers)
519 "Send QUERY string to the terminal and watch for a response.
520 HANDLERS is an alist with elements of the form (STRING . FUNCTION).
521 We run the first FUNCTION whose STRING matches the input events."
522 ;; We used to query synchronously, but the need to use `discard-input' is
523 ;; rather annoying (bug#6758). Maybe we could always use the asynchronous
524 ;; approach, but it's less tested.
525 ;; FIXME: Merge the two branches.
526 (if (input-pending-p)
527 (progn
528 (dolist (handler handlers)
529 (define-key input-decode-map (car handler)
530 (lambda (&optional _prompt)
531 ;; Unregister the handler, since we don't expect further answers.
532 (dolist (handler handlers)
533 (define-key input-decode-map (car handler) nil))
534 (funcall (cdr handler))
535 [])))
536 (send-string-to-terminal query))
537 ;; Pending input can be mistakenly returned by the calls to
538 ;; read-event below. Discard it.
539 (send-string-to-terminal query)
540 (while handlers
541 (let ((handler (pop handlers))
542 (i 0))
543 (while (and (< i (length (car handler)))
544 (let ((evt (read-event nil nil 2)))
545 (or (eq evt (aref (car handler) i))
546 (progn (if evt (push evt unread-command-events))
547 nil))))
548 (setq i (1+ i)))
549 (if (= i (length (car handler)))
550 (progn (setq handlers nil)
551 (funcall (cdr handler)))
552 (while (> i 0)
553 (push (aref (car handler) (setq i (1- i)))
554 unread-command-events)))))))
556 (defun terminal-init-xterm ()
557 "Terminal initialization function for xterm."
558 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
559 ;; rxvt's keybindings are incompatible with xterm's. It is
560 ;; better in that case to use rxvt's initialization function.
561 (if (and (getenv "COLORTERM" (selected-frame))
562 (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
563 (tty-run-terminal-initialization (selected-frame) "rxvt")
565 (let ((map (copy-keymap xterm-alternatives-map)))
566 (set-keymap-parent map (keymap-parent local-function-key-map))
567 (set-keymap-parent local-function-key-map map))
569 (let ((map (copy-keymap xterm-function-map)))
571 ;; Use inheritance to let the main keymap override those defaults.
572 ;; This way we don't override terminfo-derived settings or settings
573 ;; made in the init file.
574 (set-keymap-parent map (keymap-parent input-decode-map))
575 (set-keymap-parent input-decode-map map)))
577 (xterm-register-default-colors)
578 (tty-set-up-initial-frame-faces)
580 (if (eq xterm-extra-capabilities 'check)
581 ;; Try to find out the type of terminal by sending a "Secondary
582 ;; Device Attributes (DA)" query.
583 (xterm--query "\e[>0c"
584 ;; Some terminals (like OS X's Terminal.app) respond to
585 ;; this query as if it were a "Primary Device Attributes"
586 ;; query instead, so we should handle that too.
587 '(("\e[?" . xterm--version-handler)
588 ("\e[>" . xterm--version-handler)))
590 (when (memq 'reportBackground xterm-extra-capabilities)
591 (xterm--query "\e]11;?\e\\"
592 '(("\e]11;" . xterm--report-background-handler))))
594 (when (memq 'modifyOtherKeys xterm-extra-capabilities)
595 (terminal-init-xterm-modify-other-keys)))
597 (run-hooks 'terminal-init-xterm-hook))
599 (defun terminal-init-xterm-modify-other-keys ()
600 "Terminal initialization for xterm's modifyOtherKeys support."
601 ;; Make sure that the modifyOtherKeys state is restored when
602 ;; suspending, resuming and exiting.
603 (add-hook 'suspend-hook 'xterm-turn-off-modify-other-keys)
604 (add-hook 'suspend-resume-hook 'xterm-turn-on-modify-other-keys)
605 (add-hook 'kill-emacs-hook 'xterm-remove-modify-other-keys)
606 (add-hook 'delete-terminal-functions 'xterm-remove-modify-other-keys)
607 ;; Add the selected frame to the list of frames that
608 ;; need to deal with modify-other-keys.
609 (push (frame-terminal)
610 xterm-modify-other-keys-terminal-list)
611 (xterm-turn-on-modify-other-keys))
613 ;; Set up colors, for those versions of xterm that support it.
614 (defvar xterm-standard-colors
615 ;; The names in the comments taken from XTerm-col.ad in the xterm
616 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
617 ;; from rgb.txt.
618 '(("black" 0 ( 0 0 0)) ; black
619 ("red" 1 (205 0 0)) ; red3
620 ("green" 2 ( 0 205 0)) ; green3
621 ("yellow" 3 (205 205 0)) ; yellow3
622 ("blue" 4 ( 0 0 238)) ; blue2
623 ("magenta" 5 (205 0 205)) ; magenta3
624 ("cyan" 6 ( 0 205 205)) ; cyan3
625 ("white" 7 (229 229 229)) ; gray90
626 ("brightblack" 8 (127 127 127)) ; gray50
627 ("brightred" 9 (255 0 0)) ; red
628 ("brightgreen" 10 ( 0 255 0)) ; green
629 ("brightyellow" 11 (255 255 0)) ; yellow
630 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
631 ("brightmagenta" 13 (255 0 255)) ; magenta
632 ("brightcyan" 14 ( 0 255 255)) ; cyan
633 ("brightwhite" 15 (255 255 255))) ; white
634 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
636 (defun xterm-rgb-convert-to-16bit (prim)
637 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
638 (logior prim (lsh prim 8)))
640 (defun xterm-register-default-colors ()
641 "Register the default set of colors for xterm or compatible emulator.
643 This function registers the number of colors returned by `display-color-cells'
644 for the currently selected frame. The first 16 colors are taken from
645 `xterm-standard-colors', which see, while the rest are computed assuming
646 either the 88- or 256-color standard color scheme supported by latest
647 versions of xterm."
648 (let* ((ncolors (display-color-cells (selected-frame)))
649 (colors xterm-standard-colors)
650 (color (car colors)))
651 (if (> ncolors 0)
652 ;; Clear the 8 default tty colors registered by startup.el
653 (tty-color-clear))
654 ;; Only register as many colors as are supported by the display.
655 (while (and (> ncolors 0) colors)
656 (tty-color-define (car color) (cadr color)
657 (mapcar 'xterm-rgb-convert-to-16bit
658 (car (cddr color))))
659 (setq colors (cdr colors)
660 color (car colors)
661 ncolors (1- ncolors)))
662 ;; We've exhausted the colors from `xterm-standard-colors'. If there
663 ;; are more colors to support, compute them now.
664 (when (> ncolors 0)
665 (cond
666 ((= ncolors 240) ; 256-color xterm
667 ;; 216 non-gray colors first
668 (let ((r 0) (g 0) (b 0))
669 (while (> ncolors 24)
670 ;; This and other formulas taken from 256colres.pl and
671 ;; 88colres.pl in the xterm distribution.
672 (tty-color-define (format "color-%d" (- 256 ncolors))
673 (- 256 ncolors)
674 (mapcar 'xterm-rgb-convert-to-16bit
675 (list (if (zerop r) 0 (+ (* r 40) 55))
676 (if (zerop g) 0 (+ (* g 40) 55))
677 (if (zerop b) 0 (+ (* b 40) 55)))))
679 (setq b (1+ b))
680 (if (> b 5)
681 (setq g (1+ g)
682 b 0))
683 (if (> g 5)
684 (setq r (1+ r)
685 g 0))
686 (setq ncolors (1- ncolors))))
687 ;; Now the 24 gray colors
688 (while (> ncolors 0)
689 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
690 (tty-color-define (format "color-%d" (- 256 ncolors))
691 (- 256 ncolors)
692 (list color color color))
693 (setq ncolors (1- ncolors))))
694 ((= ncolors 72) ; 88-color xterm
695 ;; 64 non-gray colors
696 (let ((levels '(0 139 205 255))
697 (r 0) (g 0) (b 0))
698 (while (> ncolors 8)
699 (tty-color-define (format "color-%d" (- 88 ncolors))
700 (- 88 ncolors)
701 (mapcar 'xterm-rgb-convert-to-16bit
702 (list (nth r levels)
703 (nth g levels)
704 (nth b levels))))
705 (setq b (1+ b))
706 (if (> b 3)
707 (setq g (1+ g)
708 b 0))
709 (if (> g 3)
710 (setq r (1+ r)
711 g 0))
712 (setq ncolors (1- ncolors))))
713 ;; Now the 8 gray colors
714 (while (> ncolors 0)
715 (setq color (xterm-rgb-convert-to-16bit
716 (floor
717 (if (= ncolors 8)
718 46.36363636
719 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
720 (tty-color-define (format "color-%d" (- 88 ncolors))
721 (- 88 ncolors)
722 (list color color color))
723 (setq ncolors (1- ncolors))))
724 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
725 ;; Modifying color mappings means realized faces don't use the
726 ;; right colors, so clear them.
727 (clear-face-cache)))
729 (defun xterm-turn-on-modify-other-keys ()
730 "Turn the modifyOtherKeys feature of xterm back on."
731 (let ((terminal (frame-terminal)))
732 (when (and (terminal-live-p terminal)
733 (memq terminal xterm-modify-other-keys-terminal-list))
734 (send-string-to-terminal "\e[>4;1m" terminal))))
736 (defun xterm-turn-off-modify-other-keys (&optional frame)
737 "Temporarily turn off the modifyOtherKeys feature of xterm."
738 (let ((terminal (when frame (frame-terminal frame))))
739 (when (and (terminal-live-p terminal)
740 (memq terminal xterm-modify-other-keys-terminal-list))
741 (send-string-to-terminal "\e[>4m" terminal))))
743 (defun xterm-remove-modify-other-keys (&optional terminal)
744 "Turn off the modifyOtherKeys feature of xterm for good."
745 (setq terminal (or terminal (frame-terminal)))
746 (when (and (terminal-live-p terminal)
747 (memq terminal xterm-modify-other-keys-terminal-list))
748 (setq xterm-modify-other-keys-terminal-list
749 (delq terminal xterm-modify-other-keys-terminal-list))
750 (send-string-to-terminal "\e[>4m" terminal)))
752 (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
753 ;; Use the heuristic in `frame-set-background-mode' to decide if a
754 ;; frame is dark.
755 (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
756 (set-terminal-parameter nil 'background-mode 'dark)
759 (provide 'xterm)
761 ;;; xterm.el ends here