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 ;; Because Windows scrollbars look and act quite differently compared
81 ;; with the standard X scroll-bars, we don't try to use the normal
82 ;; scroll bar routines.
84 (defun win32-handle-scroll-bar-event (event)
85 "Handle Win32 scroll bar events to do normal Window style scrolling."
87 (let ((old-window (selected-window)))
89 (let* ((position (event-start event
))
90 (window (nth 0 position
))
91 (portion-whole (nth 2 position
))
92 (bar-part (nth 4 position
)))
94 (select-window window
)
98 ((eq bar-part
'above-handle
)
100 ((eq bar-part
'handle
)
101 (scroll-bar-maybe-set-window-start event
))
102 ((eq bar-part
'below-handle
)
107 (select-window old-window
))))
109 ;; The following definition is used for debugging.
110 ;(defun win32-handle-scroll-bar-event (event) (interactive "e") (princ event))
112 (global-set-key [vertical-scroll-bar mouse-1
] 'win32-handle-scroll-bar-event
)
114 ;; (scroll-bar-mode nil)
116 (defvar x-invocation-args
)
118 (defvar x-command-line-resources nil
)
120 (defconst x-option-alist
121 '(("-bw" . x-handle-numeric-switch
)
122 ("-d" . x-handle-display
)
123 ("-display" . x-handle-display
)
124 ("-name" . x-handle-name-rn-switch
)
125 ("-rn" . x-handle-name-rn-switch
)
126 ("-T" . x-handle-switch
)
127 ("-r" . x-handle-switch
)
128 ("-rv" . x-handle-switch
)
129 ("-reverse" . x-handle-switch
)
130 ("-fn" . x-handle-switch
)
131 ("-font" . x-handle-switch
)
132 ("-ib" . x-handle-numeric-switch
)
133 ("-g" . x-handle-geometry
)
134 ("-geometry" . x-handle-geometry
)
135 ("-fg" . x-handle-switch
)
136 ("-foreground". x-handle-switch
)
137 ("-bg" . x-handle-switch
)
138 ("-background". x-handle-switch
)
139 ("-ms" . x-handle-switch
)
140 ("-itype" . x-handle-switch
)
141 ("-i" . x-handle-switch
)
142 ("-iconic" . x-handle-iconic
)
143 ("-xrm" . x-handle-xrm-switch
)
144 ("-cr" . x-handle-switch
)
145 ("-vb" . x-handle-switch
)
146 ("-hb" . x-handle-switch
)
147 ("-bd" . x-handle-switch
)))
149 (defconst x-long-option-alist
150 '(("--border-width" .
"-bw")
154 ("--reverse-video" .
"-reverse")
156 ("--internal-border" .
"-ib")
157 ("--geometry" .
"-geometry")
158 ("--foreground-color" .
"-fg")
159 ("--background-color" .
"-bg")
160 ("--mouse-color" .
"-ms")
161 ("--icon-type" .
"-itype")
162 ("--iconic" .
"-iconic")
164 ("--cursor-color" .
"-cr")
165 ("--vertical-scroll-bars" .
"-vb")
166 ("--border-color" .
"-bd")))
168 (defconst x-switch-definitions
173 ("-reverse" reverse t
)
176 ("-ib" internal-border-width
)
177 ("-fg" foreground-color
)
178 ("-foreground" foreground-color
)
179 ("-bg" background-color
)
180 ("-background" background-color
)
183 ("-itype" icon-type t
)
185 ("-vb" vertical-scroll-bars t
)
186 ("-hb" horizontal-scroll-bars t
)
188 ("-bw" border-width
)))
190 ;; Handler for switches of the form "-switch value" or "-switch".
191 (defun x-handle-switch (switch)
192 (let ((aelt (assoc switch x-switch-definitions
)))
195 (setq default-frame-alist
196 (cons (cons (nth 1 aelt
) (nth 2 aelt
))
197 default-frame-alist
))
198 (setq default-frame-alist
199 (cons (cons (nth 1 aelt
)
200 (car x-invocation-args
))
202 x-invocation-args
(cdr x-invocation-args
))))))
204 ;; Make -iconic apply only to the initial frame!
205 (defun x-handle-iconic (switch)
206 (setq initial-frame-alist
207 (cons '(visibility . icon
) initial-frame-alist
)))
209 ;; Handler for switches of the form "-switch n"
210 (defun x-handle-numeric-switch (switch)
211 (let ((aelt (assoc switch x-switch-definitions
)))
213 (setq default-frame-alist
214 (cons (cons (nth 1 aelt
)
215 (string-to-int (car x-invocation-args
)))
218 (cdr x-invocation-args
)))))
220 ;; Handle the -xrm option.
221 (defun x-handle-xrm-switch (switch)
222 (or (consp x-invocation-args
)
223 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
224 (setq x-command-line-resources
(car x-invocation-args
))
225 (setq x-invocation-args
(cdr x-invocation-args
)))
227 ;; Handle the geometry option
228 (defun x-handle-geometry (switch)
229 (let ((geo (x-parse-geometry (car x-invocation-args
))))
230 (setq initial-frame-alist
231 (append initial-frame-alist
232 (if (or (assq 'left geo
) (assq 'top geo
))
233 '((user-position . t
)))
234 (if (or (assq 'height geo
) (assq 'width geo
))
237 x-invocation-args
(cdr x-invocation-args
))))
239 ;; Handle the -name and -rn options. Set the variable x-resource-name
240 ;; to the option's operand; if the switch was `-name', set the name of
241 ;; the initial frame, too.
242 (defun x-handle-name-rn-switch (switch)
243 (or (consp x-invocation-args
)
244 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
245 (setq x-resource-name
(car x-invocation-args
)
246 x-invocation-args
(cdr x-invocation-args
))
247 (if (string= switch
"-name")
248 (setq initial-frame-alist
(cons (cons 'name x-resource-name
)
249 initial-frame-alist
))))
251 (defvar x-display-name nil
252 "The display name specifying server and frame.")
254 (defun x-handle-display (switch)
255 (setq x-display-name
(car x-invocation-args
)
256 x-invocation-args
(cdr x-invocation-args
)))
258 (defvar x-invocation-args nil
)
260 (defun x-handle-args (args)
261 "Process the X-related command line options in ARGS.
262 This is done before the user's startup file is loaded. They are copied to
263 x-invocation args from which the X-related things are extracted, first
264 the switch (e.g., \"-fg\") in the following code, and possible values
265 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
266 This returns ARGS with the arguments that have been processed removed."
268 (setq x-invocation-args args
270 (while x-invocation-args
271 (let* ((this-switch (car x-invocation-args
))
272 (orig-this-switch this-switch
)
273 completion argval aelt
)
274 (setq x-invocation-args
(cdr x-invocation-args
))
275 ;; Check for long options with attached arguments
276 ;; and separate out the attached option argument into argval.
277 (if (string-match "^--[^=]*=" this-switch
)
278 (setq argval
(substring this-switch
(match-end 0))
279 this-switch
(substring this-switch
0 (1- (match-end 0)))))
280 (setq completion
(try-completion this-switch x-long-option-alist
))
281 (if (eq completion t
)
282 ;; Exact match for long option.
283 (setq this-switch
(cdr (assoc this-switch x-long-option-alist
)))
284 (if (stringp completion
)
285 (let ((elt (assoc completion x-long-option-alist
)))
286 ;; Check for abbreviated long option.
288 (error "Option `%s' is ambiguous" this-switch
))
289 (setq this-switch
(cdr elt
)))
290 ;; Check for a short option.
291 (setq argval nil this-switch orig-this-switch
)))
292 (setq aelt
(assoc this-switch x-option-alist
))
295 (let ((x-invocation-args
296 (cons argval x-invocation-args
)))
297 (funcall (cdr aelt
) this-switch
))
298 (funcall (cdr aelt
) this-switch
))
299 (setq args
(cons this-switch args
)))))
300 (setq args
(nreverse args
)))
308 (defvar x-colors
'("aquamarine"
366 "medium spring green"
446 "The full list of X colors from the `rgb.text' file.")
448 (defun x-defined-colors (&optional frame
)
449 "Return a list of colors supported for a particular frame.
450 The argument FRAME specifies which frame to try.
451 The value may be different for frames on different X displays."
452 (or frame
(setq frame
(selected-frame)))
453 (let ((all-colors x-colors
)
455 (defined-colors nil
))
457 (setq this-color
(car all-colors
)
458 all-colors
(cdr all-colors
))
459 (and (face-color-supported-p frame this-color t
)
460 (setq defined-colors
(cons this-color defined-colors
))))
465 (defun iconify-or-deiconify-frame ()
466 "Iconify the selected frame, or deiconify if it's currently an icon."
468 (if (eq (cdr (assq 'visibility
(frame-parameters))) t
)
470 (make-frame-visible)))
472 (substitute-key-definition 'suspend-emacs
'iconify-or-deiconify-frame
475 ;; Map certain keypad keys into ASCII characters
476 ;; that people usually expect.
477 (define-key function-key-map
[tab] [?\t])
478 (define-key function-key-map [linefeed] [?\n])
479 (define-key function-key-map [clear] [11])
480 (define-key function-key-map [return] [13])
481 (define-key function-key-map [escape] [?\e])
482 (define-key function-key-map [M-tab] [?\M-\t])
483 (define-key function-key-map [M-linefeed] [?\M-\n])
484 (define-key function-key-map [M-clear] [?\M-\013])
485 (define-key function-key-map [M-return] [?\M-\015])
486 (define-key function-key-map [M-escape] [?\M-\e])
488 ;; These don't do the right thing (voelker)
489 ;(define-key function-key-map [backspace] [127])
490 ;(define-key function-key-map [delete] [127])
491 ;(define-key function-key-map [M-backspace] [?\M-\d])
492 ;(define-key function-key-map [M-delete] [?\M-\d])
494 ;; These tell read-char how to convert
495 ;; these special chars to ASCII.
496 (put 'tab 'ascii-character ?\t)
497 (put 'linefeed 'ascii-character ?\n)
498 (put 'clear 'ascii-character 12)
499 (put 'return 'ascii-character 13)
500 (put 'escape 'ascii-character ?\e)
501 ;; These don't seem to be necessary (voelker)
502 ;(put 'backspace 'ascii-character 127)
503 ;(put 'delete 'ascii-character 127)
506 ;;;; Selections and cut buffers
508 ;;; We keep track of the last text selected here, so we can check the
509 ;;; current selection against it, and avoid passing back our own text
510 ;;; from x-cut-buffer-or-selection-value.
511 (defvar x-last-selected-text nil)
513 ;;; It is said that overlarge strings are slow to put into the cut buffer.
514 ;;; Note this value is overridden below.
515 (defvar x-cut-buffer-max 20000
516 "Max number of characters to put in the cut buffer.")
518 (defvar x-select-enable-clipboard t
519 "Non-nil means cutting and pasting uses the clipboard.
520 This is in addition to the primary selection.")
522 (defun x-select-text (text &optional push)
523 (if x-select-enable-clipboard
524 (win32-set-clipboard-data text))
525 (setq x-last-selected-text text))
527 ;;; Return the value of the current selection.
528 ;;; Consult the selection, then the cut buffer. Treat empty strings
529 ;;; as if they were unset.
530 (defun x-get-selection-value ()
531 (if x-select-enable-clipboard
533 ;; Don't die if x-get-selection signals an error.
535 (setq text (win32-get-clipboard-data))
536 (error (message "win32-get-clipboard-data:%s" c)))
537 (if (string= text "") (setq text nil))
540 ((eq text x-last-selected-text) nil)
541 ((string= text x-last-selected-text)
542 ;; Record the newer string, so subsequent calls can use the 'eq' test.
543 (setq x-last-selected-text text)
546 (setq x-last-selected-text text))))))
548 ;;; Do the actual Windows setup here; the above code just defines
549 ;;; functions and variables that we use now.
551 (setq command-line-args (x-handle-args command-line-args))
553 ;;; Make sure we have a valid resource name.
554 (or (stringp x-resource-name)
556 (setq x-resource-name (invocation-name))
558 ;; Change any . or * characters in x-resource-name to hyphens,
559 ;; so as not to choke when we use it in X resource queries.
560 (while (setq i (string-match "[.*]" x-resource-name))
561 (aset x-resource-name i ?-))))
563 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
564 ;; the same lisp directory, don't pass the third argument unless we seem
565 ;; to have the multi-display support.
566 (if (fboundp 'x-close-connection)
567 (x-open-connection ""
568 x-command-line-resources
569 ;; Exit Emacs with fatal error if this fails.
571 (x-open-connection ""
572 x-command-line-resources))
574 (setq frame-creation-function 'x-create-frame-with-faces)
576 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
579 ;; Win32 expects the menu bar cut and paste commands to use the clipboard.
580 ;; This has ,? to match both on Sunos and on Solaris.
581 (menu-bar-enable-clipboard)
583 ;; Apply a geometry resource to the initial frame. Put it at the end
584 ;; of the alist, so that anything specified on the command line takes
586 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
590 (setq parsed (x-parse-geometry res-geometry))
591 ;; If the resource specifies a position,
592 ;; call the position and size "user-specified".
593 (if (or (assq 'top parsed) (assq 'left parsed))
594 (setq parsed (cons '(user-position . t)
595 (cons '(user-size . t) parsed))))
596 ;; All geometry parms apply to the initial frame.
597 (setq initial-frame-alist (append initial-frame-alist parsed))
598 ;; The size parms apply to all frames.
599 (if (assq 'height parsed)
600 (setq default-frame-alist
601 (cons (cons 'height (cdr (assq 'height parsed)))
602 default-frame-alist)))
603 (if (assq 'width parsed)
604 (setq default-frame-alist
605 (cons (cons 'width (cdr (assq 'width parsed)))
606 default-frame-alist))))))
608 ;; Check the reverseVideo resource.
609 (let ((case-fold-search t))
610 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
612 (string-match "^\\(true\\|yes\\|on\\)$" rv))
613 (setq default-frame-alist
614 (cons '(reverse . t) default-frame-alist)))))
616 ;; Set x-selection-timeout, measured in milliseconds.
617 (let ((res-selection-timeout
618 (x-get-resource "selectionTimeout" "SelectionTimeout")))
619 (setq x-selection-timeout 20000)
620 (if res-selection-timeout
621 (setq x-selection-timeout (string-to-number res-selection-timeout))))
623 (defun x-win-suspend-error ()
624 (error "Suspending an emacs running under Win32 makes no sense"))
625 (add-hook 'suspend-hook 'x-win-suspend-error)
627 ;;; Arrange for the kill and yank functions to set and check the clipboard.
628 (setq interprogram-cut-function 'x-select-text)
629 (setq interprogram-paste-function 'x-get-selection-value)
631 ;;; Turn off window-splitting optimization; win32 is usually fast enough
632 ;;; that this is only annoying.
633 (setq split-window-keep-point t)
635 ;; Don't show the frame name; that's redundant.
636 (setq-default mode-line-buffer-identification '("Emacs: %12b"))
638 ;;; Set to a system sound if you want a fancy bell.
639 (set-message-beep 'ok)
641 ;; Remap some functions to call win32 common dialogs
643 (defun internal-face-interactive (what &optional bool)
644 (let* ((fn (intern (concat "face-" what)))
645 (prompt (concat "Set " what " of face"))
646 (face (read-face-name (concat prompt ": ")))
647 (default (if (fboundp fn)
648 (or (funcall fn face (selected-frame))
649 (funcall fn 'default (selected-frame)))))
650 (fn-win (intern (concat (symbol-name window-system) "-select-" what)))
655 (y-or-n-p (concat "Should face " (symbol-name face)
657 (read-string (concat prompt " " (symbol-name face) " to: ")
659 (list face (if (equal value "") nil value))))
661 ;; Redefine the font selection to use the Win32 dialog
663 (defun mouse-set-font (&rest fonts)
665 (set-default-font (win32-select-font)))
667 ;;; win32-win.el ends here