Store client's environment in terminal parameters, not server parameters.
[bpt/emacs.git] / lisp / term / rxvt.el
1 ;;; rxvt.el --- define function key sequences and standard colors for rxvt
2
3 ;; Copyright (C) 2002, 2003, 2004, 2005 Free Software Foundation, Inc.
4
5 ;; Author: Eli Zaretskii
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 rxvt-function-map nil
30 "Function key overrides for rxvt.")
31
32 ;; Protect against reloads.
33 (unless rxvt-function-map
34 (setq rxvt-function-map (make-sparse-keymap))
35
36 ;; Set up function-key-map entries that termcap and terminfo don't know.
37 (define-key rxvt-function-map "\e[A" [up])
38 (define-key rxvt-function-map "\e[B" [down])
39 (define-key rxvt-function-map "\e[C" [right])
40 (define-key rxvt-function-map "\e[D" [left])
41 (define-key rxvt-function-map "\e[2~" [insert])
42 (define-key rxvt-function-map "\e[3~" [delete])
43 (define-key rxvt-function-map "\e[4~" [select])
44 (define-key rxvt-function-map "\e[5~" [prior])
45 (define-key rxvt-function-map "\e[6~" [next])
46 (define-key rxvt-function-map "\e[7~" [home])
47 (define-key rxvt-function-map "\e[8~" [end])
48 (define-key rxvt-function-map "\e[11~" [f1])
49 (define-key rxvt-function-map "\e[12~" [f2])
50 (define-key rxvt-function-map "\e[13~" [f3])
51 (define-key rxvt-function-map "\e[14~" [f4])
52 (define-key rxvt-function-map "\e[15~" [f5])
53 (define-key rxvt-function-map "\e[17~" [f6])
54 (define-key rxvt-function-map "\e[18~" [f7])
55 (define-key rxvt-function-map "\e[19~" [f8])
56 (define-key rxvt-function-map "\e[20~" [f9])
57 (define-key rxvt-function-map "\e[21~" [f10])
58 ;; The strings emitted by f11 and f12 are the same as the strings
59 ;; emitted by S-f1 and S-f2, so don't define f11 and f12.
60 ;; (define-key rxvt-function-map "\e[23~" [f11])
61 ;; (define-key rxvt-function-map "\e[24~" [f12])
62 (define-key rxvt-function-map "\e[29~" [print])
63
64 (define-key rxvt-function-map "\e[11^" [C-f1])
65 (define-key rxvt-function-map "\e[12^" [C-f2])
66 (define-key rxvt-function-map "\e[13^" [C-f3])
67 (define-key rxvt-function-map "\e[14^" [C-f4])
68 (define-key rxvt-function-map "\e[15^" [C-f5])
69 (define-key rxvt-function-map "\e[17^" [C-f6])
70 (define-key rxvt-function-map "\e[18^" [C-f7])
71 (define-key rxvt-function-map "\e[19^" [C-f8])
72 (define-key rxvt-function-map "\e[20^" [C-f9])
73 (define-key rxvt-function-map "\e[21^" [C-f10])
74
75 (define-key rxvt-function-map "\e[23~" [S-f1])
76 (define-key rxvt-function-map "\e[24~" [S-f2])
77 (define-key rxvt-function-map "\e[25~" [S-f3])
78 (define-key rxvt-function-map "\e[26~" [S-f4])
79 (define-key rxvt-function-map "\e[28~" [S-f5])
80 (define-key rxvt-function-map "\e[29~" [S-f6])
81 (define-key rxvt-function-map "\e[31~" [S-f7])
82 (define-key rxvt-function-map "\e[32~" [S-f8])
83 (define-key rxvt-function-map "\e[33~" [S-f9])
84 (define-key rxvt-function-map "\e[34~" [S-f10])
85
86 (define-key rxvt-function-map "\e[23^" [C-S-f1])
87 (define-key rxvt-function-map "\e[24^" [C-S-f2])
88 (define-key rxvt-function-map "\e[25^" [C-S-f3])
89 (define-key rxvt-function-map "\e[26^" [C-S-f4])
90 (define-key rxvt-function-map "\e[28^" [C-S-f5])
91 (define-key rxvt-function-map "\e[29^" [C-S-f6])
92 (define-key rxvt-function-map "\e[31^" [C-S-f7])
93 (define-key rxvt-function-map "\e[32^" [C-S-f8])
94 (define-key rxvt-function-map "\e[33^" [C-S-f9])
95 (define-key rxvt-function-map "\e[34^" [C-S-f10])
96
97 (define-key rxvt-function-map "\e[2^" [C-insert])
98 (define-key rxvt-function-map "\e[3^" [C-delete])
99 (define-key rxvt-function-map "\e[5^" [C-prior])
100 (define-key rxvt-function-map "\e[6^" [C-next])
101 (define-key rxvt-function-map "\e[7^" [C-home])
102 (define-key rxvt-function-map "\e[8^" [C-end])
103 (define-key rxvt-function-map "\eOd" [C-left])
104 (define-key rxvt-function-map "\eOc" [C-right])
105 (define-key rxvt-function-map "\eOa" [C-up])
106 (define-key rxvt-function-map "\eOb" [C-down])
107
108 (define-key rxvt-function-map "\e[2;2~" [S-insert])
109 (define-key rxvt-function-map "\e[3$" [S-delete])
110 (define-key rxvt-function-map "\e[5$" [S-prior])
111 (define-key rxvt-function-map "\e[6$" [S-next])
112 (define-key rxvt-function-map "\e[7$" [S-home])
113 (define-key rxvt-function-map "\e[8$" [S-end])
114 (define-key rxvt-function-map "\e[d" [S-left])
115 (define-key rxvt-function-map "\e[c" [S-right])
116 (define-key rxvt-function-map "\e[a" [S-up])
117 (define-key rxvt-function-map "\e[b" [S-down]))
118
119 (defun terminal-init-rxvt ()
120 "Terminal initialization function for rxvt."
121
122 ;; The terminal-local stuff only need to be set up on the first
123 ;; frame on that device.
124 (when (eq 1 (length (frames-on-display-list)))
125 ;; The terminal intialization C code file might have initialized
126 ;; function keys F11->F42 from the termcap/terminfo information. On
127 ;; a PC-style keyboard these keys correspond to
128 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C-, C-S-. The
129 ;; code here subsitutes the corresponding defintions in
130 ;; function-key-map. This substitution is needed because if a key
131 ;; definition if found in function-key-map, there are no further
132 ;; lookups in other keymaps.
133 (substitute-key-definition [f11] [S-f1] local-function-key-map)
134 (substitute-key-definition [f12] [S-f2] local-function-key-map)
135 (substitute-key-definition [f13] [S-f3] local-function-key-map)
136 (substitute-key-definition [f14] [S-f4] local-function-key-map)
137 (substitute-key-definition [f15] [S-f5] local-function-key-map)
138 (substitute-key-definition [f16] [S-f6] local-function-key-map)
139 (substitute-key-definition [f17] [S-f7] local-function-key-map)
140 (substitute-key-definition [f18] [S-f8] local-function-key-map)
141 (substitute-key-definition [f19] [S-f9] local-function-key-map)
142 (substitute-key-definition [f20] [S-f10] local-function-key-map)
143
144 (substitute-key-definition [f23] [C-f1] local-function-key-map)
145 (substitute-key-definition [f24] [C-f2] local-function-key-map)
146 (substitute-key-definition [f25] [C-f3] local-function-key-map)
147 (substitute-key-definition [f26] [C-f4] local-function-key-map)
148 (substitute-key-definition [f27] [C-f5] local-function-key-map)
149 (substitute-key-definition [f28] [C-f6] local-function-key-map)
150 (substitute-key-definition [f29] [C-f7] local-function-key-map)
151 (substitute-key-definition [f30] [C-f8] local-function-key-map)
152 (substitute-key-definition [f31] [C-f9] local-function-key-map)
153 (substitute-key-definition [f32] [C-f10] local-function-key-map)
154
155 (substitute-key-definition [f33] [C-S-f1] local-function-key-map)
156 (substitute-key-definition [f34] [C-S-f2] local-function-key-map)
157 (substitute-key-definition [f35] [C-S-f3] local-function-key-map)
158 (substitute-key-definition [f36] [C-S-f4] local-function-key-map)
159 (substitute-key-definition [f37] [C-S-f5] local-function-key-map)
160 (substitute-key-definition [f38] [C-S-f6] local-function-key-map)
161 (substitute-key-definition [f39] [C-S-f7] local-function-key-map)
162 (substitute-key-definition [f40] [C-S-f8] local-function-key-map)
163 (substitute-key-definition [f41] [C-S-f9] local-function-key-map)
164 (substitute-key-definition [f42] [C-S-f10] local-function-key-map)
165
166 ;; Use inheritance to let the main keymap override those defaults.
167 ;; This way we don't override terminfo-derived settings or settings
168 ;; made in the .emacs file.
169 (let ((m (copy-keymap rxvt-function-map)))
170 (set-keymap-parent m (keymap-parent local-function-key-map))
171 (set-keymap-parent local-function-key-map m)))
172
173 ;; Initialize colors and background mode.
174 (rxvt-register-default-colors)
175 (rxvt-set-background-mode)
176 ;; This recomputes all the default faces given the colors we've just set up.
177 (tty-set-up-initial-frame-faces))
178
179 ;; Set up colors, for those versions of rxvt that support it.
180 (defvar rxvt-standard-colors
181 ;; The names of the colors in the comments taken from the rxvt.1 man
182 ;; page; the corresponding RGB values--from rgb.txt.
183 '(("black" 0 ( 0 0 0)) ; black
184 ("red" 1 (205 0 0)) ; red3
185 ("green" 2 ( 0 205 0)) ; green3
186 ("yellow" 3 (205 205 0)) ; yellow3
187 ("blue" 4 ( 0 0 205)) ; blue3
188 ("magenta" 5 (205 0 205)) ; magenta3
189 ("cyan" 6 ( 0 205 205)) ; cyan3
190 ("white" 7 (229 229 229)) ; gray90
191 ("brightblack" 8 ( 77 77 77)) ; gray30
192 ("brightred" 9 (255 0 0)) ; red
193 ("brightgreen" 10 ( 0 255 0)) ; green
194 ("brightyellow" 11 (255 255 0)) ; yellow
195 ("brightblue" 12 ( 0 0 255)) ; blue
196 ("brightmagenta" 13 (255 0 255)) ; magenta
197 ("brightcyan" 14 ( 0 255 255)) ; cyan
198 ("brightwhite" 15 (255 255 255))) ; white
199 "Names of 16 standard rxvt colors, their numbers, and RGB values.")
200
201 (defun rxvt-rgb-convert-to-16bit (prim)
202 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
203 (min 65535 (round (* (/ prim 255.0) 65535.0))))
204
205 (defun rxvt-register-default-colors ()
206 "Register the default set of colors for rxvt or compatible emulator.
207
208 This function registers the number of colors returned by `display-color-cells'
209 for the currently selected frame."
210 (let* ((ncolors (display-color-cells))
211 (colors rxvt-standard-colors)
212 (color (car colors)))
213 (if (> ncolors 0)
214 ;; Clear the 8 default tty colors registered by startup.el
215 (tty-color-clear))
216 ;; Only register as many colors as are supported by the display.
217 (while (and (> ncolors 0) colors)
218 (tty-color-define (car color) (cadr color)
219 (mapcar 'rxvt-rgb-convert-to-16bit
220 (car (cddr color))))
221 (setq colors (cdr colors)
222 color (car colors)
223 ncolors (1- ncolors)))
224 (when (> ncolors 0)
225 (cond
226 ((= ncolors 240) ; 256-color rxvt
227 ;; 216 non-gray colors first
228 (let ((r 0) (g 0) (b 0))
229 (while (> ncolors 24)
230 ;; This and other formulae taken from 256colres.pl and
231 ;; 88colres.pl in the xterm distribution.
232 (tty-color-define (format "color-%d" (- 256 ncolors))
233 (- 256 ncolors)
234 (mapcar 'rxvt-rgb-convert-to-16bit
235 (list (round (* r 42.5))
236 (round (* g 42.5))
237 (round (* b 42.5)))))
238 (setq b (1+ b))
239 (if (> b 5)
240 (setq g (1+ g)
241 b 0))
242 (if (> g 5)
243 (setq r (1+ r)
244 g 0))
245 (setq ncolors (1- ncolors))))
246 ;; Now the 24 gray colors
247 (while (> ncolors 0)
248 (setq color (rxvt-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
249 (tty-color-define (format "color-%d" (- 256 ncolors))
250 (- 256 ncolors)
251 (list color color color))
252 (setq ncolors (1- ncolors))))
253
254 ((= ncolors 72) ; rxvt-unicode
255 ;; 64 non-gray colors
256 (let ((levels '(0 139 205 255))
257 (r 0) (g 0) (b 0))
258 (while (> ncolors 8)
259 (tty-color-define (format "color-%d" (- 88 ncolors))
260 (- 88 ncolors)
261 (mapcar 'rxvt-rgb-convert-to-16bit
262 (list (nth r levels)
263 (nth g levels)
264 (nth b levels))))
265 (setq b (1+ b))
266 (if (> b 3)
267 (setq g (1+ g)
268 b 0))
269 (if (> g 3)
270 (setq r (1+ r)
271 g 0))
272 (setq ncolors (1- ncolors))))
273 ;; Now the 8 gray colors
274 (while (> ncolors 0)
275 (setq color (rxvt-rgb-convert-to-16bit
276 (floor
277 (if (= ncolors 8)
278 46.36363636
279 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
280 (tty-color-define (format "color-%d" (- 88 ncolors))
281 (- 88 ncolors)
282 (list color color color))
283 (setq ncolors (1- ncolors))))
284 (t (error "Unsupported number of rxvt colors (%d)" (+ 16 ncolors)))))
285 ;; Modifying color mappings means realized faces don't use the
286 ;; right colors, so clear them.
287 (clear-face-cache)))
288
289 ;; rxvt puts the default colors into an environment variable
290 ;; COLORFGBG. We use this to set the background mode in a more
291 ;; intelligent way than the default guesswork in startup.el.
292 (defun rxvt-set-background-mode ()
293 "Set background mode as appropriate for the default rxvt colors."
294 (let ((fgbg (terminal-getenv "COLORFGBG"))
295 bg rgb)
296 (setq default-frame-background-mode 'light)
297 (when (and fgbg
298 (string-match ".*;\\([0-9][0-9]?\\)\\'" fgbg))
299 (setq bg (string-to-number (substring fgbg (match-beginning 1))))
300 ;; The next line assumes that rxvt-standard-colors are ordered
301 ;; by the color index in the ascending order!
302 (setq rgb (car (cddr (nth bg rxvt-standard-colors))))
303 ;; See the commentary in frame-set-background-mode about the
304 ;; computation below.
305 (if (< (apply '+ rgb)
306 ;; The following line assumes that white is the 15th
307 ;; color in rxvt-standard-colors.
308 (* (apply '+ (car (cddr (nth 15 rxvt-standard-colors)))) 0.6))
309 (setq default-frame-background-mode 'dark)))
310 (frame-set-background-mode (selected-frame))))
311
312 ;; arch-tag: 20cf2fb6-6318-4bab-9dbf-1d15048f2257
313 ;;; rxvt.el ends here