pc-win.el 18.6 KB
Newer Older
1
;;; pc-win.el --- setup support for `PC windows' (whatever that is)
Morten Welinder's avatar
Morten Welinder committed
2

3
;; Copyright (C) 1994, 1996-1997, 1999, 2001-2012
4
;;   Free Software Foundation, Inc.
Morten Welinder's avatar
Morten Welinder committed
5 6

;; Author: Morten Welinder <terra@diku.dk>
Richard M. Stallman's avatar
Richard M. Stallman committed
7
;; Maintainer: FSF
Morten Welinder's avatar
Morten Welinder committed
8 9 10

;; This file is part of GNU Emacs.

11
;; GNU Emacs is free software: you can redistribute it and/or modify
Morten Welinder's avatar
Morten Welinder committed
12
;; it under the terms of the GNU General Public License as published by
13 14
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
Morten Welinder's avatar
Morten Welinder committed
15 16 17 18 19 20 21

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; 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/>.
Erik Naggum's avatar
Erik Naggum committed
23

24 25
;;; Commentary:

26 27 28 29 30 31 32 33 34 35 36
;; This file is preloaded into Emacs by loadup.el.  The functions in
;; this file are then called during startup from startup.el.  This
;; means that just loading this file should not have any side effects
;; besides defining functions and variables, and in particular should
;; NOT initialize any window systems.

;; The main entry points to this file's features are msdos-handle-args,
;; msdos-create-frame-with-faces, msdos-initialize-window-system,
;; terminal-init-internal.  The last one is not supposed to be called,
;; so it just errors out.

Erik Naggum's avatar
Erik Naggum committed
37 38
;;; Code:

39 40 41 42
(if (not (fboundp 'msdos-remember-default-colors))
    (error "%s: Loading pc-win.el but not compiled for MS-DOS"
	   (invocation-name)))

Morten Welinder's avatar
Morten Welinder committed
43 44
(load "term/internal" nil t)

45 46 47
(declare-function msdos-remember-default-colors "msdos.c")
(declare-function w16-set-clipboard-data "w16select.c")
(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 52 53 54 55 56 57
;;; a bit to make them consistent with DOS console colors, and the RGB
;;; values were scaled up to 16 bits, as `tty-define-color' requires.
;;;
;;; The mapping between the 16 standard EGA/VGA colors and X color names
;;; was done by running a Unix version of Emacs inside an X client and a
;;; DJGPP-compiled Emacs on the same PC.  The names of X colors used to
;;; define the pixel values are shown as comments to each color below.
58 59 60
;;;
;;; If you want to change the RGB values, keep in mind that various pieces
;;; 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 63 64
;;; color is ``light''; see `frame-set-background-mode' in lisp/faces.el for
;;; an example.
(defvar msdos-color-values
65
  '(("black"          0     0     0     0)
66
    ("blue"           1     0     0 52480) ; MediumBlue
67 68 69 70 71 72 73 74 75 76 77 78 79 80
    ("green"          2  8704 35584  8704) ; ForestGreen
    ("cyan"           3     0 52736 53504) ; DarkTurquoise
    ("red"            4 45568  8704  8704) ; FireBrick
    ("magenta"        5 35584     0 35584) ; DarkMagenta
    ("brown"          6 40960 20992 11520) ; Sienna
    ("lightgray"      7 48640 48640 48640) ; Gray
    ("darkgray"       8 26112 26112 26112) ; Gray40
    ("lightblue"      9     0     0 65535) ; Blue
    ("lightgreen"    10     0 65535     0) ; Green
    ("lightcyan"     11     0 65535 65535) ; Cyan
    ("lightred"      12 65535     0     0) ; Red
    ("lightmagenta"  13 65535     0 65535) ; Magenta
    ("yellow"        14 65535 65535     0) ; Yellow
    ("white"         15 65535 65535 65535))
81
  "A list of MS-DOS console colors, their indices and 16-bit RGB values.")
82

Morten Welinder's avatar
Morten Welinder committed
83 84 85
;; ---------------------------------------------------------------------------
;; We want to delay setting frame parameters until the faces are setup
(defvar default-frame-alist nil)
86
;(modify-frame-parameters terminal-frame default-frame-alist)
Morten Welinder's avatar
Morten Welinder committed
87 88

(defun msdos-face-setup ()
89 90 91 92 93
  "Initial setup of faces for the MS-DOS display."
  (set-face-foreground 'bold "yellow")
  (set-face-foreground 'italic "red")
  (set-face-foreground 'bold-italic "lightred")
  (set-face-foreground 'underline "white")
Morten Welinder's avatar
Morten Welinder committed
94 95 96 97

  (make-face 'msdos-menu-active-face)
  (make-face 'msdos-menu-passive-face)
  (make-face 'msdos-menu-select-face)
98 99 100 101 102
  (set-face-foreground 'msdos-menu-active-face "white")
  (set-face-foreground 'msdos-menu-passive-face "lightgray")
  (set-face-background 'msdos-menu-active-face "blue")
  (set-face-background 'msdos-menu-passive-face "blue")
  (set-face-background 'msdos-menu-select-face "red"))
Morten Welinder's avatar
Morten Welinder committed
103

104 105
(defun msdos-handle-reverse-video (frame parameters)
  "Handle the reverse-video frame parameter on MS-DOS frames."
106 107
  (when (cdr (or (assq 'reverse parameters)
		 (assq 'reverse default-frame-alist)))
108
      (let* ((params (frame-parameters frame))
109 110 111
	     (fg (cdr (assq 'foreground-color params)))
	     (bg (cdr (assq 'background-color params))))
	(if (equal fg (cdr (assq 'mouse-color params)))
112
	    (modify-frame-parameters frame
113 114
				     (list (cons 'mouse-color bg))))
	(if (equal fg (cdr (assq 'cursor-color params)))
115
	    (modify-frame-parameters frame
116
				     (list (cons 'cursor-color bg)))))))
117 118 119

;; This must run after all the default colors are inserted into
;; tty-color-alist, since msdos-handle-reverse-video needs to know the
120
;; actual frame colors.
121 122 123 124 125 126 127 128 129 130 131 132
(defun msdos-setup-initial-frame ()
  (modify-frame-parameters terminal-frame default-frame-alist)
  ;; This remembers the screen colors after applying default-frame-alist,
  ;; so that all subsequent frames could begin with those colors.
  (msdos-remember-default-colors terminal-frame)
  (modify-frame-parameters terminal-frame initial-frame-alist)
  (msdos-handle-reverse-video terminal-frame
			      (frame-parameters terminal-frame))

  (frame-set-background-mode terminal-frame)
  (face-set-after-frame-default terminal-frame))

133 134 135 136
;; We create frames as if we were a terminal, but without invoking the
;; terminal-initialization function.  Also, our handling of reverse
;; video is slightly different.
(defun msdos-create-frame-with-faces (&optional parameters)
Juanma Barranquero's avatar
Juanma Barranquero committed
137
  "Create a frame on MS-DOS display.
138 139 140 141 142
Optional frame parameters PARAMETERS specify the frame parameters.
Parameters not specified by PARAMETERS are taken from
`default-frame-alist'.  If either PARAMETERS or `default-frame-alist'
contains a `reverse' parameter, handle that.  Value is the new frame
created."
143 144
  (let ((frame (make-terminal-frame parameters))
	success)
Eli Zaretskii's avatar
Eli Zaretskii committed
145
    (unwind-protect
146
	(with-selected-frame frame
147
	  (msdos-handle-reverse-video frame (frame-parameters frame))
148 149
          (unless (terminal-parameter frame 'terminal-initted)
            (set-terminal-parameter frame 'terminal-initted t))
Eli Zaretskii's avatar
Eli Zaretskii committed
150 151 152 153
	  (frame-set-background-mode frame)
	  (face-set-after-frame-default frame)
	  (setq success t))
      (unless success (delete-frame frame)))
154
    frame))
155

Morten Welinder's avatar
Morten Welinder committed
156
;; ---------------------------------------------------------------------------
Karl Heuer's avatar
Karl Heuer committed
157
;; More or less useful imitations of certain X-functions.  A lot of the
Morten Welinder's avatar
Morten Welinder committed
158 159 160 161 162
;; values returned are questionable, but usually only the form of the
;; returned value matters.  Also, by the way, recall that `ignore' is
;; a useful function for returning 'nil regardless of argument.

