Merged from emacs@sv.gnu.org
[bpt/emacs.git] / 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 (make-sparse-keymap)
31 "Function key map overrides for xterm.")
32
33 ;; xterm from X.org 6.8.2 uses these key definitions.
34 (define-key xterm-function-map "\eOP" [f1])
35 (define-key xterm-function-map "\eOQ" [f2])
36 (define-key xterm-function-map "\eOR" [f3])
37 (define-key xterm-function-map "\eOS" [f4])
38 (define-key xterm-function-map "\e[15~" [f5])
39 (define-key xterm-function-map "\e[17~" [f6])
40 (define-key xterm-function-map "\e[18~" [f7])
41 (define-key xterm-function-map "\e[19~" [f8])
42 (define-key xterm-function-map "\e[20~" [f9])
43 (define-key xterm-function-map "\e[21~" [f10])
44 (define-key xterm-function-map "\e[23~" [f11])
45 (define-key xterm-function-map "\e[24~" [f12])
46
47 (define-key xterm-function-map "\eO2P" [S-f1])
48 (define-key xterm-function-map "\eO2Q" [S-f2])
49 (define-key xterm-function-map "\eO2R" [S-f3])
50 (define-key xterm-function-map "\eO2S" [S-f4])
51 (define-key xterm-function-map "\e[15;2~" [S-f5])
52 (define-key xterm-function-map "\e[17;2~" [S-f6])
53 (define-key xterm-function-map "\e[18;2~" [S-f7])
54 (define-key xterm-function-map "\e[19;2~" [S-f8])
55 (define-key xterm-function-map "\e[20;2~" [S-f9])
56 (define-key xterm-function-map "\e[21;2~" [S-f10])
57 (define-key xterm-function-map "\e[23;2~" [S-f11])
58 (define-key xterm-function-map "\e[24;2~" [S-f12])
59
60 (define-key xterm-function-map "\eO5P" [C-f1])
61 (define-key xterm-function-map "\eO5Q" [C-f2])
62 (define-key xterm-function-map "\eO5R" [C-f3])
63 (define-key xterm-function-map "\eO5S" [C-f4])
64 (define-key xterm-function-map "\e[15;5~" [C-f5])
65 (define-key xterm-function-map "\e[17;5~" [C-f6])
66 (define-key xterm-function-map "\e[18;5~" [C-f7])
67 (define-key xterm-function-map "\e[19;5~" [C-f8])
68 (define-key xterm-function-map "\e[20;5~" [C-f9])
69 (define-key xterm-function-map "\e[21;5~" [C-f10])
70 (define-key xterm-function-map "\e[23;5~" [C-f11])
71 (define-key xterm-function-map "\e[24;5~" [C-f12])
72
73 (define-key xterm-function-map "\eO6P" [C-S-f1])
74 (define-key xterm-function-map "\eO6Q" [C-S-f2])
75 (define-key xterm-function-map "\eO6R" [C-S-f3])
76 (define-key xterm-function-map "\eO6S" [C-S-f4])
77 (define-key xterm-function-map "\e[15;6~" [C-S-f5])
78 (define-key xterm-function-map "\e[17;6~" [C-S-f6])
79 (define-key xterm-function-map "\e[18;6~" [C-S-f7])
80 (define-key xterm-function-map "\e[19;6~" [C-S-f8])
81 (define-key xterm-function-map "\e[20;6~" [C-S-f9])
82 (define-key xterm-function-map "\e[21;6~" [C-S-f10])
83 (define-key xterm-function-map "\e[23;6~" [C-S-f11])
84 (define-key xterm-function-map "\e[24;6~" [C-S-f12])
85
86 (define-key xterm-function-map "\eO3P" [A-f1])
87 (define-key xterm-function-map "\eO3Q" [A-f2])
88 (define-key xterm-function-map "\eO3R" [A-f3])
89 (define-key xterm-function-map "\eO3S" [A-f4])
90 (define-key xterm-function-map "\e[15;3~" [A-f5])
91 (define-key xterm-function-map "\e[17;3~" [A-f6])
92 (define-key xterm-function-map "\e[18;3~" [A-f7])
93 (define-key xterm-function-map "\e[19;3~" [A-f8])
94 (define-key xterm-function-map "\e[20;3~" [A-f9])
95 (define-key xterm-function-map "\e[21;3~" [A-f10])
96 (define-key xterm-function-map "\e[23;3~" [A-f11])
97 (define-key xterm-function-map "\e[24;3~" [A-f12])
98
99 (define-key xterm-function-map "\eOA" [up])
100 (define-key xterm-function-map "\eOB" [down])
101 (define-key xterm-function-map "\eOC" [right])
102 (define-key xterm-function-map "\eOD" [left])
103 (define-key xterm-function-map "\eOF" [end])
104 (define-key xterm-function-map "\eOH" [home])
105
106 (define-key xterm-function-map "\e[1;2A" [S-up])
107 (define-key xterm-function-map "\e[1;2B" [S-down])
108 (define-key xterm-function-map "\e[1;2C" [S-right])
109 (define-key xterm-function-map "\e[1;2D" [S-left])
110 (define-key xterm-function-map "\e[1;2F" [S-end])
111 (define-key xterm-function-map "\e[1;2H" [S-home])
112
113 (define-key xterm-function-map "\e[1;5A" [C-up])
114 (define-key xterm-function-map "\e[1;5B" [C-down])
115 (define-key xterm-function-map "\e[1;5C" [C-right])
116 (define-key xterm-function-map "\e[1;5D" [C-left])
117 (define-key xterm-function-map "\e[1;5F" [C-end])
118 (define-key xterm-function-map "\e[1;5H" [C-home])
119
120 (define-key xterm-function-map "\e[1;6A" [C-S-up])
121 (define-key xterm-function-map "\e[1;6B" [C-S-down])
122 (define-key xterm-function-map "\e[1;6C" [C-S-right])
123 (define-key xterm-function-map "\e[1;6D" [C-S-left])
124 (define-key xterm-function-map "\e[1;6F" [C-S-end])
125 (define-key xterm-function-map "\e[1;6H" [C-S-home])
126
127 (define-key xterm-function-map "\e[1;3A" [A-up])
128 (define-key xterm-function-map "\e[1;3B" [A-down])
129 (define-key xterm-function-map "\e[1;3C" [A-right])
130 (define-key xterm-function-map "\e[1;3D" [A-left])
131 (define-key xterm-function-map "\e[1;3F" [A-end])
132 (define-key xterm-function-map "\e[1;3H" [A-home])
133
134 (define-key xterm-function-map "\e[2~" [insert])
135 (define-key xterm-function-map "\e[3~" [delete])
136 (define-key xterm-function-map "\e[5~" [prior])
137 (define-key xterm-function-map "\e[6~" [next])
138
139 (define-key xterm-function-map "\e[2;2~" [S-insert])
140 (define-key xterm-function-map "\e[3;2~" [S-delete])
141 (define-key xterm-function-map "\e[5;2~" [S-prior])
142 (define-key xterm-function-map "\e[6;2~" [S-next])
143
144 (define-key xterm-function-map "\e[2;5~" [C-insert])
145 (define-key xterm-function-map "\e[3;5~" [C-delete])
146 (define-key xterm-function-map "\e[5;5~" [C-prior])
147 (define-key xterm-function-map "\e[6;5~" [C-next])
148
149 (define-key xterm-function-map "\e[2;6~" [C-S-insert])
150 (define-key xterm-function-map "\e[3;6~" [C-S-delete])
151 (define-key xterm-function-map "\e[5;6~" [C-S-prior])
152 (define-key xterm-function-map "\e[6;6~" [C-S-next])
153
154 (define-key xterm-function-map "\e[2;3~" [A-insert])
155 (define-key xterm-function-map "\e[3;3~" [A-delete])
156 (define-key xterm-function-map "\e[5;3~" [A-prior])
157 (define-key xterm-function-map "\e[6;3~" [A-next])
158
159 (define-key xterm-function-map "\e[4~" [select])
160 (define-key xterm-function-map "\e[29~" [print])
161
162 ;; These keys are available in xterm starting from version 216
163 ;; if the modifyOtherKeys resource is set to 1.
164
165 (define-key xterm-function-map "\e[27;5;9~" [C-tab])
166 (define-key xterm-function-map "\e[27;5;13~" [C-return])
167 (define-key xterm-function-map "\e[27;5;39~" [?\C-\'])
168 (define-key xterm-function-map "\e[27;5;44~" [?\C-,])
169 (define-key xterm-function-map "\e[27;5;45~" [?\C--])
170 (define-key xterm-function-map "\e[27;5;46~" [?\C-.])
171 (define-key xterm-function-map "\e[27;5;47~" [?\C-/])
172 (define-key xterm-function-map "\e[27;5;48~" [?\C-0])
173 (define-key xterm-function-map "\e[27;5;49~" [?\C-1])
174 ;; Not all C-DIGIT keys have a distinct binding.
175 (define-key xterm-function-map "\e[27;5;57~" [?\C-9])
176 (define-key xterm-function-map "\e[27;5;59~" [(C-\;)])
177 (define-key xterm-function-map "\e[27;5;61~" [?\C-=])
178 (define-key xterm-function-map "\e[27;5;92~" [?\C-\\])
179
180 (define-key xterm-function-map "\e[27;6;33~" [?\C-!])
181 (define-key xterm-function-map "\e[27;6;34~" [?\C-\"])
182 (define-key xterm-function-map "\e[27;6;35~" [?\C-#])
183 (define-key xterm-function-map "\e[27;6;36~" [?\C-$])
184 (define-key xterm-function-map "\e[27;6;37~" [?\C-%])
185 (define-key xterm-function-map "\e[27;6;38~" [(C-&)])
186 (define-key xterm-function-map "\e[27;6;40~" [?\C-(])
187 (define-key xterm-function-map "\e[27;6;41~" [?\C-)])
188 (define-key xterm-function-map "\e[27;6;42~" [?\C-*])
189 (define-key xterm-function-map "\e[27;6;43~" [?\C-+])
190 (define-key xterm-function-map "\e[27;6;58~" [?\C-:])
191 (define-key xterm-function-map "\e[27;6;60~" [?\C-<])
192 (define-key xterm-function-map "\e[27;6;62~" [?\C->])
193 (define-key xterm-function-map "\e[27;6;63~" [(C-\?)])
194
195 ;; These are the strings emitted for various C-M- combinations
196 ;; for keyboards that the Meta and Alt modifiers are on the same
197 ;; key (usually labeled "Alt").
198 (define-key xterm-function-map "\e[27;13;9~" [(C-M-tab)])
199 (define-key xterm-function-map "\e[27;13;13~" [(C-M-return)])
200
201 (define-key xterm-function-map "\e[27;13;39~" [?\C-\M-\'])
202 (define-key xterm-function-map "\e[27;13;44~" [?\C-\M-,])
203 (define-key xterm-function-map "\e[27;13;45~" [?\C-\M--])
204 (define-key xterm-function-map "\e[27;13;46~" [?\C-\M-.])
205 (define-key xterm-function-map "\e[27;13;47~" [?\C-\M-/])
206 (define-key xterm-function-map "\e[27;13;48~" [?\C-\M-0])
207 (define-key xterm-function-map "\e[27;13;49~" [?\C-\M-1])
208 (define-key xterm-function-map "\e[27;13;50~" [?\C-\M-2])
209 (define-key xterm-function-map "\e[27;13;51~" [?\C-\M-3])
210 (define-key xterm-function-map "\e[27;13;52~" [?\C-\M-4])
211 (define-key xterm-function-map "\e[27;13;53~" [?\C-\M-5])
212 (define-key xterm-function-map "\e[27;13;54~" [?\C-\M-6])
213 (define-key xterm-function-map "\e[27;13;55~" [?\C-\M-7])
214 (define-key xterm-function-map "\e[27;13;56~" [?\C-\M-8])
215 (define-key xterm-function-map "\e[27;13;57~" [?\C-\M-9])
216 (define-key xterm-function-map "\e[27;13;59~" [?\C-\M-\;])
217 (define-key xterm-function-map "\e[27;13;61~" [?\C-\M-=])
218 (define-key xterm-function-map "\e[27;13;92~" [?\C-\M-\\])
219
220 (define-key xterm-function-map "\e[27;14;33~" [?\C-\M-!])
221 (define-key xterm-function-map "\e[27;14;34~" [?\C-\M-\"])
222 (define-key xterm-function-map "\e[27;14;35~" [?\C-\M-#])
223 (define-key xterm-function-map "\e[27;14;36~" [?\C-\M-$])
224 (define-key xterm-function-map "\e[27;14;37~" [?\C-\M-%])
225 (define-key xterm-function-map "\e[27;14;38~" [(C-M-&)])
226 (define-key xterm-function-map "\e[27;14;40~" [?\C-\M-(])
227 (define-key xterm-function-map "\e[27;14;41~" [?\C-\M-)])
228 (define-key xterm-function-map "\e[27;14;42~" [?\C-\M-*])
229 (define-key xterm-function-map "\e[27;14;43~" [?\C-\M-+])
230 (define-key xterm-function-map "\e[27;14;58~" [?\C-\M-:])
231 (define-key xterm-function-map "\e[27;14;60~" [?\C-\M-<])
232 (define-key xterm-function-map "\e[27;14;62~" [?\C-\M->])
233 (define-key xterm-function-map "\e[27;14;63~" [(C-M-\?)])
234
235 (define-key xterm-function-map "\e[27;7;9~" [(C-M-tab)])
236 (define-key xterm-function-map "\e[27;7;13~" [(C-M-return)])
237
238 (define-key xterm-function-map "\e[27;7;39~" [?\C-\M-\'])
239 (define-key xterm-function-map "\e[27;7;44~" [?\C-\M-,])
240 (define-key xterm-function-map "\e[27;7;45~" [?\C-\M--])
241 (define-key xterm-function-map "\e[27;7;46~" [?\C-\M-.])
242 (define-key xterm-function-map "\e[27;7;47~" [?\C-\M-/])
243 (define-key xterm-function-map "\e[27;7;48~" [?\C-\M-0])
244 (define-key xterm-function-map "\e[27;7;49~" [?\C-\M-1])
245 (define-key xterm-function-map "\e[27;7;50~" [?\C-\M-2])
246 (define-key xterm-function-map "\e[27;7;51~" [?\C-\M-3])
247 (define-key xterm-function-map "\e[27;7;52~" [?\C-\M-4])
248 (define-key xterm-function-map "\e[27;7;53~" [?\C-\M-5])
249 (define-key xterm-function-map "\e[27;7;54~" [?\C-\M-6])
250 (define-key xterm-function-map "\e[27;7;55~" [?\C-\M-7])
251 (define-key xterm-function-map "\e[27;7;56~" [?\C-\M-8])
252 (define-key xterm-function-map "\e[27;7;57~" [?\C-\M-9])
253 (define-key xterm-function-map "\e[27;7;59~" [?\C-\M-\;])
254 (define-key xterm-function-map "\e[27;7;61~" [?\C-\M-=])
255 (define-key xterm-function-map "\e[27;7;92~" [?\C-\M-\\])
256
257 (define-key xterm-function-map "\e[27;8;33~" [?\C-\M-!])
258 (define-key xterm-function-map "\e[27;8;34~" [?\C-\M-\"])
259 (define-key xterm-function-map "\e[27;8;35~" [?\C-\M-#])
260 (define-key xterm-function-map "\e[27;8;36~" [?\C-\M-$])
261 (define-key xterm-function-map "\e[27;8;37~" [?\C-\M-%])
262 (define-key xterm-function-map "\e[27;8;38~" [(C-M-&)])
263 (define-key xterm-function-map "\e[27;8;40~" [?\C-\M-(])
264 (define-key xterm-function-map "\e[27;8;41~" [?\C-\M-)])
265 (define-key xterm-function-map "\e[27;8;42~" [?\C-\M-*])
266 (define-key xterm-function-map "\e[27;8;43~" [?\C-\M-+])
267 (define-key xterm-function-map "\e[27;8;58~" [?\C-\M-:])
268 (define-key xterm-function-map "\e[27;8;60~" [?\C-\M-<])
269 (define-key xterm-function-map "\e[27;8;62~" [?\C-\M->])
270 (define-key xterm-function-map "\e[27;8;63~" [(C-M-\?)])
271
272 (define-key xterm-function-map "\e[27;2;9~" [S-tab])
273 (define-key xterm-function-map "\e[27;2;13~" [S-return])
274
275 (define-key xterm-function-map "\e[27;6;9~" [(C-S-tab)])
276 (define-key xterm-function-map "\e[27;6;13~" [(C-S-return)])
277
278 ;; Other versions of xterm might emit these.
279 (define-key xterm-function-map "\e[A" [up])
280 (define-key xterm-function-map "\e[B" [down])
281 (define-key xterm-function-map "\e[C" [right])
282 (define-key xterm-function-map "\e[D" [left])
283 (define-key xterm-function-map "\e[1~" [home])
284
285 (define-key xterm-function-map "\e[1;2A" [S-up])
286 (define-key xterm-function-map "\e[1;2B" [S-down])
287 (define-key xterm-function-map "\e[1;2C" [S-right])
288 (define-key xterm-function-map "\e[1;2D" [S-left])
289 (define-key xterm-function-map "\e[1;2F" [S-end])
290 (define-key xterm-function-map "\e[1;2H" [S-home])
291
292 (define-key xterm-function-map "\e[1;5A" [C-up])
293 (define-key xterm-function-map "\e[1;5B" [C-down])
294 (define-key xterm-function-map "\e[1;5C" [C-right])
295 (define-key xterm-function-map "\e[1;5D" [C-left])
296 (define-key xterm-function-map "\e[1;5F" [C-end])
297 (define-key xterm-function-map "\e[1;5H" [C-home])
298
299 (define-key xterm-function-map "\e[11~" [f1])
300 (define-key xterm-function-map "\e[12~" [f2])
301 (define-key xterm-function-map "\e[13~" [f3])
302 (define-key xterm-function-map "\e[14~" [f4])
303
304 (defun terminal-init-xterm ()
305 "Terminal initialization function for xterm."
306 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
307 ;; rxvt's keybindings are incompatible with xterm's. It is
308 ;; better in that case to use rxvt's initializion function.
309 (if (and (getenv "COLORTERM" (terminal-id))
310 (string-match "\\`rxvt" (getenv "COLORTERM" (terminal-id))))
311 (tty-run-terminal-initialization (selected-frame) "rxvt")
312
313 ;; The terminal intialization C code file might have initialized
314 ;; function keys F13->F60 from the termcap/terminfo information. On
315 ;; a PC-style keyboard these keys correspond to
316 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The
317 ;; code here subsitutes the corresponding defintions in
318 ;; function-key-map. This substitution is needed because if a key
319 ;; definition if found in function-key-map, there are no further
320 ;; lookups in other keymaps.
321 (substitute-key-definition [f13] [S-f1] local-function-key-map)
322 (substitute-key-definition [f14] [S-f2] local-function-key-map)
323 (substitute-key-definition [f15] [S-f3] local-function-key-map)
324 (substitute-key-definition [f16] [S-f4] local-function-key-map)
325 (substitute-key-definition [f17] [S-f5] local-function-key-map)
326 (substitute-key-definition [f18] [S-f6] local-function-key-map)
327 (substitute-key-definition [f19] [S-f7] local-function-key-map)
328 (substitute-key-definition [f20] [S-f8] local-function-key-map)
329 (substitute-key-definition [f21] [S-f9] local-function-key-map)
330 (substitute-key-definition [f22] [S-f10] local-function-key-map)
331 (substitute-key-definition [f23] [S-f11] local-function-key-map)
332 (substitute-key-definition [f24] [S-f12] local-function-key-map)
333
334 (substitute-key-definition [f25] [C-f1] local-function-key-map)
335 (substitute-key-definition [f26] [C-f2] local-function-key-map)
336 (substitute-key-definition [f27] [C-f3] local-function-key-map)
337 (substitute-key-definition [f28] [C-f4] local-function-key-map)
338 (substitute-key-definition [f29] [C-f5] local-function-key-map)
339 (substitute-key-definition [f30] [C-f6] local-function-key-map)
340 (substitute-key-definition [f31] [C-f7] local-function-key-map)
341 (substitute-key-definition [f32] [C-f8] local-function-key-map)
342 (substitute-key-definition [f33] [C-f9] local-function-key-map)
343 (substitute-key-definition [f34] [C-f10] local-function-key-map)
344 (substitute-key-definition [f35] [C-f11] local-function-key-map)
345 (substitute-key-definition [f36] [C-f12] local-function-key-map)
346
347 (substitute-key-definition [f37] [C-S-f1] local-function-key-map)
348 (substitute-key-definition [f38] [C-S-f2] local-function-key-map)
349 (substitute-key-definition [f39] [C-S-f3] local-function-key-map)
350 (substitute-key-definition [f40] [C-S-f4] local-function-key-map)
351 (substitute-key-definition [f41] [C-S-f5] local-function-key-map)
352 (substitute-key-definition [f42] [C-S-f6] local-function-key-map)
353 (substitute-key-definition [f43] [C-S-f7] local-function-key-map)
354 (substitute-key-definition [f44] [C-S-f8] local-function-key-map)
355 (substitute-key-definition [f45] [C-S-f9] local-function-key-map)
356 (substitute-key-definition [f46] [C-S-f10] local-function-key-map)
357 (substitute-key-definition [f47] [C-S-f11] local-function-key-map)
358 (substitute-key-definition [f48] [C-S-f12] local-function-key-map)
359
360 (substitute-key-definition [f49] [A-f1] local-function-key-map)
361 (substitute-key-definition [f50] [A-f2] local-function-key-map)
362 (substitute-key-definition [f51] [A-f3] local-function-key-map)
363 (substitute-key-definition [f52] [A-f4] local-function-key-map)
364 (substitute-key-definition [f53] [A-f5] local-function-key-map)
365 (substitute-key-definition [f54] [A-f6] local-function-key-map)
366 (substitute-key-definition [f55] [A-f7] local-function-key-map)
367 (substitute-key-definition [f56] [A-f8] local-function-key-map)
368 (substitute-key-definition [f57] [A-f9] local-function-key-map)
369 (substitute-key-definition [f58] [A-f10] local-function-key-map)
370 (substitute-key-definition [f59] [A-f11] local-function-key-map)
371 (substitute-key-definition [f60] [A-f12] local-function-key-map)
372
373 ;; Use inheritance to let the main keymap override those defaults.
374 ;; This way we don't override terminfo-derived settings or settings
375 ;; made in the .emacs file.
376 (let ((m (copy-keymap xterm-function-map)))
377 (set-keymap-parent m (keymap-parent local-function-key-map))
378 (set-keymap-parent local-function-key-map m)))
379
380 (xterm-register-default-colors)
381 ;; This recomputes all the default faces given the colors we've just set up.
382 (tty-set-up-initial-frame-faces))
383
384 ;; Set up colors, for those versions of xterm that support it.
385 (defvar xterm-standard-colors
386 ;; The names in the comments taken from XTerm-col.ad in the xterm
387 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
388 ;; from rgb.txt.
389 '(("black" 0 ( 0 0 0)) ; black
390 ("red" 1 (205 0 0)) ; red3
391 ("green" 2 ( 0 205 0)) ; green3
392 ("yellow" 3 (205 205 0)) ; yellow3
393 ("blue" 4 ( 0 0 238)) ; blue2
394 ("magenta" 5 (205 0 205)) ; magenta3
395 ("cyan" 6 ( 0 205 205)) ; cyan3
396 ("white" 7 (229 229 229)) ; gray90
397 ("brightblack" 8 (127 127 127)) ; gray50
398 ("brightred" 9 (255 0 0)) ; red
399 ("brightgreen" 10 ( 0 255 0)) ; green
400 ("brightyellow" 11 (255 255 0)) ; yellow
401 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
402 ("brightmagenta" 13 (255 0 255)) ; magenta
403 ("brightcyan" 14 ( 0 255 255)) ; cyan
404 ("brightwhite" 15 (255 255 255))) ; white
405 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
406
407 (defun xterm-rgb-convert-to-16bit (prim)
408 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
409 (logior prim (lsh prim 8)))
410
411 (defun xterm-register-default-colors ()
412 "Register the default set of colors for xterm or compatible emulator.
413
414 This function registers the number of colors returned by `display-color-cells'
415 for the currently selected frame. The first 16 colors are taken from
416 `xterm-standard-colors', which see, while the rest are computed assuming
417 either the 88- or 256-color standard color scheme supported by latest
418 versions of xterm."
419 (let* ((ncolors (display-color-cells (selected-frame)))
420 (colors xterm-standard-colors)
421 (color (car colors)))
422 (if (> ncolors 0)
423 ;; Clear the 8 default tty colors registered by startup.el
424 (tty-color-clear))
425 ;; Only register as many colors as are supported by the display.
426 (while (and (> ncolors 0) colors)
427 (tty-color-define (car color) (cadr color)
428 (mapcar 'xterm-rgb-convert-to-16bit
429 (car (cddr color))))
430 (setq colors (cdr colors)
431 color (car colors)
432 ncolors (1- ncolors)))
433 ;; We've exhausted the colors from `xterm-standard-colors'. If there
434 ;; are more colors to support, compute them now.
435 (when (> ncolors 0)
436 (cond
437 ((= ncolors 240) ; 256-color xterm
438 ;; 216 non-gray colors first
439 (let ((r 0) (g 0) (b 0))
440 (while (> ncolors 24)
441 ;; This and other formulae taken from 256colres.pl and
442 ;; 88colres.pl in the xterm distribution.
443 (tty-color-define (format "color-%d" (- 256 ncolors))
444 (- 256 ncolors)
445 (mapcar 'xterm-rgb-convert-to-16bit
446 (list (if (zerop r) 0 (+ (* r 40) 55))
447 (if (zerop g) 0 (+ (* g 40) 55))
448 (if (zerop b) 0 (+ (* b 40) 55)))))
449
450 (setq b (1+ b))
451 (if (> b 5)
452 (setq g (1+ g)
453 b 0))
454 (if (> g 5)
455 (setq r (1+ r)
456 g 0))
457 (setq ncolors (1- ncolors))))
458 ;; Now the 24 gray colors
459 (while (> ncolors 0)
460 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
461 (tty-color-define (format "color-%d" (- 256 ncolors))
462 (- 256 ncolors)
463 (list color color color))
464 (setq ncolors (1- ncolors))))
465 ((= ncolors 72) ; 88-color xterm
466 ;; 64 non-gray colors
467 (let ((levels '(0 139 205 255))
468 (r 0) (g 0) (b 0))
469 (while (> ncolors 8)
470 (tty-color-define (format "color-%d" (- 88 ncolors))
471 (- 88 ncolors)
472 (mapcar 'xterm-rgb-convert-to-16bit
473 (list (nth r levels)
474 (nth g levels)
475 (nth b levels))))
476 (setq b (1+ b))
477 (if (> b 3)
478 (setq g (1+ g)
479 b 0))
480 (if (> g 3)
481 (setq r (1+ r)
482 g 0))
483 (setq ncolors (1- ncolors))))
484 ;; Now the 8 gray colors
485 (while (> ncolors 0)
486 (setq color (xterm-rgb-convert-to-16bit
487 (floor
488 (if (= ncolors 8)
489 46.36363636
490 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
491 (tty-color-define (format "color-%d" (- 88 ncolors))
492 (- 88 ncolors)
493 (list color color color))
494 (setq ncolors (1- ncolors))))
495 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
496 ;; Modifying color mappings means realized faces don't use the
497 ;; right colors, so clear them.
498 (clear-face-cache)))
499
500 ;; arch-tag: 12e7ebdd-1e6c-4b25-b0f9-35ace25e855a
501 ;;; xterm.el ends here