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