(shrink-window-if-larger-than-buffer): Don't try to
[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
MW
295
296(defun msdos-face-setup ()
9000684d 297 (modify-frame-parameters terminal-frame default-frame-alist)
c4d64969
EZ
298 (face-clear-tty-colors)
299 (let ((colors msdos-color-values)
300 (i 0))
301 (while colors
302 (face-register-tty-color (car (car colors)) i)
303 (setq colors (cdr colors) i (1+ i))))
ef88bd2d 304
68a89a25 305 (frame-set-background-mode terminal-frame)
c13c88d7
RS
306 (face-set-after-frame-default terminal-frame)
307
9000684d
RS
308 (set-face-foreground 'bold "yellow" terminal-frame)
309 (set-face-foreground 'italic "red" terminal-frame)
310 (set-face-foreground 'bold-italic "lightred" terminal-frame)
311 (set-face-foreground 'underline "white" terminal-frame)
ef88bd2d
MW
312
313 (make-face 'msdos-menu-active-face)
314 (make-face 'msdos-menu-passive-face)
315 (make-face 'msdos-menu-select-face)
9000684d
RS
316 (set-face-foreground 'msdos-menu-active-face "white" terminal-frame)
317 (set-face-foreground 'msdos-menu-passive-face "lightgray" terminal-frame)
318 (set-face-background 'msdos-menu-active-face "blue" terminal-frame)
319 (set-face-background 'msdos-menu-passive-face "blue" terminal-frame)
c13c88d7 320 (set-face-background 'msdos-menu-select-face "red" terminal-frame))
ef88bd2d
MW
321
322;; We have only one font, so...
323(add-hook 'before-init-hook 'msdos-face-setup)
9000684d
RS
324
325;; We create frames as if we were a terminal, but with a twist.
326(defun make-msdos-frame (&optional parameters)
e2f35ede
RS
327 (let* ((parms
328 (append initial-frame-alist default-frame-alist parameters nil))
68a89a25
EZ
329 (frame (make-terminal-frame parms))
330 success)
331 (unwind-protect
332 (progn
333 (x-handle-reverse-video frame parms)
334 (frame-set-background-mode frame)
335 (face-set-after-frame-default frame)
336 (setq success t))
337 (unless success (delete-frame frame)))
e2f35ede 338 frame))
9000684d
RS
339
340(setq frame-creation-function 'make-msdos-frame)
341
ef88bd2d 342;; ---------------------------------------------------------------------------
a7acbbe4 343;; More or less useful imitations of certain X-functions. A lot of the
ef88bd2d
MW
344;; values returned are questionable, but usually only the form of the
345;; returned value matters. Also, by the way, recall that `ignore' is
346;; a useful function for returning 'nil regardless of argument.
347
348;; From src/xfns.c
beb4ba68 349(defun x-display-color-p (&optional display) 't)
cee30b8f
RS
350(defun x-list-fonts (pattern &optional face frame maximum width)
351 (if (and (numberp width) (= width 1))
352 (list "default")
353 (list "no-such-font")))
ef88bd2d 354(defun x-color-defined-p (color) (numberp (msdos-color-translate color)))
a259d337
RS
355(defun x-display-pixel-width (&optional frame) (frame-width frame))
356(defun x-display-pixel-height (&optional frame) (frame-height frame))
ef88bd2d
MW
357(defun x-display-planes (&optional frame) 4) ; 3 for background, actually
358(defun x-display-color-cells (&optional frame) 16) ; ???
359(defun x-server-max-request-size (&optional frame) 1000000) ; ???
360(defun x-server-vendor (&optional frame) t "GNU")
361(defun x-server-version (&optional frame) '(1 0 0))
362(defun x-display-screens (&optional frame) 1)
363(defun x-display-mm-height (&optional frame) 200) ; Guess the size of my
364(defun x-display-mm-width (&optional frame) 253) ; monitor, MW...
365(defun x-display-backing-store (&optional frame) 'not-useful)
366(defun x-display-visual-class (&optional frame) 'static-color)
367(fset 'x-display-save-under 'ignore)
368(fset 'x-get-resource 'ignore)
a0c712ae
EZ
369
370(defun x-color-values (color &optional frame)
371 "Return a description of the color named COLOR on frame FRAME.\n\
372The value is a list of integer RGB values--(RED GREEN BLUE).\n\
373These values range from 0 to 255; white is (255 255 255).\n\
374If FRAME is omitted or nil, use the selected frame."
375 (if (x-color-defined-p color)
376 (let ((frame (or frame (selected-frame)))
377 (color-code (msdos-color-translate color)))
378 (cdr (nth color-code msdos-color-values)))))
ef88bd2d
MW
379
380;; From lisp/term/x-win.el
381(setq x-display-name "pc")
382(setq split-window-keep-point t)
bb3a4574
RS
383(defvar x-colors '("black"
384 "blue"
385 "green"
386 "cyan"
387 "red"
388 "magenta"
389 "brown"
390 "lightgray"
391 "darkgray"
392 "lightblue"
393 "lightgreen"
394 "lightcyan"
395 "lightred"
396 "lightmagenta"
397 "yellow"
398 "white")
399 "The list of colors available on a PC display under MS-DOS.")
400(defun x-defined-colors (&optional frame)
401 "Return a list of colors supported for a particular frame.
402The argument FRAME specifies which frame to try.
403The value may be different for frames on different X displays."
404 x-colors)
c432c362 405
c4d64969
EZ
406(defun face-color-supported-p (color)
407 (x-color-defined-p color))
408
409(defun face-color-gray-p (color)
410 (member (msdos-color-translate color)
411 '("black" "lightgray" "darkgray" "white")))
412
8254b6b4 413;; From lisp/term/w32-win.el
c432c362
EZ
414;
415;;;; Selections and cut buffers
bb3a4574 416;
c432c362
EZ
417;;; We keep track of the last text selected here, so we can check the
418;;; current selection against it, and avoid passing back our own text
419;;; from x-cut-buffer-or-selection-value.
420(defvar x-last-selected-text nil)
421
422(defvar x-select-enable-clipboard t
423 "Non-nil means cutting and pasting uses the clipboard.
424This is in addition to the primary selection.")
425
426(defun x-select-text (text &optional push)
427 (if x-select-enable-clipboard
8254b6b4 428 (w16-set-clipboard-data text))
c432c362
EZ
429 (setq x-last-selected-text text))
430
431;;; Return the value of the current selection.
432;;; Consult the selection, then the cut buffer. Treat empty strings
433;;; as if they were unset.
434(defun x-get-selection-value ()
435 (if x-select-enable-clipboard
436 (let (text)
437 ;; Don't die if x-get-selection signals an error.
438 (condition-case c
8254b6b4
EZ
439 (setq text (w16-get-clipboard-data))
440 (error (message "w16-get-clipboard-data:%s" c)))
c432c362
EZ
441 (if (string= text "") (setq text nil))
442 (cond
443 ((not text) nil)
444 ((eq text x-last-selected-text) nil)
445 ((string= text x-last-selected-text)
446 ;; Record the newer string, so subsequent calls can use the 'eq' test.
447 (setq x-last-selected-text text)
448 nil)
449 (t
450 (setq x-last-selected-text text))))))
451
452;;; Arrange for the kill and yank functions to set and check the clipboard.
453(setq interprogram-cut-function 'x-select-text)
454(setq interprogram-paste-function 'x-get-selection-value)
ef88bd2d 455
ea6401d7
RS
456;; From lisp/faces.el: we only have one font, so always return
457;; it, no matter which variety they've asked for.
458(defun x-frob-font-slant (font which)
459 font)
460
cee30b8f
RS
461;; From src/fontset.c:
462(fset 'query-fontset 'ignore)
463
e1ddc4b4
RS
464;; From lisp/term/x-win.el: make iconify-or-deiconify-frame a no-op.
465(fset 'iconify-or-deiconify-frame 'ignore)
466
ef88bd2d
MW
467;; From lisp/frame.el
468(fset 'set-default-font 'ignore)
469(fset 'set-mouse-color 'ignore) ; We cannot, I think.
470(fset 'set-cursor-color 'ignore) ; Hardware determined by char under.
471(fset 'set-border-color 'ignore) ; Not useful.
c8a6e3b9
EZ
472
473;; From lisp/term/x-win.el:
474(defconst x-long-option-alist
475 '(("--name" . "-name")
476 ("--title" . "-T")
477 ("--reverse-video" . "-reverse")
478 ("--foreground-color" . "-fg")
479 ("--background-color" . "-bg")))
ef88bd2d 480;; ---------------------------------------------------------------------------
c8a6e3b9 481;; Handle the X-like command line parameters "-fg", "-bg", "-name", etc.
ef88bd2d
MW
482(defun msdos-handle-args (args)
483 (let ((rest nil))
c8a6e3b9 484 (message "%s" args)
ef88bd2d 485 (while args
c8a6e3b9
EZ
486 (let* ((this (car args))
487 (orig-this this)
488 completion argval)
ef88bd2d 489 (setq args (cdr args))
c8a6e3b9
EZ
490 ;; Check for long options with attached arguments
491 ;; and separate out the attached option argument into argval.
492 (if (string-match "^--[^=]*=" this)
493 (setq argval (substring this (match-end 0))
494 this (substring this 0 (1- (match-end 0)))))
495 (setq completion (try-completion this x-long-option-alist))
496 (if (eq completion t)
497 ;; Exact match for long option.
498 (setq this (cdr (assoc this x-long-option-alist)))
499 (if (stringp completion)
500 (let ((elt (assoc completion x-long-option-alist)))
501 ;; Check for abbreviated long option.
502 (or elt
503 (error "Option `%s' is ambiguous" this))
504 (setq this (cdr elt)))
505 ;; Check for a short option.
506 (setq argval nil this orig-this)))
ef88bd2d 507 (cond ((or (string= this "-fg") (string= this "-foreground"))
c8a6e3b9
EZ
508 (or argval (setq argval (car args) args (cdr args)))
509 (setq default-frame-alist
510 (cons (cons 'foreground-color argval)
511 default-frame-alist)))
ef88bd2d 512 ((or (string= this "-bg") (string= this "-background"))
c8a6e3b9
EZ
513 (or argval (setq argval (car args) args (cdr args)))
514 (setq default-frame-alist
515 (cons (cons 'background-color argval)
516 default-frame-alist)))
517 ((or (string= this "-T") (string= this "-name"))
518 (or argval (setq argval (car args) args (cdr args)))
519 (setq default-frame-alist
520 (cons
521 (cons 'title
522 (if (stringp argval)
523 argval
524 (let ((case-fold-search t)
525 i)
526 (setq argval (invocation-name))
527
528 ;; Change any . or * characters in name to
529 ;; hyphens, so as to emulate behavior on X.
530 (while
531 (setq i (string-match "[.*]" argval))
532 (aset argval i ?-))
533 argval)))
534 default-frame-alist)))
535 ((or (string= this "-r")
536 (string= this "-rv")
537 (string= this "-reverse"))
538 (setq default-frame-alist
539 (cons '(reverse . t)
540 default-frame-alist)))
ef88bd2d 541 (t (setq rest (cons this rest))))))
c8a6e3b9 542 (nreverse rest)))
ef88bd2d
MW
543
544(setq command-line-args (msdos-handle-args command-line-args))
545;; ---------------------------------------------------------------------------
092af6d8
RS
546
547;;; pc-win.el ends here