Fix bug #17588 with mouse highlight when an X frame is redisplayed frequently.
[emacs.git] / lisp / term / xterm.el
blob52487f3bdedb49ba05e2c552aac1adcb9518b9f0
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 ;; Since xterm-280, the terminal type (NUMBER1) is now 41 instead of 0.
504 (when (string-match "\\([0-9]+\\);\\([0-9]+\\);0" str)
505 (let ((version (string-to-number (match-string 2 str))))
506 (when (and (> version 2000) (equal (match-string 1 str) "1"))
507 ;; Hack attack! bug#16988: gnome-terminal reports "1;NNNN;0"
508 ;; with a large NNNN but is based on a rather old xterm code.
509 ;; Gnome terminal 3.6.1 reports 1;3406;0
510 ;; Gnome terminal 2.32.1 reports 1;2802;0
511 (setq version 200))
512 ;; If version is 242 or higher, assume the xterm supports
513 ;; reporting the background color (TODO: maybe earlier
514 ;; versions do too...)
515 (when (>= version 242)
516 (xterm--query "\e]11;?\e\\"
517 '(("\e]11;" . xterm--report-background-handler))))
519 ;; If version is 216 (the version when modifyOtherKeys was
520 ;; introduced) or higher, initialize the
521 ;; modifyOtherKeys support.
522 (when (>= version 216)
523 (terminal-init-xterm-modify-other-keys))))))
525 (defun xterm--query (query handlers)
526 "Send QUERY string to the terminal and watch for a response.
527 HANDLERS is an alist with elements of the form (STRING . FUNCTION).
528 We run the first FUNCTION whose STRING matches the input events."
529 ;; We used to query synchronously, but the need to use `discard-input' is
530 ;; rather annoying (bug#6758). Maybe we could always use the asynchronous
531 ;; approach, but it's less tested.
532 ;; FIXME: Merge the two branches.
533 (if (input-pending-p)
534 (progn
535 (dolist (handler handlers)
536 (define-key input-decode-map (car handler)
537 (lambda (&optional _prompt)
538 ;; Unregister the handler, since we don't expect further answers.
539 (dolist (handler handlers)
540 (define-key input-decode-map (car handler) nil))
541 (funcall (cdr handler))
542 [])))
543 (send-string-to-terminal query))
544 ;; Pending input can be mistakenly returned by the calls to
545 ;; read-event below. Discard it.
546 (send-string-to-terminal query)
547 (while handlers
548 (let ((handler (pop handlers))
549 (i 0))
550 (while (and (< i (length (car handler)))
551 (let ((evt (read-event nil nil 2)))
552 (or (eq evt (aref (car handler) i))
553 (progn (if evt (push evt unread-command-events))
554 nil))))
555 (setq i (1+ i)))
556 (if (= i (length (car handler)))
557 (progn (setq handlers nil)
558 (funcall (cdr handler)))
559 (while (> i 0)
560 (push (aref (car handler) (setq i (1- i)))
561 unread-command-events)))))))
563 (defun terminal-init-xterm ()
564 "Terminal initialization function for xterm."
565 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
566 ;; rxvt's keybindings are incompatible with xterm's. It is
567 ;; better in that case to use rxvt's initialization function.
568 (if (and (getenv "COLORTERM" (selected-frame))
569 (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
570 (tty-run-terminal-initialization (selected-frame) "rxvt")
572 (let ((map (copy-keymap xterm-alternatives-map)))
573 (set-keymap-parent map (keymap-parent local-function-key-map))
574 (set-keymap-parent local-function-key-map map))
576 (let ((map (copy-keymap xterm-function-map)))
578 ;; Use inheritance to let the main keymap override those defaults.
579 ;; This way we don't override terminfo-derived settings or settings
580 ;; made in the init file.
581 (set-keymap-parent map (keymap-parent input-decode-map))
582 (set-keymap-parent input-decode-map map)))
584 (xterm-register-default-colors)
585 (tty-set-up-initial-frame-faces)
587 (if (eq xterm-extra-capabilities 'check)
588 ;; Try to find out the type of terminal by sending a "Secondary
589 ;; Device Attributes (DA)" query.
590 (xterm--query "\e[>0c"
591 ;; Some terminals (like OS X's Terminal.app) respond to
592 ;; this query as if it were a "Primary Device Attributes"
593 ;; query instead, so we should handle that too.
594 '(("\e[?" . xterm--version-handler)
595 ("\e[>" . xterm--version-handler)))
597 (when (memq 'reportBackground xterm-extra-capabilities)
598 (xterm--query "\e]11;?\e\\"
599 '(("\e]11;" . xterm--report-background-handler))))
601 (when (memq 'modifyOtherKeys xterm-extra-capabilities)
602 (terminal-init-xterm-modify-other-keys)))
604 (run-hooks 'terminal-init-xterm-hook))
606 (defun terminal-init-xterm-modify-other-keys ()
607 "Terminal initialization for xterm's modifyOtherKeys support."
608 ;; Make sure that the modifyOtherKeys state is restored when
609 ;; suspending, resuming and exiting.
610 (add-hook 'suspend-hook 'xterm-turn-off-modify-other-keys)
611 (add-hook 'suspend-resume-hook 'xterm-turn-on-modify-other-keys)
612 (add-hook 'kill-emacs-hook 'xterm-remove-modify-other-keys)
613 (add-hook 'delete-terminal-functions 'xterm-remove-modify-other-keys)
614 ;; Add the selected frame to the list of frames that
615 ;; need to deal with modify-other-keys.
616 (push (frame-terminal)
617 xterm-modify-other-keys-terminal-list)
618 (xterm-turn-on-modify-other-keys))
620 ;; Set up colors, for those versions of xterm that support it.
621 (defvar xterm-standard-colors
622 ;; The names in the comments taken from XTerm-col.ad in the xterm
623 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
624 ;; from rgb.txt.
625 '(("black" 0 ( 0 0 0)) ; black
626 ("red" 1 (205 0 0)) ; red3
627 ("green" 2 ( 0 205 0)) ; green3
628 ("yellow" 3 (205 205 0)) ; yellow3
629 ("blue" 4 ( 0 0 238)) ; blue2
630 ("magenta" 5 (205 0 205)) ; magenta3
631 ("cyan" 6 ( 0 205 205)) ; cyan3
632 ("white" 7 (229 229 229)) ; gray90
633 ("brightblack" 8 (127 127 127)) ; gray50
634 ("brightred" 9 (255 0 0)) ; red
635 ("brightgreen" 10 ( 0 255 0)) ; green
636 ("brightyellow" 11 (255 255 0)) ; yellow
637 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
638 ("brightmagenta" 13 (255 0 255)) ; magenta
639 ("brightcyan" 14 ( 0 255 255)) ; cyan
640 ("brightwhite" 15 (255 255 255))) ; white
641 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
643 (defun xterm-rgb-convert-to-16bit (prim)
644 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
645 (logior prim (lsh prim 8)))
647 (defun xterm-register-default-colors ()
648 "Register the default set of colors for xterm or compatible emulator.
650 This function registers the number of colors returned by `display-color-cells'
651 for the currently selected frame. The first 16 colors are taken from
652 `xterm-standard-colors', which see, while the rest are computed assuming
653 either the 88- or 256-color standard color scheme supported by latest
654 versions of xterm."
655 (let* ((ncolors (display-color-cells (selected-frame)))
656 (colors xterm-standard-colors)
657 (color (car colors)))
658 (if (> ncolors 0)
659 ;; Clear the 8 default tty colors registered by startup.el
660 (tty-color-clear))
661 ;; Only register as many colors as are supported by the display.
662 (while (and (> ncolors 0) colors)
663 (tty-color-define (car color) (cadr color)
664 (mapcar 'xterm-rgb-convert-to-16bit
665 (car (cddr color))))
666 (setq colors (cdr colors)
667 color (car colors)
668 ncolors (1- ncolors)))
669 ;; We've exhausted the colors from `xterm-standard-colors'. If there
670 ;; are more colors to support, compute them now.
671 (when (> ncolors 0)
672 (cond
673 ((= ncolors 240) ; 256-color xterm
674 ;; 216 non-gray colors first
675 (let ((r 0) (g 0) (b 0))
676 (while (> ncolors 24)
677 ;; This and other formulas taken from 256colres.pl and
678 ;; 88colres.pl in the xterm distribution.
679 (tty-color-define (format "color-%d" (- 256 ncolors))
680 (- 256 ncolors)
681 (mapcar 'xterm-rgb-convert-to-16bit
682 (list (if (zerop r) 0 (+ (* r 40) 55))
683 (if (zerop g) 0 (+ (* g 40) 55))
684 (if (zerop b) 0 (+ (* b 40) 55)))))
686 (setq b (1+ b))
687 (if (> b 5)
688 (setq g (1+ g)
689 b 0))
690 (if (> g 5)
691 (setq r (1+ r)
692 g 0))
693 (setq ncolors (1- ncolors))))
694 ;; Now the 24 gray colors
695 (while (> ncolors 0)
696 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
697 (tty-color-define (format "color-%d" (- 256 ncolors))
698 (- 256 ncolors)
699 (list color color color))
700 (setq ncolors (1- ncolors))))
701 ((= ncolors 72) ; 88-color xterm
702 ;; 64 non-gray colors
703 (let ((levels '(0 139 205 255))
704 (r 0) (g 0) (b 0))
705 (while (> ncolors 8)
706 (tty-color-define (format "color-%d" (- 88 ncolors))
707 (- 88 ncolors)
708 (mapcar 'xterm-rgb-convert-to-16bit
709 (list (nth r levels)
710 (nth g levels)
711 (nth b levels))))
712 (setq b (1+ b))
713 (if (> b 3)
714 (setq g (1+ g)
715 b 0))
716 (if (> g 3)
717 (setq r (1+ r)
718 g 0))
719 (setq ncolors (1- ncolors))))
720 ;; Now the 8 gray colors
721 (while (> ncolors 0)
722 (setq color (xterm-rgb-convert-to-16bit
723 (floor
724 (if (= ncolors 8)
725 46.36363636
726 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
727 (tty-color-define (format "color-%d" (- 88 ncolors))
728 (- 88 ncolors)
729 (list color color color))
730 (setq ncolors (1- ncolors))))
731 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
732 ;; Modifying color mappings means realized faces don't use the
733 ;; right colors, so clear them.
734 (clear-face-cache)))
736 (defun xterm-turn-on-modify-other-keys ()
737 "Turn the modifyOtherKeys feature of xterm back on."
738 (let ((terminal (frame-terminal)))
739 (when (and (terminal-live-p terminal)
740 (memq terminal xterm-modify-other-keys-terminal-list))
741 (send-string-to-terminal "\e[>4;1m" terminal))))
743 (defun xterm-turn-off-modify-other-keys (&optional frame)
744 "Temporarily turn off the modifyOtherKeys feature of xterm."
745 (let ((terminal (when frame (frame-terminal frame))))
746 (when (and (terminal-live-p terminal)
747 (memq terminal xterm-modify-other-keys-terminal-list))
748 (send-string-to-terminal "\e[>4m" terminal))))
750 (defun xterm-remove-modify-other-keys (&optional terminal)
751 "Turn off the modifyOtherKeys feature of xterm for good."
752 (setq terminal (or terminal (frame-terminal)))
753 (when (and (terminal-live-p terminal)
754 (memq terminal xterm-modify-other-keys-terminal-list))
755 (setq xterm-modify-other-keys-terminal-list
756 (delq terminal xterm-modify-other-keys-terminal-list))
757 (send-string-to-terminal "\e[>4m" terminal)))
759 (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
760 ;; Use the heuristic in `frame-set-background-mode' to decide if a
761 ;; frame is dark.
762 (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
763 (set-terminal-parameter nil 'background-mode 'dark)
766 (provide 'xterm)
768 ;;; xterm.el ends here