1 ;;; win32-win.el --- parse switches controlling interface with win32
3 ;; Copyright (C) 1993, 1994 Free Software Foundation, Inc.
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
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.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; win32-win.el: this file is loaded from ../lisp/startup.el when it recognizes
28 ;; that win32 windows are to be used. Command line switches are parsed and those
29 ;; pertaining to win32 are processed and removed from the command line. The
30 ;; win32 display is opened and hooks are set for popping up the initial window.
32 ;; startup.el will then examine startup files, and eventually call the hooks
33 ;; which create the first window (s).
38 ;; These are the standard X switches from the Xt Initialize.c file of
41 ;; Command line Resource Manager string
44 ;; +synchronous *synchronous
45 ;; -background *background
48 ;; -bordercolor *borderColor
49 ;; -borderwidth .borderWidth
55 ;; -foreground *foreground
56 ;; -geometry .geometry
61 ;; -reverse *reverseVideo
63 ;; -selectionTimeout .selectionTimeout
64 ;; -synchronous *synchronous
67 ;; An alist of X options and the function which handles them. See
70 (if (not (eq window-system
'win32
))
71 (error "%s: Loading win32-win.el but not compiled for win32" (invocation-name)))
80 ;; Disable until scrollbars are fully functional
83 (defvar x-invocation-args
)
85 (defvar x-command-line-resources nil
)
87 (defconst x-option-alist
88 '(("-bw" . x-handle-numeric-switch
)
89 ("-d" . x-handle-display
)
90 ("-display" . x-handle-display
)
91 ("-name" . x-handle-name-rn-switch
)
92 ("-rn" . x-handle-name-rn-switch
)
93 ("-T" . x-handle-switch
)
94 ("-r" . x-handle-switch
)
95 ("-rv" . x-handle-switch
)
96 ("-reverse" . x-handle-switch
)
97 ("-fn" . x-handle-switch
)
98 ("-font" . x-handle-switch
)
99 ("-ib" . x-handle-numeric-switch
)
100 ("-g" . x-handle-geometry
)
101 ("-geometry" . x-handle-geometry
)
102 ("-fg" . x-handle-switch
)
103 ("-foreground". x-handle-switch
)
104 ("-bg" . x-handle-switch
)
105 ("-background". x-handle-switch
)
106 ("-ms" . x-handle-switch
)
107 ("-itype" . x-handle-switch
)
108 ("-i" . x-handle-switch
)
109 ("-iconic" . x-handle-iconic
)
110 ("-xrm" . x-handle-xrm-switch
)
111 ("-cr" . x-handle-switch
)
112 ("-vb" . x-handle-switch
)
113 ("-hb" . x-handle-switch
)
114 ("-bd" . x-handle-switch
)))
116 (defconst x-long-option-alist
117 '(("--border-width" .
"-bw")
121 ("--reverse-video" .
"-reverse")
123 ("--internal-border" .
"-ib")
124 ("--geometry" .
"-geometry")
125 ("--foreground-color" .
"-fg")
126 ("--background-color" .
"-bg")
127 ("--mouse-color" .
"-ms")
128 ("--icon-type" .
"-itype")
129 ("--iconic" .
"-iconic")
131 ("--cursor-color" .
"-cr")
132 ("--vertical-scroll-bars" .
"-vb")
133 ("--border-color" .
"-bd")))
135 (defconst x-switch-definitions
140 ("-reverse" reverse t
)
143 ("-ib" internal-border-width
)
144 ("-fg" foreground-color
)
145 ("-foreground" foreground-color
)
146 ("-bg" background-color
)
147 ("-background" background-color
)
150 ("-itype" icon-type t
)
152 ("-vb" vertical-scroll-bars t
)
153 ("-hb" horizontal-scroll-bars t
)
155 ("-bw" border-width
)))
157 ;; Handler for switches of the form "-switch value" or "-switch".
158 (defun x-handle-switch (switch)
159 (let ((aelt (assoc switch x-switch-definitions
)))
162 (setq default-frame-alist
163 (cons (cons (nth 1 aelt
) (nth 2 aelt
))
164 default-frame-alist
))
165 (setq default-frame-alist
166 (cons (cons (nth 1 aelt
)
167 (car x-invocation-args
))
169 x-invocation-args
(cdr x-invocation-args
))))))
171 ;; Make -iconic apply only to the initial frame!
172 (defun x-handle-iconic (switch)
173 (setq initial-frame-alist
174 (cons '(visibility . icon
) initial-frame-alist
)))
176 ;; Handler for switches of the form "-switch n"
177 (defun x-handle-numeric-switch (switch)
178 (let ((aelt (assoc switch x-switch-definitions
)))
180 (setq default-frame-alist
181 (cons (cons (nth 1 aelt
)
182 (string-to-int (car x-invocation-args
)))
185 (cdr x-invocation-args
)))))
187 ;; Handle the -xrm option.
188 (defun x-handle-xrm-switch (switch)
189 (or (consp x-invocation-args
)
190 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
191 (setq x-command-line-resources
(car x-invocation-args
))
192 (setq x-invocation-args
(cdr x-invocation-args
)))
194 ;; Handle the geometry option
195 (defun x-handle-geometry (switch)
196 (let ((geo (x-parse-geometry (car x-invocation-args
))))
197 (setq initial-frame-alist
198 (append initial-frame-alist
199 (if (or (assq 'left geo
) (assq 'top geo
))
200 '((user-position . t
)))
201 (if (or (assq 'height geo
) (assq 'width geo
))
204 x-invocation-args
(cdr x-invocation-args
))))
206 ;; Handle the -name and -rn options. Set the variable x-resource-name
207 ;; to the option's operand; if the switch was `-name', set the name of
208 ;; the initial frame, too.
209 (defun x-handle-name-rn-switch (switch)
210 (or (consp x-invocation-args
)
211 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
212 (setq x-resource-name
(car x-invocation-args
)
213 x-invocation-args
(cdr x-invocation-args
))
214 (if (string= switch
"-name")
215 (setq initial-frame-alist
(cons (cons 'name x-resource-name
)
216 initial-frame-alist
))))
218 (defvar x-display-name nil
219 "The display name specifying server and frame.")
221 (defun x-handle-display (switch)
222 (setq x-display-name
(car x-invocation-args
)
223 x-invocation-args
(cdr x-invocation-args
)))
225 (defvar x-invocation-args nil
)
227 (defun x-handle-args (args)
228 "Process the X-related command line options in ARGS.
229 This is done before the user's startup file is loaded. They are copied to
230 x-invocation args from which the X-related things are extracted, first
231 the switch (e.g., \"-fg\") in the following code, and possible values
232 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
233 This returns ARGS with the arguments that have been processed removed."
235 (setq x-invocation-args args
237 (while x-invocation-args
238 (let* ((this-switch (car x-invocation-args
))
239 (orig-this-switch this-switch
)
240 completion argval aelt
)
241 (setq x-invocation-args
(cdr x-invocation-args
))
242 ;; Check for long options with attached arguments
243 ;; and separate out the attached option argument into argval.
244 (if (string-match "^--[^=]*=" this-switch
)
245 (setq argval
(substring this-switch
(match-end 0))
246 this-switch
(substring this-switch
0 (1- (match-end 0)))))
247 (setq completion
(try-completion this-switch x-long-option-alist
))
248 (if (eq completion t
)
249 ;; Exact match for long option.
250 (setq this-switch
(cdr (assoc this-switch x-long-option-alist
)))
251 (if (stringp completion
)
252 (let ((elt (assoc completion x-long-option-alist
)))
253 ;; Check for abbreviated long option.
255 (error "Option `%s' is ambiguous" this-switch
))
256 (setq this-switch
(cdr elt
)))
257 ;; Check for a short option.
258 (setq argval nil this-switch orig-this-switch
)))
259 (setq aelt
(assoc this-switch x-option-alist
))
262 (let ((x-invocation-args
263 (cons argval x-invocation-args
)))
264 (funcall (cdr aelt
) this-switch
))
265 (funcall (cdr aelt
) this-switch
))
266 (setq args
(cons this-switch args
)))))
267 (setq args
(nreverse args
)))
275 (defvar x-colors
'("aquamarine"
333 "medium spring green"
413 "The full list of X colors from the `rgb.text' file.")
415 (defun x-defined-colors (&optional frame
)
416 "Return a list of colors supported for a particular frame.
417 The argument FRAME specifies which frame to try.
418 The value may be different for frames on different X displays."
419 (or frame
(setq frame
(selected-frame)))
420 (let ((all-colors x-colors
)
422 (defined-colors nil
))
424 (setq this-color
(car all-colors
)
425 all-colors
(cdr all-colors
))
426 (and (face-color-supported-p frame this-color t
)
427 (setq defined-colors
(cons this-color defined-colors
))))
432 (defun iconify-or-deiconify-frame ()
433 "Iconify the selected frame, or deiconify if it's currently an icon."
435 (if (eq (cdr (assq 'visibility
(frame-parameters))) t
)
437 (make-frame-visible)))
439 (substitute-key-definition 'suspend-emacs
'iconify-or-deiconify-frame
442 ;; Map certain keypad keys into ASCII characters
443 ;; that people usually expect.
444 (define-key function-key-map
[backspace] [127])
445 (define-key function-key-map [delete] [127])
446 (define-key function-key-map [tab] [?\t])
447 (define-key function-key-map [linefeed] [?\n])
448 (define-key function-key-map [clear] [11])
449 (define-key function-key-map [return] [13])
450 (define-key function-key-map [escape] [?\e])
451 (define-key function-key-map [M-backspace] [?\M-\d])
452 (define-key function-key-map [M-delete] [?\M-\d])
453 (define-key function-key-map [M-tab] [?\M-\t])
454 (define-key function-key-map [M-linefeed] [?\M-\n])
455 (define-key function-key-map [M-clear] [?\M-\013])
456 (define-key function-key-map [M-return] [?\M-\015])
457 (define-key function-key-map [M-escape] [?\M-\e])
459 ;; These tell read-char how to convert
460 ;; these special chars to ASCII.
461 (put 'backspace 'ascii-character 127)
462 (put 'delete 'ascii-character 127)
463 (put 'tab 'ascii-character ?\t)
464 (put 'linefeed 'ascii-character ?\n)
465 (put 'clear 'ascii-character 12)
466 (put 'return 'ascii-character 13)
467 (put 'escape 'ascii-character ?\e)
470 ;;;; Selections and cut buffers
472 ;;; We keep track of the last text selected here, so we can check the
473 ;;; current selection against it, and avoid passing back our own text
474 ;;; from x-cut-buffer-or-selection-value.
475 (defvar x-last-selected-text nil)
477 ;;; It is said that overlarge strings are slow to put into the cut buffer.
478 ;;; Note this value is overridden below.
479 (defvar x-cut-buffer-max 20000
480 "Max number of characters to put in the cut buffer.")
482 (defvar x-select-enable-clipboard t
483 "Non-nil means cutting and pasting uses the clipboard.
484 This is in addition to the primary selection.")
486 (defun x-select-text (text &optional push)
487 (if x-select-enable-clipboard
488 (win32-set-clipboard-data text)))
490 ;;; Return the value of the current selection.
491 ;;; Consult the selection, then the cut buffer. Treat empty strings
492 ;;; as if they were unset.
493 (defun x-get-selection-value ()
494 (if x-select-enable-clipboard
496 ;; Don't die if x-get-selection signals an error.
498 (setq text (win32-get-clipboard-data))
499 (error (message "win32-get-clipboard-data:%s" c)))
500 (if (string= text "") (setq text nil))
503 ;;; Do the actual Windows setup here; the above code just defines
504 ;;; functions and variables that we use now.
506 (setq command-line-args (x-handle-args command-line-args))
508 ;;; Make sure we have a valid resource name.
509 (or (stringp x-resource-name)
511 (setq x-resource-name (invocation-name))
513 ;; Change any . or * characters in x-resource-name to hyphens,
514 ;; so as not to choke when we use it in X resource queries.
515 (while (setq i (string-match "[.*]" x-resource-name))
516 (aset x-resource-name i ?-))))
518 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
519 ;; the same lisp directory, don't pass the third argument unless we seem
520 ;; to have the multi-display support.
521 (if (fboundp 'x-close-connection)
522 (x-open-connection ""
523 x-command-line-resources
524 ;; Exit Emacs with fatal error if this fails.
526 (x-open-connection ""
527 x-command-line-resources))
529 (setq frame-creation-function 'x-create-frame-with-faces)
531 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
534 ;; Win32 expects the menu bar cut and paste commands to use the clipboard.
535 ;; This has ,? to match both on Sunos and on Solaris.
536 (menu-bar-enable-clipboard)
538 ;; Apply a geometry resource to the initial frame. Put it at the end
539 ;; of the alist, so that anything specified on the command line takes
541 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
545 (setq parsed (x-parse-geometry res-geometry))
546 ;; If the resource specifies a position,
547 ;; call the position and size "user-specified".
548 (if (or (assq 'top parsed) (assq 'left parsed))
549 (setq parsed (cons '(user-position . t)
550 (cons '(user-size . t) parsed))))
551 ;; All geometry parms apply to the initial frame.
552 (setq initial-frame-alist (append initial-frame-alist parsed))
553 ;; The size parms apply to all frames.
554 (if (assq 'height parsed)
555 (setq default-frame-alist
556 (cons (cons 'height (cdr (assq 'height parsed)))
557 default-frame-alist)))
558 (if (assq 'width parsed)
559 (setq default-frame-alist
560 (cons (cons 'width (cdr (assq 'width parsed)))
561 default-frame-alist))))))
563 ;; Check the reverseVideo resource.
564 (let ((case-fold-search t))
565 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
567 (string-match "^\\(true\\|yes\\|on\\)$" rv))
568 (setq default-frame-alist
569 (cons '(reverse . t) default-frame-alist)))))
571 ;; Set x-selection-timeout, measured in milliseconds.
572 (let ((res-selection-timeout
573 (x-get-resource "selectionTimeout" "SelectionTimeout")))
574 (setq x-selection-timeout 20000)
575 (if res-selection-timeout
576 (setq x-selection-timeout (string-to-number res-selection-timeout))))
578 (defun x-win-suspend-error ()
579 (error "Suspending an emacs running under Win32 makes no sense"))
580 (add-hook 'suspend-hook 'x-win-suspend-error)
582 ;;; Arrange for the kill and yank functions to set and check the clipboard.
583 (setq interprogram-cut-function 'x-select-text)
584 (setq interprogram-paste-function 'x-get-selection-value)
586 ;;; Turn off window-splitting optimization; win32 is usually fast enough
587 ;;; that this is only annoying.
588 (setq split-window-keep-point t)
590 ;; Don't show the frame name; that's redundant.
591 (setq-default mode-line-buffer-identification '("Emacs: %12b"))
593 ;;; Set to a system sound if you want a fancy bell.
594 (set-message-beep 'ok)
596 ;; Remap some functions to call win32 common dialogs
598 (defun internal-face-interactive (what &optional bool)
599 (let* ((fn (intern (concat "face-" what)))
600 (prompt (concat "Set " what " of face"))
601 (face (read-face-name (concat prompt ": ")))
602 (default (if (fboundp fn)
603 (or (funcall fn face (selected-frame))
604 (funcall fn 'default (selected-frame)))))
605 (fn-win (intern (concat (symbol-name window-system) "-select-" what)))
610 (y-or-n-p (concat "Should face " (symbol-name face)
612 (read-string (concat prompt " " (symbol-name face) " to: ")
614 (list face (if (equal value "") nil value))))
616 ;; Redefine the font selection to use the Win32 dialog
618 (defun mouse-set-font (&rest fonts)
620 (set-default-font (win32-select-font)))
622 ;;; win32-win.el ends here