pc-win.el 19 KB

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