*** empty log message ***
[emacs.git] / lisp / term / xterm.el
blob372b5f65ff1f3e895a6e731186b3fbda5dd6ce19
1 ;;; xterm.el --- define function key sequences and standard colors for xterm
3 ;; Copyright (C) 1995, 2002 Free Software Foundation, Inc.
5 ;; Author: FSF
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 2, or (at your option)
13 ;; 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; 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.
25 ;;; Commentary:
27 ;;; Code:
29 (let ((map (make-sparse-keymap)))
30 (define-key map "\e[A" [up])
31 (define-key map "\e[B" [down])
32 (define-key map "\e[C" [right])
33 (define-key map "\e[D" [left])
34 (define-key map "\e[1~" [home])
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[11~" [f1])
41 (define-key map "\e[12~" [f2])
42 (define-key map "\e[13~" [f3])
43 (define-key map "\e[14~" [f4])
44 (define-key map "\e[15~" [f5])
45 (define-key map "\e[17~" [f6])
46 (define-key map "\e[18~" [f7])
47 (define-key map "\e[19~" [f8])
48 (define-key map "\e[20~" [f9])
49 (define-key map "\e[21~" [f10])
50 (define-key map "\e[23~" [f11])
51 (define-key map "\e[24~" [f12])
52 (define-key map "\e[29~" [print])
54 (define-key map "\e[2;2~" [S-insert])
55 (define-key map "\e[3;2~" [S-delete])
56 (define-key map "\e[5;2~" [S-prior])
57 (define-key map "\e[6;2~" [S-next])
59 (define-key map "\e[2;5~" [C-insert])
60 (define-key map "\e[3;5~" [C-delete])
61 (define-key map "\e[5;5~" [C-prior])
62 (define-key map "\e[6;5~" [C-next])
64 (define-key map "\eOA" [up])
65 (define-key map "\eOB" [down])
66 (define-key map "\eOC" [right])
67 (define-key map "\eOD" [left])
68 (define-key map "\eOF" [end])
69 (define-key map "\eOH" [home])
71 (define-key map "\eO2A" [S-up])
72 (define-key map "\eO2B" [S-down])
73 (define-key map "\eO2C" [S-right])
74 (define-key map "\eO2D" [S-left])
75 (define-key map "\eO2F" [S-end])
76 (define-key map "\eO2H" [S-home])
78 (define-key map "\eO5A" [C-up])
79 (define-key map "\eO5B" [C-down])
80 (define-key map "\eO5C" [C-right])
81 (define-key map "\eO5D" [C-left])
82 (define-key map "\eO5F" [C-end])
83 (define-key map "\eO5H" [C-home])
85 ;; Use inheritance to let the main keymap override those defaults.
86 ;; This way we don't override terminfo-derived settings or settings
87 ;; made in the .emacs file.
88 (set-keymap-parent map (keymap-parent function-key-map))
89 (set-keymap-parent function-key-map map))
91 ;; Set up colors, for those versions of xterm that support it.
92 (defvar xterm-standard-colors
93 ;; The names in the comments taken from XTerm-col.ad in the xterm
94 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
95 ;; from rgb.txt.
96 '(("black" 0 ( 0 0 0)) ; black
97 ("red" 1 (205 0 0)) ; red3
98 ("green" 2 ( 0 205 0)) ; green3
99 ("yellow" 3 (205 205 0)) ; yellow3
100 ("blue" 4 ( 0 0 205)) ; blue3
101 ("magenta" 5 (205 0 205)) ; magenta3
102 ("cyan" 6 ( 0 205 205)) ; cyan3
103 ("white" 7 (229 229 229)) ; gray90
104 ("brightblack" 8 ( 77 77 77)) ; gray30
105 ("brightred" 9 (255 0 0)) ; red
106 ("brightgreen" 10 ( 0 255 0)) ; green
107 ("brightyellow" 11 (255 255 0)) ; yellow
108 ("brightblue" 12 ( 0 0 255)) ; blue
109 ("brightmagenta" 13 (255 0 255)) ; magenta
110 ("brightcyan" 14 ( 0 255 255)) ; cyan
111 ("brightwhite" 15 (255 255 255))) ; white
112 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
114 (defun xterm-rgb-convert-to-16bit (prim)
115 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
116 (min 65535 (round (* (/ prim 255.0) 65535.0))))
118 (defun xterm-register-default-colors ()
119 "Register the default set of colors for xterm or compatible emulator.
121 This function registers the number of colors returned by `display-color-cells'
122 for the currently selected frame. The first 16 colors are taken from
123 `xterm-standard-colors', which see, while the rest are computed assuming
124 either the 88- or 256-color standard color scheme supported by latest
125 versions of xterm."
126 (let* ((ncolors (display-color-cells))
127 (colors xterm-standard-colors)
128 (color (car colors)))
129 (if (> ncolors 0)
130 ;; Clear the 8 default tty colors registered by startup.el
131 (tty-color-clear))
132 ;; Only register as many colors as are supported by the display.
133 (while (and (> ncolors 0) colors)
134 (tty-color-define (car color) (cadr color)
135 (mapcar 'xterm-rgb-convert-to-16bit
136 (car (cddr color))))
137 (setq colors (cdr colors)
138 color (car colors)
139 ncolors (1- ncolors)))
140 ;; We've exhausted the colors from `xterm-standard-colors'. If there
141 ;; are more colors to support, compute them now.
142 (when (> ncolors 0)
143 (cond
144 ((= ncolors 240) ; 256-color xterm
145 ;; 216 non-gray colors first
146 (let ((r 0) (g 0) (b 0))
147 (while (> ncolors 24)
148 ;; This and other formulae taken from 256colres.pl and
149 ;; 88colres.pl in the xterm distribution.
150 (tty-color-define (format "color-%d" (- 256 ncolors))
151 (- 256 ncolors)
152 (mapcar 'xterm-rgb-convert-to-16bit
153 (list (round (* r 42.5))
154 (round (* g 42.5))
155 (round (* b 42.5)))))
156 (setq b (1+ b))
157 (if (> b 5)
158 (setq g (1+ g)
159 b 0))
160 (if (> g 5)
161 (setq r (1+ r)
162 g 0))
163 (setq ncolors (1- ncolors))))
164 ;; Now the 24 gray colors
165 (while (> ncolors 0)
166 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
167 (tty-color-define (format "color-%d" (- 256 ncolors))
168 (- 256 ncolors)
169 (list color color color))
170 (setq ncolors (1- ncolors))))
171 ((= ncolors 72) ; 88-color xterm
172 ;; 64 non-gray colors
173 (let ((levels '(0 139 205 255))
174 (r 0) (g 0) (b 0))
175 (while (> ncolors 8)
176 (tty-color-define (format "color-%d" (- 88 ncolors))
177 (- 88 ncolors)
178 (mapcar 'xterm-rgb-convert-to-16bit
179 (list (nth r levels)
180 (nth g levels)
181 (nth b levels))))
182 (setq b (1+ b))
183 (if (> b 3)
184 (setq g (1+ g)
185 b 0))
186 (if (> g 3)
187 (setq r (1+ r)
188 g 0))
189 (setq ncolors (1- ncolors))))
190 ;; Now the 8 gray colors
191 (while (> ncolors 0)
192 (setq color (xterm-rgb-convert-to-16bit
193 (round
194 (if (= ncolors 8)
195 46.36363636
196 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
197 (tty-color-define (format "color-%d" (- 88 ncolors))
198 (- 88 ncolors)
199 (list color color color))
200 (setq ncolors (1- ncolors))))
201 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
202 ;; Modifying color mappings means realized faces don't use the
203 ;; right colors, so clear them.
204 (clear-face-cache)))
206 ;; rxvt puts the default colors into an environment variable
207 ;; COLORFGBG. We use this to set the background mode in a more
208 ;; intelligent way than the default guesswork in startup.el.
209 (defun xterm-rxvt-set-background-mode ()
210 "Set background mode as appropriate for the default rxvt colors."
211 (let ((fgbg (getenv "COLORFGBG"))
212 bg rgb)
213 (setq frame-background-mode 'light) ; default
214 (when (and fgbg
215 (string-match ".*;\\([0-9][0-9]?\\)\\'" fgbg))
216 (setq bg (string-to-number (substring fgbg (match-beginning 1))))
217 ;; The next line assumes that xterm-standard-colors are ordered
218 ;; by the color index in the ascending order!
219 (setq rgb (car (cddr (nth bg xterm-standard-colors))))
220 ;; See the commentary in frame-set-background-mode about the
221 ;; computation below.
222 (if (< (apply '+ rgb)
223 ;; The following line assumes that white is the 15th
224 ;; color in xterm-standard-colors.
225 (* (apply '+ (car (cddr (nth 15 xterm-standard-colors)))) 0.6))
226 (setq frame-background-mode 'dark)))
227 (frame-set-background-mode (selected-frame))))
229 ;; Do it!
230 (xterm-register-default-colors)
231 ;; If this xterm is actually a disguised rxvt, be more intelligent about
232 ;; determining the background mode.
233 (and (getenv "COLORTERM")
234 (string-match "\\`rxvt" (getenv "COLORTERM"))
235 (xterm-rxvt-set-background-mode))
236 ;; This recomputes all the default faces given the colors we've just set up.
237 (tty-set-up-initial-frame-faces)
239 ;;; xterm.el ends here