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