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