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