]> code.delx.au - gnu-emacs/blob - lisp/term/xterm.el
Merged from emacs@sv.gnu.org
[gnu-emacs] / lisp / term / xterm.el
1 ;;; xterm.el --- define function key sequences and standard colors for xterm
2
3 ;; Copyright (C) 1995, 2002, 2003, 2004, 2005,
4 ;; 2006 Free Software Foundation, Inc.
5
6 ;; Author: FSF
7 ;; Keywords: terminals
8
9 ;; This file is part of GNU Emacs.
10
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
14 ;; any later version.
15
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
25
26 ;;; Commentary:
27
28 ;;; Code:
29
30 (defvar xterm-function-map nil
31 "Function key map overrides for xterm.")
32
33 ;; Make reloads faster.
34 (unless xterm-function-map
35 (setq xterm-function-map (make-sparse-keymap))
36 ;; xterm from X.org 6.8.2 uses these key definitions.
37 (define-key xterm-function-map "\eOP" [f1])
38 (define-key xterm-function-map "\eOQ" [f2])
39 (define-key xterm-function-map "\eOR" [f3])
40 (define-key xterm-function-map "\eOS" [f4])
41 (define-key xterm-function-map "\e[15~" [f5])
42 (define-key xterm-function-map "\e[17~" [f6])
43 (define-key xterm-function-map "\e[18~" [f7])
44 (define-key xterm-function-map "\e[19~" [f8])
45 (define-key xterm-function-map "\e[20~" [f9])
46 (define-key xterm-function-map "\e[21~" [f10])
47 (define-key xterm-function-map "\e[23~" [f11])
48 (define-key xterm-function-map "\e[24~" [f12])
49
50 (define-key xterm-function-map "\eO2P" [S-f1])
51 (define-key xterm-function-map "\eO2Q" [S-f2])
52 (define-key xterm-function-map "\eO2R" [S-f3])
53 (define-key xterm-function-map "\eO2S" [S-f4])
54 (define-key xterm-function-map "\e[15;2~" [S-f5])
55 (define-key xterm-function-map "\e[17;2~" [S-f6])
56 (define-key xterm-function-map "\e[18;2~" [S-f7])
57 (define-key xterm-function-map "\e[19;2~" [S-f8])
58 (define-key xterm-function-map "\e[20;2~" [S-f9])
59 (define-key xterm-function-map "\e[21;2~" [S-f10])
60 (define-key xterm-function-map "\e[23;2~" [S-f11])
61 (define-key xterm-function-map "\e[24;2~" [S-f12])
62
63 (define-key xterm-function-map "\eO5P" [C-f1])
64 (define-key xterm-function-map "\eO5Q" [C-f2])
65 (define-key xterm-function-map "\eO5R" [C-f3])
66 (define-key xterm-function-map "\eO5S" [C-f4])
67 (define-key xterm-function-map "\e[15;5~" [C-f5])
68 (define-key xterm-function-map "\e[17;5~" [C-f6])
69 (define-key xterm-function-map "\e[18;5~" [C-f7])
70 (define-key xterm-function-map "\e[19;5~" [C-f8])
71 (define-key xterm-function-map "\e[20;5~" [C-f9])
72 (define-key xterm-function-map "\e[21;5~" [C-f10])
73 (define-key xterm-function-map "\e[23;5~" [C-f11])
74 (define-key xterm-function-map "\e[24;5~" [C-f12])
75
76 (define-key xterm-function-map "\eO6P" [C-S-f1])
77 (define-key xterm-function-map "\eO6Q" [C-S-f2])
78 (define-key xterm-function-map "\eO6R" [C-S-f3])
79 (define-key xterm-function-map "\eO6S" [C-S-f4])
80 (define-key xterm-function-map "\e[15;6~" [C-S-f5])
81 (define-key xterm-function-map "\e[17;6~" [C-S-f6])
82 (define-key xterm-function-map "\e[18;6~" [C-S-f7])
83 (define-key xterm-function-map "\e[19;6~" [C-S-f8])
84 (define-key xterm-function-map "\e[20;6~" [C-S-f9])
85 (define-key xterm-function-map "\e[21;6~" [C-S-f10])
86 (define-key xterm-function-map "\e[23;6~" [C-S-f11])
87 (define-key xterm-function-map "\e[24;6~" [C-S-f12])
88
89 (define-key xterm-function-map "\eO3P" [A-f1])
90 (define-key xterm-function-map "\eO3Q" [A-f2])
91 (define-key xterm-function-map "\eO3R" [A-f3])
92 (define-key xterm-function-map "\eO3S" [A-f4])
93 (define-key xterm-function-map "\e[15;3~" [A-f5])
94 (define-key xterm-function-map "\e[17;3~" [A-f6])
95 (define-key xterm-function-map "\e[18;3~" [A-f7])
96 (define-key xterm-function-map "\e[19;3~" [A-f8])
97 (define-key xterm-function-map "\e[20;3~" [A-f9])
98 (define-key xterm-function-map "\e[21;3~" [A-f10])
99 (define-key xterm-function-map "\e[23;3~" [A-f11])
100 (define-key xterm-function-map "\e[24;3~" [A-f12])
101
102 (define-key xterm-function-map "\eOA" [up])
103 (define-key xterm-function-map "\eOB" [down])
104 (define-key xterm-function-map "\eOC" [right])
105 (define-key xterm-function-map "\eOD" [left])
106 (define-key xterm-function-map "\eOF" [end])
107 (define-key xterm-function-map "\eOH" [home])
108
109 (define-key xterm-function-map "\e[1;2A" [S-up])
110 (define-key xterm-function-map "\e[1;2B" [S-down])
111 (define-key xterm-function-map "\e[1;2C" [S-right])
112 (define-key xterm-function-map "\e[1;2D" [S-left])
113 (define-key xterm-function-map "\e[1;2F" [S-end])
114 (define-key xterm-function-map "\e[1;2H" [S-home])
115
116 (define-key xterm-function-map "\e[1;5A" [C-up])
117 (define-key xterm-function-map "\e[1;5B" [C-down])
118 (define-key xterm-function-map "\e[1;5C" [C-right])
119 (define-key xterm-function-map "\e[1;5D" [C-left])
120 (define-key xterm-function-map "\e[1;5F" [C-end])
121 (define-key xterm-function-map "\e[1;5H" [C-home])
122
123 (define-key xterm-function-map "\e[1;6A" [C-S-up])
124 (define-key xterm-function-map "\e[1;6B" [C-S-down])
125 (define-key xterm-function-map "\e[1;6C" [C-S-right])
126 (define-key xterm-function-map "\e[1;6D" [C-S-left])
127 (define-key xterm-function-map "\e[1;6F" [C-S-end])
128 (define-key xterm-function-map "\e[1;6H" [C-S-home])
129
130 (define-key xterm-function-map "\e[1;3A" [A-up])
131 (define-key xterm-function-map "\e[1;3B" [A-down])
132 (define-key xterm-function-map "\e[1;3C" [A-right])
133 (define-key xterm-function-map "\e[1;3D" [A-left])
134 (define-key xterm-function-map "\e[1;3F" [A-end])
135 (define-key xterm-function-map "\e[1;3H" [A-home])
136
137 (define-key xterm-function-map "\e[2~" [insert])
138 (define-key xterm-function-map "\e[3~" [delete])
139 (define-key xterm-function-map "\e[5~" [prior])
140 (define-key xterm-function-map "\e[6~" [next])
141
142 (define-key xterm-function-map "\e[2;2~" [S-insert])
143 (define-key xterm-function-map "\e[3;2~" [S-delete])
144 (define-key xterm-function-map "\e[5;2~" [S-prior])
145 (define-key xterm-function-map "\e[6;2~" [S-next])
146
147 (define-key xterm-function-map "\e[2;5~" [C-insert])
148 (define-key xterm-function-map "\e[3;5~" [C-delete])
149 (define-key xterm-function-map "\e[5;5~" [C-prior])
150 (define-key xterm-function-map "\e[6;5~" [C-next])
151
152 (define-key xterm-function-map "\e[2;6~" [C-S-insert])
153 (define-key xterm-function-map "\e[3;6~" [C-S-delete])
154 (define-key xterm-function-map "\e[5;6~" [C-S-prior])
155 (define-key xterm-function-map "\e[6;6~" [C-S-next])
156
157 (define-key xterm-function-map "\e[2;3~" [A-insert])
158 (define-key xterm-function-map "\e[3;3~" [A-delete])
159 (define-key xterm-function-map "\e[5;3~" [A-prior])
160 (define-key xterm-function-map "\e[6;3~" [A-next])
161
162 (define-key xterm-function-map "\e[4~" [select])
163 (define-key xterm-function-map "\e[29~" [print])
164
165 ;; Other versions of xterm might emit these.
166 (define-key xterm-function-map "\e[A" [up])
167 (define-key xterm-function-map "\e[B" [down])
168 (define-key xterm-function-map "\e[C" [right])
169 (define-key xterm-function-map "\e[D" [left])
170 (define-key xterm-function-map "\e[1~" [home])
171
172 (define-key xterm-function-map "\e[1;2A" [S-up])
173 (define-key xterm-function-map "\e[1;2B" [S-down])
174 (define-key xterm-function-map "\e[1;2C" [S-right])
175 (define-key xterm-function-map "\e[1;2D" [S-left])
176 (define-key xterm-function-map "\e[1;2F" [S-end])
177 (define-key xterm-function-map "\e[1;2H" [S-home])
178
179 (define-key xterm-function-map "\e[1;5A" [C-up])
180 (define-key xterm-function-map "\e[1;5B" [C-down])
181 (define-key xterm-function-map "\e[1;5C" [C-right])
182 (define-key xterm-function-map "\e[1;5D" [C-left])
183 (define-key xterm-function-map "\e[1;5F" [C-end])
184 (define-key xterm-function-map "\e[1;5H" [C-home])
185
186 (define-key xterm-function-map "\e[11~" [f1])
187 (define-key xterm-function-map "\e[12~" [f2])
188 (define-key xterm-function-map "\e[13~" [f3])
189 (define-key xterm-function-map "\e[14~" [f4]))
190
191 (defun terminal-init-xterm ()
192 "Terminal initialization function for xterm."
193 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
194 ;; rxvt's keybindings that are incompatible with xterm's. It is
195 ;; better in that case to use rxvt's initializion function.
196 (if (and (getenv "COLORTERM" (terminal-id))
197 (string-match "\\`rxvt" (getenv "COLORTERM" (terminal-id))))
198 (progn
199 (eval-and-compile (load "term/rxvt"))
200 (terminal-init-rxvt))
201
202 ;; The terminal-local stuff only need to be set up on the first
203 ;; frame on that device.
204 (when (eq 1 (length (frames-on-display-list)))
205 ;; The terminal intialization C code file might have initialized
206 ;; function keys F13->F60 from the termcap/terminfo information. On
207 ;; a PC-style keyboard these keys correspond to
208 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The
209 ;; code here subsitutes the corresponding defintions in
210 ;; function-key-map. This substitution is needed because if a key
211 ;; definition if found in function-key-map, there are no further
212 ;; lookups in other keymaps.
213 (substitute-key-definition [f13] [S-f1] local-function-key-map)
214 (substitute-key-definition [f14] [S-f2] local-function-key-map)
215 (substitute-key-definition [f15] [S-f3] local-function-key-map)
216 (substitute-key-definition [f16] [S-f4] local-function-key-map)
217 (substitute-key-definition [f17] [S-f5] local-function-key-map)
218 (substitute-key-definition [f18] [S-f6] local-function-key-map)
219 (substitute-key-definition [f19] [S-f7] local-function-key-map)
220 (substitute-key-definition [f20] [S-f8] local-function-key-map)
221 (substitute-key-definition [f21] [S-f9] local-function-key-map)
222 (substitute-key-definition [f22] [S-f10] local-function-key-map)
223 (substitute-key-definition [f23] [S-f11] local-function-key-map)
224 (substitute-key-definition [f24] [S-f12] local-function-key-map)
225
226 (substitute-key-definition [f25] [C-f1] local-function-key-map)
227 (substitute-key-definition [f26] [C-f2] local-function-key-map)
228 (substitute-key-definition [f27] [C-f3] local-function-key-map)
229 (substitute-key-definition [f28] [C-f4] local-function-key-map)
230 (substitute-key-definition [f29] [C-f5] local-function-key-map)
231 (substitute-key-definition [f30] [C-f6] local-function-key-map)
232 (substitute-key-definition [f31] [C-f7] local-function-key-map)
233 (substitute-key-definition [f32] [C-f8] local-function-key-map)
234 (substitute-key-definition [f33] [C-f9] local-function-key-map)
235 (substitute-key-definition [f34] [C-f10] local-function-key-map)
236 (substitute-key-definition [f35] [C-f11] local-function-key-map)
237 (substitute-key-definition [f36] [C-f12] local-function-key-map)
238
239 (substitute-key-definition [f37] [C-S-f1] local-function-key-map)
240 (substitute-key-definition [f38] [C-S-f2] local-function-key-map)
241 (substitute-key-definition [f39] [C-S-f3] local-function-key-map)
242 (substitute-key-definition [f40] [C-S-f4] local-function-key-map)
243 (substitute-key-definition [f41] [C-S-f5] local-function-key-map)
244 (substitute-key-definition [f42] [C-S-f6] local-function-key-map)
245 (substitute-key-definition [f43] [C-S-f7] local-function-key-map)
246 (substitute-key-definition [f44] [C-S-f8] local-function-key-map)
247 (substitute-key-definition [f45] [C-S-f9] local-function-key-map)
248 (substitute-key-definition [f46] [C-S-f10] local-function-key-map)
249 (substitute-key-definition [f47] [C-S-f11] local-function-key-map)
250 (substitute-key-definition [f48] [C-S-f12] local-function-key-map)
251
252 (substitute-key-definition [f49] [A-f1] local-function-key-map)
253 (substitute-key-definition [f50] [A-f2] local-function-key-map)
254 (substitute-key-definition [f51] [A-f3] local-function-key-map)
255 (substitute-key-definition [f52] [A-f4] local-function-key-map)
256 (substitute-key-definition [f53] [A-f5] local-function-key-map)
257 (substitute-key-definition [f54] [A-f6] local-function-key-map)
258 (substitute-key-definition [f55] [A-f7] local-function-key-map)
259 (substitute-key-definition [f56] [A-f8] local-function-key-map)
260 (substitute-key-definition [f57] [A-f9] local-function-key-map)
261 (substitute-key-definition [f58] [A-f10] local-function-key-map)
262 (substitute-key-definition [f59] [A-f11] local-function-key-map)
263 (substitute-key-definition [f60] [A-f12] local-function-key-map))
264
265 ;; Use inheritance to let the main keymap override those defaults.
266 ;; This way we don't override terminfo-derived settings or settings
267 ;; made in the .emacs file.
268 (let ((m (copy-keymap xterm-function-map)))
269 (set-keymap-parent m (keymap-parent local-function-key-map))
270 (set-keymap-parent local-function-key-map m)))
271
272 ;; Do it!
273 (xterm-register-default-colors)
274 ;; This recomputes all the default faces given the colors we've just set up.
275 (tty-set-up-initial-frame-faces))
276
277 ;; Set up colors, for those versions of xterm that support it.
278 (defvar xterm-standard-colors
279 ;; The names in the comments taken from XTerm-col.ad in the xterm
280 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
281 ;; from rgb.txt.
282 '(("black" 0 ( 0 0 0)) ; black
283 ("red" 1 (205 0 0)) ; red3
284 ("green" 2 ( 0 205 0)) ; green3
285 ("yellow" 3 (205 205 0)) ; yellow3
286 ("blue" 4 ( 0 0 238)) ; blue2
287 ("magenta" 5 (205 0 205)) ; magenta3
288 ("cyan" 6 ( 0 205 205)) ; cyan3
289 ("white" 7 (229 229 229)) ; gray90
290 ("brightblack" 8 (127 127 127)) ; gray50
291 ("brightred" 9 (255 0 0)) ; red
292 ("brightgreen" 10 ( 0 255 0)) ; green
293 ("brightyellow" 11 (255 255 0)) ; yellow
294 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
295 ("brightmagenta" 13 (255 0 255)) ; magenta
296 ("brightcyan" 14 ( 0 255 255)) ; cyan
297 ("brightwhite" 15 (255 255 255))) ; white
298 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
299
300 (defun xterm-rgb-convert-to-16bit (prim)
301 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
302 (logior prim (lsh prim 8)))
303
304 (defun xterm-register-default-colors ()
305 "Register the default set of colors for xterm or compatible emulator.
306
307 This function registers the number of colors returned by `display-color-cells'
308 for the currently selected frame. The first 16 colors are taken from
309 `xterm-standard-colors', which see, while the rest are computed assuming
310 either the 88- or 256-color standard color scheme supported by latest
311 versions of xterm."
312 (let* ((ncolors (display-color-cells (selected-frame)))
313 (colors xterm-standard-colors)
314 (color (car colors)))
315 (if (> ncolors 0)
316 ;; Clear the 8 default tty colors registered by startup.el
317 (tty-color-clear))
318 ;; Only register as many colors as are supported by the display.
319 (while (and (> ncolors 0) colors)
320 (tty-color-define (car color) (cadr color)
321 (mapcar 'xterm-rgb-convert-to-16bit
322 (car (cddr color))))
323 (setq colors (cdr colors)
324 color (car colors)
325 ncolors (1- ncolors)))
326 ;; We've exhausted the colors from `xterm-standard-colors'. If there
327 ;; are more colors to support, compute them now.
328 (when (> ncolors 0)
329 (cond
330 ((= ncolors 240) ; 256-color xterm
331 ;; 216 non-gray colors first
332 (let ((r 0) (g 0) (b 0))
333 (while (> ncolors 24)
334 ;; This and other formulae taken from 256colres.pl and
335 ;; 88colres.pl in the xterm distribution.
336 (tty-color-define (format "color-%d" (- 256 ncolors))
337 (- 256 ncolors)
338 (mapcar 'xterm-rgb-convert-to-16bit
339 (list (if (zerop r) 0 (+ (* r 40) 55))
340 (if (zerop g) 0 (+ (* g 40) 55))
341 (if (zerop b) 0 (+ (* b 40) 55)))))
342
343 (setq b (1+ b))
344 (if (> b 5)
345 (setq g (1+ g)
346 b 0))
347 (if (> g 5)
348 (setq r (1+ r)
349 g 0))
350 (setq ncolors (1- ncolors))))
351 ;; Now the 24 gray colors
352 (while (> ncolors 0)
353 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
354 (tty-color-define (format "color-%d" (- 256 ncolors))
355 (- 256 ncolors)
356 (list color color color))
357 (setq ncolors (1- ncolors))))
358 ((= ncolors 72) ; 88-color xterm
359 ;; 64 non-gray colors
360 (let ((levels '(0 139 205 255))
361 (r 0) (g 0) (b 0))
362 (while (> ncolors 8)
363 (tty-color-define (format "color-%d" (- 88 ncolors))
364 (- 88 ncolors)
365 (mapcar 'xterm-rgb-convert-to-16bit
366 (list (nth r levels)
367 (nth g levels)
368 (nth b levels))))
369 (setq b (1+ b))
370 (if (> b 3)
371 (setq g (1+ g)
372 b 0))
373 (if (> g 3)
374 (setq r (1+ r)
375 g 0))
376 (setq ncolors (1- ncolors))))
377 ;; Now the 8 gray colors
378 (while (> ncolors 0)
379 (setq color (xterm-rgb-convert-to-16bit
380 (floor
381 (if (= ncolors 8)
382 46.36363636
383 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
384 (tty-color-define (format "color-%d" (- 88 ncolors))
385 (- 88 ncolors)
386 (list color color color))
387 (setq ncolors (1- ncolors))))
388 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
389 ;; Modifying color mappings means realized faces don't use the
390 ;; right colors, so clear them.
391 (clear-face-cache)))
392
393 ;; arch-tag: 12e7ebdd-1e6c-4b25-b0f9-35ace25e855a
394 ;;; xterm.el ends here