1 ;;; win32-win.el --- parse switches controlling interface with win32
2 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
7 ;;; This file is part of GNU Emacs.
9 ;;; GNU Emacs is free software; you can redistribute it and/or modify
10 ;;; it under the terms of the GNU General Public License as published by
11 ;;; the Free Software Foundation; either version 2, or (at your option)
12 ;;; any later version.
14 ;;; GNU Emacs is distributed in the hope that it will be useful,
15 ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
16 ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 ;;; GNU General Public License for more details.
19 ;;; You should have received a copy of the GNU General Public License
20 ;;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
25 ;; win32-win.el: this file is loaded from ../lisp/startup.el when it recognizes
26 ;; that win32 windows are to be used. Command line switches are parsed and those
27 ;; pertaining to win32 are processed and removed from the command line. The
28 ;; win32 display is opened and hooks are set for popping up the initial window.
30 ;; startup.el will then examine startup files, and eventually call the hooks
31 ;; which create the first window (s).
36 ;; These are the standard X switches from the Xt Initialize.c file of
39 ;; Command line Resource Manager string
42 ;; +synchronous *synchronous
43 ;; -background *background
46 ;; -bordercolor *borderColor
47 ;; -borderwidth .borderWidth
53 ;; -foreground *foreground
54 ;; -geometry .geometry
59 ;; -reverse *reverseVideo
61 ;; -selectionTimeout .selectionTimeout
62 ;; -synchronous *synchronous
65 ;; An alist of X options and the function which handles them. See
68 (if (not (eq (console-type) 'win32))
69 (error "%s: Loading win32-win.el but not compiled for win32" (invocation-name)))
71 (defvar x-invocation-args)
73 (defvar x-command-line-resources nil)
75 (defconst x-option-alist
76 '(("-bw" . x-handle-numeric-switch)
77 ("-d" . x-handle-display)
78 ("-display" . x-handle-display)
79 ("-name" . x-handle-name-rn-switch)
80 ("-rn" . x-handle-name-rn-switch)
81 ("-T" . x-handle-switch)
82 ("-r" . x-handle-switch)
83 ("-rv" . x-handle-switch)
84 ("-reverse" . x-handle-switch)
85 ("-fn" . x-handle-switch)
86 ("-font" . x-handle-switch)
87 ("-ib" . x-handle-numeric-switch)
88 ("-g" . x-handle-geometry)
89 ("-geometry" . x-handle-geometry)
90 ("-fg" . x-handle-switch)
91 ("-foreground". x-handle-switch)
92 ("-bg" . x-handle-switch)
93 ("-background". x-handle-switch)
94 ("-ms" . x-handle-switch)
95 ("-itype" . x-handle-switch)
96 ("-i" . x-handle-switch)
97 ("-iconic" . x-handle-iconic)
98 ("-xrm" . x-handle-xrm-switch)
99 ("-cr" . x-handle-switch)
100 ("-vb" . x-handle-switch)
101 ("-hb" . x-handle-switch)
102 ("-bd" . x-handle-switch)))
104 (defconst x-long-option-alist
105 '(("--border-width" . "-bw")
109 ("--reverse-video" . "-reverse")
111 ("--internal-border" . "-ib")
112 ("--geometry" . "-geometry")
113 ("--foreground-color" . "-fg")
114 ("--background-color" . "-bg")
115 ("--mouse-color" . "-ms")
116 ("--icon-type" . "-itype")
117 ("--iconic" . "-iconic")
119 ("--cursor-color" . "-cr")
120 ("--vertical-scroll-bars" . "-vb")
121 ("--border-color" . "-bd")))
123 (defconst x-switch-definitions
128 ("-reverse" reverse t)
131 ("-ib" internal-border-width)
132 ("-fg" foreground-color)
133 ("-foreground" foreground-color)
134 ("-bg" background-color)
135 ("-background" background-color)
138 ("-itype" icon-type t)
140 ("-vb" vertical-scroll-bars t)
141 ("-hb" horizontal-scroll-bars t)
143 ("-bw" border-width)))
145 ;; Handler for switches of the form "-switch value" or "-switch".
146 (defun x-handle-switch (switch)
147 (let ((aelt (assoc switch x-switch-definitions)))
150 (setq default-frame-alist
151 (cons (cons (nth 1 aelt) (nth 2 aelt))
152 default-frame-alist))
153 (setq default-frame-alist
154 (cons (cons (nth 1 aelt)
155 (car x-invocation-args))
157 x-invocation-args (cdr x-invocation-args))))))
159 ;; Make -iconic apply only to the initial frame!
160 (defun x-handle-iconic (switch)
161 (setq initial-frame-alist
162 (cons '(visibility . icon) initial-frame-alist)))
164 ;; Handler for switches of the form "-switch n"
165 (defun x-handle-numeric-switch (switch)
166 (let ((aelt (assoc switch x-switch-definitions)))
168 (setq default-frame-alist
169 (cons (cons (nth 1 aelt)
170 (string-to-int (car x-invocation-args)))
173 (cdr x-invocation-args)))))
175 ;; Handle the -xrm option.
176 (defun x-handle-xrm-switch (switch)
177 (or (consp x-invocation-args)
178 (error "%s: missing argument to `%s' option" (invocation-name) switch))
179 (setq x-command-line-resources (car x-invocation-args))
180 (setq x-invocation-args (cdr x-invocation-args)))
182 ;; Handle the geometry option
183 (defun x-handle-geometry (switch)
184 (let ((geo (x-parse-geometry (car x-invocation-args))))
185 (setq initial-frame-alist
186 (append initial-frame-alist
187 (if (or (assq 'left geo) (assq 'top geo))
188 '((user-position . t)))
189 (if (or (assq 'height geo) (assq 'width geo))
192 x-invocation-args (cdr x-invocation-args))))
194 ;; Handle the -name and -rn options. Set the variable x-resource-name
195 ;; to the option's operand; if the switch was `-name', set the name of
196 ;; the initial frame, too.
197 (defun x-handle-name-rn-switch (switch)
198 (or (consp x-invocation-args)
199 (error "%s: missing argument to `%s' option" (invocation-name) switch))
200 (setq x-resource-name (car x-invocation-args)
201 x-invocation-args (cdr x-invocation-args))
202 (if (string= switch "-name")
203 (setq initial-frame-alist (cons (cons 'name x-resource-name)
204 initial-frame-alist))))
206 (defvar x-display-name nil
207 "The display name specifying server and frame.")
209 (defun x-handle-display (switch)
210 (setq x-display-name (car x-invocation-args)
211 x-invocation-args (cdr x-invocation-args)))
213 (defvar x-invocation-args nil)
215 (defun x-handle-args (args)
216 "Process the X-related command line options in ARGS.
217 This is done before the user's startup file is loaded. They are copied to
218 x-invocation args from which the X-related things are extracted, first
219 the switch (e.g., \"-fg\") in the following code, and possible values
220 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
221 This returns ARGS with the arguments that have been processed removed."
223 (setq x-invocation-args args
225 (while x-invocation-args
226 (let* ((this-switch (car x-invocation-args))
227 (orig-this-switch this-switch)
228 completion argval aelt)
229 (setq x-invocation-args (cdr x-invocation-args))
230 ;; Check for long options with attached arguments
231 ;; and separate out the attached option argument into argval.
232 (if (string-match "^--[^=]*=" this-switch)
233 (setq argval (substring this-switch (match-end 0))
234 this-switch (substring this-switch 0 (1- (match-end 0)))))
235 (setq completion (try-completion this-switch x-long-option-alist))
236 (if (eq completion t)
237 ;; Exact match for long option.
238 (setq this-switch (cdr (assoc this-switch x-long-option-alist)))
239 (if (stringp completion)
240 (let ((elt (assoc completion x-long-option-alist)))
241 ;; Check for abbreviated long option.
243 (error "Option `%s' is ambiguous" this-switch))
244 (setq this-switch (cdr elt)))
245 ;; Check for a short option.
246 (setq argval nil this-switch orig-this-switch)))
247 (setq aelt (assoc this-switch x-option-alist))
250 (let ((x-invocation-args
251 (cons argval x-invocation-args)))
252 (funcall (cdr aelt) this-switch))
253 (funcall (cdr aelt) this-switch))
254 (setq args (cons this-switch args)))))
255 (setq args (nreverse args)))
263 (defvar x-colors '("aquamarine"
321 "medium spring green"
401 "The full list of X colors from the `rgb.text' file.")
403 (defun x-defined-colors (&optional frame)
404 "Return a list of colors supported for a particular frame.
405 The argument FRAME specifies which frame to try.
406 The value may be different for frames on different X displays."
407 (or frame (setq frame (selected-frame)))
408 (let ((all-colors x-colors)
410 (defined-colors nil))
412 (setq this-color (car all-colors)
413 all-colors (cdr all-colors))
414 (and (face-color-supported-p frame this-color t)
415 (setq defined-colors (cons this-color defined-colors))))
420 (defun iconify-or-deiconify-frame ()
421 "Iconify the selected frame, or deiconify if it's currently an icon."
423 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
425 (make-frame-visible)))
427 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
430 ;; Map certain keypad keys into ASCII characters
431 ;; that people usually expect.
432 (define-key function-key-map [backspace] [127])
433 (define-key function-key-map [delete] [127])
434 (define-key function-key-map [tab] [?\t])
435 (define-key function-key-map [linefeed] [?\n])
436 (define-key function-key-map [clear] [11])
437 (define-key function-key-map [return] [13])
438 (define-key function-key-map [escape] [?\e])
439 (define-key function-key-map [M-backspace] [?\M-\d])
440 (define-key function-key-map [M-delete] [?\M-\d])
441 (define-key function-key-map [M-tab] [?\M-\t])
442 (define-key function-key-map [M-linefeed] [?\M-\n])
443 (define-key function-key-map [M-clear] [?\M-\013])
444 (define-key function-key-map [M-return] [?\M-\015])
445 (define-key function-key-map [M-escape] [?\M-\e])
447 ;; These tell read-char how to convert
448 ;; these special chars to ASCII.
449 (put 'backspace 'ascii-character 127)
450 (put 'delete 'ascii-character 127)
451 (put 'tab 'ascii-character ?\t)
452 (put 'linefeed 'ascii-character ?\n)
453 (put 'clear 'ascii-character 12)
454 (put 'return 'ascii-character 13)
455 (put 'escape 'ascii-character ?\e)
458 ;;;; Selections and cut buffers
460 ;;; We keep track of the last text selected here, so we can check the
461 ;;; current selection against it, and avoid passing back our own text
462 ;;; from x-cut-buffer-or-selection-value.
463 (defvar x-last-selected-text nil)
465 ;;; It is said that overlarge strings are slow to put into the cut buffer.
466 ;;; Note this value is overridden below.
467 (defvar x-cut-buffer-max 20000
468 "Max number of characters to put in the cut buffer.")
470 (defvar x-select-enable-clipboard t
471 "Non-nil means cutting and pasting uses the clipboard.
472 This is in addition to the primary selection.")
474 (defun x-select-text (text &optional push)
475 (if x-select-enable-clipboard
476 (win32-set-clipboard-data text)))
478 ;;; Return the value of the current selection.
479 ;;; Consult the selection, then the cut buffer. Treat empty strings
480 ;;; as if they were unset.
481 (defun x-get-selection-value ()
482 (if x-select-enable-clipboard
484 ;; Don't die if x-get-selection signals an error.
486 (setq text (win32-get-clipboard-data))
487 (error (message "win32-get-clipboard-data:%s" c)))
488 (if (string= text "") (setq text nil))
491 ;;; Do the actual Windows setup here; the above code just defines
492 ;;; functions and variables that we use now.
494 (setq command-line-args (x-handle-args command-line-args))
496 ;;; Make sure we have a valid resource name.
497 (or (stringp x-resource-name)
499 (setq x-resource-name (invocation-name))
501 ;; Change any . or * characters in x-resource-name to hyphens,
502 ;; so as not to choke when we use it in X resource queries.
503 (while (setq i (string-match "[.*]" x-resource-name))
504 (aset x-resource-name i ?-))))
506 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
507 ;; the same lisp directory, don't pass the third argument unless we seem
508 ;; to have the multi-display support.
509 (if (fboundp 'x-close-connection)
510 (x-open-connection ""
511 x-command-line-resources
512 ;; Exit Emacs with fatal error if this fails.
514 (x-open-connection ""
515 x-command-line-resources))
517 (setq frame-creation-function 'x-create-frame-with-faces)
519 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
522 ;; Win32 expects the menu bar cut and paste commands to use the clipboard.
523 ;; This has ,? to match both on Sunos and on Solaris.
524 (menu-bar-enable-clipboard)
526 ;; Apply a geometry resource to the initial frame. Put it at the end
527 ;; of the alist, so that anything specified on the command line takes
529 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
533 (setq parsed (x-parse-geometry res-geometry))
534 ;; If the resource specifies a position,
535 ;; call the position and size "user-specified".
536 (if (or (assq 'top parsed) (assq 'left parsed))
537 (setq parsed (cons '(user-position . t)
538 (cons '(user-size . t) parsed))))
539 ;; All geometry parms apply to the initial frame.
540 (setq initial-frame-alist (append initial-frame-alist parsed))
541 ;; The size parms apply to all frames.
542 (if (assq 'height parsed)
543 (setq default-frame-alist
544 (cons (cons 'height (cdr (assq 'height parsed)))
545 default-frame-alist)))
546 (if (assq 'width parsed)
547 (setq default-frame-alist
548 (cons (cons 'width (cdr (assq 'width parsed)))
549 default-frame-alist))))))
551 ;; Check the reverseVideo resource.
552 (let ((case-fold-search t))
553 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
555 (string-match "^\\(true\\|yes\\|on\\)$" rv))
556 (setq default-frame-alist
557 (cons '(reverse . t) default-frame-alist)))))
559 ;; Set x-selection-timeout, measured in milliseconds.
560 (let ((res-selection-timeout
561 (x-get-resource "selectionTimeout" "SelectionTimeout")))
562 (setq x-selection-timeout 20000)
563 (if res-selection-timeout
564 (setq x-selection-timeout (string-to-number res-selection-timeout))))
566 (defun x-win-suspend-error ()
567 (error "Suspending an emacs running under Win32 makes no sense"))
568 (add-hook 'suspend-hook 'x-win-suspend-error)
570 ;;; Arrange for the kill and yank functions to set and check the clipboard.
571 (setq interprogram-cut-function 'x-select-text)
572 (setq interprogram-paste-function 'x-get-selection-value)
574 ;;; Turn off window-splitting optimization; win32 is usually fast enough
575 ;;; that this is only annoying.
576 (setq split-window-keep-point t)
578 ;; Don't show the frame name; that's redundant.
579 (setq-default mode-line-buffer-identification '("Emacs: %12b"))
581 ;;; Set to a system sound if you want a fancy bell.
582 (set-message-beep 'ok)
584 ;; Remap some functions to call win32 common dialogs
586 (defun internal-face-interactive (what &optional bool)
587 (let* ((fn (intern (concat "face-" what)))
588 (prompt (concat "Set " what " of face"))
589 (face (read-face-name (concat prompt ": ")))
590 (default (if (fboundp fn)
591 (or (funcall fn face (selected-frame))
592 (funcall fn 'default (selected-frame)))))
593 (fn-win (intern (concat (symbol-name window-system) "-select-" what)))
598 (y-or-n-p (concat "Should face " (symbol-name face)
600 (read-string (concat prompt " " (symbol-name face) " to: ")
602 (list face (if (equal value "") nil value))))
604 ;; Redefine the font selection to use the Win32 dialog
606 (defun mouse-set-font (&rest fonts)
608 (set-default-font (win32-select-font)))
610 ;;; win32-win.el ends here