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