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