1 ;;; xterm.el --- define function key sequences and standard colors for xterm -*- lexical-binding: t -*-
3 ;; Copyright (C) 1995, 2001-2014 Free Software Foundation, Inc.
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 3 of the License, or
13 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
32 (defcustom xterm-extra-capabilities 'check
33 "Whether Xterm supports some additional, more modern, features.
34 If nil, just assume that it does not.
35 If `check', try to check if it does.
36 If a list, assume that the listed features are supported, without checking.
38 The relevant features are:
39 modifyOtherKeys -- if supported, more key bindings work (e.g., \"\\C-,\")
40 reportBackground -- if supported, Xterm reports its background color"
43 :type '(choice (const :tag "No" nil)
44 (const :tag "Check" check)
45 ;; NOTE: If you add entries here, make sure to update
46 ;; `terminal-init-xterm' as well.
47 (set (const :tag "modifyOtherKeys support" modifyOtherKeys)
48 (const :tag "report background" reportBackground))))
50 (defconst xterm-paste-ending-sequence "\e[201~"
51 "Characters send by the terminal to end a bracketed paste.")
54 "Handle the start of a terminal paste operation."
56 (let* ((end-marker-length (length xterm-paste-ending-sequence))
57 (pasted-text (with-temp-buffer
58 (set-buffer-multibyte nil)
59 (while (not (search-backward
60 xterm-paste-ending-sequence
61 (- (point) end-marker-length) t))
62 (let ((event (read-event)))
66 (let ((last-coding-system-used))
69 (keyboard-coding-system) t))))
70 (interprogram-paste-function (lambda () pasted-text)))
73 (define-key global-map [xterm-paste] #'xterm-paste)
75 (defvar xterm-function-map
76 (let ((map (make-sparse-keymap)))
78 ;; xterm from X.org 6.8.2 uses these key definitions.
79 (define-key map "\eOP" [f1])
80 (define-key map "\eOQ" [f2])
81 (define-key map "\eOR" [f3])
82 (define-key map "\eOS" [f4])
83 (define-key map "\e[15~" [f5])
84 (define-key map "\e[17~" [f6])
85 (define-key map "\e[18~" [f7])
86 (define-key map "\e[19~" [f8])
87 (define-key map "\e[20~" [f9])
88 (define-key map "\e[21~" [f10])
89 (define-key map "\e[23~" [f11])
90 (define-key map "\e[24~" [f12])
92 (define-key map "\eO2P" [S-f1])
93 (define-key map "\eO2Q" [S-f2])
94 (define-key map "\eO2R" [S-f3])
95 (define-key map "\eO2S" [S-f4])
96 (define-key map "\e[1;2P" [S-f1])
97 (define-key map "\e[1;2Q" [S-f2])
98 (define-key map "\e[1;2R" [S-f3])
99 (define-key map "\e[1;2S" [S-f4])
100 (define-key map "\e[15;2~" [S-f5])
101 (define-key map "\e[17;2~" [S-f6])
102 (define-key map "\e[18;2~" [S-f7])
103 (define-key map "\e[19;2~" [S-f8])
104 (define-key map "\e[20;2~" [S-f9])
105 (define-key map "\e[21;2~" [S-f10])
106 (define-key map "\e[23;2~" [S-f11])
107 (define-key map "\e[24;2~" [S-f12])
109 (define-key map "\eO5P" [C-f1])
110 (define-key map "\eO5Q" [C-f2])
111 (define-key map "\eO5R" [C-f3])
112 (define-key map "\eO5S" [C-f4])
113 (define-key map "\e[15;5~" [C-f5])
114 (define-key map "\e[17;5~" [C-f6])
115 (define-key map "\e[18;5~" [C-f7])
116 (define-key map "\e[19;5~" [C-f8])
117 (define-key map "\e[20;5~" [C-f9])
118 (define-key map "\e[21;5~" [C-f10])
119 (define-key map "\e[23;5~" [C-f11])
120 (define-key map "\e[24;5~" [C-f12])
122 (define-key map "\eO6P" [C-S-f1])
123 (define-key map "\eO6Q" [C-S-f2])
124 (define-key map "\eO6R" [C-S-f3])
125 (define-key map "\eO6S" [C-S-f4])
126 (define-key map "\e[15;6~" [C-S-f5])
127 (define-key map "\e[17;6~" [C-S-f6])
128 (define-key map "\e[18;6~" [C-S-f7])
129 (define-key map "\e[19;6~" [C-S-f8])
130 (define-key map "\e[20;6~" [C-S-f9])
131 (define-key map "\e[21;6~" [C-S-f10])
132 (define-key map "\e[23;6~" [C-S-f11])
133 (define-key map "\e[24;6~" [C-S-f12])
135 (define-key map "\eO3P" [M-f1])
136 (define-key map "\eO3Q" [M-f2])
137 (define-key map "\eO3R" [M-f3])
138 (define-key map "\eO3S" [M-f4])
139 (define-key map "\e[15;3~" [M-f5])
140 (define-key map "\e[17;3~" [M-f6])
141 (define-key map "\e[18;3~" [M-f7])
142 (define-key map "\e[19;3~" [M-f8])
143 (define-key map "\e[20;3~" [M-f9])
144 (define-key map "\e[21;3~" [M-f10])
145 (define-key map "\e[23;3~" [M-f11])
146 (define-key map "\e[24;3~" [M-f12])
148 (define-key map "\eO4P" [M-S-f1])
149 (define-key map "\eO4Q" [M-S-f2])
150 (define-key map "\eO4R" [M-S-f3])
151 (define-key map "\eO4S" [M-S-f4])
152 (define-key map "\e[15;4~" [M-S-f5])
153 (define-key map "\e[17;4~" [M-S-f6])
154 (define-key map "\e[18;4~" [M-S-f7])
155 (define-key map "\e[19;4~" [M-S-f8])
156 (define-key map "\e[20;4~" [M-S-f9])
157 (define-key map "\e[21;4~" [M-S-f10])
158 (define-key map "\e[23;4~" [M-S-f11])
159 (define-key map "\e[24;4~" [M-S-f12])
161 (define-key map "\eOA" [up])
162 (define-key map "\eOB" [down])
163 (define-key map "\eOC" [right])
164 (define-key map "\eOD" [left])
165 (define-key map "\eOF" [end])
166 (define-key map "\eOH" [home])
168 (define-key map "\e[1;2A" [S-up])
169 (define-key map "\e[1;2B" [S-down])
170 (define-key map "\e[1;2C" [S-right])
171 (define-key map "\e[1;2D" [S-left])
172 (define-key map "\e[1;2F" [S-end])
173 (define-key map "\e[1;2H" [S-home])
175 (define-key map "\e[1;4A" [M-S-up])
176 (define-key map "\e[1;4B" [M-S-down])
177 (define-key map "\e[1;4C" [M-S-right])
178 (define-key map "\e[1;4D" [M-S-left])
179 (define-key map "\e[1;4F" [M-S-end])
180 (define-key map "\e[1;4H" [M-S-home])
182 (define-key map "\e[1;5A" [C-up])
183 (define-key map "\e[1;5B" [C-down])
184 (define-key map "\e[1;5C" [C-right])
185 (define-key map "\e[1;5D" [C-left])
186 (define-key map "\e[1;5F" [C-end])
187 (define-key map "\e[1;5H" [C-home])
189 (define-key map "\e[1;6A" [C-S-up])
190 (define-key map "\e[1;6B" [C-S-down])
191 (define-key map "\e[1;6C" [C-S-right])
192 (define-key map "\e[1;6D" [C-S-left])
193 (define-key map "\e[1;6F" [C-S-end])
194 (define-key map "\e[1;6H" [C-S-home])
196 (define-key map "\e[1;7A" [C-M-up])
197 (define-key map "\e[1;7B" [C-M-down])
198 (define-key map "\e[1;7C" [C-M-right])
199 (define-key map "\e[1;7D" [C-M-left])
200 (define-key map "\e[1;7F" [C-M-end])
201 (define-key map "\e[1;7H" [C-M-home])
203 (define-key map "\e[1;8A" [C-M-S-up])
204 (define-key map "\e[1;8B" [C-M-S-down])
205 (define-key map "\e[1;8C" [C-M-S-right])
206 (define-key map "\e[1;8D" [C-M-S-left])
207 (define-key map "\e[1;8F" [C-M-S-end])
208 (define-key map "\e[1;8H" [C-M-S-home])
210 (define-key map "\e[1;3A" [M-up])
211 (define-key map "\e[1;3B" [M-down])
212 (define-key map "\e[1;3C" [M-right])
213 (define-key map "\e[1;3D" [M-left])
214 (define-key map "\e[1;3F" [M-end])
215 (define-key map "\e[1;3H" [M-home])
217 (define-key map "\e[2~" [insert])
218 (define-key map "\e[3~" [delete])
219 (define-key map "\e[5~" [prior])
220 (define-key map "\e[6~" [next])
222 (define-key map "\e[2;2~" [S-insert])
223 (define-key map "\e[3;2~" [S-delete])
224 (define-key map "\e[5;2~" [S-prior])
225 (define-key map "\e[6;2~" [S-next])
227 (define-key map "\e[2;4~" [M-S-insert])
228 (define-key map "\e[3;4~" [M-S-delete])
229 (define-key map "\e[5;4~" [M-S-prior])
230 (define-key map "\e[6;4~" [M-S-next])
232 (define-key map "\e[2;5~" [C-insert])
233 (define-key map "\e[3;5~" [C-delete])
234 (define-key map "\e[5;5~" [C-prior])
235 (define-key map "\e[6;5~" [C-next])
237 (define-key map "\e[2;6~" [C-S-insert])
238 (define-key map "\e[3;6~" [C-S-delete])
239 (define-key map "\e[5;6~" [C-S-prior])
240 (define-key map "\e[6;6~" [C-S-next])
242 (define-key map "\e[2;7~" [C-M-insert])
243 (define-key map "\e[3;7~" [C-M-delete])
244 (define-key map "\e[5;7~" [C-M-prior])
245 (define-key map "\e[6;7~" [C-M-next])
247 (define-key map "\e[2;8~" [C-M-S-insert])
248 (define-key map "\e[3;8~" [C-M-S-delete])
249 (define-key map "\e[5;8~" [C-M-S-prior])
250 (define-key map "\e[6;8~" [C-M-S-next])
252 (define-key map "\e[2;3~" [M-insert])
253 (define-key map "\e[3;3~" [M-delete])
254 (define-key map "\e[5;3~" [M-prior])
255 (define-key map "\e[6;3~" [M-next])
257 (define-key map "\e[4~" [select])
258 (define-key map "\e[29~" [print])
260 (define-key map "\eOj" [kp-multiply])
261 (define-key map "\eOk" [kp-add])
262 (define-key map "\eOl" [kp-separator])
263 (define-key map "\eOm" [kp-subtract])
264 (define-key map "\eOo" [kp-divide])
265 (define-key map "\eOp" [kp-0])
266 (define-key map "\eOq" [kp-1])
267 (define-key map "\eOr" [kp-2])
268 (define-key map "\eOs" [kp-3])
269 (define-key map "\eOt" [kp-4])
270 (define-key map "\eOu" [kp-5])
271 (define-key map "\eOv" [kp-6])
272 (define-key map "\eOw" [kp-7])
273 (define-key map "\eOx" [kp-8])
274 (define-key map "\eOy" [kp-9])
276 ;; These keys are available in xterm starting from version 216
277 ;; if the modifyOtherKeys resource is set to 1.
278 (dolist (bind '((5 9 [C-tab])
287 ;; Not all C-DIGIT keys have a distinct binding.
306 (6 63 [(control ??)])
308 ;; These are the strings emitted for various C-M-
309 ;; combinations for keyboards whose Meta and Alt
310 ;; modifiers are on the same key (usually labeled "Alt").
346 (14 63 [(control meta ??)])
384 (8 63 [(control meta ??)])
390 (6 13 [C-S-return])))
392 (format "\e[27;%d;%d~" (nth 0 bind) (nth 1 bind)) (nth 2 bind))
393 ;; For formatOtherKeys=1, the sequence is a bit shorter (bug#13839).
395 (format "\e[%d;%du" (nth 1 bind) (nth 0 bind)) (nth 2 bind)))
397 ;; Other versions of xterm might emit these.
398 (define-key map "\e[A" [up])
399 (define-key map "\e[B" [down])
400 (define-key map "\e[C" [right])
401 (define-key map "\e[D" [left])
402 (define-key map "\e[1~" [home])
404 (define-key map "\eO2A" [S-up])
405 (define-key map "\eO2B" [S-down])
406 (define-key map "\eO2C" [S-right])
407 (define-key map "\eO2D" [S-left])
408 (define-key map "\eO2F" [S-end])
409 (define-key map "\eO2H" [S-home])
411 (define-key map "\eO5A" [C-up])
412 (define-key map "\eO5B" [C-down])
413 (define-key map "\eO5C" [C-right])
414 (define-key map "\eO5D" [C-left])
415 (define-key map "\eO5F" [C-end])
416 (define-key map "\eO5H" [C-home])
418 (define-key map "\e[11~" [f1])
419 (define-key map "\e[12~" [f2])
420 (define-key map "\e[13~" [f3])
421 (define-key map "\e[14~" [f4])
423 ;; Recognize the start of a bracketed paste sequence. The handler
424 ;; internally recognizes the end.
425 (define-key map "\e[200~" [xterm-paste])
428 "Function key map overrides for xterm.")
430 (defvar xterm-alternatives-map
431 (let ((map (make-sparse-keymap)))
432 ;; The terminal initialization C code file might have initialized
433 ;; function keys F13->F60 from the termcap/terminfo information.
434 ;; On a PC-style keyboard these keys correspond to
435 ;; MODIFIER-FUNCTION_KEY, where modifier is S-, C, A-, C-S-. The code
436 ;; here substitutes the corresponding definitions in function-key-map.
437 ;; The mapping from escape sequences to Fn is done in input-decode-map
438 ;; whereas this here mapping is done in local-function-key-map so that
439 ;; bindings to f45 still work, in case your keyboard really has an f45
440 ;; key rather than C-S-f9.
441 (define-key map [f13] [S-f1])
442 (define-key map [f14] [S-f2])
443 (define-key map [f15] [S-f3])
444 (define-key map [f16] [S-f4])
445 (define-key map [f17] [S-f5])
446 (define-key map [f18] [S-f6])
447 (define-key map [f19] [S-f7])
448 (define-key map [f20] [S-f8])
449 (define-key map [f21] [S-f9])
450 (define-key map [f22] [S-f10])
451 (define-key map [f23] [S-f11])
452 (define-key map [f24] [S-f12])
454 (define-key map [f25] [C-f1])
455 (define-key map [f26] [C-f2])
456 (define-key map [f27] [C-f3])
457 (define-key map [f28] [C-f4])
458 (define-key map [f29] [C-f5])
459 (define-key map [f30] [C-f6])
460 (define-key map [f31] [C-f7])
461 (define-key map [f32] [C-f8])
462 (define-key map [f33] [C-f9])
463 (define-key map [f34] [C-f10])
464 (define-key map [f35] [C-f11])
465 (define-key map [f36] [C-f12])
467 (define-key map [f37] [C-S-f1])
468 (define-key map [f38] [C-S-f2])
469 (define-key map [f39] [C-S-f3])
470 (define-key map [f40] [C-S-f4])
471 (define-key map [f41] [C-S-f5])
472 (define-key map [f42] [C-S-f6])
473 (define-key map [f43] [C-S-f7])
474 (define-key map [f44] [C-S-f8])
475 (define-key map [f45] [C-S-f9])
476 (define-key map [f46] [C-S-f10])
477 (define-key map [f47] [C-S-f11])
478 (define-key map [f48] [C-S-f12])
480 (define-key map [f49] [M-f1])
481 (define-key map [f50] [M-f2])
482 (define-key map [f51] [M-f3])
483 (define-key map [f52] [M-f4])
484 (define-key map [f53] [M-f5])
485 (define-key map [f54] [M-f6])
486 (define-key map [f55] [M-f7])
487 (define-key map [f56] [M-f8])
488 (define-key map [f57] [M-f9])
489 (define-key map [f58] [M-f10])
490 (define-key map [f59] [M-f11])
491 (define-key map [f60] [M-f12])
494 "Keymap of possible alternative meanings for some keys.")
496 (defun xterm--report-background-handler ()
499 ;; The reply should be: \e ] 11 ; rgb: NUMBER1 / NUMBER2 / NUMBER3 \e \\
500 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?\\)))
501 (setq str (concat str (string chr))))
503 "rgb:\\([a-f0-9]+\\)/\\([a-f0-9]+\\)/\\([a-f0-9]+\\)" str)
504 (let ((recompute-faces
505 (xterm-maybe-set-dark-background-mode
506 (string-to-number (match-string 1 str) 16)
507 (string-to-number (match-string 2 str) 16)
508 (string-to-number (match-string 3 str) 16))))
510 ;; Recompute faces here in case the background mode was
511 ;; set to dark. We used to call
512 ;; `tty-set-up-initial-frame-faces' only once, but that
513 ;; caused the light background faces to be computed
515 ;; http://permalink.gmane.org/gmane.emacs.devel/119627
516 (when recompute-faces
517 (tty-set-up-initial-frame-faces))))))
519 (defun xterm--version-handler ()
522 ;; The reply should be: \e [ > NUMBER1 ; NUMBER2 ; NUMBER3 c
523 ;; If the timeout is completely removed for read-event, this
524 ;; might hang for terminals that pretend to be xterm, but don't
525 ;; respond to this escape sequence. RMS' opinion was to remove
526 ;; it completely. That might be right, but let's first try to
527 ;; see if by using a longer timeout we get rid of most issues.
528 (while (and (setq chr (read-event nil nil 2)) (not (equal chr ?c)))
529 (setq str (concat str (string chr))))
530 ;; Since xterm-280, the terminal type (NUMBER1) is now 41 instead of 0.
531 (when (string-match "\\([0-9]+\\);\\([0-9]+\\);0" str)
532 (let ((version (string-to-number (match-string 2 str))))
533 ;; If version is 242 or higher, assume the xterm supports
534 ;; reporting the background color (TODO: maybe earlier
535 ;; versions do too...)
536 (when (>= version 242)
537 (xterm--query "\e]11;?\e\\"
538 '(("\e]11;" . xterm--report-background-handler))))
540 ;; If version is 216 (the version when modifyOtherKeys was
541 ;; introduced) or higher, initialize the
542 ;; modifyOtherKeys support.
543 (when (>= version 216)
544 (terminal-init-xterm-modify-other-keys))))))
546 (defun xterm--query (query handlers)
547 "Send QUERY string to the terminal and watch for a response.
548 HANDLERS is an alist with elements of the form (STRING . FUNCTION).
549 We run the first FUNCTION whose STRING matches the input events."
550 ;; We used to query synchronously, but the need to use `discard-input' is
551 ;; rather annoying (bug#6758). Maybe we could always use the asynchronous
552 ;; approach, but it's less tested.
553 ;; FIXME: Merge the two branches.
554 (if (input-pending-p)
556 (dolist (handler handlers)
557 (define-key input-decode-map (car handler)
558 (lambda (&optional _prompt)
559 ;; Unregister the handler, since we don't expect further answers.
560 (dolist (handler handlers)
561 (define-key input-decode-map (car handler) nil))
562 (funcall (cdr handler))
564 (send-string-to-terminal query))
565 ;; Pending input can be mistakenly returned by the calls to
566 ;; read-event below. Discard it.
567 (send-string-to-terminal query)
569 (let ((handler (pop handlers))
571 (while (and (< i (length (car handler)))
572 (let ((evt (read-event nil nil 2)))
573 (or (eq evt (aref (car handler) i))
574 (progn (if evt (push evt unread-command-events))
577 (if (= i (length (car handler)))
578 (progn (setq handlers nil)
579 (funcall (cdr handler)))
581 (push (aref (car handler) (setq i (1- i)))
582 unread-command-events)))))))
584 (defun terminal-init-xterm ()
585 "Terminal initialization function for xterm."
586 ;; rxvt terminals sometimes set the TERM variable to "xterm", but
587 ;; rxvt's keybindings are incompatible with xterm's. It is
588 ;; better in that case to use rxvt's initialization function.
589 (if (and (getenv "COLORTERM" (selected-frame))
590 (string-match "\\`rxvt" (getenv "COLORTERM" (selected-frame))))
591 (tty-run-terminal-initialization (selected-frame) "rxvt")
593 (let ((map (copy-keymap xterm-alternatives-map)))
594 (set-keymap-parent map (keymap-parent local-function-key-map))
595 (set-keymap-parent local-function-key-map map))
597 (let ((map (copy-keymap xterm-function-map)))
599 ;; Use inheritance to let the main keymap override those defaults.
600 ;; This way we don't override terminfo-derived settings or settings
601 ;; made in the init file.
602 (set-keymap-parent map (keymap-parent input-decode-map))
603 (set-keymap-parent input-decode-map map)))
605 (xterm-register-default-colors)
606 (tty-set-up-initial-frame-faces)
608 (if (eq xterm-extra-capabilities 'check)
609 ;; Try to find out the type of terminal by sending a "Secondary
610 ;; Device Attributes (DA)" query.
611 (xterm--query "\e[>0c"
612 ;; Some terminals (like OS X's Terminal.app) respond to
613 ;; this query as if it were a "Primary Device Attributes"
614 ;; query instead, so we should handle that too.
615 '(("\e[?" . xterm--version-handler)
616 ("\e[>" . xterm--version-handler)))
618 (when (memq 'reportBackground xterm-extra-capabilities)
619 (xterm--query "\e]11;?\e\\"
620 '(("\e]11;" . xterm--report-background-handler))))
622 (when (memq 'modifyOtherKeys xterm-extra-capabilities)
623 (terminal-init-xterm-modify-other-keys)))
625 ;; Unconditionally enable bracketed paste mode: terminals that don't
626 ;; support it just ignore the sequence.
627 (terminal-init-xterm-bracketed-paste-mode)
629 (run-hooks 'terminal-init-xterm-hook))
631 (defun terminal-init-xterm-modify-other-keys ()
632 "Terminal initialization for xterm's modifyOtherKeys support."
633 (send-string-to-terminal "\e[>4;1m")
634 (push "\e[>4m" (terminal-parameter nil 'tty-mode-reset-strings))
635 (push "\e[>4;1m" (terminal-parameter nil 'tty-mode-set-strings)))
637 (defun terminal-init-xterm-bracketed-paste-mode ()
638 "Terminal initialization for bracketed paste mode."
639 (send-string-to-terminal "\e[?2004h")
640 (push "\e[?2004l" (terminal-parameter nil 'tty-mode-reset-strings))
641 (push "\e[?2004h" (terminal-parameter nil 'tty-mode-set-strings)))
643 ;; Set up colors, for those versions of xterm that support it.
644 (defvar xterm-standard-colors
645 ;; The names in the comments taken from XTerm-col.ad in the xterm
646 ;; distribution, see ftp://dickey.his.com/xterm/. RGB values are
648 '(("black" 0 ( 0 0 0)) ; black
649 ("red" 1 (205 0 0)) ; red3
650 ("green" 2 ( 0 205 0)) ; green3
651 ("yellow" 3 (205 205 0)) ; yellow3
652 ("blue" 4 ( 0 0 238)) ; blue2
653 ("magenta" 5 (205 0 205)) ; magenta3
654 ("cyan" 6 ( 0 205 205)) ; cyan3
655 ("white" 7 (229 229 229)) ; gray90
656 ("brightblack" 8 (127 127 127)) ; gray50
657 ("brightred" 9 (255 0 0)) ; red
658 ("brightgreen" 10 ( 0 255 0)) ; green
659 ("brightyellow" 11 (255 255 0)) ; yellow
660 ("brightblue" 12 (92 92 255)) ; rgb:5c/5c/ff
661 ("brightmagenta" 13 (255 0 255)) ; magenta
662 ("brightcyan" 14 ( 0 255 255)) ; cyan
663 ("brightwhite" 15 (255 255 255))) ; white
664 "Names of 16 standard xterm/aixterm colors, their numbers, and RGB values.")
666 (defun xterm-rgb-convert-to-16bit (prim)
667 "Convert an 8-bit primary color value PRIM to a corresponding 16-bit value."
668 (logior prim (lsh prim 8)))
670 (defun xterm-register-default-colors ()
671 "Register the default set of colors for xterm or compatible emulator.
673 This function registers the number of colors returned by `display-color-cells'
674 for the currently selected frame. The first 16 colors are taken from
675 `xterm-standard-colors', which see, while the rest are computed assuming
676 either the 88- or 256-color standard color scheme supported by latest
678 (let* ((ncolors (display-color-cells (selected-frame)))
679 (colors xterm-standard-colors)
680 (color (car colors)))
682 ;; Clear the 8 default tty colors registered by startup.el
684 ;; Only register as many colors as are supported by the display.
685 (while (and (> ncolors 0) colors)
686 (tty-color-define (car color) (cadr color)
687 (mapcar 'xterm-rgb-convert-to-16bit
689 (setq colors (cdr colors)
691 ncolors (1- ncolors)))
692 ;; We've exhausted the colors from `xterm-standard-colors'. If there
693 ;; are more colors to support, compute them now.
696 ((= ncolors 240) ; 256-color xterm
697 ;; 216 non-gray colors first
698 (let ((r 0) (g 0) (b 0))
699 (while (> ncolors 24)
700 ;; This and other formulas taken from 256colres.pl and
701 ;; 88colres.pl in the xterm distribution.
702 (tty-color-define (format "color-%d" (- 256 ncolors))
704 (mapcar 'xterm-rgb-convert-to-16bit
705 (list (if (zerop r) 0 (+ (* r 40) 55))
706 (if (zerop g) 0 (+ (* g 40) 55))
707 (if (zerop b) 0 (+ (* b 40) 55)))))
716 (setq ncolors (1- ncolors))))
717 ;; Now the 24 gray colors
719 (setq color (xterm-rgb-convert-to-16bit (+ 8 (* (- 24 ncolors) 10))))
720 (tty-color-define (format "color-%d" (- 256 ncolors))
722 (list color color color))
723 (setq ncolors (1- ncolors))))
724 ((= ncolors 72) ; 88-color xterm
725 ;; 64 non-gray colors
726 (let ((levels '(0 139 205 255))
729 (tty-color-define (format "color-%d" (- 88 ncolors))
731 (mapcar 'xterm-rgb-convert-to-16bit
742 (setq ncolors (1- ncolors))))
743 ;; Now the 8 gray colors
745 (setq color (xterm-rgb-convert-to-16bit
749 (+ (* (- 8 ncolors) 23.18181818) 69.54545454)))))
750 (tty-color-define (format "color-%d" (- 88 ncolors))
752 (list color color color))
753 (setq ncolors (1- ncolors))))
754 (t (error "Unsupported number of xterm colors (%d)" (+ 16 ncolors)))))
755 ;; Modifying color mappings means realized faces don't use the
756 ;; right colors, so clear them.
759 (defun xterm-maybe-set-dark-background-mode (redc greenc bluec)
760 ;; Use the heuristic in `frame-set-background-mode' to decide if a
762 (when (< (+ redc greenc bluec) (* .6 (+ 65535 65535 65535)))
763 (set-terminal-parameter nil 'background-mode 'dark)
768 ;;; xterm.el ends here