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