]> code.delx.au - gnu-emacs/blob - lisp/term/xterm.el
Update copyright year to 2015
[gnu-emacs] / lisp / term / xterm.el
1 ;;; xterm.el --- define function key sequences and standard colors for xterm -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1995, 2001-2015 Free Software Foundation, Inc.
4
5 ;; Author: FSF
6 ;; Keywords: terminals
7
8 ;; This file is part of GNU Emacs.
9
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.
14
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.
19
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/>.
22
23 ;;; Commentary:
24
25 ;;; Code:
26
27 (defgroup xterm nil
28 "XTerm support."
29 :version "24.1"
30 :group 'terminals)
31
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.
37
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))))
49
50 (defvar xterm-function-map
51 (let ((map (make-sparse-keymap)))
52
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])
66
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])
83
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])
96
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])
109
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])
122
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])
135
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])
142
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])
149
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])
156
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])
163
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])
170
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])
177
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])
184
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])
191
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])
196
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])
201
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])
206
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])
211
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])
216
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])
221
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])
226
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])
231
232 (define-key map "\e[4~" [select])
233 (define-key map "\e[29~" [print])
234
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])
250
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-\\])
267
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 ??)])
282
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])
288
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-\\])
307
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 ??)])
322
323 (7 9 [C-M-tab])
324 (7 13 [C-M-return])
325
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-\\])
345
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 ??)])
360
361 (2 9 [S-tab])
362 (2 13 [S-return])
363
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)))
371
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])
378
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])
385
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])
392
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.")
399
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])
423
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])
436
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])
449
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])
462
463 map)
464 "Keymap of possible alternative meanings for some keys.")
465
466 ;; List of terminals for which modify-other-keys has been turned on.
467 (defvar xterm-modify-other-keys-terminal-list nil)
468
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))))
482
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))))))
491
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 (when (equal (match-string 1 str) "83")
513 ;; `screen' (which returns 83;40003;0) seems to also lack support for
514 ;; some of these (bug#17607).
515 (setq version 240))
516 ;; If version is 242 or higher, assume the xterm supports
517 ;; reporting the background color (TODO: maybe earlier
518 ;; versions do too...)
519 (when (>= version 242)
520 (xterm--query "\e]11;?\e\\"
521 '(("\e]11;" . xterm--report-background-handler))))
522
523 ;; If version is 216 (the version when modifyOtherKeys was
524 ;; introduced) or higher, initialize the
525 ;; modifyOtherKeys support.
526 (when (>= version 216)
527 (terminal-init-xterm-modify-other-keys))))))
528
529 (defun xterm--query (query handlers)
530 "Send QUERY string to the terminal and watch for a response.
531 HANDLERS is an alist with elements of the form (STRING . FUNCTION).
532 We run the first FUNCTION whose STRING matches the input events."
533 ;; We used to query synchronously, but the need to use `discard-input' is
534 ;; rather annoying (bug#6758). Maybe we could always use the asynchronous
535 ;; approach, but it's less tested.
536 ;; FIXME: Merge the two branches.
537 (if (input-pending-p)
538 (progn
539 (dolist (handler handlers)
540 (define-key input-decode-map (car handler)
541 (lambda (&optional _prompt)
542 ;; Unregister the handler, since we don't expect further answers.
543 (dolist (handler handlers)
544 (define-key input-decode-map (car handler) nil))
545 (funcall (cdr handler))
546 [])))
547 (send-string-to-terminal query))
548 ;; Pending input can be mistakenly returned by the calls to
549 ;; read-event below. Discard it.
550 (send-string-to-terminal query)
551 (while handlers
552 (let ((handler (pop handlers))
553 (i 0))
554 (while (and (< i (length (car handler)))
555 (let ((evt (read-event nil nil 2)))
556 (or (eq evt (aref (car handler) i))
557 (progn (if evt (push evt unread-command-events))
558 nil))))
559 (setq i (1+ i)))
560 (if (= i (length (car handler)))
561 (progn (setq handlers nil)
562 (funcall (cdr handler)))
563 (while (> i 0)
564 (push (aref (car handler) (setq i (1- i)))
565 unread-command-events)))))))
566
567 (defun terminal-init-xterm ()
568 "Terminal initialization function for xterm."
569 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
570 ;; rxvt's keybindings are incompatible with xterm's. It is
571 ;; better in that case to use rxvt's initialization function.
572 (if (and (getenv "COLORTERM" (selected-frame))
573 (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
574 (tty-run-terminal-initialization (selected-frame) "rxvt")
575
576 (let ((map (copy-keymap xterm-alternatives-map)))
577 (set-keymap-parent map (keymap-parent local-function-key-map))
578 (set-keymap-parent local-function-key-map map))
579
580 (let ((map (copy-keymap xterm-function-map)))
581
582 ;; Use inheritance to let the main keymap override those defaults.
583 ;; This way we don't override terminfo-derived settings or settings
584 ;; made in the init file.
585 (set-keymap-parent map (keymap-parent input-decode-map))
586 (set-keymap-parent input-decode-map map)))
587
588 (xterm-register-default-colors)
589 (tty-set-up-initial-frame-faces)
590
591 (if (eq xterm-extra-capabilities 'check)
592 ;; Try to find out the type of terminal by sending a "Secondary
593 ;; Device Attributes (DA)" query.
594 (xterm--query "\e[>0c"
595 ;; Some terminals (like OS X's Terminal.app) respond to
596 ;; this query as if it were a "Primary Device Attributes"
597 ;; query instead, so we should handle that too.
598 '(("\e[?" . xterm--version-handler)
599 ("\e[>" . xterm--version-handler)))
600
601 (when (memq 'reportBackground xterm-extra-capabilities)
602 (xterm--query "\e]11;?\e\\"
603 '(("\e]11;" . xterm--report-background-handler))))
604
605 (when (memq 'modifyOtherKeys xterm-extra-capabilities)
606 (terminal-init-xterm-modify-other-keys)))
607
608 (run-hooks 'terminal-init-xterm-hook))
609
610 (defun terminal-init-xterm-modify-other-keys ()
611 "Terminal initialization for xterm's modifyOtherKeys support."
612 ;; Make sure that the modifyOtherKeys state is restored when
613 ;; suspending, resuming and exiting.
614 (add-hook 'suspend-hook 'xterm-turn-off-modify-other-keys)
615 (add-hook 'suspend-resume-hook 'xterm-turn-on-modify-other-keys)
616 (add-hook 'kill-emacs-hook 'xterm-remove-modify-other-keys)
617 (add-hook 'delete-terminal-functions 'xterm-remove-modify-other-keys)
618 ;; Add the selected frame to the list of frames that
619 ;; need to deal with modify-other-keys.
620 (push (frame-terminal)
621 xterm-modify-other-keys-terminal-list)
622 (xterm-turn-on-modify-other-keys))
623
624 ;; Set up colors, for those versions of xterm that support it.
625 (defvar xterm-standard-colors
626 ;; The names in the comments taken from XTerm-col.ad in the xterm
627 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
628 ;; from rgb.txt.
629 '(("black" 0 ( 0 0 0)) ; black
630 ("red" 1 (205 0 0)) ; red3
631 ("green" 2 ( 0 205 0)) ; green3
632 ("yellow" 3 (205 205 0)) ; yellow3
633 ("blue" 4 ( 0 0 238)) ; blue2
634 ("magenta" 5 (205 0 205)) ; magenta3
635 ("cyan" 6 ( 0 205 205)) ; cyan3
636 ("white" 7 (229 229 229)) ; gray90
637 ("brightblack" 8 (127 127 127)) ; gray50
638 ("brightred" 9 (255 0 0)) ; red
639 ("brightgreen" 10 ( 0 255 0)) ; green
640 ("brightyellow" 11 (255 255 0)) ; yellow
641 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
642 ("brightmagenta" 13 (255 0 255)) ; magenta
643 ("brightcyan" 14 ( 0 255 255)) ; cyan
644 ("brightwhite" 15 (255 255 255))) ; white
645 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
646
647 (defun xterm-rgb-convert-to-16bit (prim)
648 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
649 (logior prim (lsh prim 8)))
650
651 (defun xterm-register-default-colors ()
652 "Register the default set of colors for xterm or compatible emulator.
653
654 This function registers the number of colors returned by `display-color-cells'
655 for the currently selected frame. The first 16 colors are taken from
656 `xterm-standard-colors', which see, while the rest are computed assuming
657 either the 88- or 256-color standard color scheme supported by latest
658 versions of xterm."
659 (let* ((ncolors (display-color-cells (selected-frame)))
660 (colors xterm-standard-colors)
661 (color (car colors)))
662 (if (> ncolors 0)
663 ;; Clear the 8 default tty colors registered by startup.el
664 (tty-color-clear))
665 ;; Only register as many colors as are supported by the display.
666 (while (and (> ncolors 0) colors)
667 (tty-color-define (car color) (cadr color)
668 (mapcar 'xterm-rgb-convert-to-16bit
669 (car (cddr color))))
670 (setq colors (cdr colors)
671 color (car colors)
672 ncolors (1- ncolors)))
673 ;; We've exhausted the colors from `xterm-standard-colors'. If there
674 ;; are more colors to support, compute them now.
675 (when (> ncolors 0)
676 (cond
677 ((= ncolors 240) ; 256-color xterm
678 ;; 216 non-gray colors first
679 (let ((r 0) (g 0) (b 0))
680 (while (> ncolors 24)
681 ;; This and other formulas taken from 256colres.pl and
682 ;; 88colres.pl in the xterm distribution.
683 (tty-color-define (format "color-%d" (- 256 ncolors))
684 (- 256 ncolors)
685 (mapcar 'xterm-rgb-convert-to-16bit
686 (list (if (zerop r) 0 (+ (* r 40) 55))
687 (if (zerop g) 0 (+ (* g 40) 55))
688 (if (zerop b) 0 (+ (* b 40) 55)))))
689
690 (setq b (1+ b))
691 (if (> b 5)
692 (setq g (1+ g)
693 b 0))
694 (if (> g 5)
695 (setq r (1+ r)
696 g 0))
697 (setq ncolors (1- ncolors))))
698 ;; Now the 24 gray colors
699 (while (> ncolors 0)
700 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
701 (tty-color-define (format "color-%d" (- 256 ncolors))
702 (- 256 ncolors)
703 (list color color color))
704 (setq ncolors (1- ncolors))))
705 ((= ncolors 72) ; 88-color xterm
706 ;; 64 non-gray colors
707 (let ((levels '(0 139 205 255))
708 (r 0) (g 0) (b 0))
709 (while (> ncolors 8)
710 (tty-color-define (format "color-%d" (- 88 ncolors))
711 (- 88 ncolors)
712 (mapcar 'xterm-rgb-convert-to-16bit
713 (list (nth r levels)
714 (nth g levels)
715 (nth b levels))))
716 (setq b (1+ b))
717 (if (> b 3)
718 (setq g (1+ g)
719 b 0))
720 (if (> g 3)
721 (setq r (1+ r)
722 g 0))
723 (setq ncolors (1- ncolors))))
724 ;; Now the 8 gray colors
725 (while (> ncolors 0)
726 (setq color (xterm-rgb-convert-to-16bit
727 (floor
728 (if (= ncolors 8)
729 46.36363636
730 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
731 (tty-color-define (format "color-%d" (- 88 ncolors))
732 (- 88 ncolors)
733 (list color color color))
734 (setq ncolors (1- ncolors))))
735 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
736 ;; Modifying color mappings means realized faces don't use the
737 ;; right colors, so clear them.
738 (clear-face-cache)))
739
740 (defun xterm-turn-on-modify-other-keys ()
741 "Turn the modifyOtherKeys feature of xterm back on."
742 (let ((terminal (frame-terminal)))
743 (when (and (terminal-live-p terminal)
744 (memq terminal xterm-modify-other-keys-terminal-list))
745 (send-string-to-terminal "\e[>4;1m" terminal))))
746
747 (defun xterm-turn-off-modify-other-keys (&optional frame)
748 "Temporarily turn off the modifyOtherKeys feature of xterm."
749 (let ((terminal (when frame (frame-terminal frame))))
750 (when (and (terminal-live-p terminal)
751 (memq terminal xterm-modify-other-keys-terminal-list))
752 (send-string-to-terminal "\e[>4m" terminal))))
753
754 (defun xterm-remove-modify-other-keys (&optional terminal)
755 "Turn off the modifyOtherKeys feature of xterm for good."
756 (setq terminal (or terminal (frame-terminal)))
757 (when (and (terminal-live-p terminal)
758 (memq terminal xterm-modify-other-keys-terminal-list))
759 (setq xterm-modify-other-keys-terminal-list
760 (delq terminal xterm-modify-other-keys-terminal-list))
761 (send-string-to-terminal "\e[>4m" terminal)))
762
763 (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
764 ;; Use the heuristic in `frame-set-background-mode' to decide if a
765 ;; frame is dark.
766 (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
767 (set-terminal-parameter nil 'background-mode 'dark)
768 t))
769
770 (provide 'xterm)
771
772 ;;; xterm.el ends here