(terminal-init-xterm): Fix bindings for C-tab,
[emacs.git] / lisp / term / xterm.el
blob7622f23752b709c494d1bece59277971e3f346d6
1 ;;; xterm.el --- define function key sequences and standard colors for xterm
3 ;; Copyright (C) 1995, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
6 ;; Author: FSF
7 ;; Keywords: terminals
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;;; Code:
30 (defun terminal-init-xterm ()
31 "Terminal initialization function for xterm."
32 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
33 ;; rxvt's keybindings that are incompatible with xterm's. It is
34 ;; better in that case to use rxvt's initializion function.
35 (if (and (getenv "COLORTERM")
36 (string-match "\\`rxvt" (getenv "COLORTERM")))
37 (progn
38 (eval-and-compile (load "term/rxvt"))
39 (terminal-init-rxvt))
41 ;; The terminal intialization C code file might have initialized
42 ;; function keys F13->F60 from the termcap/terminfo information. On
43 ;; a PC-style keyboard these keys correspond to
44 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The
45 ;; code here subsitutes the corresponding defintions in
46 ;; function-key-map. This substitution is needed because if a key
47 ;; definition if found in function-key-map, there are no further
48 ;; lookups in other keymaps.
49 (substitute-key-definition [f13] [S-f1] function-key-map)
50 (substitute-key-definition [f14] [S-f2] function-key-map)
51 (substitute-key-definition [f15] [S-f3] function-key-map)
52 (substitute-key-definition [f16] [S-f4] function-key-map)
53 (substitute-key-definition [f17] [S-f5] function-key-map)
54 (substitute-key-definition [f18] [S-f6] function-key-map)
55 (substitute-key-definition [f19] [S-f7] function-key-map)
56 (substitute-key-definition [f20] [S-f8] function-key-map)
57 (substitute-key-definition [f21] [S-f9] function-key-map)
58 (substitute-key-definition [f22] [S-f10] function-key-map)
59 (substitute-key-definition [f23] [S-f11] function-key-map)
60 (substitute-key-definition [f24] [S-f12] function-key-map)
62 (substitute-key-definition [f25] [C-f1] function-key-map)
63 (substitute-key-definition [f26] [C-f2] function-key-map)
64 (substitute-key-definition [f27] [C-f3] function-key-map)
65 (substitute-key-definition [f28] [C-f4] function-key-map)
66 (substitute-key-definition [f29] [C-f5] function-key-map)
67 (substitute-key-definition [f30] [C-f6] function-key-map)
68 (substitute-key-definition [f31] [C-f7] function-key-map)
69 (substitute-key-definition [f32] [C-f8] function-key-map)
70 (substitute-key-definition [f33] [C-f9] function-key-map)
71 (substitute-key-definition [f34] [C-f10] function-key-map)
72 (substitute-key-definition [f35] [C-f11] function-key-map)
73 (substitute-key-definition [f36] [C-f12] function-key-map)
75 (substitute-key-definition [f37] [C-S-f1] function-key-map)
76 (substitute-key-definition [f38] [C-S-f2] function-key-map)
77 (substitute-key-definition [f39] [C-S-f3] function-key-map)
78 (substitute-key-definition [f40] [C-S-f4] function-key-map)
79 (substitute-key-definition [f41] [C-S-f5] function-key-map)
80 (substitute-key-definition [f42] [C-S-f6] function-key-map)
81 (substitute-key-definition [f43] [C-S-f7] function-key-map)
82 (substitute-key-definition [f44] [C-S-f8] function-key-map)
83 (substitute-key-definition [f45] [C-S-f9] function-key-map)
84 (substitute-key-definition [f46] [C-S-f10] function-key-map)
85 (substitute-key-definition [f47] [C-S-f11] function-key-map)
86 (substitute-key-definition [f48] [C-S-f12] function-key-map)
88 (substitute-key-definition [f49] [A-f1] function-key-map)
89 (substitute-key-definition [f50] [A-f2] function-key-map)
90 (substitute-key-definition [f51] [A-f3] function-key-map)
91 (substitute-key-definition [f52] [A-f4] function-key-map)
92 (substitute-key-definition [f53] [A-f5] function-key-map)
93 (substitute-key-definition [f54] [A-f6] function-key-map)
94 (substitute-key-definition [f55] [A-f7] function-key-map)
95 (substitute-key-definition [f56] [A-f8] function-key-map)
96 (substitute-key-definition [f57] [A-f9] function-key-map)
97 (substitute-key-definition [f58] [A-f10] function-key-map)
98 (substitute-key-definition [f59] [A-f11] function-key-map)
99 (substitute-key-definition [f60] [A-f12] function-key-map)
101 (let ((map (make-sparse-keymap)))
102 ;; xterm from X.org 6.8.2 uses these key definitions.
103 (define-key map "\eOP" [f1])
104 (define-key map "\eOQ" [f2])
105 (define-key map "\eOR" [f3])
106 (define-key map "\eOS" [f4])
107 (define-key map "\e[15~" [f5])
108 (define-key map "\e[17~" [f6])
109 (define-key map "\e[18~" [f7])
110 (define-key map "\e[19~" [f8])
111 (define-key map "\e[20~" [f9])
112 (define-key map "\e[21~" [f10])
113 (define-key map "\e[23~" [f11])
114 (define-key map "\e[24~" [f12])
116 (define-key map "\eO2P" [S-f1])
117 (define-key map "\eO2Q" [S-f2])
118 (define-key map "\eO2R" [S-f3])
119 (define-key map "\eO2S" [S-f4])
120 (define-key map "\e[15;2~" [S-f5])
121 (define-key map "\e[17;2~" [S-f6])
122 (define-key map "\e[18;2~" [S-f7])
123 (define-key map "\e[19;2~" [S-f8])
124 (define-key map "\e[20;2~" [S-f9])
125 (define-key map "\e[21;2~" [S-f10])
126 (define-key map "\e[23;2~" [S-f11])
127 (define-key map "\e[24;2~" [S-f12])
129 (define-key map "\eO5P" [C-f1])
130 (define-key map "\eO5Q" [C-f2])
131 (define-key map "\eO5R" [C-f3])
132 (define-key map "\eO5S" [C-f4])
133 (define-key map "\e[15;5~" [C-f5])
134 (define-key map "\e[17;5~" [C-f6])
135 (define-key map "\e[18;5~" [C-f7])
136 (define-key map "\e[19;5~" [C-f8])
137 (define-key map "\e[20;5~" [C-f9])
138 (define-key map "\e[21;5~" [C-f10])
139 (define-key map "\e[23;5~" [C-f11])
140 (define-key map "\e[24;5~" [C-f12])
142 (define-key map "\eO6P" [C-S-f1])
143 (define-key map "\eO6Q" [C-S-f2])
144 (define-key map "\eO6R" [C-S-f3])
145 (define-key map "\eO6S" [C-S-f4])
146 (define-key map "\e[15;6~" [C-S-f5])
147 (define-key map "\e[17;6~" [C-S-f6])
148 (define-key map "\e[18;6~" [C-S-f7])
149 (define-key map "\e[19;6~" [C-S-f8])
150 (define-key map "\e[20;6~" [C-S-f9])
151 (define-key map "\e[21;6~" [C-S-f10])
152 (define-key map "\e[23;6~" [C-S-f11])
153 (define-key map "\e[24;6~" [C-S-f12])
155 (define-key map "\eO3P" [A-f1])
156 (define-key map "\eO3Q" [A-f2])
157 (define-key map "\eO3R" [A-f3])
158 (define-key map "\eO3S" [A-f4])
159 (define-key map "\e[15;3~" [A-f5])
160 (define-key map "\e[17;3~" [A-f6])
161 (define-key map "\e[18;3~" [A-f7])
162 (define-key map "\e[19;3~" [A-f8])
163 (define-key map "\e[20;3~" [A-f9])
164 (define-key map "\e[21;3~" [A-f10])
165 (define-key map "\e[23;3~" [A-f11])
166 (define-key map "\e[24;3~" [A-f12])
168 (define-key map "\eOA" [up])
169 (define-key map "\eOB" [down])
170 (define-key map "\eOC" [right])
171 (define-key map "\eOD" [left])
172 (define-key map "\eOF" [end])
173 (define-key map "\eOH" [home])
175 (define-key map "\e[1;2A" [S-up])
176 (define-key map "\e[1;2B" [S-down])
177 (define-key map "\e[1;2C" [S-right])
178 (define-key map "\e[1;2D" [S-left])
179 (define-key map "\e[1;2F" [S-end])
180 (define-key map "\e[1;2H" [S-home])
182 (define-key map "\e[1;5A" [C-up])
183 (define-key map "\e[1;5B" [C-down])
184 (define-key map "\e[1;5C" [C-right])
185 (define-key map "\e[1;5D" [C-left])
186 (define-key map "\e[1;5F" [C-end])
187 (define-key map "\e[1;5H" [C-home])
189 (define-key map "\e[1;6A" [C-S-up])
190 (define-key map "\e[1;6B" [C-S-down])
191 (define-key map "\e[1;6C" [C-S-right])
192 (define-key map "\e[1;6D" [C-S-left])
193 (define-key map "\e[1;6F" [C-S-end])
194 (define-key map "\e[1;6H" [C-S-home])
196 (define-key map "\e[1;3A" [A-up])
197 (define-key map "\e[1;3B" [A-down])
198 (define-key map "\e[1;3C" [A-right])
199 (define-key map "\e[1;3D" [A-left])
200 (define-key map "\e[1;3F" [A-end])
201 (define-key map "\e[1;3H" [A-home])
203 (define-key map "\e[2~" [insert])
204 (define-key map "\e[3~" [delete])
205 (define-key map "\e[5~" [prior])
206 (define-key map "\e[6~" [next])
208 (define-key map "\e[2;2~" [S-insert])
209 (define-key map "\e[3;2~" [S-delete])
210 (define-key map "\e[5;2~" [S-prior])
211 (define-key map "\e[6;2~" [S-next])
213 (define-key map "\e[2;5~" [C-insert])
214 (define-key map "\e[3;5~" [C-delete])
215 (define-key map "\e[5;5~" [C-prior])
216 (define-key map "\e[6;5~" [C-next])
218 (define-key map "\e[2;6~" [C-S-insert])
219 (define-key map "\e[3;6~" [C-S-delete])
220 (define-key map "\e[5;6~" [C-S-prior])
221 (define-key map "\e[6;6~" [C-S-next])
223 (define-key map "\e[2;3~" [A-insert])
224 (define-key map "\e[3;3~" [A-delete])
225 (define-key map "\e[5;3~" [A-prior])
226 (define-key map "\e[6;3~" [A-next])
228 (define-key map "\e[4~" [select])
229 (define-key map "\e[29~" [print])
231 ;; These keys are available in xterm starting from version 214
232 ;; if the modifyOtherKeys resource is set to 1.
233 (define-key map "\e[27;5;9~" [C-tab])
234 (define-key map "\e[27;5;13~" [C-return])
235 (define-key map "\e[27;5;44~" [?\C-,])
236 (define-key map "\e[27;5;46~" [?\C-.])
237 (define-key map "\e[27;5;47~" [?\C-/])
238 (define-key map "\e[27;5;92~" [?\C-\\])
240 (define-key map "\e[27;2;9~" [S-tab])
241 (define-key map "\e[27;2;13~" [S-return])
243 (define-key map "\e[27;6;9~" [(C-S-tab)])
245 (define-key map "\e[27;13;46~" [?\C-\M-.])
248 ;; Other versions of xterm might emit these.
249 (define-key map "\e[A" [up])
250 (define-key map "\e[B" [down])
251 (define-key map "\e[C" [right])
252 (define-key map "\e[D" [left])
253 (define-key map "\e[1~" [home])
255 (define-key map "\eO2A" [S-up])
256 (define-key map "\eO2B" [S-down])
257 (define-key map "\eO2C" [S-right])
258 (define-key map "\eO2D" [S-left])
259 (define-key map "\eO2F" [S-end])
260 (define-key map "\eO2H" [S-home])
262 (define-key map "\eO5A" [C-up])
263 (define-key map "\eO5B" [C-down])
264 (define-key map "\eO5C" [C-right])
265 (define-key map "\eO5D" [C-left])
266 (define-key map "\eO5F" [C-end])
267 (define-key map "\eO5H" [C-home])
269 (define-key map "\e[11~" [f1])
270 (define-key map "\e[12~" [f2])
271 (define-key map "\e[13~" [f3])
272 (define-key map "\e[14~" [f4])
274 ;; Use inheritance to let the main keymap override those defaults.
275 ;; This way we don't override terminfo-derived settings or settings
276 ;; made in the .emacs file.
277 (set-keymap-parent map (keymap-parent function-key-map))
278 (set-keymap-parent function-key-map map))
280 ;; Do it!
281 (xterm-register-default-colors)
282 ;; This recomputes all the default faces given the colors we've just set up.
283 (tty-set-up-initial-frame-faces)))
285 ;; Set up colors, for those versions of xterm that support it.
286 (defvar xterm-standard-colors
287 ;; The names in the comments taken from XTerm-col.ad in the xterm
288 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
289 ;; from rgb.txt.
290 '(("black" 0 ( 0 0 0)) ; black
291 ("red" 1 (205 0 0)) ; red3
292 ("green" 2 ( 0 205 0)) ; green3
293 ("yellow" 3 (205 205 0)) ; yellow3
294 ("blue" 4 ( 0 0 238)) ; blue2
295 ("magenta" 5 (205 0 205)) ; magenta3
296 ("cyan" 6 ( 0 205 205)) ; cyan3
297 ("white" 7 (229 229 229)) ; gray90
298 ("brightblack" 8 (127 127 127)) ; gray50
299 ("brightred" 9 (255 0 0)) ; red
300 ("brightgreen" 10 ( 0 255 0)) ; green
301 ("brightyellow" 11 (255 255 0)) ; yellow
302 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
303 ("brightmagenta" 13 (255 0 255)) ; magenta
304 ("brightcyan" 14 ( 0 255 255)) ; cyan
305 ("brightwhite" 15 (255 255 255))) ; white
306 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
308 (defun xterm-rgb-convert-to-16bit (prim)
309 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
310 (logior prim (lsh prim 8)))
312 (defun xterm-register-default-colors ()
313 "Register the default set of colors for xterm or compatible emulator.
315 This function registers the number of colors returned by `display-color-cells'
316 for the currently selected frame. The first 16 colors are taken from
317 `xterm-standard-colors', which see, while the rest are computed assuming
318 either the 88- or 256-color standard color scheme supported by latest
319 versions of xterm."
320 (let* ((ncolors (display-color-cells))
321 (colors xterm-standard-colors)
322 (color (car colors)))
323 (if (> ncolors 0)
324 ;; Clear the 8 default tty colors registered by startup.el
325 (tty-color-clear))
326 ;; Only register as many colors as are supported by the display.
327 (while (and (> ncolors 0) colors)
328 (tty-color-define (car color) (cadr color)
329 (mapcar 'xterm-rgb-convert-to-16bit
330 (car (cddr color))))
331 (setq colors (cdr colors)
332 color (car colors)
333 ncolors (1- ncolors)))
334 ;; We've exhausted the colors from `xterm-standard-colors'. If there
335 ;; are more colors to support, compute them now.
336 (when (> ncolors 0)
337 (cond
338 ((= ncolors 240) ; 256-color xterm
339 ;; 216 non-gray colors first
340 (let ((r 0) (g 0) (b 0))
341 (while (> ncolors 24)
342 ;; This and other formulae taken from 256colres.pl and
343 ;; 88colres.pl in the xterm distribution.
344 (tty-color-define (format "color-%d" (- 256 ncolors))
345 (- 256 ncolors)
346 (mapcar 'xterm-rgb-convert-to-16bit
347 (list (if (zerop r) 0 (+ (* r 40) 55))
348 (if (zerop g) 0 (+ (* g 40) 55))
349 (if (zerop b) 0 (+ (* b 40) 55)))))
351 (setq b (1+ b))
352 (if (> b 5)
353 (setq g (1+ g)
354 b 0))
355 (if (> g 5)
356 (setq r (1+ r)
357 g 0))
358 (setq ncolors (1- ncolors))))
359 ;; Now the 24 gray colors
360 (while (> ncolors 0)
361 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
362 (tty-color-define (format "color-%d" (- 256 ncolors))
363 (- 256 ncolors)
364 (list color color color))
365 (setq ncolors (1- ncolors))))
366 ((= ncolors 72) ; 88-color xterm
367 ;; 64 non-gray colors
368 (let ((levels '(0 139 205 255))
369 (r 0) (g 0) (b 0))
370 (while (> ncolors 8)
371 (tty-color-define (format "color-%d" (- 88 ncolors))
372 (- 88 ncolors)
373 (mapcar 'xterm-rgb-convert-to-16bit
374 (list (nth r levels)
375 (nth g levels)
376 (nth b levels))))
377 (setq b (1+ b))
378 (if (> b 3)
379 (setq g (1+ g)
380 b 0))
381 (if (> g 3)
382 (setq r (1+ r)
383 g 0))
384 (setq ncolors (1- ncolors))))
385 ;; Now the 8 gray colors
386 (while (> ncolors 0)
387 (setq color (xterm-rgb-convert-to-16bit
388 (floor
389 (if (= ncolors 8)
390 46.36363636
391 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
392 (tty-color-define (format "color-%d" (- 88 ncolors))
393 (- 88 ncolors)
394 (list color color color))
395 (setq ncolors (1- ncolors))))
396 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
397 ;; Modifying color mappings means realized faces don't use the
398 ;; right colors, so clear them.
399 (clear-face-cache)))
401 ;; arch-tag: 12e7ebdd-1e6c-4b25-b0f9-35ace25e855a
402 ;;; xterm.el ends here