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