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 window-system 'win32))
69 (error "%s: Loading win32-win.el but not compiled for win32" (invocation-name)))
78 ;; Disable until scrollbars are fully functional
81 (defvar x-invocation-args)
83 (defvar x-command-line-resources nil)
85 (defconst x-option-alist
86 '(("-bw" . x-handle-numeric-switch)
87 ("-d" . x-handle-display)
88 ("-display" . x-handle-display)
89 ("-name" . x-handle-name-rn-switch)
90 ("-rn" . x-handle-name-rn-switch)
91 ("-T" . x-handle-switch)
92 ("-r" . x-handle-switch)
93 ("-rv" . x-handle-switch)
94 ("-reverse" . x-handle-switch)
95 ("-fn" . x-handle-switch)
96 ("-font" . x-handle-switch)
97 ("-ib" . x-handle-numeric-switch)
98 ("-g" . x-handle-geometry)
99 ("-geometry" . x-handle-geometry)
100 ("-fg" . x-handle-switch)
101 ("-foreground". x-handle-switch)
102 ("-bg" . x-handle-switch)
103 ("-background". x-handle-switch)
104 ("-ms" . x-handle-switch)
105 ("-itype" . x-handle-switch)
106 ("-i" . x-handle-switch)
107 ("-iconic" . x-handle-iconic)
108 ("-xrm" . x-handle-xrm-switch)
109 ("-cr" . x-handle-switch)
110 ("-vb" . x-handle-switch)
111 ("-hb" . x-handle-switch)
112 ("-bd" . x-handle-switch)))
114 (defconst x-long-option-alist
115 '(("--border-width" . "-bw")
119 ("--reverse-video" . "-reverse")
121 ("--internal-border" . "-ib")
122 ("--geometry" . "-geometry")
123 ("--foreground-color" . "-fg")
124 ("--background-color" . "-bg")
125 ("--mouse-color" . "-ms")
126 ("--icon-type" . "-itype")
127 ("--iconic" . "-iconic")
129 ("--cursor-color" . "-cr")
130 ("--vertical-scroll-bars" . "-vb")
131 ("--border-color" . "-bd")))
133 (defconst x-switch-definitions
138 ("-reverse" reverse t)
141 ("-ib" internal-border-width)
142 ("-fg" foreground-color)
143 ("-foreground" foreground-color)
144 ("-bg" background-color)
145 ("-background" background-color)
148 ("-itype" icon-type t)
150 ("-vb" vertical-scroll-bars t)
151 ("-hb" horizontal-scroll-bars t)
153 ("-bw" border-width)))
155 ;; Handler for switches of the form "-switch value" or "-switch".
156 (defun x-handle-switch (switch)
157 (let ((aelt (assoc switch x-switch-definitions)))
160 (setq default-frame-alist
161 (cons (cons (nth 1 aelt) (nth 2 aelt))
162 default-frame-alist))
163 (setq default-frame-alist
164 (cons (cons (nth 1 aelt)
165 (car x-invocation-args))
167 x-invocation-args (cdr x-invocation-args))))))
169 ;; Make -iconic apply only to the initial frame!
170 (defun x-handle-iconic (switch)
171 (setq initial-frame-alist
172 (cons '(visibility . icon) initial-frame-alist)))
174 ;; Handler for switches of the form "-switch n"
175 (defun x-handle-numeric-switch (switch)
176 (let ((aelt (assoc switch x-switch-definitions)))
178 (setq default-frame-alist
179 (cons (cons (nth 1 aelt)
180 (string-to-int (car x-invocation-args)))
183 (cdr x-invocation-args)))))
185 ;; Handle the -xrm option.
186 (defun x-handle-xrm-switch (switch)
187 (or (consp x-invocation-args)
188 (error "%s: missing argument to `%s' option" (invocation-name) switch))
189 (setq x-command-line-resources (car x-invocation-args))
190 (setq x-invocation-args (cdr x-invocation-args)))
192 ;; Handle the geometry option
193 (defun x-handle-geometry (switch)
194 (let ((geo (x-parse-geometry (car x-invocation-args))))
195 (setq initial-frame-alist
196 (append initial-frame-alist
197 (if (or (assq 'left geo) (assq 'top geo))
198 '((user-position . t)))
199 (if (or (assq 'height geo) (assq 'width geo))
202 x-invocation-args (cdr x-invocation-args))))
204 ;; Handle the -name and -rn options. Set the variable x-resource-name
205 ;; to the option's operand; if the switch was `-name', set the name of
206 ;; the initial frame, too.
207 (defun x-handle-name-rn-switch (switch)
208 (or (consp x-invocation-args)
209 (error "%s: missing argument to `%s' option" (invocation-name) switch))
210 (setq x-resource-name (car x-invocation-args)
211 x-invocation-args (cdr x-invocation-args))
212 (if (string= switch "-name")
213 (setq initial-frame-alist (cons (cons 'name x-resource-name)
214 initial-frame-alist))))
216 (defvar x-display-name nil
217 "The display name specifying server and frame.")
219 (defun x-handle-display (switch)
220 (setq x-display-name (car x-invocation-args)
221 x-invocation-args (cdr x-invocation-args)))
223 (defvar x-invocation-args nil)
225 (defun x-handle-args (args)
226 "Process the X-related command line options in ARGS.
227 This is done before the user's startup file is loaded. They are copied to
228 x-invocation args from which the X-related things are extracted, first
229 the switch (e.g., \"-fg\") in the following code, and possible values
230 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
231 This returns ARGS with the arguments that have been processed removed."
233 (setq x-invocation-args args
235 (while x-invocation-args
236 (let* ((this-switch (car x-invocation-args))
237 (orig-this-switch this-switch)
238 completion argval aelt)
239 (setq x-invocation-args (cdr x-invocation-args))
240 ;; Check for long options with attached arguments
241 ;; and separate out the attached option argument into argval.
242 (if (string-match "^--[^=]*=" this-switch)
243 (setq argval (substring this-switch (match-end 0))
244 this-switch (substring this-switch 0 (1- (match-end 0)))))
245 (setq completion (try-completion this-switch x-long-option-alist))
246 (if (eq completion t)
247 ;; Exact match for long option.
248 (setq this-switch (cdr (assoc this-switch x-long-option-alist)))
249 (if (stringp completion)
250 (let ((elt (assoc completion x-long-option-alist)))
251 ;; Check for abbreviated long option.
253 (error "Option `%s' is ambiguous" this-switch))
254 (setq this-switch (cdr elt)))
255 ;; Check for a short option.
256 (setq argval nil this-switch orig-this-switch)))
257 (setq aelt (assoc this-switch x-option-alist))
260 (let ((x-invocation-args
261 (cons argval x-invocation-args)))
262 (funcall (cdr aelt) this-switch))
263 (funcall (cdr aelt) this-switch))
264 (setq args (cons this-switch args)))))
265 (setq args (nreverse args)))
273 (defvar x-colors '("aquamarine"
331 "medium spring green"
411 "The full list of X colors from the `rgb.text' file.")
413 (defun x-defined-colors (&optional frame)
414 "Return a list of colors supported for a particular frame.
415 The argument FRAME specifies which frame to try.
416 The value may be different for frames on different X displays."
417 (or frame (setq frame (selected-frame)))
418 (let ((all-colors x-colors)
420 (defined-colors nil))
422 (setq this-color (car all-colors)
423 all-colors (cdr all-colors))
424 (and (face-color-supported-p frame this-color t)
425 (setq defined-colors (cons this-color defined-colors))))
430 (defun iconify-or-deiconify-frame ()
431 "Iconify the selected frame, or deiconify if it's currently an icon."
433 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
435 (make-frame-visible)))
437 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
440 ;; Map certain keypad keys into ASCII characters
441 ;; that people usually expect.
442 (define-key function-key-map [backspace] [127])
443 (define-key function-key-map [delete] [127])
444 (define-key function-key-map [tab] [?\t])
445 (define-key function-key-map [linefeed] [?\n])
446 (define-key function-key-map [clear] [11])
447 (define-key function-key-map [return] [13])
448 (define-key function-key-map [escape] [?\e])
449 (define-key function-key-map [M-backspace] [?\M-\d])
450 (define-key function-key-map [M-delete] [?\M-\d])
451 (define-key function-key-map [M-tab] [?\M-\t])
452 (define-key function-key-map [M-linefeed] [?\M-\n])
453 (define-key function-key-map [M-clear] [?\M-\013])
454 (define-key function-key-map [M-return] [?\M-\015])
455 (define-key function-key-map [M-escape] [?\M-\e])
457 ;; These tell read-char how to convert
458 ;; these special chars to ASCII.
459 (put 'backspace 'ascii-character 127)
460 (put 'delete 'ascii-character 127)
461 (put 'tab 'ascii-character ?\t)
462 (put 'linefeed 'ascii-character ?\n)
463 (put 'clear 'ascii-character 12)
464 (put 'return 'ascii-character 13)
465 (put 'escape 'ascii-character ?\e)
468 ;;;; Selections and cut buffers
470 ;;; We keep track of the last text selected here, so we can check the
471 ;;; current selection against it, and avoid passing back our own text
472 ;;; from x-cut-buffer-or-selection-value.
473 (defvar x-last-selected-text nil)
475 ;;; It is said that overlarge strings are slow to put into the cut buffer.
476 ;;; Note this value is overridden below.
477 (defvar x-cut-buffer-max 20000
478 "Max number of characters to put in the cut buffer.")
480 (defvar x-select-enable-clipboard t
481 "Non-nil means cutting and pasting uses the clipboard.
482 This is in addition to the primary selection.")
484 (defun x-select-text (text &optional push)
485 (if x-select-enable-clipboard
486 (win32-set-clipboard-data text)))
488 ;;; Return the value of the current selection.
489 ;;; Consult the selection, then the cut buffer. Treat empty strings
490 ;;; as if they were unset.
491 (defun x-get-selection-value ()
492 (if x-select-enable-clipboard
494 ;; Don't die if x-get-selection signals an error.
496 (setq text (win32-get-clipboard-data))
497 (error (message "win32-get-clipboard-data:%s" c)))
498 (if (string= text "") (setq text nil))
501 ;;; Do the actual Windows setup here; the above code just defines
502 ;;; functions and variables that we use now.
504 (setq command-line-args (x-handle-args command-line-args))
506 ;;; Make sure we have a valid resource name.
507 (or (stringp x-resource-name)
509 (setq x-resource-name (invocation-name))
511 ;; Change any . or * characters in x-resource-name to hyphens,
512 ;; so as not to choke when we use it in X resource queries.
513 (while (setq i (string-match "[.*]" x-resource-name))
514 (aset x-resource-name i ?-))))
516 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
517 ;; the same lisp directory, don't pass the third argument unless we seem
518 ;; to have the multi-display support.
519 (if (fboundp 'x-close-connection)
520 (x-open-connection ""
521 x-command-line-resources
522 ;; Exit Emacs with fatal error if this fails.
524 (x-open-connection ""
525 x-command-line-resources))
527 (setq frame-creation-function 'x-create-frame-with-faces)
529 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
532 ;; Win32 expects the menu bar cut and paste commands to use the clipboard.
533 ;; This has ,? to match both on Sunos and on Solaris.
534 (menu-bar-enable-clipboard)
536 ;; Apply a geometry resource to the initial frame. Put it at the end
537 ;; of the alist, so that anything specified on the command line takes
539 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
543 (setq parsed (x-parse-geometry res-geometry))
544 ;; If the resource specifies a position,
545 ;; call the position and size "user-specified".
546 (if (or (assq 'top parsed) (assq 'left parsed))
547 (setq parsed (cons '(user-position . t)
548 (cons '(user-size . t) parsed))))
549 ;; All geometry parms apply to the initial frame.
550 (setq initial-frame-alist (append initial-frame-alist parsed))
551 ;; The size parms apply to all frames.
552 (if (assq 'height parsed)
553 (setq default-frame-alist
554 (cons (cons 'height (cdr (assq 'height parsed)))
555 default-frame-alist)))
556 (if (assq 'width parsed)
557 (setq default-frame-alist
558 (cons (cons 'width (cdr (assq 'width parsed)))
559 default-frame-alist))))))
561 ;; Check the reverseVideo resource.
562 (let ((case-fold-search t))
563 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
565 (string-match "^\\(true\\|yes\\|on\\)$" rv))
566 (setq default-frame-alist
567 (cons '(reverse . t) default-frame-alist)))))
569 ;; Set x-selection-timeout, measured in milliseconds.
570 (let ((res-selection-timeout
571 (x-get-resource "selectionTimeout" "SelectionTimeout")))
572 (setq x-selection-timeout 20000)
573 (if res-selection-timeout
574 (setq x-selection-timeout (string-to-number res-selection-timeout))))
576 (defun x-win-suspend-error ()
577 (error "Suspending an emacs running under Win32 makes no sense"))
578 (add-hook 'suspend-hook 'x-win-suspend-error)
580 ;;; Arrange for the kill and yank functions to set and check the clipboard.
581 (setq interprogram-cut-function 'x-select-text)
582 (setq interprogram-paste-function 'x-get-selection-value)
584 ;;; Turn off window-splitting optimization; win32 is usually fast enough
585 ;;; that this is only annoying.
586 (setq split-window-keep-point t)
588 ;; Don't show the frame name; that's redundant.
589 (setq-default mode-line-buffer-identification '("Emacs: %12b"))
591 ;;; Set to a system sound if you want a fancy bell.
592 (set-message-beep 'ok)
594 ;; Remap some functions to call win32 common dialogs
596 (defun internal-face-interactive (what &optional bool)
597 (let* ((fn (intern (concat "face-" what)))
598 (prompt (concat "Set " what " of face"))
599 (face (read-face-name (concat prompt ": ")))
600 (default (if (fboundp fn)
601 (or (funcall fn face (selected-frame))
602 (funcall fn 'default (selected-frame)))))
603 (fn-win (intern (concat (symbol-name window-system) "-select-" what)))
608 (y-or-n-p (concat "Should face " (symbol-name face)
610 (read-string (concat prompt " " (symbol-name face) " to: ")
612 (list face (if (equal value "") nil value))))
614 ;; Redefine the font selection to use the Win32 dialog
616 (defun mouse-set-font (&rest fonts)
618 (set-default-font (win32-select-font)))
620 ;;; win32-win.el ends here