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