;; From src/xfns.c
163
(defun x-list-fonts (pattern &optional face frame maximum width)
164 165
  (if (or (null width) (and (numberp width) (= width 1)))
      (list "ms-dos")
166
    (list "no-such-font")))
167 168
(defun x-display-pixel-width (&optional frame) (frame-width frame))
(defun x-display-pixel-height (&optional frame) (frame-height frame))
169 170
(defun x-display-planes (&optional frame) 4) ;bg switched to 16 colors as well
(defun x-display-color-cells (&optional frame) 16)
Morten Welinder's avatar
Morten Welinder committed
171 172 173 174
(defun x-server-max-request-size (&optional frame) 1000000) ; ???
(defun x-server-vendor (&optional frame) t "GNU")
(defun x-server-version (&optional frame) '(1 0 0))
(defun x-display-screens (&optional frame) 1)
175 176
(defun x-display-mm-height (&optional frame) 245) ; Guess the size of my
(defun x-display-mm-width (&optional frame) 322)  ; monitor, EZ...
Morten Welinder's avatar
Morten Welinder committed
177 178 179 180
(defun x-display-backing-store (&optional frame) 'not-useful)
(defun x-display-visual-class (&optional frame) 'static-color)
(fset 'x-display-save-under 'ignore)
(fset 'x-get-resource 'ignore)
181

Morten Welinder's avatar
Morten Welinder committed
182
;; From lisp/term/x-win.el
183
(defvar x-display-name "pc"
Eli Zaretskii's avatar
Eli Zaretskii committed
184 185 186
  "The name of the window display on which Emacs was started.
On X, the display name of individual X frames is recorded in the
`display' frame parameter.")
187
(defvar x-colors (mapcar 'car msdos-color-values)
Eli Zaretskii's avatar
Eli Zaretskii committed
188 189 190 191
  "List of basic colors available on color displays.
For X, the list comes from the `rgb.txt' file,v 10.41 94/02/20.
For Nextstep, this is a list of non-PANTONE colors returned by
the operating system.")
192

193
;; From lisp/term/w32-win.el
194
;
195
;;;; Selections
196
;
197 198
;;; We keep track of the last text selected here, so we can check the
;;; current selection against it, and avoid passing back our own text
Jan Djärv's avatar
Jan Djärv committed
199
;;; from x-selection-value.
200 201
(defvar x-last-selected-text nil)

202 203
(defcustom x-select-enable-clipboard t
  "Non-nil means cutting and pasting uses the clipboard.
Eli Zaretskii's avatar
Eli Zaretskii committed
204 205
This is in addition to, but in preference to, the primary selection.

206
Note that MS-Windows does not support selection types other than the
Glenn Morris's avatar
Glenn Morris committed
207 208 209 210
clipboard.  (The primary selection that is set by Emacs is not
accessible to other programs on MS-Windows.)

This variable is not used by the Nextstep port."
211 212
  :type 'boolean
  :group 'killing)
213

Jan Djärv's avatar
Jan Djärv committed
214
(defun x-select-text (text)
Eli Zaretskii's avatar
Eli Zaretskii committed
215 216
  "Select TEXT, a string, according to the window system.

Jan Djärv's avatar
Jan Djärv committed
217 218 219
On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
clipboard.  If `x-select-enable-primary' is non-nil, put TEXT in
the primary selection.
Eli Zaretskii's avatar
Eli Zaretskii committed
220

221
On MS-Windows, make TEXT the current selection.  If
Eli Zaretskii's avatar
Eli Zaretskii committed
222
`x-select-enable-clipboard' is non-nil, copy the text to the
Jan Djärv's avatar
Jan Djärv committed
223
clipboard as well.
Eli Zaretskii's avatar
Eli Zaretskii committed
224

225 226
On Nextstep, put TEXT in the pasteboard (`x-select-enable-clipboard'
is not used)."
227
  (if x-select-enable-clipboard
228
      (w16-set-clipboard-data text))
229
  (setq x-last-selected-text text))
230

231
;;; Return the value of the current selection.
232
;;; Consult the selection.  Treat empty strings as if they were unset.
233
(defun x-get-selection-value ()
234
  (if x-select-enable-clipboard
235 236 237
      (let (text)
	;; Don't die if x-get-selection signals an error.
	(condition-case c
238 239
	    (setq text (w16-get-clipboard-data))
	  (error (message "w16-get-clipboard-data:%s" c)))
240 241 242 243 244 245 246 247 248 249 250
	(if (string= text "") (setq text nil))
	(cond
	 ((not text) nil)
	 ((eq text x-last-selected-text) nil)
	 ((string= text x-last-selected-text)
	  ;; Record the newer string, so subsequent calls can use the 'eq' test.
	  (setq x-last-selected-text text)
	  nil)
	 (t
	  (setq x-last-selected-text text))))))

251
;; x-selection-owner-p is used in simple.el.
252
(defun x-selection-owner-p (&optional selection terminal)
253 254 255 256 257
  "Whether the current Emacs process owns the given X Selection.
The arg should be the name of the selection in question, typically one of
the symbols `PRIMARY', `SECONDARY', or `CLIPBOARD'.
\(Those are literal upper-case symbol names, since that's what X expects.)
For convenience, the symbol nil is the same as `PRIMARY',
258 259 260 261
and t is the same as `SECONDARY'.

TERMINAL should be a terminal object or a frame specifying the X
server to query.  If omitted or nil, that stands for the selected
262 263 264
frame's display, or the first available X display.

On Nextstep, TERMINAL is unused."
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281
    (if x-select-enable-clipboard
      (let (text)
	;; Don't die if w16-get-clipboard-data signals an error.
	(ignore-errors
	  (setq text (w16-get-clipboard-data)))
	;; We consider ourselves the owner of the selection if it does
	;; not exist, or exists and compares equal with the last text
	;; we've put into the Windows clipboard.
	(cond
	 ((not text) t)
	 ((or (eq text x-last-selected-text)
	      (string= text x-last-selected-text))
	  text)
	 (t nil)))))

;; x-own-selection-internal and x-disown-selection-internal are used
;; in select.el:x-set-selection.
282 283 284
(defun x-own-selection-internal (selection value &optional frame)
  "Assert an X selection of the type SELECTION with and value VALUE.
SELECTION is a symbol, typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
285 286
\(Those are literal upper-case symbol names, since that's what X expects.)
VALUE is typically a string, or a cons of two markers, but may be
287 288 289
anything that the functions on `selection-converter-alist' know about.

FRAME should be a frame that should own the selection.  If omitted or
290 291 292
nil, it defaults to the selected frame.

On Nextstep, FRAME is unused."
293 294 295 296
  (ignore-errors
    (x-select-text value))
  value)

297
(defun x-disown-selection-internal (selection &optional time-object terminal)
298
  "If we own the selection SELECTION, disown it.
299 300
Disowning it means there is no such selection.

301 302 303
Sets the last-change time for the selection to TIME-OBJECT (by default
the time of the last event).

304 305
TERMINAL should be a terminal object or a frame specifying the X
server to query.  If omitted or nil, that stands for the selected
306 307 308 309
frame's display, or the first available X display.

On Nextstep, the TIME-OBJECT and TERMINAL arguments are unused.
On MS-DOS, all this does is return non-nil if we own the selection."
310 311 312
  (if (x-selection-owner-p selection)
      t))

313
;; x-get-selection-internal is used in select.el
314
(defun x-get-selection-internal (selection-symbol target-type &optional time-stamp terminal)
315
  "Return text selected from some X window.
316
SELECTION-SYMBOL is typically `PRIMARY', `SECONDARY', or `CLIPBOARD'.
317
\(Those are literal upper-case symbol names, since that's what X expects.)
318 319 320
TARGET-TYPE is the type of data desired, typically `STRING'.

TIME-STAMP is the time to use in the XConvertSelection call for foreign
321 322 323 324
selections.  If omitted, defaults to the time for the last event.

TERMINAL should be a terminal object or a frame specifying the X
server to query.  If omitted or nil, that stands for the selected
325 326 327
frame's display, or the first available X display.

On Nextstep, TIME-STAMP and TERMINAL are unused."
328 329
  (x-get-selection-value))

330 331 332
;; From src/fontset.c:
(fset 'query-fontset 'ignore)

333 334 335
;; From lisp/term/x-win.el: make iconify-or-deiconify-frame a no-op.
(fset 'iconify-or-deiconify-frame 'ignore)

Morten Welinder's avatar
Morten Welinder committed
336 337 338 339 340
;; From lisp/frame.el
(fset 'set-default-font 'ignore)
(fset 'set-mouse-color 'ignore)		; We cannot, I think.
(fset 'set-cursor-color 'ignore)	; Hardware determined by char under.
(fset 'set-border-color 'ignore)	; Not useful.
341

342 343 344 345
(defvar msdos-last-help-message nil
  "The last help message received via `show-help-function'.
This is used by `msdos-show-help'.")

346 347 348 349
(defvar msdos-previous-message nil
  "The content of the echo area before help echo was displayed.")

(defun msdos-show-help (help)
350
  "Function installed as `show-help-function' on MS-DOS frames."
351 352 353 354
  (when (and (not (window-minibuffer-p)) ;Don't overwrite minibuffer contents.
             (not cursor-in-echo-area)) ;Don't overwrite a prompt.
    (cond
     ((stringp help)
355 356 357 358 359 360
      (setq help (replace-regexp-in-string "\n" ", " help))
      (unless (or msdos-previous-message
		  (string-equal help (current-message))
		  (and (stringp msdos-last-help-message)
		       (string-equal msdos-last-help-message
				     (current-message))))
361
        (setq msdos-previous-message (current-message)))
362
      (setq msdos-last-help-message help)
363
      (let ((message-truncate-lines nil)
364
            (message-log-max nil))
365
        (message "%s" help)))
366 367 368 369 370 371 372 373
     ((stringp msdos-previous-message)
      (let ((message-log-max nil))
        (message "%s" msdos-previous-message)
        (setq msdos-previous-message nil)))
     (t
      (message nil)))))


374
;; Initialization.
Morten Welinder's avatar
Morten Welinder committed
375
;; ---------------------------------------------------------------------------
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415
;; This function is run, by faces.el:tty-create-frame-with-faces, only
;; for the initial frame (on each terminal, but we have only one).
;; This works by setting the `terminal-initted' terminal parameter to
;; this function, the first time `tty-create-frame-with-faces' is
;; called on that terminal.  `tty-create-frame-with-faces' is called
;; directly from startup.el and also by `make-frame' through
;; `frame-creation-function-alist'.  `make-frame' will call this
;; function if `msdos-create-frame-with-faces' (see below) is not
;; found in `frame-creation-function-alist', which means something is
;; _very_ wrong, because "internal" terminal emulator should not be
;; turned on if our window-system is not `pc'.  Therefore, the only
;; Right Thing for us to do here is scream bloody murder.
(defun terminal-init-internal ()
  "Terminal initialization function for the MS-DOS \"internal\" terminal.
Errors out because it is not supposed to be called, ever."
  (error "terminal-init-internal called for window-system `%s'"
	 (window-system)))

(defun msdos-initialize-window-system ()
  "Initialization function for the `pc' \"window system\"."
  (or (eq (window-system) 'pc)
      (error
       "`msdos-initialize-window-system' called, but window-system is `%s'"
       (window-system)))
  ;; First, the keyboard.
  (msdos-setup-keyboard terminal-frame)	; see internal.el
  ;; Next, register the default colors.
  (let* ((colors msdos-color-values)
	 (color (car colors)))
    (tty-color-clear)
    (while colors
      (tty-color-define (car color) (cadr color) (cddr color))
      (setq colors (cdr colors) color (car colors))))
  ;; Modifying color mappings means realized faces don't
  ;; use the right colors, so clear them.
  (clear-face-cache)
  ;; Now set up some additional faces.
  (msdos-face-setup)
  ;; Set up the initial frame.
  (msdos-setup-initial-frame)
416 417
  ;; Help echo is displayed in the echo area.
  (setq show-help-function 'msdos-show-help)
418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450
  ;; We want to delay the codepage-related setup until after user's
  ;; .emacs is processed, because people might define their
  ;; `dos-codepage-setup-hook' there.
  (add-hook 'after-init-hook 'dos-codepage-setup)
  ;; In multibyte mode, we want unibyte buffers to be displayed
  ;; using the terminal coding system, so that they display
  ;; correctly on the DOS terminal; in unibyte mode we want to see
  ;; all 8-bit characters verbatim.  In both cases, we want the
  ;; entire range of 8-bit characters to arrive at our display code
  ;; verbatim.
  (standard-display-8bit 127 255)
  ;; We are fast enough to make this optimization unnecessary.
  (setq split-window-keep-point t)
  ;; Arrange for the kill and yank functions to set and check the
  ;; clipboard.
  (setq interprogram-cut-function 'x-select-text)
  (setq interprogram-paste-function 'x-get-selection-value)
  (menu-bar-enable-clipboard)
  (run-hooks 'terminal-init-msdos-hook))

;; frame-creation-function-alist is examined by frame.el:make-frame.
(add-to-list 'frame-creation-function-alist
	     '(pc . msdos-create-frame-with-faces))
;; window-system-initialization-alist is examined by startup.el:command-line.
(add-to-list 'window-system-initialization-alist
	     '(pc . msdos-initialize-window-system))
;; We don't need anything beyond tty-handle-args for handling
;; command-line argument; see startup.el.
(add-to-list 'handle-args-function-alist '(pc . tty-handle-args))

;; ---------------------------------------------------------------------------

(provide 'pc-win)
Richard M. Stallman's avatar
Richard M. Stallman committed
451 452

;;; pc-win.el ends here