Fix typos.
[bpt/emacs.git] / lisp / term / pc-win.el
1 ;;; pc-win.el --- setup support for `PC windows' (whatever that is)
2
3 ;; Copyright (C) 1994, 1996, 1997, 1999, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
5
6 ;; Author: Morten Welinder <terra@diku.dk>
7 ;; Maintainer: FSF
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 3 of the License, or
14 ;; (at your option) 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. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; This file is preloaded into Emacs by loadup.el. The functions in
27 ;; this file are then called during startup from startup.el. This
28 ;; means that just loading this file should not have any side effects
29 ;; besides defining functions and variables, and in particular should
30 ;; NOT initialize any window systems.
31
32 ;; The main entry points to this file's features are msdos-handle-args,
33 ;; msdos-create-frame-with-faces, msdos-initialize-window-system,
34 ;; terminal-init-internal. The last one is not supposed to be called,
35 ;; so it just errors out.
36
37 ;;; Code:
38
39 (if (not (fboundp 'msdos-remember-default-colors))
40 (error "%s: Loading pc-win.el but not compiled for MS-DOS"
41 (invocation-name)))
42
43 (load "term/internal" nil t)
44
45 (declare-function msdos-remember-default-colors "msdos.c")
46 (declare-function w16-set-clipboard-data "w16select.c")
47 (declare-function w16-get-clipboard-data "w16select.c")
48 (declare-function msdos-setup-keyboard "internal" (frame))
49
50 ;;; This was copied from etc/rgb.txt, except that some values were changed
51 ;;; a bit to make them consistent with DOS console colors, and the RGB
52 ;;; values were scaled up to 16 bits, as `tty-define-color' requires.
53 ;;;
54 ;;; The mapping between the 16 standard EGA/VGA colors and X color names
55 ;;; was done by running a Unix version of Emacs inside an X client and a
56 ;;; DJGPP-compiled Emacs on the same PC. The names of X colors used to
57 ;;; define the pixel values are shown as comments to each color below.
58 ;;;
59 ;;; If you want to change the RGB values, keep in mind that various pieces
60 ;;; of Emacs think that a color whose RGB values add up to less than 0.6 of
61 ;;; the values for WHITE (i.e. less than 117963) are ``dark'', otherwise the
62 ;;; color is ``light''; see `frame-set-background-mode' in lisp/faces.el for
63 ;;; an example.
64 (defvar msdos-color-values
65 '(("black" 0 0 0 0)
66 ("blue" 1 0 0 52480) ; MediumBlue
67 ("green" 2 8704 35584 8704) ; ForestGreen
68 ("cyan" 3 0 52736 53504) ; DarkTurquoise
69 ("red" 4 45568 8704 8704) ; FireBrick
70 ("magenta" 5 35584 0 35584) ; DarkMagenta
71 ("brown" 6 40960 20992 11520) ; Sienna
72 ("lightgray" 7 48640 48640 48640) ; Gray
73 ("darkgray" 8 26112 26112 26112) ; Gray40
74 ("lightblue" 9 0 0 65535) ; Blue
75 ("lightgreen" 10 0 65535 0) ; Green
76 ("lightcyan" 11 0 65535 65535) ; Cyan
77 ("lightred" 12 65535 0 0) ; Red
78 ("lightmagenta" 13 65535 0 65535) ; Magenta
79 ("yellow" 14 65535 65535 0) ; Yellow
80 ("white" 15 65535 65535 65535))
81 "A list of MS-DOS console colors, their indices and 16-bit RGB values.")
82
83 ;; ---------------------------------------------------------------------------
84 ;; We want to delay setting frame parameters until the faces are setup
85 (defvar default-frame-alist nil)
86 ;(modify-frame-parameters terminal-frame default-frame-alist)
87
88 (defun msdos-face-setup ()
89 "Initial setup of faces for the MS-DOS display."
90 (set-face-foreground 'bold "yellow")
91 (set-face-foreground 'italic "red")
92 (set-face-foreground 'bold-italic "lightred")
93 (set-face-foreground 'underline "white")
94
95 (make-face 'msdos-menu-active-face)
96 (make-face 'msdos-menu-passive-face)
97 (make-face 'msdos-menu-select-face)
98 (set-face-foreground 'msdos-menu-active-face "white")
99 (set-face-foreground 'msdos-menu-passive-face "lightgray")
100 (set-face-background 'msdos-menu-active-face "blue")
101 (set-face-background 'msdos-menu-passive-face "blue")
102 (set-face-background 'msdos-menu-select-face "red"))
103
104 (defun msdos-handle-reverse-video (frame parameters)
105 "Handle the reverse-video frame parameter on MS-DOS frames."
106 (when (cdr (or (assq 'reverse parameters)
107 (assq 'reverse default-frame-alist)))
108 (let* ((params (frame-parameters frame))
109 (fg (cdr (assq 'foreground-color params)))
110 (bg (cdr (assq 'background-color params))))
111 (if (equal fg (cdr (assq 'mouse-color params)))
112 (modify-frame-parameters frame
113 (list (cons 'mouse-color bg))))
114 (if (equal fg (cdr (assq 'cursor-color params)))
115 (modify-frame-parameters frame
116 (list (cons 'cursor-color bg)))))))
117
118 ;; This must run after all the default colors are inserted into
119 ;; tty-color-alist, since msdos-handle-reverse-video needs to know the
120 ;; actual frame colors.
121 (defun msdos-setup-initial-frame ()
122 (modify-frame-parameters terminal-frame default-frame-alist)
123 ;; This remembers the screen colors after applying default-frame-alist,
124 ;; so that all subsequent frames could begin with those colors.
125 (msdos-remember-default-colors terminal-frame)
126 (modify-frame-parameters terminal-frame initial-frame-alist)
127 (msdos-handle-reverse-video terminal-frame
128 (frame-parameters terminal-frame))
129
130 (frame-set-background-mode terminal-frame)
131 (face-set-after-frame-default terminal-frame))
132
133 ;; We create frames as if we were a terminal, but without invoking the
134 ;; terminal-initialization function. Also, our handling of reverse
135 ;; video is slightly different.
136 (defun msdos-create-frame-with-faces (&optional parameters)
137 "Create an frame on MS-DOS display.
138 Optional frame parameters PARAMETERS specify the frame parameters.
139 Parameters not specified by PARAMETERS are taken from
140 `default-frame-alist'. If either PARAMETERS or `default-frame-alist'
141 contains a `reverse' parameter, handle that. Value is the new frame
142 created."
143 (let ((frame (make-terminal-frame parameters))
144 success)
145 (unwind-protect
146 (with-selected-frame frame
147 (msdos-handle-reverse-video frame (frame-parameters frame))
148 (unless (terminal-parameter frame 'terminal-initted)
149 (set-terminal-parameter frame 'terminal-initted t))
150 (frame-set-background-mode frame)
151 (face-set-after-frame-default frame)
152 (setq success t))
153 (unless success (delete-frame frame)))
154 frame))
155
156 ;; ---------------------------------------------------------------------------
157 ;; More or less useful imitations of certain X-functions. A lot of the
158 ;; values returned are questionable, but usually only the form of the
159 ;; returned value matters. Also, by the way, recall that `ignore' is
160 ;; a useful function for returning 'nil regardless of argument.
161
162 ;; From src/xfns.c
163 (defun x-list-fonts (pattern &optional face frame maximum width)
164 (if (or (null width) (and (numberp width) (= width 1)))
165 (list "ms-dos")
166 (list "no-such-font")))
167 (defun x-display-pixel-width (&optional frame) (frame-width frame))
168 (defun x-display-pixel-height (&optional frame) (frame-height frame))
169 (defun x-display-planes (&optional frame) 4) ;bg switched to 16 colors as well
170 (defun x-display-color-cells (&optional frame) 16)
171 (defun x-server-max-request-size (&optional frame) 1000000) ; ???
172 (defun x-server-vendor (&optional frame) t "GNU")
173 (defun x-server-version (&optional frame) '(1 0 0))
174 (defun x-display-screens (&optional frame) 1)
175 (defun x-display-mm-height (&optional frame) 245) ; Guess the size of my
176 (defun x-display-mm-width (&optional frame) 322) ; monitor, EZ...
177 (defun x-display-backing-store (&optional frame) 'not-useful)
178 (defun x-display-visual-class (&optional frame) 'static-color)
179 (fset 'x-display-save-under 'ignore)
180 (fset 'x-get-resource 'ignore)
181
182 ;; From lisp/term/x-win.el
183 (defvar x-display-name "pc"
184 "The display name specifying the MS-DOS display and frame type.")
185 (defvar x-colors (mapcar 'car msdos-color-values)
186 "The list of colors available on a PC display under MS-DOS.")
187
188 ;; From lisp/term/w32-win.el
189 ;
190 ;;;; Selections and cut buffers
191 ;
192 ;;; We keep track of the last text selected here, so we can check the
193 ;;; current selection against it, and avoid passing back our own text
194 ;;; from x-cut-buffer-or-selection-value.
195 (defvar x-last-selected-text nil)
196
197 (defcustom x-select-enable-clipboard t
198 "Non-nil means cutting and pasting uses the clipboard.
199 This is the default on this system, since MS-Windows does not
200 support other types of selections."
201 :type 'boolean
202 :group 'killing)
203
204 (defun x-select-text (text &optional push)
205 (if x-select-enable-clipboard
206 (w16-set-clipboard-data text))
207 (setq x-last-selected-text text))
208
209 ;;; Return the value of the current selection.
210 ;;; Consult the selection, then the cut buffer. Treat empty strings
211 ;;; as if they were unset.
212 (defun x-get-selection-value ()
213 (if x-select-enable-clipboard
214 (let (text)
215 ;; Don't die if x-get-selection signals an error.
216 (condition-case c
217 (setq text (w16-get-clipboard-data))
218 (error (message "w16-get-clipboard-data:%s" c)))
219 (if (string= text "") (setq text nil))
220 (cond
221 ((not text) nil)
222 ((eq text x-last-selected-text) nil)
223 ((string= text x-last-selected-text)
224 ;; Record the newer string, so subsequent calls can use the 'eq' test.
225 (setq x-last-selected-text text)
226 nil)
227 (t
228 (setq x-last-selected-text text))))))
229
230 ;; From lisp/faces.el: we only have one font, so always return
231 ;; it, no matter which variety they've asked for.
232 (defun x-frob-font-slant (font which)
233 font)
234 (make-obsolete 'x-frob-font-slant 'make-face-... "21.1")
235 (defun x-frob-font-weight (font which)
236 font)
237 (make-obsolete 'x-frob-font-weight 'make-face-... "21.1")
238
239 ;; From src/fontset.c:
240 (fset 'query-fontset 'ignore)
241
242 ;; From lisp/term/x-win.el: make iconify-or-deiconify-frame a no-op.
243 (fset 'iconify-or-deiconify-frame 'ignore)
244
245 ;; From lisp/frame.el
246 (fset 'set-default-font 'ignore)
247 (fset 'set-mouse-color 'ignore) ; We cannot, I think.
248 (fset 'set-cursor-color 'ignore) ; Hardware determined by char under.
249 (fset 'set-border-color 'ignore) ; Not useful.
250
251 (defvar msdos-previous-message nil
252 "The content of the echo area before help echo was displayed.")
253
254 (defun msdos-show-help (help)
255 "Function installed as `show-help-function' on MS-DOS frames."
256 (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
257 ;; Don't know how to reproduce it in Elisp:
258 ;; Don't overwrite a keystroke echo.
259 ;; (NILP (echo_message_buffer) || ok_to_overwrite_keystroke_echo)
260 (not cursor-in-echo-area)) ;Don't overwrite a prompt.
261 (cond
262 ((stringp help)
263 (unless msdos-previous-message
264 (setq msdos-previous-message (current-message)))
265 (let ((message-truncate-lines nil)
266 (message-log-max nil))
267 (message "%s" (replace-regexp-in-string "\n" ", " help))))
268 ((stringp msdos-previous-message)
269 (let ((message-log-max nil))
270 (message "%s" msdos-previous-message)
271 (setq msdos-previous-message nil)))
272 (t
273 (message nil)))))
274
275
276 ;; Initialization.
277 ;; ---------------------------------------------------------------------------
278 ;; This function is run, by faces.el:tty-create-frame-with-faces, only
279 ;; for the initial frame (on each terminal, but we have only one).
280 ;; This works by setting the `terminal-initted' terminal parameter to
281 ;; this function, the first time `tty-create-frame-with-faces' is
282 ;; called on that terminal. `tty-create-frame-with-faces' is called
283 ;; directly from startup.el and also by `make-frame' through
284 ;; `frame-creation-function-alist'. `make-frame' will call this
285 ;; function if `msdos-create-frame-with-faces' (see below) is not
286 ;; found in `frame-creation-function-alist', which means something is
287 ;; _very_ wrong, because "internal" terminal emulator should not be
288 ;; turned on if our window-system is not `pc'. Therefore, the only
289 ;; Right Thing for us to do here is scream bloody murder.
290 (defun terminal-init-internal ()
291 "Terminal initialization function for the MS-DOS \"internal\" terminal.
292 Errors out because it is not supposed to be called, ever."
293 (error "terminal-init-internal called for window-system `%s'"
294 (window-system)))
295
296 (defun msdos-initialize-window-system ()
297 "Initialization function for the `pc' \"window system\"."
298 (or (eq (window-system) 'pc)
299 (error
300 "`msdos-initialize-window-system' called, but window-system is `%s'"
301 (window-system)))
302 ;; First, the keyboard.
303 (msdos-setup-keyboard terminal-frame) ; see internal.el
304 ;; Next, register the default colors.
305 (let* ((colors msdos-color-values)
306 (color (car colors)))
307 (tty-color-clear)
308 (while colors
309 (tty-color-define (car color) (cadr color) (cddr color))
310 (setq colors (cdr colors) color (car colors))))
311 ;; Modifying color mappings means realized faces don't
312 ;; use the right colors, so clear them.
313 (clear-face-cache)
314 ;; Now set up some additional faces.
315 (msdos-face-setup)
316 ;; Set up the initial frame.
317 (msdos-setup-initial-frame)
318 ;; Help echo is displayed in the echo area.
319 (setq show-help-function 'msdos-show-help)
320 ;; We want to delay the codepage-related setup until after user's
321 ;; .emacs is processed, because people might define their
322 ;; `dos-codepage-setup-hook' there.
323 (add-hook 'after-init-hook 'dos-codepage-setup)
324 ;; In multibyte mode, we want unibyte buffers to be displayed
325 ;; using the terminal coding system, so that they display
326 ;; correctly on the DOS terminal; in unibyte mode we want to see
327 ;; all 8-bit characters verbatim. In both cases, we want the
328 ;; entire range of 8-bit characters to arrive at our display code
329 ;; verbatim.
330 (standard-display-8bit 127 255)
331 ;; We are fast enough to make this optimization unnecessary.
332 (setq split-window-keep-point t)
333 ;; Arrange for the kill and yank functions to set and check the
334 ;; clipboard.
335 (setq interprogram-cut-function 'x-select-text)
336 (setq interprogram-paste-function 'x-get-selection-value)
337 (menu-bar-enable-clipboard)
338 (run-hooks 'terminal-init-msdos-hook))
339
340 ;; frame-creation-function-alist is examined by frame.el:make-frame.
341 (add-to-list 'frame-creation-function-alist
342 '(pc . msdos-create-frame-with-faces))
343 ;; window-system-initialization-alist is examined by startup.el:command-line.
344 (add-to-list 'window-system-initialization-alist
345 '(pc . msdos-initialize-window-system))
346 ;; We don't need anything beyond tty-handle-args for handling
347 ;; command-line argument; see startup.el.
348 (add-to-list 'handle-args-function-alist '(pc . tty-handle-args))
349
350 ;; ---------------------------------------------------------------------------
351
352 (provide 'pc-win)
353
354 ;; arch-tag: 5cbdb455-b495-427b-95d0-e417d77d00b4
355 ;;; pc-win.el ends here