Merge changes made in Gnus trunk.
[bpt/emacs.git] / lisp / term / pc-win.el
CommitLineData
55535639 1;;; pc-win.el --- setup support for `PC windows' (whatever that is)
ef88bd2d 2
73b0cd50 3;; Copyright (C) 1994, 1996-1997, 1999, 2001-2011 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
1fecc8fe 10;; GNU Emacs is free software: you can redistribute it and/or modify
ef88bd2d 11;; it under the terms of the GNU General Public License as published by
1fecc8fe
GM
12;; the Free Software Foundation, either version 3 of the License, or
13;; (at your option) any later version.
ef88bd2d
MW
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
1fecc8fe 21;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
2fe590dc 22
55535639
PJ
23;;; Commentary:
24
cbcc5ad4
EZ
25;; This file is preloaded into Emacs by loadup.el. The functions in
26;; this file are then called during startup from startup.el. This
27;; means that just loading this file should not have any side effects
28;; besides defining functions and variables, and in particular should
29;; NOT initialize any window systems.
30
31;; The main entry points to this file's features are msdos-handle-args,
32;; msdos-create-frame-with-faces, msdos-initialize-window-system,
33;; terminal-init-internal. The last one is not supposed to be called,
34;; so it just errors out.
35
2fe590dc
EN
36;;; Code:
37
cbcc5ad4
EZ
38(if (not (fboundp 'msdos-remember-default-colors))
39 (error "%s: Loading pc-win.el but not compiled for MS-DOS"
40 (invocation-name)))
41
ef88bd2d
MW
42(load "term/internal" nil t)
43
73e6adaa
DN
44(declare-function msdos-remember-default-colors "msdos.c")
45(declare-function w16-set-clipboard-data "w16select.c")
46(declare-function w16-get-clipboard-data "w16select.c")
92f9269e 47(declare-function msdos-setup-keyboard "internal" (frame))
73e6adaa 48
cbcc5ad4 49;;; This was copied from etc/rgb.txt, except that some values were changed
f795f633
EZ
50;;; a bit to make them consistent with DOS console colors, and the RGB
51;;; values were scaled up to 16 bits, as `tty-define-color' requires.
52;;;
53;;; The mapping between the 16 standard EGA/VGA colors and X color names
54;;; was done by running a Unix version of Emacs inside an X client and a
55;;; DJGPP-compiled Emacs on the same PC. The names of X colors used to
56;;; define the pixel values are shown as comments to each color below.
c4d64969
EZ
57;;;
58;;; If you want to change the RGB values, keep in mind that various pieces
59;;; of Emacs think that a color whose RGB values add up to less than 0.6 of
f795f633 60;;; the values for WHITE (i.e. less than 117963) are ``dark'', otherwise the
c4d64969
EZ
61;;; color is ``light''; see `frame-set-background-mode' in lisp/faces.el for
62;;; an example.
63(defvar msdos-color-values
1400df92 64 '(("black" 0 0 0 0)
f795f633 65 ("blue" 1 0 0 52480) ; MediumBlue
1400df92
EZ
66 ("green" 2 8704 35584 8704) ; ForestGreen
67 ("cyan" 3 0 52736 53504) ; DarkTurquoise
68 ("red" 4 45568 8704 8704) ; FireBrick
69 ("magenta" 5 35584 0 35584) ; DarkMagenta
70 ("brown" 6 40960 20992 11520) ; Sienna
71 ("lightgray" 7 48640 48640 48640) ; Gray
72 ("darkgray" 8 26112 26112 26112) ; Gray40
73 ("lightblue" 9 0 0 65535) ; Blue
74 ("lightgreen" 10 0 65535 0) ; Green
75 ("lightcyan" 11 0 65535 65535) ; Cyan
76 ("lightred" 12 65535 0 0) ; Red
77 ("lightmagenta" 13 65535 0 65535) ; Magenta
78 ("yellow" 14 65535 65535 0) ; Yellow
79 ("white" 15 65535 65535 65535))
f795f633 80 "A list of MS-DOS console colors, their indices and 16-bit RGB values.")
a13b5fad 81
ef88bd2d
MW
82;; ---------------------------------------------------------------------------
83;; We want to delay setting frame parameters until the faces are setup
84(defvar default-frame-alist nil)
cbcc5ad4 85;(modify-frame-parameters terminal-frame default-frame-alist)
ef88bd2d
MW
86
87(defun msdos-face-setup ()
7ced34de
EZ
88 "Initial setup of faces for the MS-DOS display."
89 (set-face-foreground 'bold "yellow")
90 (set-face-foreground 'italic "red")
91 (set-face-foreground 'bold-italic "lightred")
92 (set-face-foreground 'underline "white")
ef88bd2d
MW
93
94 (make-face 'msdos-menu-active-face)
95 (make-face 'msdos-menu-passive-face)
96 (make-face 'msdos-menu-select-face)
7ced34de
EZ
97 (set-face-foreground 'msdos-menu-active-face "white")
98 (set-face-foreground 'msdos-menu-passive-face "lightgray")
99 (set-face-background 'msdos-menu-active-face "blue")
100 (set-face-background 'msdos-menu-passive-face "blue")
101 (set-face-background 'msdos-menu-select-face "red"))
ef88bd2d 102
f795f633
EZ
103(defun msdos-handle-reverse-video (frame parameters)
104 "Handle the reverse-video frame parameter on MS-DOS frames."
c88c89a0
EZ
105 (when (cdr (or (assq 'reverse parameters)
106 (assq 'reverse default-frame-alist)))
f795f633 107 (let* ((params (frame-parameters frame))
8fcacf13
EZ
108 (fg (cdr (assq 'foreground-color params)))
109 (bg (cdr (assq 'background-color params))))
110 (if (equal fg (cdr (assq 'mouse-color params)))
f795f633 111 (modify-frame-parameters frame
8fcacf13
EZ
112 (list (cons 'mouse-color bg))))
113 (if (equal fg (cdr (assq 'cursor-color params)))
f795f633 114 (modify-frame-parameters frame
8fcacf13 115 (list (cons 'cursor-color bg)))))))
f795f633
EZ
116
117;; This must run after all the default colors are inserted into
118;; tty-color-alist, since msdos-handle-reverse-video needs to know the
cbcc5ad4 119;; actual frame colors.
f795f633
EZ
120(defun msdos-setup-initial-frame ()
121 (modify-frame-parameters terminal-frame default-frame-alist)
122 ;; This remembers the screen colors after applying default-frame-alist,
123 ;; so that all subsequent frames could begin with those colors.
124 (msdos-remember-default-colors terminal-frame)
125 (modify-frame-parameters terminal-frame initial-frame-alist)
126 (msdos-handle-reverse-video terminal-frame
127 (frame-parameters terminal-frame))
128
129 (frame-set-background-mode terminal-frame)
130 (face-set-after-frame-default terminal-frame))
131
cbcc5ad4
EZ
132;; We create frames as if we were a terminal, but without invoking the
133;; terminal-initialization function. Also, our handling of reverse
134;; video is slightly different.
135(defun msdos-create-frame-with-faces (&optional parameters)
136 "Create an frame on MS-DOS display.
137Optional frame parameters PARAMETERS specify the frame parameters.
138Parameters not specified by PARAMETERS are taken from
139`default-frame-alist'. If either PARAMETERS or `default-frame-alist'
140contains a `reverse' parameter, handle that. Value is the new frame
141created."
f795f633
EZ
142 (let ((frame (make-terminal-frame parameters))
143 success)
68a89a25 144 (unwind-protect
cbcc5ad4 145 (with-selected-frame frame
f795f633 146 (msdos-handle-reverse-video frame (frame-parameters frame))
cbcc5ad4
EZ
147 (unless (terminal-parameter frame 'terminal-initted)
148 (set-terminal-parameter frame 'terminal-initted t))
68a89a25
EZ
149 (frame-set-background-mode frame)
150 (face-set-after-frame-default frame)
151 (setq success t))
152 (unless success (delete-frame frame)))
e2f35ede 153 frame))
9000684d 154
ef88bd2d 155;; ---------------------------------------------------------------------------
a7acbbe4 156;; More or less useful imitations of certain X-functions. A lot of the
ef88bd2d
MW
157;; values returned are questionable, but usually only the form of the
158;; returned value matters. Also, by the way, recall that `ignore' is
159;; a useful function for returning 'nil regardless of argument.
160
161;; From src/xfns.c
cee30b8f 162(defun x-list-fonts (pattern &optional face frame maximum width)
c0e2e77f
EZ
163 (if (or (null width) (and (numberp width) (= width 1)))
164 (list "ms-dos")
cee30b8f 165 (list "no-such-font")))
a259d337
RS
166(defun x-display-pixel-width (&optional frame) (frame-width frame))
167(defun x-display-pixel-height (&optional frame) (frame-height frame))
c0e2e77f
EZ
168(defun x-display-planes (&optional frame) 4) ;bg switched to 16 colors as well
169(defun x-display-color-cells (&optional frame) 16)
ef88bd2d
MW
170(defun x-server-max-request-size (&optional frame) 1000000) ; ???
171(defun x-server-vendor (&optional frame) t "GNU")
172(defun x-server-version (&optional frame) '(1 0 0))
173(defun x-display-screens (&optional frame) 1)
c0e2e77f
EZ
174(defun x-display-mm-height (&optional frame) 245) ; Guess the size of my
175(defun x-display-mm-width (&optional frame) 322) ; monitor, EZ...
ef88bd2d
MW
176(defun x-display-backing-store (&optional frame) 'not-useful)
177(defun x-display-visual-class (&optional frame) 'static-color)
178(fset 'x-display-save-under 'ignore)
179(fset 'x-get-resource 'ignore)
a0c712ae 180
ef88bd2d 181;; From lisp/term/x-win.el
f795f633 182(defvar x-display-name "pc"
3077d1f6
EZ
183 "The name of the window display on which Emacs was started.
184On X, the display name of individual X frames is recorded in the
185`display' frame parameter.")
c0e2e77f 186(defvar x-colors (mapcar 'car msdos-color-values)
3077d1f6
EZ
187 "List of basic colors available on color displays.
188For X, the list comes from the `rgb.txt' file,v 10.41 94/02/20.
189For Nextstep, this is a list of non-PANTONE colors returned by
190the operating system.")
c4d64969 191
8254b6b4 192;; From lisp/term/w32-win.el
c432c362 193;
45240125 194;;;; Selections
bb3a4574 195;
c432c362
EZ
196;;; We keep track of the last text selected here, so we can check the
197;;; current selection against it, and avoid passing back our own text
6d7cc563 198;;; from x-selection-value.
c432c362
EZ
199(defvar x-last-selected-text nil)
200
8c717810
EZ
201(defcustom x-select-enable-clipboard t
202 "Non-nil means cutting and pasting uses the clipboard.
3077d1f6
EZ
203This is in addition to, but in preference to, the primary selection.
204
6f748f70 205Note that MS-Windows does not support selection types other than the
725513b7
GM
206clipboard. (The primary selection that is set by Emacs is not
207accessible to other programs on MS-Windows.)
208
209This variable is not used by the Nextstep port."
8c717810
EZ
210 :type 'boolean
211 :group 'killing)
c432c362 212
6d7cc563 213(defun x-select-text (text)
3077d1f6
EZ
214 "Select TEXT, a string, according to the window system.
215
6d7cc563
JD
216On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
217clipboard. If `x-select-enable-primary' is non-nil, put TEXT in
218the primary selection.
3077d1f6
EZ
219
220On Windows, make TEXT the current selection. If
221`x-select-enable-clipboard' is non-nil, copy the text to the
6d7cc563 222clipboard as well.
3077d1f6 223
6d7cc563 224On Nextstep, put TEXT in the pasteboard."
db95369b 225 (if x-select-enable-clipboard
8254b6b4 226 (w16-set-clipboard-data text))
c432c362 227 (setq x-last-selected-text text))
db95369b 228
c432c362 229;;; Return the value of the current selection.
45240125 230;;; Consult the selection. Treat empty strings as if they were unset.
c432c362 231(defun x-get-selection-value ()
db95369b 232 (if x-select-enable-clipboard
c432c362
EZ
233 (let (text)
234 ;; Don't die if x-get-selection signals an error.
235 (condition-case c
8254b6b4
EZ
236 (setq text (w16-get-clipboard-data))
237 (error (message "w16-get-clipboard-data:%s" c)))
c432c362
EZ
238 (if (string= text "") (setq text nil))
239 (cond
240 ((not text) nil)
241 ((eq text x-last-selected-text) nil)
242 ((string= text x-last-selected-text)
243 ;; Record the newer string, so subsequent calls can use the 'eq' test.
244 (setq x-last-selected-text text)
245 nil)
246 (t
247 (setq x-last-selected-text text))))))
248
f22693fc
EZ
249;; x-selection-owner-p is used in simple.el.
250(defun x-selection-owner-p (&optional type)
251 "Whether the current Emacs process owns the given X Selection.
252The arg should be the name of the selection in question, typically one of
253the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
254\(Those are literal upper-case symbol names, since that's what X expects.)
255For convenience, the symbol nil is the same as `PRIMARY',
256and t is the same as `SECONDARY'."
257 (if x-select-enable-clipboard
258 (let (text)
259 ;; Don't die if w16-get-clipboard-data signals an error.
260 (ignore-errors
261 (setq text (w16-get-clipboard-data)))
262 ;; We consider ourselves the owner of the selection if it does
263 ;; not exist, or exists and compares equal with the last text
264 ;; we've put into the Windows clipboard.
265 (cond
266 ((not text) t)
267 ((or (eq text x-last-selected-text)
268 (string= text x-last-selected-text))
269 text)
270 (t nil)))))
271
272;; x-own-selection-internal and x-disown-selection-internal are used
273;; in select.el:x-set-selection.
274(defun x-own-selection-internal (type value)
275 "Assert an X selection of the given TYPE with the given VALUE.
276TYPE is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
277\(Those are literal upper-case symbol names, since that's what X expects.)
278VALUE is typically a string, or a cons of two markers, but may be
279anything that the functions on `selection-converter-alist' know about."
280 (ignore-errors
281 (x-select-text value))
282 value)
283
284(defun x-disown-selection-internal (selection &optional time)
285 "If we own the selection SELECTION, disown it.
286Disowning it means there is no such selection."
287 (if (x-selection-owner-p selection)
288 t))
289
a2249e66
EZ
290;; x-get-selection-internal is used in select.el
291(defun x-get-selection-internal (selection type &optional time_stamp)
292 "Return text selected from some X window.
293SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
294\(Those are literal upper-case symbol names, since that's what X expects.)
295TYPE is the type of data desired, typically `STRING'.
296TIME_STAMP is the time to use in the XConvertSelection call for foreign
297selections. If omitted, defaults to the time for the last event."
298 (x-get-selection-value))
299
cee30b8f
RS
300;; From src/fontset.c:
301(fset 'query-fontset 'ignore)
302
e1ddc4b4
RS
303;; From lisp/term/x-win.el: make iconify-or-deiconify-frame a no-op.
304(fset 'iconify-or-deiconify-frame 'ignore)
305
ef88bd2d
MW
306;; From lisp/frame.el
307(fset 'set-default-font 'ignore)
308(fset 'set-mouse-color 'ignore) ; We cannot, I think.
309(fset 'set-cursor-color 'ignore) ; Hardware determined by char under.
310(fset 'set-border-color 'ignore) ; Not useful.
c8a6e3b9 311
9993f59a
EZ
312(defvar msdos-last-help-message nil
313 "The last help message received via `show-help-function'.
314This is used by `msdos-show-help'.")
315
23608640
EZ
316(defvar msdos-previous-message nil
317 "The content of the echo area before help echo was displayed.")
318
319(defun msdos-show-help (help)
8fc29035 320 "Function installed as `show-help-function' on MS-DOS frames."
23608640 321 (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
23608640
EZ
322 (not cursor-in-echo-area)) ;Don't overwrite a prompt.
323 (cond
324 ((stringp help)
9993f59a
EZ
325 (setq help (replace-regexp-in-string "\n" ", " help))
326 (unless (or msdos-previous-message
327 (string-equal help (current-message))
328 (and (stringp msdos-last-help-message)
329 (string-equal msdos-last-help-message
330 (current-message))))
23608640 331 (setq msdos-previous-message (current-message)))
9993f59a 332 (setq msdos-last-help-message help)
08792c11 333 (let ((message-truncate-lines nil)
23608640 334 (message-log-max nil))
9993f59a 335 (message "%s" help)))
23608640
EZ
336 ((stringp msdos-previous-message)
337 (let ((message-log-max nil))
338 (message "%s" msdos-previous-message)
339 (setq msdos-previous-message nil)))
340 (t
341 (message nil)))))
342
343
cbcc5ad4 344;; Initialization.
ef88bd2d 345;; ---------------------------------------------------------------------------
cbcc5ad4
EZ
346;; This function is run, by faces.el:tty-create-frame-with-faces, only
347;; for the initial frame (on each terminal, but we have only one).
348;; This works by setting the `terminal-initted' terminal parameter to
349;; this function, the first time `tty-create-frame-with-faces' is
350;; called on that terminal. `tty-create-frame-with-faces' is called
351;; directly from startup.el and also by `make-frame' through
352;; `frame-creation-function-alist'. `make-frame' will call this
353;; function if `msdos-create-frame-with-faces' (see below) is not
354;; found in `frame-creation-function-alist', which means something is
355;; _very_ wrong, because "internal" terminal emulator should not be
356;; turned on if our window-system is not `pc'. Therefore, the only
357;; Right Thing for us to do here is scream bloody murder.
358(defun terminal-init-internal ()
359 "Terminal initialization function for the MS-DOS \"internal\" terminal.
360Errors out because it is not supposed to be called, ever."
361 (error "terminal-init-internal called for window-system `%s'"
362 (window-system)))
363
364(defun msdos-initialize-window-system ()
365 "Initialization function for the `pc' \"window system\"."
366 (or (eq (window-system) 'pc)
367 (error
368 "`msdos-initialize-window-system' called, but window-system is `%s'"
369 (window-system)))
370 ;; First, the keyboard.
371 (msdos-setup-keyboard terminal-frame) ; see internal.el
372 ;; Next, register the default colors.
373 (let* ((colors msdos-color-values)
374 (color (car colors)))
375 (tty-color-clear)
376 (while colors
377 (tty-color-define (car color) (cadr color) (cddr color))
378 (setq colors (cdr colors) color (car colors))))
379 ;; Modifying color mappings means realized faces don't
380 ;; use the right colors, so clear them.
381 (clear-face-cache)
382 ;; Now set up some additional faces.
383 (msdos-face-setup)
384 ;; Set up the initial frame.
385 (msdos-setup-initial-frame)
23608640
EZ
386 ;; Help echo is displayed in the echo area.
387 (setq show-help-function 'msdos-show-help)
cbcc5ad4
EZ
388 ;; We want to delay the codepage-related setup until after user's
389 ;; .emacs is processed, because people might define their
390 ;; `dos-codepage-setup-hook' there.
391 (add-hook 'after-init-hook 'dos-codepage-setup)
392 ;; In multibyte mode, we want unibyte buffers to be displayed
393 ;; using the terminal coding system, so that they display
394 ;; correctly on the DOS terminal; in unibyte mode we want to see
395 ;; all 8-bit characters verbatim. In both cases, we want the
396 ;; entire range of 8-bit characters to arrive at our display code
397 ;; verbatim.
398 (standard-display-8bit 127 255)
399 ;; We are fast enough to make this optimization unnecessary.
400 (setq split-window-keep-point t)
401 ;; Arrange for the kill and yank functions to set and check the
402 ;; clipboard.
403 (setq interprogram-cut-function 'x-select-text)
404 (setq interprogram-paste-function 'x-get-selection-value)
405 (menu-bar-enable-clipboard)
406 (run-hooks 'terminal-init-msdos-hook))
407
408;; frame-creation-function-alist is examined by frame.el:make-frame.
409(add-to-list 'frame-creation-function-alist
410 '(pc . msdos-create-frame-with-faces))
411;; window-system-initialization-alist is examined by startup.el:command-line.
412(add-to-list 'window-system-initialization-alist
413 '(pc . msdos-initialize-window-system))
414;; We don't need anything beyond tty-handle-args for handling
415;; command-line argument; see startup.el.
416(add-to-list 'handle-args-function-alist '(pc . tty-handle-args))
417
418;; ---------------------------------------------------------------------------
419
420(provide 'pc-win)
092af6d8
RS
421
422;;; pc-win.el ends here