Merged from miles@gnu.org--gnu-2005 (patch 292-295)
[bpt/emacs.git] / lisp / term / rxvt.el
1 ;;; rxvt.el --- define function key sequences and standard colors for rxvt
2
3 ;; Copyright (C) 2002 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., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'server)
30
31 (defvar rxvt-function-map (make-sparse-keymap)
32 "Function key rxvt-function-mapoverrides for rxvt.")
33
34 ;; Set up function-key-map entries that termcap and terminfo don't know.
35 ;; XXX We need to find a way to have these define-keys be display-local. -- Lorentey
36 (define-key rxvt-function-map "\e[A" [up])
37 (define-key rxvt-function-map "\e[B" [down])
38 (define-key rxvt-function-map "\e[C" [right])
39 (define-key rxvt-function-map "\e[D" [left])
40 (define-key rxvt-function-map "\e[7~" [home])
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[11~" [f1])
47 (define-key rxvt-function-map "\e[12~" [f2])
48 (define-key rxvt-function-map "\e[13~" [f3])
49 (define-key rxvt-function-map "\e[14~" [f4])
50 (define-key rxvt-function-map "\e[15~" [f5])
51 (define-key rxvt-function-map "\e[17~" [f6])
52 (define-key rxvt-function-map "\e[18~" [f7])
53 (define-key rxvt-function-map "\e[19~" [f8])
54 (define-key rxvt-function-map "\e[20~" [f9])
55 (define-key rxvt-function-map "\e[21~" [f10])
56 (define-key rxvt-function-map "\e[23~" [f11])
57 (define-key rxvt-function-map "\e[24~" [f12])
58 (define-key rxvt-function-map "\e[29~" [print])
59
60 (define-key rxvt-function-map "\e[11^" [C-f1])
61 (define-key rxvt-function-map "\e[12^" [C-f2])
62 (define-key rxvt-function-map "\e[13^" [C-f3])
63 (define-key rxvt-function-map "\e[14^" [C-f4])
64 (define-key rxvt-function-map "\e[15^" [C-f5])
65 (define-key rxvt-function-map "\e[17^" [C-f6])
66 (define-key rxvt-function-map "\e[18^" [C-f7])
67 (define-key rxvt-function-map "\e[19^" [C-f8])
68 (define-key rxvt-function-map "\e[20^" [C-f9])
69 (define-key rxvt-function-map "\e[21^" [C-f10])
70 (define-key rxvt-function-map "\e[23^" [C-f11])
71 (define-key rxvt-function-map "\e[24^" [C-f12])
72
73 (define-key rxvt-function-map "\e[29~" [print])
74
75 (define-key rxvt-function-map "\e[2;2~" [S-insert])
76 (define-key rxvt-function-map "\e[3$" [S-delete])
77
78 (define-key rxvt-function-map "\e[2^" [C-insert])
79 (define-key rxvt-function-map "\e[3^" [C-delete])
80 (define-key rxvt-function-map "\e[5^" [C-prior])
81 (define-key rxvt-function-map "\e[6^" [C-next])
82 (define-key rxvt-function-map "\eOd" [C-left])
83 (define-key rxvt-function-map "\eOc" [C-right])
84 (define-key rxvt-function-map "\eOa" [C-up])
85 (define-key rxvt-function-map "\eOb" [C-down])
86
87 (define-key rxvt-function-map "\e[5$" [S-prior])
88 (define-key rxvt-function-map "\e[6$" [S-next])
89 (define-key rxvt-function-map "\e[8$" [S-end])
90 (define-key rxvt-function-map "\e[7$" [S-home])
91 (define-key rxvt-function-map "\e[d" [S-left])
92 (define-key rxvt-function-map "\e[c" [S-right])
93 (define-key rxvt-function-map "\e[a" [S-up])
94 (define-key rxvt-function-map "\e[b" [S-down])
95
96 ;; Use inheritance to let the main keymap override those defaults.
97 ;; This way we don't override terminfo-derived settings or settings
98 ;; made in the .emacs file.
99 (unless (let ((map (keymap-parent function-key-map))
100 found)
101 (while (and (not found) map)
102 (setq found (eq rxvt-function-map map)
103 map (keymap-parent map)))
104 found)
105 (set-keymap-parent rxvt-function-map (keymap-parent function-key-map))
106 (set-keymap-parent function-key-map rxvt-function-map))
107
108
109 ;; Set up colors, for those versions of rxvt that support it.
110 (defvar rxvt-standard-colors
111 ;; The names of the colors in the comments taken from the rxvt.1 man
112 ;; page; the corresponding RGB values--from rgb.txt.
113 '(("black" 0 ( 0 0 0)) ; black
114 ("red" 1 (205 0 0)) ; red3
115 ("green" 2 ( 0 205 0)) ; green3
116 ("yellow" 3 (205 205 0)) ; yellow3
117 ("blue" 4 ( 0 0 205)) ; blue3
118 ("magenta" 5 (205 0 205)) ; magenta3
119 ("cyan" 6 ( 0 205 205)) ; cyan3
120 ("white" 7 (250 235 215)) ; AntiqueWhite
121 ("brightblack" 8 ( 64 64 64)) ; gray25
122 ("brightred" 9 (255 0 0)) ; red
123 ("brightgreen" 10 ( 0 255 0)) ; green
124 ("brightyellow" 11 (255 255 0)) ; yellow
125 ("brightblue" 12 ( 0 0 255)) ; blue
126 ("brightmagenta" 13 (255 0 255)) ; magenta
127 ("brightcyan" 14 ( 0 255 255)) ; cyan
128 ("brightwhite" 15 (255 255 255))) ; white
129 "Names of 16 standard rxvt colors, their numbers, and RGB values.")
130
131 (defun rxvt-rgb-convert-to-16bit (prim)
132 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
133 (min 65535 (round (* (/ prim 255.0) 65535.0))))
134
135 (defun rxvt-register-default-colors ()
136 "Register the default set of colors for rxvt or compatible emulator.
137
138 This function registers the number of colors returned by `display-color-cells'
139 for the currently selected frame."
140 (let* ((ncolors (display-color-cells))
141 (colors rxvt-standard-colors)
142 (color (car colors)))
143 (if (> ncolors 0)
144 ;; Clear the 8 default tty colors registered by startup.el
145 (tty-color-clear))
146 ;; Only register as many colors as are supported by the display.
147 (while (and (> ncolors 0) colors)
148 (tty-color-define (car color) (cadr color)
149 (mapcar 'rxvt-rgb-convert-to-16bit
150 (car (cddr color))))
151 (setq colors (cdr colors)
152 color (car colors)
153 ncolors (1- ncolors)))
154 ;; Modifying color mappings means realized faces don't use the
155 ;; right colors, so clear them.
156 (clear-face-cache)))
157
158 ;; rxvt puts the default colors into an environment variable
159 ;; COLORFGBG. We use this to set the background mode in a more
160 ;; intelligent way than the default guesswork in startup.el.
161 (defun rxvt-set-background-mode ()
162 "Set background mode as appropriate for the default rxvt colors."
163 (let ((fgbg (server-getenv "COLORFGBG"))
164 bg rgb)
165 (setq frame-background-mode 'light) ; default
166 (when (and fgbg
167 (string-match ".*;\\([0-9][0-9]?\\)\\'" fgbg))
168 (setq bg (string-to-number (substring fgbg (match-beginning 1))))
169 ;; The next line assumes that rxvt-standard-colors are ordered
170 ;; by the color index in the ascending order!
171 (setq rgb (car (cddr (nth bg rxvt-standard-colors))))
172 ;; See the commentary in frame-set-background-mode about the
173 ;; computation below.
174 (if (< (apply '+ rgb)
175 ;; The following line assumes that white is the 15th
176 ;; color in rxvt-standard-colors.
177 (* (apply '+ (car (cddr (nth 15 rxvt-standard-colors)))) 0.6))
178 (setq frame-background-mode 'dark)))
179 (frame-set-background-mode (selected-frame))))
180
181 ;; Do it!
182 (rxvt-register-default-colors)
183 (rxvt-set-background-mode)
184 ;; This recomputes all the default faces given the colors we've just set up.
185 (tty-set-up-initial-frame-faces)
186
187 ;;; arch-tag: 20cf2fb6-6318-4bab-9dbf-1d15048f2257
188 ;;; rxvt.el ends here