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