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