(face-set-after-frame-default): Don't call
[bpt/emacs.git] / lisp / term / pc-win.el
CommitLineData
092af6d8 1;;; pc-win.el --- setup support for `PC windows' (whatever that is).
ef88bd2d 2
e2f35ede 3;; Copyright (C) 1994, 1996, 1997 Free Software Foundation, Inc.
ef88bd2d
MW
4
5;; Author: Morten Welinder <terra@diku.dk>
9596811a 6;; Maintainer: FSF
ef88bd2d
MW
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
2fe590dc
EN
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;;; Code:
26
ef88bd2d
MW
27(load "term/internal" nil t)
28
ec46c6e8
RS
29;; Color translation -- doesn't really need to be fast.
30;; Colors listed here do not include the "light-",
31;; "medium-" and "dark-" prefixes that are accounted for
32;; by `msdos-color-translate', which see below).
ef88bd2d
MW
33
34(defvar msdos-color-aliases
ec46c6e8
RS
35 '(("snow" . "white")
36 ("ghost white" . "white")
37 ("ghostwhite" . "white")
38 ("white smoke" . "white")
39 ("whitesmoke" . "white")
40 ("gainsboro" . "white")
41 ("floral white" . "white")
42 ("floralwhite" . "white")
43 ("old lace" . "white")
44 ("oldlace" . "white")
45 ("linen" . "white")
46 ("antique white" . "white")
47 ("antiquewhite" . "white")
48 ("papaya whip" . "white")
49 ("papayawhip" . "white")
50 ("blanched almond" . "white")
51 ("blanchedalmond" . "white")
52 ("bisque" . "white")
53 ("peach puff" . "lightred")
54 ("peachpuff" . "lightred")
55 ("navajo white" . "lightred")
56 ("navajowhite" . "lightred")
57 ("moccasin" . "lightred")
58 ("cornsilk" . "white")
59 ("ivory" . "white")
60 ("lemon chiffon" . "yellow")
61 ("lemonchiffon" . "yellow")
62 ("seashell" . "white")
63 ("honeydew" . "white")
64 ("mint cream" . "white")
65 ("mintcream" . "white")
66 ("azure" . "lightcyan")
67 ("alice blue" . "lightcyan")
68 ("aliceblue" . "lightcyan")
69 ("lavender" . "lightcyan")
70 ("lavender blush" . "lightcyan")
71 ("lavenderblush" . "lightcyan")
72 ("misty rose" . "lightred")
73 ("mistyrose" . "lightred")
74 ("aquamarine" . "blue")
75 ("cadet blue" . "blue")
76 ("cadetblue" . "blue")
77 ("cornflower blue" . "lightblue")
78 ("cornflowerblue" . "lightblue")
79 ("midnight blue" . "blue")
80 ("midnightblue" . "blue")
81 ("navy blue" . "cyan")
82 ("navyblue" . "cyan")
83 ("navy" . "cyan")
9de91fc5
EZ
84 ("royalblue" . "blue")
85 ("royal blue" . "blue")
ec46c6e8
RS
86 ("sky blue" . "lightblue")
87 ("skyblue" . "lightblue")
88 ("dodger blue" . "blue")
89 ("dodgerblue" . "blue")
90 ("powder blue" . "lightblue")
91 ("powderblue" . "lightblue")
92 ("slate blue" . "cyan")
93 ("slateblue" . "cyan")
94 ("steel blue" . "blue")
95 ("steelblue" . "blue")
96 ("coral" . "lightred")
9de91fc5 97 ("tomato" . "lightred")
ec46c6e8
RS
98 ("firebrick" . "red")
99 ("gold" . "yellow")
100 ("goldenrod" . "yellow")
9de91fc5
EZ
101 ("goldenrod yellow" . "yellow")
102 ("goldenrodyellow" . "yellow")
ec46c6e8
RS
103 ("pale goldenrod" . "yellow")
104 ("palegoldenrod" . "yellow")
105 ("olive green" . "lightgreen")
106 ("olivegreen" . "lightgreen")
107 ("olive drab" . "green")
108 ("olivedrab" . "green")
109 ("forest green" . "green")
110 ("forestgreen" . "green")
111 ("lime green" . "lightgreen")
112 ("limegreen" . "lightgreen")
113 ("sea green" . "lightcyan")
114 ("seagreen" . "lightcyan")
115 ("spring green" . "green")
116 ("springgreen" . "green")
ec46c6e8
RS
117 ("lawn green" . "lightgreen")
118 ("lawngreen" . "lightgreen")
119 ("chartreuse" . "yellow")
120 ("yellow green" . "lightgreen")
121 ("yellowgreen" . "lightgreen")
122 ("green yellow" . "lightgreen")
123 ("greenyellow" . "lightgreen")
124 ("slate grey" . "lightgray")
125 ("slategrey" . "lightgray")
126 ("slate gray" . "lightgray")
127 ("slategray" . "lightgray")
128 ("dim grey" . "darkgray")
129 ("dimgrey" . "darkgray")
130 ("dim gray" . "darkgray")
131 ("dimgray" . "darkgray")
132 ("light grey" . "lightgray")
133 ("lightgrey" . "lightgray")
134 ("light gray" . "lightgray")
135 ("gray" . "darkgray")
136 ("grey" . "darkgray")
ec46c6e8
RS
137 ("khaki" . "green")
138 ("maroon" . "red")
139 ("orange" . "brown")
140 ("orchid" . "brown")
141 ("saddle brown" . "red")
142 ("saddlebrown" . "red")
ec46c6e8 143 ("peru" . "red")
9de91fc5
EZ
144 ("burlywood" . "brown")
145 ("sandy brown" . "brown")
146 ("sandybrown" . "brown")
ec46c6e8 147 ("pink" . "lightred")
9de91fc5
EZ
148 ("hotpink" . "lightred")
149 ("hot pink" ."lightred")
ec46c6e8
RS
150 ("plum" . "magenta")
151 ("indian red" . "red")
152 ("indianred" . "red")
153 ("violet red" . "magenta")
154 ("violetred" . "magenta")
155 ("orange red" . "red")
156 ("orangered" . "red")
157 ("salmon" . "lightred")
158 ("sienna" . "lightred")
159 ("tan" . "lightred")
9de91fc5 160 ("chocolate" . "brown")
ec46c6e8
RS
161 ("thistle" . "magenta")
162 ("turquoise" . "lightgreen")
163 ("pale turquoise" . "cyan")
164 ("paleturquoise" . "cyan")
165 ("violet" . "magenta")
166 ("blue violet" . "lightmagenta")
167 ("blueviolet" . "lightmagenta")
168 ("wheat" . "white")
169 ("green yellow" . "yellow")
170 ("greenyellow" . "yellow")
171 ("purple" . "magenta")
ec46c6e8
RS
172 ("rosybrown" . "brown")
173 ("rosy brown" . "brown")
174 ("beige" . "brown"))
ef88bd2d
MW
175 "List of alternate names for colors.")
176
177(defun msdos-color-translate (name)
a13b5fad 178 "Translate color specification in NAME into something DOS terminal groks."
ef88bd2d
MW
179 (setq name (downcase name))
180 (let* ((len (length name))
bb3a4574
RS
181 (val (- (length x-colors)
182 (length (member name x-colors))))
ef88bd2d 183 (try))
bb3a4574 184 (if (or (< val 0) (>= val (length x-colors))) (setq val nil))
ef88bd2d
MW
185 (or val
186 (and (setq try (cdr (assoc name msdos-color-aliases)))
187 (msdos-color-translate try))
188 (and (> len 5)
ec46c6e8 189 (string= "light" (substring name 0 5))
ef88bd2d
MW
190 (setq try (msdos-color-translate (substring name 5)))
191 (logior try 8))
192 (and (> len 6)
ec46c6e8 193 (string= "light " (substring name 0 6))
ef88bd2d
MW
194 (setq try (msdos-color-translate (substring name 6)))
195 (logior try 8))
9de91fc5
EZ
196 (and (> len 4)
197 (string= "pale" (substring name 0 4))
198 (setq try (msdos-color-translate (substring name 4)))
199 (logior try 8))
200 (and (> len 5)
201 (string= "pale " (substring name 0 5))
202 (setq try (msdos-color-translate (substring name 5)))
203 (logior try 8))
ec46c6e8
RS
204 (and (> len 6)
205 (string= "medium" (substring name 0 6))
206 (msdos-color-translate (substring name 6)))
207 (and (> len 7)
208 (string= "medium " (substring name 0 7))
209 (msdos-color-translate (substring name 7)))
ef88bd2d 210 (and (> len 4)
9de91fc5
EZ
211 (or (string= "dark" (substring name 0 4))
212 (string= "deep" (substring name 0 4)))
ef88bd2d
MW
213 (msdos-color-translate (substring name 4)))
214 (and (> len 5)
9de91fc5
EZ
215 (or (string= "dark " (substring name 0 5))
216 (string= "deep " (substring name 0 5)))
c13c88d7
RS
217 (msdos-color-translate (substring name 5)))
218 (and (> len 4) ;; gray shades: gray0 to gray100
219 (save-match-data
220 (and
221 (string-match "gr[ae]y[0-9]" name)
222 (string-match "[0-9]+\\'" name)
223 (let ((num (string-to-int
224 (substring name (match-beginning 0)))))
225 (msdos-color-translate
226 (cond
227 ((> num 90) "white")
228 ((> num 50) "lightgray")
229 ((> num 10) "darkgray")
230 (t "black")))))))
231 (and (> len 1) ;; purple1 to purple4 and the like
232 (save-match-data
233 (and
234 (string-match "[1-4]\\'" name)
235 (msdos-color-translate
a13b5fad
EZ
236 (substring name 0 (match-beginning 0))))))
237 (and (= len 7) ;; X-style "#XXYYZZ" color spec
238 (eq (aref name 0) ?#)
239 (member (aref name 1)
240 '(?0 ?1 ?2 ?3 ?4 ?5 ?6 ?7 ?8 ?9
241 ?A ?B ?C ?D ?E ?F ?a ?b ?c ?d ?e ?f))
242 (msdos-color-translate
243 (msdos-approximate-color (string-to-number
244 (substring name 1) 16)))))))
c4d64969
EZ
245;;;
246;;; This is copied from etc/rgb.txt, except that some values were changed
247;;; a bit to make them consistent with DOS console colors. The order of
248;;; the colors is according to the PC text mode color codes.
249;;;
250;;; If you want to change the RGB values, keep in mind that various pieces
251;;; of Emacs think that a color whose RGB values add up to less than 0.6 of
252;;; the values for WHITE (i.e. less than 459) are ``dark'', otherwise the
253;;; color is ``light''; see `frame-set-background-mode' in lisp/faces.el for
254;;; an example.
255(defvar msdos-color-values
256 '(("black" 0 0 0)
257 ("blue" 0 0 255)
258 ("green" 0 255 0)
259 ("cyan" 0 255 255)
260 ("red" 255 0 0)
261 ("magenta" 139 0 139) ; dark magenta
262 ("brown" 165 42 42)
263 ("lightgray" 211 211 211)
264 ("darkgray" 102 102 102) ; gray40
265 ("lightblue" 173 216 230)
266 ("lightgreen" 144 238 144)
267 ("lightcyan" 224 255 255)
268 ("lightred" 255 52 179) ; maroon1
269 ("lightmagenta" 238 0 238) ; magenta2
270 ("yellow" 255 255 0)
271 ("white" 255 255 255))
272 "A list of MS-DOS console colors and their RGB values.")
a13b5fad
EZ
273
274(defun msdos-approximate-color (num)
275 "Return a DOS color name which is the best approximation for the number NUM."
276 (let ((color-values msdos-color-values)
277 (candidate (car msdos-color-values))
278 (best-distance 16777216) ;; 0xFFFFFF + 1
279 best-color)
280 (while candidate
281 (let* ((values (cdr candidate))
282 (value (+ (lsh (car values) 16)
283 (lsh (car (cdr values)) 8)
284 (nth 2 values))))
285 (if (< (abs (- value num)) best-distance)
286 (setq best-distance (abs (- value num))
287 best-color (car candidate))))
288 (setq color-values (cdr color-values))
289 (setq candidate (car color-values)))
290 best-color))
ef88bd2d
MW
291;; ---------------------------------------------------------------------------
292;; We want to delay setting frame parameters until the faces are setup
293(defvar default-frame-alist nil)
9000684d 294(modify-frame-parameters terminal-frame default-frame-alist)
ef88bd2d 295
e2f35ede
RS
296(defun msdos-bg-mode (&optional frame)
297 (let* ((frame (or frame (selected-frame)))
298 (params (frame-parameters frame))
299 (bg (cdr (assq 'background-color params))))
a0c712ae
EZ
300 ;; The list of ``dark'' colors should be consistent with
301 ;; `x-color-values' (below) and the dark/light color
302 ;; decisions `frame-set-background-mode' in lisp/faces.el.
303 (if (member bg
304 '("black" "blue" "green" "red" "magenta" "brown" "darkgray"))
e2f35ede
RS
305 'dark
306 'light)))
307
ef88bd2d 308(defun msdos-face-setup ()
9000684d 309 (modify-frame-parameters terminal-frame default-frame-alist)
c4d64969
EZ
310 (face-clear-tty-colors)
311 (let ((colors msdos-color-values)
312 (i 0))
313 (while colors
314 (face-register-tty-color (car (car colors)) i)
315 (setq colors (cdr colors) i (1+ i))))
ef88bd2d 316
c13c88d7
RS
317 (modify-frame-parameters terminal-frame
318 (list (cons 'background-mode
319 (msdos-bg-mode terminal-frame))
320 (cons 'display-type 'color)))
321 (face-set-after-frame-default terminal-frame)
322
9000684d
RS
323 (set-face-foreground 'bold "yellow" terminal-frame)
324 (set-face-foreground 'italic "red" terminal-frame)
325 (set-face-foreground 'bold-italic "lightred" terminal-frame)
326 (set-face-foreground 'underline "white" terminal-frame)
ef88bd2d
MW
327
328 (make-face 'msdos-menu-active-face)
329 (make-face 'msdos-menu-passive-face)
330 (make-face 'msdos-menu-select-face)
9000684d
RS
331 (set-face-foreground 'msdos-menu-active-face "white" terminal-frame)
332 (set-face-foreground 'msdos-menu-passive-face "lightgray" terminal-frame)
333 (set-face-background 'msdos-menu-active-face "blue" terminal-frame)
334 (set-face-background 'msdos-menu-passive-face "blue" terminal-frame)
c13c88d7 335 (set-face-background 'msdos-menu-select-face "red" terminal-frame))
ef88bd2d
MW
336
337;; We have only one font, so...
338(add-hook 'before-init-hook 'msdos-face-setup)
9000684d
RS
339
340;; We create frames as if we were a terminal, but with a twist.
341(defun make-msdos-frame (&optional parameters)
e2f35ede
RS
342 (let* ((parms
343 (append initial-frame-alist default-frame-alist parameters nil))
344 (frame (make-terminal-frame parms)))
345 (modify-frame-parameters frame
346 (list (cons 'background-mode
347 (msdos-bg-mode frame))
348 (cons 'display-type 'color)))
349 frame))
9000684d
RS
350
351(setq frame-creation-function 'make-msdos-frame)
352
ef88bd2d 353;; ---------------------------------------------------------------------------
a7acbbe4 354;; More or less useful imitations of certain X-functions. A lot of the
ef88bd2d
MW
355;; values returned are questionable, but usually only the form of the
356;; returned value matters. Also, by the way, recall that `ignore' is
357;; a useful function for returning 'nil regardless of argument.
358
359;; From src/xfns.c
beb4ba68 360(defun x-display-color-p (&optional display) 't)
cee30b8f
RS
361(defun x-list-fonts (pattern &optional face frame maximum width)
362 (if (and (numberp width) (= width 1))
363 (list "default")
364 (list "no-such-font")))
ef88bd2d 365(defun x-color-defined-p (color) (numberp (msdos-color-translate color)))
a259d337
RS
366(defun x-display-pixel-width (&optional frame) (frame-width frame))
367(defun x-display-pixel-height (&optional frame) (frame-height frame))
ef88bd2d
MW
368(defun x-display-planes (&optional frame) 4) ; 3 for background, actually
369(defun x-display-color-cells (&optional frame) 16) ; ???
370(defun x-server-max-request-size (&optional frame) 1000000) ; ???
371(defun x-server-vendor (&optional frame) t "GNU")
372(defun x-server-version (&optional frame) '(1 0 0))
373(defun x-display-screens (&optional frame) 1)
374(defun x-display-mm-height (&optional frame) 200) ; Guess the size of my
375(defun x-display-mm-width (&optional frame) 253) ; monitor, MW...
376(defun x-display-backing-store (&optional frame) 'not-useful)
377(defun x-display-visual-class (&optional frame) 'static-color)
378(fset 'x-display-save-under 'ignore)
379(fset 'x-get-resource 'ignore)
a0c712ae
EZ
380
381(defun x-color-values (color &optional frame)
382 "Return a description of the color named COLOR on frame FRAME.\n\
383The value is a list of integer RGB values--(RED GREEN BLUE).\n\
384These values range from 0 to 255; white is (255 255 255).\n\
385If FRAME is omitted or nil, use the selected frame."
386 (if (x-color-defined-p color)
387 (let ((frame (or frame (selected-frame)))
388 (color-code (msdos-color-translate color)))
389 (cdr (nth color-code msdos-color-values)))))
ef88bd2d
MW
390
391;; From lisp/term/x-win.el
392(setq x-display-name "pc")
393(setq split-window-keep-point t)
bb3a4574
RS
394(defvar x-colors '("black"
395 "blue"
396 "green"
397 "cyan"
398 "red"
399 "magenta"
400 "brown"
401 "lightgray"
402 "darkgray"
403 "lightblue"
404 "lightgreen"
405 "lightcyan"
406 "lightred"
407 "lightmagenta"
408 "yellow"
409 "white")
410 "The list of colors available on a PC display under MS-DOS.")
411(defun x-defined-colors (&optional frame)
412 "Return a list of colors supported for a particular frame.
413The argument FRAME specifies which frame to try.
414The value may be different for frames on different X displays."
415 x-colors)
c432c362 416
c4d64969
EZ
417(defun face-color-supported-p (color)
418 (x-color-defined-p color))
419
420(defun face-color-gray-p (color)
421 (member (msdos-color-translate color)
422 '("black" "lightgray" "darkgray" "white")))
423
8254b6b4 424;; From lisp/term/w32-win.el
c432c362
EZ
425;
426;;;; Selections and cut buffers
bb3a4574 427;
c432c362
EZ
428;;; We keep track of the last text selected here, so we can check the
429;;; current selection against it, and avoid passing back our own text
430;;; from x-cut-buffer-or-selection-value.
431(defvar x-last-selected-text nil)
432
433(defvar x-select-enable-clipboard t
434 "Non-nil means cutting and pasting uses the clipboard.
435This is in addition to the primary selection.")
436
437(defun x-select-text (text &optional push)
438 (if x-select-enable-clipboard
8254b6b4 439 (w16-set-clipboard-data text))
c432c362
EZ
440 (setq x-last-selected-text text))
441
442;;; Return the value of the current selection.
443;;; Consult the selection, then the cut buffer. Treat empty strings
444;;; as if they were unset.
445(defun x-get-selection-value ()
446 (if x-select-enable-clipboard
447 (let (text)
448 ;; Don't die if x-get-selection signals an error.
449 (condition-case c
8254b6b4
EZ
450 (setq text (w16-get-clipboard-data))
451 (error (message "w16-get-clipboard-data:%s" c)))
c432c362
EZ
452 (if (string= text "") (setq text nil))
453 (cond
454 ((not text) nil)
455 ((eq text x-last-selected-text) nil)
456 ((string= text x-last-selected-text)
457 ;; Record the newer string, so subsequent calls can use the 'eq' test.
458 (setq x-last-selected-text text)
459 nil)
460 (t
461 (setq x-last-selected-text text))))))
462
463;;; Arrange for the kill and yank functions to set and check the clipboard.
464(setq interprogram-cut-function 'x-select-text)
465(setq interprogram-paste-function 'x-get-selection-value)
ef88bd2d 466
ea6401d7
RS
467;; From lisp/faces.el: we only have one font, so always return
468;; it, no matter which variety they've asked for.
469(defun x-frob-font-slant (font which)
470 font)
471
cee30b8f
RS
472;; From src/fontset.c:
473(fset 'query-fontset 'ignore)
474
e1ddc4b4
RS
475;; From lisp/term/x-win.el: make iconify-or-deiconify-frame a no-op.
476(fset 'iconify-or-deiconify-frame 'ignore)
477
ef88bd2d
MW
478;; From lisp/frame.el
479(fset 'set-default-font 'ignore)
480(fset 'set-mouse-color 'ignore) ; We cannot, I think.
481(fset 'set-cursor-color 'ignore) ; Hardware determined by char under.
482(fset 'set-border-color 'ignore) ; Not useful.
c8a6e3b9
EZ
483
484;; From lisp/term/x-win.el:
485(defconst x-long-option-alist
486 '(("--name" . "-name")
487 ("--title" . "-T")
488 ("--reverse-video" . "-reverse")
489 ("--foreground-color" . "-fg")
490 ("--background-color" . "-bg")))
ef88bd2d 491;; ---------------------------------------------------------------------------
c8a6e3b9 492;; Handle the X-like command line parameters "-fg", "-bg", "-name", etc.
ef88bd2d
MW
493(defun msdos-handle-args (args)
494 (let ((rest nil))
c8a6e3b9 495 (message "%s" args)
ef88bd2d 496 (while args
c8a6e3b9
EZ
497 (let* ((this (car args))
498 (orig-this this)
499 completion argval)
ef88bd2d 500 (setq args (cdr args))
c8a6e3b9
EZ
501 ;; Check for long options with attached arguments
502 ;; and separate out the attached option argument into argval.
503 (if (string-match "^--[^=]*=" this)
504 (setq argval (substring this (match-end 0))
505 this (substring this 0 (1- (match-end 0)))))
506 (setq completion (try-completion this x-long-option-alist))
507 (if (eq completion t)
508 ;; Exact match for long option.
509 (setq this (cdr (assoc this x-long-option-alist)))
510 (if (stringp completion)
511 (let ((elt (assoc completion x-long-option-alist)))
512 ;; Check for abbreviated long option.
513 (or elt
514 (error "Option `%s' is ambiguous" this))
515 (setq this (cdr elt)))
516 ;; Check for a short option.
517 (setq argval nil this orig-this)))
ef88bd2d 518 (cond ((or (string= this "-fg") (string= this "-foreground"))
c8a6e3b9
EZ
519 (or argval (setq argval (car args) args (cdr args)))
520 (setq default-frame-alist
521 (cons (cons 'foreground-color argval)
522 default-frame-alist)))
ef88bd2d 523 ((or (string= this "-bg") (string= this "-background"))
c8a6e3b9
EZ
524 (or argval (setq argval (car args) args (cdr args)))
525 (setq default-frame-alist
526 (cons (cons 'background-color argval)
527 default-frame-alist)))
528 ((or (string= this "-T") (string= this "-name"))
529 (or argval (setq argval (car args) args (cdr args)))
530 (setq default-frame-alist
531 (cons
532 (cons 'title
533 (if (stringp argval)
534 argval
535 (let ((case-fold-search t)
536 i)
537 (setq argval (invocation-name))
538
539 ;; Change any . or * characters in name to
540 ;; hyphens, so as to emulate behavior on X.
541 (while
542 (setq i (string-match "[.*]" argval))
543 (aset argval i ?-))
544 argval)))
545 default-frame-alist)))
546 ((or (string= this "-r")
547 (string= this "-rv")
548 (string= this "-reverse"))
549 (setq default-frame-alist
550 (cons '(reverse . t)
551 default-frame-alist)))
ef88bd2d 552 (t (setq rest (cons this rest))))))
c8a6e3b9 553 (nreverse rest)))
ef88bd2d
MW
554
555(setq command-line-args (msdos-handle-args command-line-args))
556;; ---------------------------------------------------------------------------
092af6d8
RS
557
558;;; pc-win.el ends here