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