Merge from origin/emacs-24
[emacs.git] / lisp / term / xterm.el
blob1d085017214e6cbb2d4fe2cdd323fae7b80d98de
1 ;;; xterm.el --- define function key sequences and standard colors for xterm -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: FSF
6 ;; Keywords: terminals
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software: you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation, either version 3 of the License, or
13 ;; (at your option) any later version.
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;;; Code:
27 (defgroup xterm nil
28 "XTerm support."
29 :version "24.1"
30 :group 'terminals)
32 (defcustom xterm-extra-capabilities 'check
33 "Whether Xterm supports some additional, more modern, features.
34 If nil, just assume that it does not.
35 If `check', try to check if it does.
36 If a list, assume that the listed features are supported, without checking.
38 The relevant features are:
39 modifyOtherKeys -- if supported, more key bindings work (e.g., \"\\C-,\")
40 reportBackground -- if supported, Xterm reports its background color"
41 :version "24.1"
42 :group 'xterm
43 :type '(choice (const :tag "No" nil)
44 (const :tag "Check" check)
45 ;; NOTE: If you add entries here, make sure to update
46 ;; `terminal-init-xterm' as well.
47 (set (const :tag "modifyOtherKeys support" modifyOtherKeys)
48 (const :tag "report background" reportBackground))))
50 (defconst xterm-paste-ending-sequence "\e[201~"
51 "Characters send by the terminal to end a bracketed paste.")
53 (defun xterm-paste ()
54 "Handle the start of a terminal paste operation."
55 (interactive)
56 (let* ((end-marker-length (length xterm-paste-ending-sequence))
57 (pasted-text (with-temp-buffer
58 (set-buffer-multibyte nil)
59 (while (not (search-backward
60 xterm-paste-ending-sequence
61 (- (point) end-marker-length) t))
62 (let ((event (read-event
63 nil nil
64 ;; Use finite timeout to avoid
65 ;; glomming the event onto
66 ;; this-command-keys.
67 most-positive-fixnum)))
68 (when (eql event ?\r)
69 (setf event ?\n))
70 (insert event)))
71 (let ((last-coding-system-used))
72 (decode-coding-region
73 (point-min) (point)
74 (keyboard-coding-system) t))))
75 (interprogram-paste-function (lambda () pasted-text)))
76 (yank)))
78 (define-key global-map [xterm-paste] #'xterm-paste)
80 (defvar xterm-function-map
81 (let ((map (make-sparse-keymap)))
83 ;; xterm from X.org 6.8.2 uses these key definitions.
84 (define-key map "\eOP" [f1])
85 (define-key map "\eOQ" [f2])
86 (define-key map "\eOR" [f3])
87 (define-key map "\eOS" [f4])
88 (define-key map "\e[15~" [f5])
89 (define-key map "\e[17~" [f6])
90 (define-key map "\e[18~" [f7])
91 (define-key map "\e[19~" [f8])
92 (define-key map "\e[20~" [f9])
93 (define-key map "\e[21~" [f10])
94 (define-key map "\e[23~" [f11])
95 (define-key map "\e[24~" [f12])
97 (define-key map "\eO2P" [S-f1])
98 (define-key map "\eO2Q" [S-f2])
99 (define-key map "\eO2R" [S-f3])
100 (define-key map "\eO2S" [S-f4])
101 (define-key map "\e[1;2P" [S-f1])
102 (define-key map "\e[1;2Q" [S-f2])
103 (define-key map "\e[1;2R" [S-f3])
104 (define-key map "\e[1;2S" [S-f4])
105 (define-key map "\e[15;2~" [S-f5])
106 (define-key map "\e[17;2~" [S-f6])
107 (define-key map "\e[18;2~" [S-f7])
108 (define-key map "\e[19;2~" [S-f8])
109 (define-key map "\e[20;2~" [S-f9])
110 (define-key map "\e[21;2~" [S-f10])
111 (define-key map "\e[23;2~" [S-f11])
112 (define-key map "\e[24;2~" [S-f12])
114 (define-key map "\eO5P" [C-f1])
115 (define-key map "\eO5Q" [C-f2])
116 (define-key map "\eO5R" [C-f3])
117 (define-key map "\eO5S" [C-f4])
118 (define-key map "\e[15;5~" [C-f5])
119 (define-key map "\e[17;5~" [C-f6])
120 (define-key map "\e[18;5~" [C-f7])
121 (define-key map "\e[19;5~" [C-f8])
122 (define-key map "\e[20;5~" [C-f9])
123 (define-key map "\e[21;5~" [C-f10])
124 (define-key map "\e[23;5~" [C-f11])
125 (define-key map "\e[24;5~" [C-f12])
127 (define-key map "\eO6P" [C-S-f1])
128 (define-key map "\eO6Q" [C-S-f2])
129 (define-key map "\eO6R" [C-S-f3])
130 (define-key map "\eO6S" [C-S-f4])
131 (define-key map "\e[15;6~" [C-S-f5])
132 (define-key map "\e[17;6~" [C-S-f6])
133 (define-key map "\e[18;6~" [C-S-f7])
134 (define-key map "\e[19;6~" [C-S-f8])
135 (define-key map "\e[20;6~" [C-S-f9])
136 (define-key map "\e[21;6~" [C-S-f10])
137 (define-key map "\e[23;6~" [C-S-f11])
138 (define-key map "\e[24;6~" [C-S-f12])
140 (define-key map "\eO3P" [M-f1])
141 (define-key map "\eO3Q" [M-f2])
142 (define-key map "\eO3R" [M-f3])
143 (define-key map "\eO3S" [M-f4])
144 (define-key map "\e[15;3~" [M-f5])
145 (define-key map "\e[17;3~" [M-f6])
146 (define-key map "\e[18;3~" [M-f7])
147 (define-key map "\e[19;3~" [M-f8])
148 (define-key map "\e[20;3~" [M-f9])
149 (define-key map "\e[21;3~" [M-f10])
150 (define-key map "\e[23;3~" [M-f11])
151 (define-key map "\e[24;3~" [M-f12])
153 (define-key map "\eO4P" [M-S-f1])
154 (define-key map "\eO4Q" [M-S-f2])
155 (define-key map "\eO4R" [M-S-f3])
156 (define-key map "\eO4S" [M-S-f4])
157 (define-key map "\e[15;4~" [M-S-f5])
158 (define-key map "\e[17;4~" [M-S-f6])
159 (define-key map "\e[18;4~" [M-S-f7])
160 (define-key map "\e[19;4~" [M-S-f8])
161 (define-key map "\e[20;4~" [M-S-f9])
162 (define-key map "\e[21;4~" [M-S-f10])
163 (define-key map "\e[23;4~" [M-S-f11])
164 (define-key map "\e[24;4~" [M-S-f12])
166 (define-key map "\eOA" [up])
167 (define-key map "\eOB" [down])
168 (define-key map "\eOC" [right])
169 (define-key map "\eOD" [left])
170 (define-key map "\eOF" [end])
171 (define-key map "\eOH" [home])
173 (define-key map "\e[1;2A" [S-up])
174 (define-key map "\e[1;2B" [S-down])
175 (define-key map "\e[1;2C" [S-right])
176 (define-key map "\e[1;2D" [S-left])
177 (define-key map "\e[1;2F" [S-end])
178 (define-key map "\e[1;2H" [S-home])
180 (define-key map "\e[1;4A" [M-S-up])
181 (define-key map "\e[1;4B" [M-S-down])
182 (define-key map "\e[1;4C" [M-S-right])
183 (define-key map "\e[1;4D" [M-S-left])
184 (define-key map "\e[1;4F" [M-S-end])
185 (define-key map "\e[1;4H" [M-S-home])
187 (define-key map "\e[1;5A" [C-up])
188 (define-key map "\e[1;5B" [C-down])
189 (define-key map "\e[1;5C" [C-right])
190 (define-key map "\e[1;5D" [C-left])
191 (define-key map "\e[1;5F" [C-end])
192 (define-key map "\e[1;5H" [C-home])
194 (define-key map "\e[1;6A" [C-S-up])
195 (define-key map "\e[1;6B" [C-S-down])
196 (define-key map "\e[1;6C" [C-S-right])
197 (define-key map "\e[1;6D" [C-S-left])
198 (define-key map "\e[1;6F" [C-S-end])
199 (define-key map "\e[1;6H" [C-S-home])
201 (define-key map "\e[1;7A" [C-M-up])
202 (define-key map "\e[1;7B" [C-M-down])
203 (define-key map "\e[1;7C" [C-M-right])
204 (define-key map "\e[1;7D" [C-M-left])
205 (define-key map "\e[1;7F" [C-M-end])
206 (define-key map "\e[1;7H" [C-M-home])
208 (define-key map "\e[1;8A" [C-M-S-up])
209 (define-key map "\e[1;8B" [C-M-S-down])
210 (define-key map "\e[1;8C" [C-M-S-right])
211 (define-key map "\e[1;8D" [C-M-S-left])
212 (define-key map "\e[1;8F" [C-M-S-end])
213 (define-key map "\e[1;8H" [C-M-S-home])
215 (define-key map "\e[1;3A" [M-up])
216 (define-key map "\e[1;3B" [M-down])
217 (define-key map "\e[1;3C" [M-right])
218 (define-key map "\e[1;3D" [M-left])
219 (define-key map "\e[1;3F" [M-end])
220 (define-key map "\e[1;3H" [M-home])
222 (define-key map "\e[2~" [insert])
223 (define-key map "\e[3~" [delete])
224 (define-key map "\e[5~" [prior])
225 (define-key map "\e[6~" [next])
227 (define-key map "\e[2;2~" [S-insert])
228 (define-key map "\e[3;2~" [S-delete])
229 (define-key map "\e[5;2~" [S-prior])
230 (define-key map "\e[6;2~" [S-next])
232 (define-key map "\e[2;4~" [M-S-insert])
233 (define-key map "\e[3;4~" [M-S-delete])
234 (define-key map "\e[5;4~" [M-S-prior])
235 (define-key map "\e[6;4~" [M-S-next])
237 (define-key map "\e[2;5~" [C-insert])
238 (define-key map "\e[3;5~" [C-delete])
239 (define-key map "\e[5;5~" [C-prior])
240 (define-key map "\e[6;5~" [C-next])
242 (define-key map "\e[2;6~" [C-S-insert])
243 (define-key map "\e[3;6~" [C-S-delete])
244 (define-key map "\e[5;6~" [C-S-prior])
245 (define-key map "\e[6;6~" [C-S-next])
247 (define-key map "\e[2;7~" [C-M-insert])
248 (define-key map "\e[3;7~" [C-M-delete])
249 (define-key map "\e[5;7~" [C-M-prior])
250 (define-key map "\e[6;7~" [C-M-next])
252 (define-key map "\e[2;8~" [C-M-S-insert])
253 (define-key map "\e[3;8~" [C-M-S-delete])
254 (define-key map "\e[5;8~" [C-M-S-prior])
255 (define-key map "\e[6;8~" [C-M-S-next])
257 (define-key map "\e[2;3~" [M-insert])
258 (define-key map "\e[3;3~" [M-delete])
259 (define-key map "\e[5;3~" [M-prior])
260 (define-key map "\e[6;3~" [M-next])
262 (define-key map "\e[4~" [select])
263 (define-key map "\e[29~" [print])
265 (define-key map "\eOj" [kp-multiply])
266 (define-key map "\eOk" [kp-add])
267 (define-key map "\eOl" [kp-separator])
268 (define-key map "\eOm" [kp-subtract])
269 (define-key map "\eOo" [kp-divide])
270 (define-key map "\eOp" [kp-0])
271 (define-key map "\eOq" [kp-1])
272 (define-key map "\eOr" [kp-2])
273 (define-key map "\eOs" [kp-3])
274 (define-key map "\eOt" [kp-4])
275 (define-key map "\eOu" [kp-5])
276 (define-key map "\eOv" [kp-6])
277 (define-key map "\eOw" [kp-7])
278 (define-key map "\eOx" [kp-8])
279 (define-key map "\eOy" [kp-9])
281 (define-key map "\eO2j" [S-kp-multiply])
282 (define-key map "\eO2k" [S-kp-add])
283 (define-key map "\eO2l" [S-kp-separator])
284 (define-key map "\eO2m" [S-kp-subtract])
285 (define-key map "\eO2o" [S-kp-divide])
286 (define-key map "\eO2p" [S-kp-0])
287 (define-key map "\eO2q" [S-kp-1])
288 (define-key map "\eO2r" [S-kp-2])
289 (define-key map "\eO2s" [S-kp-3])
290 (define-key map "\eO2t" [S-kp-4])
291 (define-key map "\eO2u" [S-kp-5])
292 (define-key map "\eO2v" [S-kp-6])
293 (define-key map "\eO2w" [S-kp-7])
294 (define-key map "\eO2x" [S-kp-8])
295 (define-key map "\eO2y" [S-kp-9])
297 (define-key map "\eO4j" [M-S-kp-multiply])
298 (define-key map "\eO4k" [M-S-kp-add])
299 (define-key map "\eO4l" [M-S-kp-separator])
300 (define-key map "\eO4m" [M-S-kp-subtract])
301 (define-key map "\eO4o" [M-S-kp-divide])
302 (define-key map "\eO4p" [M-S-kp-0])
303 (define-key map "\eO4q" [M-S-kp-1])
304 (define-key map "\eO4r" [M-S-kp-2])
305 (define-key map "\eO4s" [M-S-kp-3])
306 (define-key map "\eO4t" [M-S-kp-4])
307 (define-key map "\eO4u" [M-S-kp-5])
308 (define-key map "\eO4v" [M-S-kp-6])
309 (define-key map "\eO4w" [M-S-kp-7])
310 (define-key map "\eO4x" [M-S-kp-8])
311 (define-key map "\eO4y" [M-S-kp-9])
313 (define-key map "\eO6j" [C-S-kp-multiply])
314 (define-key map "\eO6k" [C-S-kp-add])
315 (define-key map "\eO6l" [C-S-kp-separator])
316 (define-key map "\eO6m" [C-S-kp-subtract])
317 (define-key map "\eO6o" [C-S-kp-divide])
318 (define-key map "\eO6p" [C-S-kp-0])
319 (define-key map "\eO6q" [C-S-kp-1])
320 (define-key map "\eO6r" [C-S-kp-2])
321 (define-key map "\eO6s" [C-S-kp-3])
322 (define-key map "\eO6t" [C-S-kp-4])
323 (define-key map "\eO6u" [C-S-kp-5])
324 (define-key map "\eO6v" [C-S-kp-6])
325 (define-key map "\eO6w" [C-S-kp-7])
326 (define-key map "\eO6x" [C-S-kp-8])
327 (define-key map "\eO6y" [C-S-kp-9])
329 (define-key map "\eO8j" [C-M-S-kp-multiply])
330 (define-key map "\eO8k" [C-M-S-kp-add])
331 (define-key map "\eO8l" [C-M-S-kp-separator])
332 (define-key map "\eO8m" [C-M-S-kp-subtract])
333 (define-key map "\eO8o" [C-M-S-kp-divide])
334 (define-key map "\eO8p" [C-M-S-kp-0])
335 (define-key map "\eO8q" [C-M-S-kp-1])
336 (define-key map "\eO8r" [C-M-S-kp-2])
337 (define-key map "\eO8s" [C-M-S-kp-3])
338 (define-key map "\eO8t" [C-M-S-kp-4])
339 (define-key map "\eO8u" [C-M-S-kp-5])
340 (define-key map "\eO8v" [C-M-S-kp-6])
341 (define-key map "\eO8w" [C-M-S-kp-7])
342 (define-key map "\eO8x" [C-M-S-kp-8])
343 (define-key map "\eO8y" [C-M-S-kp-9])
345 ;; These keys are available in xterm starting from version 216
346 ;; if the modifyOtherKeys resource is set to 1.
347 (dolist (bind '((5 9 [C-tab])
348 (5 13 [C-return])
349 (5 39 [?\C-\'])
350 (5 44 [?\C-,])
351 (5 45 [?\C--])
352 (5 46 [?\C-.])
353 (5 47 [?\C-/])
354 (5 48 [?\C-0])
355 (5 49 [?\C-1])
356 ;; Not all C-DIGIT keys have a distinct binding.
357 (5 57 [?\C-9])
358 (5 59 [?\C-\;])
359 (5 61 [?\C-=])
360 (5 92 [?\C-\\])
362 (6 33 [?\C-!])
363 (6 34 [?\C-\"])
364 (6 35 [?\C-#])
365 (6 36 [?\C-$])
366 (6 37 [?\C-%])
367 (6 38 [?\C-&])
368 (6 40 [?\C-\(])
369 (6 41 [?\C-\)])
370 (6 42 [?\C-*])
371 (6 43 [?\C-+])
372 (6 58 [?\C-:])
373 (6 60 [?\C-<])
374 (6 62 [?\C->])
375 (6 63 [(control ??)])
377 ;; These are the strings emitted for various C-M-
378 ;; combinations for keyboards whose Meta and Alt
379 ;; modifiers are on the same key (usually labeled "Alt").
380 (13 9 [C-M-tab])
381 (13 13 [C-M-return])
383 (13 39 [?\C-\M-\'])
384 (13 44 [?\C-\M-,])
385 (13 45 [?\C-\M--])
386 (13 46 [?\C-\M-.])
387 (13 47 [?\C-\M-/])
388 (13 48 [?\C-\M-0])
389 (13 49 [?\C-\M-1])
390 (13 50 [?\C-\M-2])
391 (13 51 [?\C-\M-3])
392 (13 52 [?\C-\M-4])
393 (13 53 [?\C-\M-5])
394 (13 54 [?\C-\M-6])
395 (13 55 [?\C-\M-7])
396 (13 56 [?\C-\M-8])
397 (13 57 [?\C-\M-9])
398 (13 59 [?\C-\M-\;])
399 (13 61 [?\C-\M-=])
400 (13 92 [?\C-\M-\\])
402 (14 33 [?\C-\M-!])
403 (14 34 [?\C-\M-\"])
404 (14 35 [?\C-\M-#])
405 (14 36 [?\C-\M-$])
406 (14 37 [?\C-\M-%])
407 (14 38 [?\C-\M-&])
408 (14 40 [?\C-\M-\(])
409 (14 41 [?\C-\M-\)])
410 (14 42 [?\C-\M-*])
411 (14 43 [?\C-\M-+])
412 (14 58 [?\C-\M-:])
413 (14 60 [?\C-\M-<])
414 (14 62 [?\C-\M->])
415 (14 63 [(control meta ??)])
417 (7 9 [C-M-tab])
418 (7 13 [C-M-return])
420 (7 32 [?\C-\M-\s])
421 (7 39 [?\C-\M-\'])
422 (7 44 [?\C-\M-,])
423 (7 45 [?\C-\M--])
424 (7 46 [?\C-\M-.])
425 (7 47 [?\C-\M-/])
426 (7 48 [?\C-\M-0])
427 (7 49 [?\C-\M-1])
428 (7 50 [?\C-\M-2])
429 (7 51 [?\C-\M-3])
430 (7 52 [?\C-\M-4])
431 (7 53 [?\C-\M-5])
432 (7 54 [?\C-\M-6])
433 (7 55 [?\C-\M-7])
434 (7 56 [?\C-\M-8])
435 (7 57 [?\C-\M-9])
436 (7 59 [?\C-\M-\;])
437 (7 61 [?\C-\M-=])
438 (7 92 [?\C-\M-\\])
440 (8 33 [?\C-\M-!])
441 (8 34 [?\C-\M-\"])
442 (8 35 [?\C-\M-#])
443 (8 36 [?\C-\M-$])
444 (8 37 [?\C-\M-%])
445 (8 38 [?\C-\M-&])
446 (8 40 [?\C-\M-\(])
447 (8 41 [?\C-\M-\)])
448 (8 42 [?\C-\M-*])
449 (8 43 [?\C-\M-+])
450 (8 58 [?\C-\M-:])
451 (8 60 [?\C-\M-<])
452 (8 62 [?\C-\M->])
453 (8 63 [(control meta ??)])
455 (2 9 [S-tab])
456 (2 13 [S-return])
458 (6 9 [C-S-tab])
459 (6 13 [C-S-return])))
460 (define-key map
461 (format "\e[27;%d;%d~" (nth 0 bind) (nth 1 bind)) (nth 2 bind))
462 ;; For formatOtherKeys=1, the sequence is a bit shorter (bug#13839).
463 (define-key map
464 (format "\e[%d;%du" (nth 1 bind) (nth 0 bind)) (nth 2 bind)))
466 ;; Other versions of xterm might emit these.
467 (define-key map "\e[A" [up])
468 (define-key map "\e[B" [down])
469 (define-key map "\e[C" [right])
470 (define-key map "\e[D" [left])
471 (define-key map "\e[1~" [home])
473 (define-key map "\eO2A" [S-up])
474 (define-key map "\eO2B" [S-down])
475 (define-key map "\eO2C" [S-right])
476 (define-key map "\eO2D" [S-left])
477 (define-key map "\eO2F" [S-end])
478 (define-key map "\eO2H" [S-home])
480 (define-key map "\eO5A" [C-up])
481 (define-key map "\eO5B" [C-down])
482 (define-key map "\eO5C" [C-right])
483 (define-key map "\eO5D" [C-left])
484 (define-key map "\eO5F" [C-end])
485 (define-key map "\eO5H" [C-home])
487 (define-key map "\e[11~" [f1])
488 (define-key map "\e[12~" [f2])
489 (define-key map "\e[13~" [f3])
490 (define-key map "\e[14~" [f4])
492 ;; Recognize the start of a bracketed paste sequence. The handler
493 ;; internally recognizes the end.
494 (define-key map "\e[200~" [xterm-paste])
496 map)
497 "Function key map overrides for xterm.")
499 (defvar xterm-alternatives-map
500 (let ((map (make-sparse-keymap)))
501 ;; The terminal initialization C code file might have initialized
502 ;; function keys F13->F60 from the termcap/terminfo information.
503 ;; On a PC-style keyboard these keys correspond to
504 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The code
505 ;; here substitutes the corresponding definitions in function-key-map.
506 ;; The mapping from escape sequences to Fn is done in input-decode-map
507 ;; whereas this here mapping is done in local-function-key-map so that
508 ;; bindings to f45 still work, in case your keyboard really has an f45
509 ;; key rather than C-S-f9.
510 (define-key map [f13] [S-f1])
511 (define-key map [f14] [S-f2])
512 (define-key map [f15] [S-f3])
513 (define-key map [f16] [S-f4])
514 (define-key map [f17] [S-f5])
515 (define-key map [f18] [S-f6])
516 (define-key map [f19] [S-f7])
517 (define-key map [f20] [S-f8])
518 (define-key map [f21] [S-f9])
519 (define-key map [f22] [S-f10])
520 (define-key map [f23] [S-f11])
521 (define-key map [f24] [S-f12])
523 (define-key map [f25] [C-f1])
524 (define-key map [f26] [C-f2])
525 (define-key map [f27] [C-f3])
526 (define-key map [f28] [C-f4])
527 (define-key map [f29] [C-f5])
528 (define-key map [f30] [C-f6])
529 (define-key map [f31] [C-f7])
530 (define-key map [f32] [C-f8])
531 (define-key map [f33] [C-f9])
532 (define-key map [f34] [C-f10])
533 (define-key map [f35] [C-f11])
534 (define-key map [f36] [C-f12])
536 (define-key map [f37] [C-S-f1])
537 (define-key map [f38] [C-S-f2])
538 (define-key map [f39] [C-S-f3])
539 (define-key map [f40] [C-S-f4])
540 (define-key map [f41] [C-S-f5])
541 (define-key map [f42] [C-S-f6])
542 (define-key map [f43] [C-S-f7])
543 (define-key map [f44] [C-S-f8])
544 (define-key map [f45] [C-S-f9])
545 (define-key map [f46] [C-S-f10])
546 (define-key map [f47] [C-S-f11])
547 (define-key map [f48] [C-S-f12])
549 (define-key map [f49] [M-f1])
550 (define-key map [f50] [M-f2])
551 (define-key map [f51] [M-f3])
552 (define-key map [f52] [M-f4])
553 (define-key map [f53] [M-f5])
554 (define-key map [f54] [M-f6])
555 (define-key map [f55] [M-f7])
556 (define-key map [f56] [M-f8])
557 (define-key map [f57] [M-f9])
558 (define-key map [f58] [M-f10])
559 (define-key map [f59] [M-f11])
560 (define-key map [f60] [M-f12])
562 map)
563 "Keymap of possible alternative meanings for some keys.")
565 (defun xterm--report-background-handler ()
566 (let ((str "")
567 chr)
568 ;; The reply should be: \e ] 11 ; rgb: NUMBER1 / NUMBER2 / NUMBER3 \e \\
569 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?\\)))
570 (setq str (concat str (string chr))))
571 (when (string-match
572 "rgb:\\([a-f0-9]+\\)/\\([a-f0-9]+\\)/\\([a-f0-9]+\\)" str)
573 (let ((recompute-faces
574 (xterm-maybe-set-dark-background-mode
575 (string-to-number (match-string 1 str) 16)
576 (string-to-number (match-string 2 str) 16)
577 (string-to-number (match-string 3 str) 16))))
579 ;; Recompute faces here in case the background mode was
580 ;; set to dark. We used to call
581 ;; `tty-set-up-initial-frame-faces' only once, but that
582 ;; caused the light background faces to be computed
583 ;; incorrectly. See:
584 ;; http://permalink.gmane.org/gmane.emacs.devel/119627
585 (when recompute-faces
586 (tty-set-up-initial-frame-faces))))))
588 (defun xterm--version-handler ()
589 (let ((str "")
590 chr)
591 ;; The reply should be: \e [ > NUMBER1 ; NUMBER2 ; NUMBER3 c
592 ;; If the timeout is completely removed for read-event, this
593 ;; might hang for terminals that pretend to be xterm, but don't
594 ;; respond to this escape sequence. RMS' opinion was to remove
595 ;; it completely. That might be right, but let's first try to
596 ;; see if by using a longer timeout we get rid of most issues.
597 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?c)))
598 (setq str (concat str (string chr))))
599 ;; Since xterm-280, the terminal type (NUMBER1) is now 41 instead of 0.
600 (when (string-match "\\([0-9]+\\);\\([0-9]+\\);0" str)
601 (let ((version (string-to-number (match-string 2 str))))
602 (when (and (> version 2000) (equal (match-string 1 str) "1"))
603 ;; Hack attack! bug#16988: gnome-terminal reports "1;NNNN;0"
604 ;; with a large NNNN but is based on a rather old xterm code.
605 ;; Gnome terminal 3.6.1 reports 1;3406;0
606 ;; Gnome terminal 2.32.1 reports 1;2802;0
607 (setq version 200))
608 (when (equal (match-string 1 str) "83")
609 ;; `screen' (which returns 83;40003;0) seems to also lack support for
610 ;; some of these (bug#17607).
611 (setq version 240))
612 ;; If version is 242 or higher, assume the xterm supports
613 ;; reporting the background color (TODO: maybe earlier
614 ;; versions do too...)
615 (when (>= version 242)
616 (xterm--query "\e]11;?\e\\"
617 '(("\e]11;" . xterm--report-background-handler))))
619 ;; If version is 216 (the version when modifyOtherKeys was
620 ;; introduced) or higher, initialize the
621 ;; modifyOtherKeys support.
622 (when (>= version 216)
623 (terminal-init-xterm-modify-other-keys))))))
625 (defun xterm--query (query handlers)
626 "Send QUERY string to the terminal and watch for a response.
627 HANDLERS is an alist with elements of the form (STRING . FUNCTION).
628 We run the first FUNCTION whose STRING matches the input events."
629 ;; We used to query synchronously, but the need to use `discard-input' is
630 ;; rather annoying (bug#6758). Maybe we could always use the asynchronous
631 ;; approach, but it's less tested.
632 ;; FIXME: Merge the two branches.
633 (if (input-pending-p)
634 (progn
635 (dolist (handler handlers)
636 (define-key input-decode-map (car handler)
637 (lambda (&optional _prompt)
638 ;; Unregister the handler, since we don't expect further answers.
639 (dolist (handler handlers)
640 (define-key input-decode-map (car handler) nil))
641 (funcall (cdr handler))
642 [])))
643 (send-string-to-terminal query))
644 ;; Pending input can be mistakenly returned by the calls to
645 ;; read-event below. Discard it.
646 (send-string-to-terminal query)
647 (while handlers
648 (let ((handler (pop handlers))
649 (i 0))
650 (while (and (< i (length (car handler)))
651 (let ((evt (read-event nil nil 2)))
652 (or (eq evt (aref (car handler) i))
653 (progn (if evt (push evt unread-command-events))
654 nil))))
655 (setq i (1+ i)))
656 (if (= i (length (car handler)))
657 (progn (setq handlers nil)
658 (funcall (cdr handler)))
659 (while (> i 0)
660 (push (aref (car handler) (setq i (1- i)))
661 unread-command-events)))))))
663 (defun terminal-init-xterm ()
664 "Terminal initialization function for xterm."
665 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
666 ;; rxvt's keybindings are incompatible with xterm's. It is
667 ;; better in that case to use rxvt's initialization function.
668 (if (and (getenv "COLORTERM" (selected-frame))
669 (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
670 (tty-run-terminal-initialization (selected-frame) "rxvt")
672 (let ((map (copy-keymap xterm-alternatives-map)))
673 (set-keymap-parent map (keymap-parent local-function-key-map))
674 (set-keymap-parent local-function-key-map map))
676 (let ((map (copy-keymap xterm-function-map)))
678 ;; Use inheritance to let the main keymap override those defaults.
679 ;; This way we don't override terminfo-derived settings or settings
680 ;; made in the init file.
681 (set-keymap-parent map (keymap-parent input-decode-map))
682 (set-keymap-parent input-decode-map map)))
684 (xterm-register-default-colors)
685 (tty-set-up-initial-frame-faces)
687 (if (eq xterm-extra-capabilities 'check)
688 ;; Try to find out the type of terminal by sending a "Secondary
689 ;; Device Attributes (DA)" query.
690 (xterm--query "\e[>0c"
691 ;; Some terminals (like OS X's Terminal.app) respond to
692 ;; this query as if it were a "Primary Device Attributes"
693 ;; query instead, so we should handle that too.
694 '(("\e[?" . xterm--version-handler)
695 ("\e[>" . xterm--version-handler)))
697 (when (memq 'reportBackground xterm-extra-capabilities)
698 (xterm--query "\e]11;?\e\\"
699 '(("\e]11;" . xterm--report-background-handler))))
701 (when (memq 'modifyOtherKeys xterm-extra-capabilities)
702 (terminal-init-xterm-modify-other-keys)))
704 ;; Unconditionally enable bracketed paste mode: terminals that don't
705 ;; support it just ignore the sequence.
706 (terminal-init-xterm-bracketed-paste-mode)
708 (run-hooks 'terminal-init-xterm-hook))
710 (defun terminal-init-xterm-modify-other-keys ()
711 "Terminal initialization for xterm's modifyOtherKeys support."
712 (send-string-to-terminal "\e[>4;1m")
713 (push "\e[>4m" (terminal-parameter nil 'tty-mode-reset-strings))
714 (push "\e[>4;1m" (terminal-parameter nil 'tty-mode-set-strings)))
716 (defun terminal-init-xterm-bracketed-paste-mode ()
717 "Terminal initialization for bracketed paste mode."
718 (send-string-to-terminal "\e[?2004h")
719 (push "\e[?2004l" (terminal-parameter nil 'tty-mode-reset-strings))
720 (push "\e[?2004h" (terminal-parameter nil 'tty-mode-set-strings)))
722 ;; Set up colors, for those versions of xterm that support it.
723 (defvar xterm-standard-colors
724 ;; The names in the comments taken from XTerm-col.ad in the xterm
725 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
726 ;; from rgb.txt.
727 '(("black" 0 ( 0 0 0)) ; black
728 ("red" 1 (205 0 0)) ; red3
729 ("green" 2 ( 0 205 0)) ; green3
730 ("yellow" 3 (205 205 0)) ; yellow3
731 ("blue" 4 ( 0 0 238)) ; blue2
732 ("magenta" 5 (205 0 205)) ; magenta3
733 ("cyan" 6 ( 0 205 205)) ; cyan3
734 ("white" 7 (229 229 229)) ; gray90
735 ("brightblack" 8 (127 127 127)) ; gray50
736 ("brightred" 9 (255 0 0)) ; red
737 ("brightgreen" 10 ( 0 255 0)) ; green
738 ("brightyellow" 11 (255 255 0)) ; yellow
739 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
740 ("brightmagenta" 13 (255 0 255)) ; magenta
741 ("brightcyan" 14 ( 0 255 255)) ; cyan
742 ("brightwhite" 15 (255 255 255))) ; white
743 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
745 (defun xterm-rgb-convert-to-16bit (prim)
746 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
747 (logior prim (lsh prim 8)))
749 (defun xterm-register-default-colors ()
750 "Register the default set of colors for xterm or compatible emulator.
752 This function registers the number of colors returned by `display-color-cells'
753 for the currently selected frame. The first 16 colors are taken from
754 `xterm-standard-colors', which see, while the rest are computed assuming
755 either the 88- or 256-color standard color scheme supported by latest
756 versions of xterm."
757 (let* ((ncolors (display-color-cells (selected-frame)))
758 (colors xterm-standard-colors)
759 (color (car colors)))
760 (if (> ncolors 0)
761 ;; Clear the 8 default tty colors registered by startup.el
762 (tty-color-clear))
763 ;; Only register as many colors as are supported by the display.
764 (while (and (> ncolors 0) colors)
765 (tty-color-define (car color) (cadr color)
766 (mapcar 'xterm-rgb-convert-to-16bit
767 (car (cddr color))))
768 (setq colors (cdr colors)
769 color (car colors)
770 ncolors (1- ncolors)))
771 ;; We've exhausted the colors from `xterm-standard-colors'. If there
772 ;; are more colors to support, compute them now.
773 (when (> ncolors 0)
774 (cond
775 ((= ncolors 240) ; 256-color xterm
776 ;; 216 non-gray colors first
777 (let ((r 0) (g 0) (b 0))
778 (while (> ncolors 24)
779 ;; This and other formulas taken from 256colres.pl and
780 ;; 88colres.pl in the xterm distribution.
781 (tty-color-define (format "color-%d" (- 256 ncolors))
782 (- 256 ncolors)
783 (mapcar 'xterm-rgb-convert-to-16bit
784 (list (if (zerop r) 0 (+ (* r 40) 55))
785 (if (zerop g) 0 (+ (* g 40) 55))
786 (if (zerop b) 0 (+ (* b 40) 55)))))
788 (setq b (1+ b))
789 (if (> b 5)
790 (setq g (1+ g)
791 b 0))
792 (if (> g 5)
793 (setq r (1+ r)
794 g 0))
795 (setq ncolors (1- ncolors))))
796 ;; Now the 24 gray colors
797 (while (> ncolors 0)
798 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
799 (tty-color-define (format "color-%d" (- 256 ncolors))
800 (- 256 ncolors)
801 (list color color color))
802 (setq ncolors (1- ncolors))))
803 ((= ncolors 72) ; 88-color xterm
804 ;; 64 non-gray colors
805 (let ((levels '(0 139 205 255))
806 (r 0) (g 0) (b 0))
807 (while (> ncolors 8)
808 (tty-color-define (format "color-%d" (- 88 ncolors))
809 (- 88 ncolors)
810 (mapcar 'xterm-rgb-convert-to-16bit
811 (list (nth r levels)
812 (nth g levels)
813 (nth b levels))))
814 (setq b (1+ b))
815 (if (> b 3)
816 (setq g (1+ g)
817 b 0))
818 (if (> g 3)
819 (setq r (1+ r)
820 g 0))
821 (setq ncolors (1- ncolors))))
822 ;; Now the 8 gray colors
823 (while (> ncolors 0)
824 (setq color (xterm-rgb-convert-to-16bit
825 (floor
826 (if (= ncolors 8)
827 46.36363636
828 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
829 (tty-color-define (format "color-%d" (- 88 ncolors))
830 (- 88 ncolors)
831 (list color color color))
832 (setq ncolors (1- ncolors))))
833 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
834 ;; Modifying color mappings means realized faces don't use the
835 ;; right colors, so clear them.
836 (clear-face-cache)))
838 (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
839 ;; Use the heuristic in `frame-set-background-mode' to decide if a
840 ;; frame is dark.
841 (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
842 (set-terminal-parameter nil 'background-mode 'dark)
845 (provide 'xterm)
847 ;;; xterm.el ends here