1 ;;; w32-win.el --- parse switches controlling interface with W32 window system
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 ;; w32-win.el: this file is loaded from ../lisp/startup.el when it recognizes
28 ;; that W32 windows are to be used. Command line switches are parsed and those
29 ;; pertaining to W32 are processed and removed from the command line. The
30 ;; W32 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 'w32))
71 (error "%s: Loading w32-win.el but not compiled for w32" (invocation-name)))
79 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles
80 (if (fboundp 'new-fontset)
83 ;; The following definition is used for debugging scroll bar events.
84 ;(defun w32-handle-scroll-bar-event (event) (interactive "e") (princ event))
86 ;; mwheel.el should probably be adapted to accept mouse-wheel events
87 ;; then this could go.
88 (defun mouse-wheel-scroll-line (event)
89 "Scroll the window in which EVENT occurred by `mouse-wheel-scroll-amount'."
92 (if (< (car (cdr (cdr event))) 0)
93 (scroll-up (car mouse-wheel-scroll-amount))
94 (scroll-down (car mouse-wheel-scroll-amount)))
97 ;; for scroll-in-place.el, this way the -scroll-line and -scroll-screen
98 ;; commands won't interact
99 (setq scroll-command-groups (list '(mouse-wheel-scroll-line)))
101 (defun mouse-wheel-scroll-screen (event)
102 "Scroll the window in which EVENT occurred by `mouse-wheel-scroll-amount'."
105 (if (< (car (cdr (cdr event))) 0)
110 ;; Bind the mouse-wheel event:
111 (global-set-key [mouse-wheel] 'mouse-wheel-scroll-line)
112 (global-set-key [C-mouse-wheel] 'mouse-wheel-scroll-screen)
114 (defun w32-drag-n-drop-debug (event)
115 "Print the drag-n-drop EVENT in a readable form."
119 (defun w32-drag-n-drop (event)
120 "Edit the files listed in the drag-n-drop EVENT.
121 Switch to a buffer editing the last file dropped."
124 ;; Make sure the drop target has positive co-ords
125 ;; before setting the selected frame - otherwise it
126 ;; won't work. <skx@tardis.ed.ac.uk>
127 (let* ((window (posn-window (event-start event)))
128 (coords (posn-x-y (event-start event)))
131 (if (and (> x 0) (> y 0))
132 (set-frame-selected-window nil window))
133 (mapcar 'find-file (car (cdr (cdr event)))))
136 (defun w32-drag-n-drop-other-frame (event)
137 "Edit the files listed in the drag-n-drop EVENT, in other frames.
138 May create new frames, or reuse existing ones. The frame editing
139 the last file dropped is selected."
141 (mapcar 'find-file-other-frame (car (cdr (cdr event)))))
143 ;; Bind the drag-n-drop event.
144 (global-set-key [drag-n-drop] 'w32-drag-n-drop)
145 (global-set-key [C-drag-n-drop] 'w32-drag-n-drop-other-frame)
147 ;; Keyboard layout/language change events
148 ;; For now ignore language-change events; in the future
149 ;; we should switch the Emacs Input Method to match the
150 ;; new layout/language selected by the user.
151 (global-set-key [language-change] 'ignore)
153 (defvar x-invocation-args)
155 (defvar x-command-line-resources nil)
157 (defun x-handle-switch (switch)
158 "Handle SWITCH of the form \"-switch value\" or \"-switch\"."
159 (let ((aelt (assoc switch command-line-x-option-alist)))
161 (let ((param (nth 3 aelt))
162 (value (nth 4 aelt)))
164 (setq default-frame-alist
165 (cons (cons param value)
166 default-frame-alist))
167 (setq default-frame-alist
169 (car x-invocation-args))
170 default-frame-alist))
171 x-invocation-args (cdr x-invocation-args))))))
173 (defun x-handle-numeric-switch (switch)
174 "Handle SWITCH of the form \"-switch n\"."
175 (let ((aelt (assoc switch command-line-x-option-alist)))
177 (let ((param (nth 3 aelt)))
178 (setq default-frame-alist
180 (string-to-int (car x-invocation-args)))
183 (cdr x-invocation-args))))))
185 ;; Handle options that apply to initial frame only
186 (defun x-handle-initial-switch (switch)
187 (let ((aelt (assoc switch command-line-x-option-alist)))
189 (let ((param (nth 3 aelt))
190 (value (nth 4 aelt)))
192 (setq initial-frame-alist
193 (cons (cons param value)
194 initial-frame-alist))
195 (setq initial-frame-alist
197 (car x-invocation-args))
199 x-invocation-args (cdr x-invocation-args)))))))
201 (defun x-handle-iconic (switch)
202 "Make \"-iconic\" SWITCH apply only to the initial frame."
203 (setq initial-frame-alist
204 (cons '(visibility . icon) initial-frame-alist)))
206 (defun x-handle-xrm-switch (switch)
207 "Handle the \"-xrm\" SWITCH."
208 (or (consp x-invocation-args)
209 (error "%s: missing argument to `%s' option" (invocation-name) switch))
210 (setq x-command-line-resources
211 (if (null x-command-line-resources)
212 (car x-invocation-args)
213 (concat x-command-line-resources "\n" (car x-invocation-args))))
214 (setq x-invocation-args (cdr x-invocation-args)))
216 (defun x-handle-geometry (switch)
217 "Handle the \"-geometry\" SWITCH."
218 (let* ((geo (x-parse-geometry (car x-invocation-args)))
219 (left (assq 'left geo))
220 (top (assq 'top geo))
221 (height (assq 'height geo))
222 (width (assq 'width geo)))
223 (if (or height width)
224 (setq default-frame-alist
225 (append default-frame-alist
227 (if height (list height))
228 (if width (list width)))))
230 (setq initial-frame-alist
231 (append initial-frame-alist
232 '((user-position . t))
233 (if left (list left))
234 (if top (list top)))))
235 (setq x-invocation-args (cdr x-invocation-args))))
237 (defun x-handle-name-switch (switch)
238 "Handle a \"-name\" SWITCH."
239 ;; Handle the -name option. Set the variable x-resource-name
240 ;; to the option's operand; set the name of the initial frame, too.
241 (or (consp x-invocation-args)
242 (error "%s: missing argument to `%s' option" (invocation-name) switch))
243 (setq x-resource-name (car x-invocation-args)
244 x-invocation-args (cdr x-invocation-args))
245 (setq initial-frame-alist (cons (cons 'name x-resource-name)
246 initial-frame-alist)))
248 (defvar x-display-name nil
249 "The display name specifying server and frame.")
251 (defun x-handle-display (switch)
252 "Handle the \"-display\" SWITCH."
253 (setq x-display-name (car x-invocation-args)
254 x-invocation-args (cdr x-invocation-args)))
256 (defun x-handle-args (args)
257 "Process the X-related command line options in ARGS.
258 This is done before the user's startup file is loaded. They are copied to
259 `x-invocation args' from which the X-related things are extracted, first
260 the switch (e.g., \"-fg\") in the following code, and possible values
261 \(e.g., \"black\") in the option handler code (e.g., x-handle-switch).
262 This returns ARGS with the arguments that have been processed removed."
263 ;; We use ARGS to accumulate the args that we don't handle here, to return.
264 (setq x-invocation-args args
266 (while (and x-invocation-args
267 (not (equal (car x-invocation-args) "--")))
268 (let* ((this-switch (car x-invocation-args))
269 (orig-this-switch this-switch)
270 completion argval aelt handler)
271 (setq x-invocation-args (cdr x-invocation-args))
272 ;; Check for long options with attached arguments
273 ;; and separate out the attached option argument into argval.
274 (if (string-match "^--[^=]*=" this-switch)
275 (setq argval (substring this-switch (match-end 0))
276 this-switch (substring this-switch 0 (1- (match-end 0)))))
277 ;; Complete names of long options.
278 (if (string-match "^--" this-switch)
280 (setq completion (try-completion this-switch command-line-x-option-alist))
281 (if (eq completion t)
282 ;; Exact match for long option.
284 (if (stringp completion)
285 (let ((elt (assoc completion command-line-x-option-alist)))
286 ;; Check for abbreviated long option.
288 (error "Option `%s' is ambiguous" this-switch))
289 (setq this-switch completion))))))
290 (setq aelt (assoc this-switch command-line-x-option-alist))
291 (if aelt (setq handler (nth 2 aelt)))
294 (let ((x-invocation-args
295 (cons argval x-invocation-args)))
296 (funcall handler this-switch))
297 (funcall handler this-switch))
298 (setq args (cons orig-this-switch args)))))
299 (nconc (nreverse args) x-invocation-args))
305 (defvar x-colors '("LightGreen"
904 "LightGoldenrodYellow"
905 "light goldenrod yellow"
922 "medium spring green"
1057 "The list of X colors from the `rgb.txt' file.
1058 XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp")
1060 (defun xw-defined-colors (&optional frame)
1061 "Internal function called by `defined-colors', which see."
1062 (or frame (setq frame (selected-frame)))
1063 (let* ((color-map-colors (mapcar (lambda (clr) (car clr)) w32-color-map))
1064 (all-colors (or color-map-colors x-colors))
1066 (defined-colors nil))
1067 (message "Defining colors...")
1069 (setq this-color (car all-colors)
1070 all-colors (cdr all-colors))
1071 (and (color-supported-p this-color frame t)
1072 (setq defined-colors (cons this-color defined-colors))))
1078 ;;; make f10 activate the real menubar rather than the mini-buffer menu
1079 ;;; navigation feature.
1080 (global-set-key [f10] (lambda ()
1081 (interactive) (w32-send-sys-command ?\xf100)))
1083 (defun iconify-or-deiconify-frame ()
1084 "Iconify the selected frame, or deiconify if it's currently an icon."
1086 (if (eq (cdr (assq 'visibility (frame-parameters))) t)
1088 (make-frame-visible)))
1090 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
1094 ;;; Do the actual Windows setup here; the above code just defines
1095 ;;; functions and variables that we use now.
1097 (setq command-line-args (x-handle-args command-line-args))
1099 ;;; Make sure we have a valid resource name.
1100 (or (stringp x-resource-name)
1102 (setq x-resource-name (invocation-name))
1104 ;; Change any . or * characters in x-resource-name to hyphens,
1105 ;; so as not to choke when we use it in X resource queries.
1106 (while (setq i (string-match "[.*]" x-resource-name))
1107 (aset x-resource-name i ?-))))
1109 ;; For the benefit of older Emacses (19.27 and earlier) that are sharing
1110 ;; the same lisp directory, don't pass the third argument unless we seem
1111 ;; to have the multi-display support.
1112 (if (fboundp 'x-close-connection)
1113 (x-open-connection ""
1114 x-command-line-resources
1115 ;; Exit Emacs with fatal error if this fails.
1117 (x-open-connection ""
1118 x-command-line-resources))
1120 (setq frame-creation-function 'x-create-frame-with-faces)
1122 (setq x-cut-buffer-max (min (- (/ (x-server-max-request-size) 2) 100)
1125 ;; W32 expects the menu bar cut and paste commands to use the clipboard.
1126 ;; This has ,? to match both on Sunos and on Solaris.
1127 (menu-bar-enable-clipboard)
1129 ;; W32 systems have different fonts than commonly found on X, so
1130 ;; we define our own standard fontset here.
1131 (defvar w32-standard-fontset-spec
1132 "-*-Courier New-normal-r-*-*-13-*-*-*-c-*-fontset-standard"
1133 "String of fontset spec of the standard fontset.
1134 This defines a fontset consisting of the Courier New variations for
1135 European languages which are distributed with Windows as
1136 \"Multilanguage Support\".
1138 See the documentation of `create-fontset-from-fontset-spec for the format.")
1140 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles
1141 (if (fboundp 'new-fontset)
1143 ;; Create the standard fontset.
1144 (create-fontset-from-fontset-spec w32-standard-fontset-spec t)
1145 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1,...).
1146 (create-fontset-from-x-resource)
1147 ;; Try to create a fontset from a font specification which comes
1148 ;; from initial-frame-alist, default-frame-alist, or X resource.
1149 ;; A font specification in command line argument (i.e. -fn XXXX)
1150 ;; should be already in default-frame-alist as a `font'
1151 ;; parameter. However, any font specifications in site-start
1152 ;; library, user's init file (.emacs), and default.el are not
1153 ;; yet handled here.
1155 (let ((font (or (cdr (assq 'font initial-frame-alist))
1156 (cdr (assq 'font default-frame-alist))
1157 (x-get-resource "font" "Font")))
1158 xlfd-fields resolved-name)
1160 (not (query-fontset font))
1161 (setq resolved-name (x-resolve-font-name font))
1162 (setq xlfd-fields (x-decompose-font-name font)))
1163 (if (string= "fontset"
1164 (aref xlfd-fields xlfd-regexp-registry-subnum))
1166 (x-complement-fontset-spec xlfd-fields nil))
1167 ;; Create a fontset from FONT. The fontset name is
1168 ;; generated from FONT.
1169 (create-fontset-from-ascii-font font
1170 resolved-name "startup"))))))
1172 ;; Apply a geometry resource to the initial frame. Put it at the end
1173 ;; of the alist, so that anything specified on the command line takes
1175 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
1179 (setq parsed (x-parse-geometry res-geometry))
1180 ;; If the resource specifies a position,
1181 ;; call the position and size "user-specified".
1182 (if (or (assq 'top parsed) (assq 'left parsed))
1183 (setq parsed (cons '(user-position . t)
1184 (cons '(user-size . t) parsed))))
1185 ;; All geometry parms apply to the initial frame.
1186 (setq initial-frame-alist (append initial-frame-alist parsed))
1187 ;; The size parms apply to all frames.
1188 (if (assq 'height parsed)
1189 (setq default-frame-alist
1190 (cons (cons 'height (cdr (assq 'height parsed)))
1191 default-frame-alist)))
1192 (if (assq 'width parsed)
1193 (setq default-frame-alist
1194 (cons (cons 'width (cdr (assq 'width parsed)))
1195 default-frame-alist))))))
1197 ;; Check the reverseVideo resource.
1198 (let ((case-fold-search t))
1199 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
1201 (string-match "^\\(true\\|yes\\|on\\)$" rv))
1202 (setq default-frame-alist
1203 (cons '(reverse . t) default-frame-alist)))))
1205 (defun x-win-suspend-error ()
1206 "Report an error when a suspend is attempted."
1207 (error "Suspending an Emacs running under W32 makes no sense"))
1208 (add-hook 'suspend-hook 'x-win-suspend-error)
1210 ;;; Turn off window-splitting optimization; w32 is usually fast enough
1211 ;;; that this is only annoying.
1212 (setq split-window-keep-point t)
1214 ;; Don't show the frame name; that's redundant.
1215 (setq-default mode-line-frame-identification " ")
1217 ;;; Set to a system sound if you want a fancy bell.
1218 (set-message-beep 'ok)
1220 ;; Remap some functions to call w32 common dialogs
1222 (defun internal-face-interactive (what &optional bool)
1223 (let* ((fn (intern (concat "face-" what)))
1224 (prompt (concat "Set " what " of face "))
1225 (face (read-face-name prompt))
1226 (default (if (fboundp fn)
1227 (or (funcall fn face (selected-frame))
1228 (funcall fn 'default (selected-frame)))))
1229 (fn-win (intern (concat (symbol-name window-system) "-select-" what)))
1232 (cond ((fboundp fn-win)
1235 (completing-read (concat prompt " " (symbol-name face) " to: ")
1236 (mapcar (function (lambda (color)
1237 (cons color color)))
1239 nil nil nil nil default))
1241 (y-or-n-p (concat "Should face " (symbol-name face)
1244 (read-string (concat prompt " " (symbol-name face) " to: ")
1246 (list face (if (equal value "") nil value))))
1248 ;;; Enable Japanese fonts on Windows to be used by default.
1249 (set-fontset-font t (make-char 'katakana-jisx0201) '("*" . "JISX0208-SJIS"))
1250 (set-fontset-font t (make-char 'latin-jisx0201) '("*" . "JISX0208-SJIS"))
1251 (set-fontset-font t (make-char 'japanese-jisx0208) '("*" . "JISX0208-SJIS"))
1252 (set-fontset-font t (make-char 'japanese-jisx0208-1978) '("*" . "JISX0208-SJIS"))
1254 (defun mouse-set-font (&rest fonts)
1256 If `w32-use-w32-font-dialog' is non-nil (the default), use the Windows
1257 font dialog to get the matching FONTS. Otherwise use a pop-up menu
1258 \(like Emacs on other platforms) initialized with the fonts in
1259 `w32-fixed-font-alist'."
1261 (if w32-use-w32-font-dialog
1262 (let ((chosen-font (w32-select-font (selected-frame)
1263 w32-list-proportional-fonts)))
1264 (and chosen-font (list chosen-font)))
1267 ;; Append list of fontsets currently defined.
1268 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles
1269 (if (fboundp 'new-fontset)
1270 (append w32-fixed-font-alist (list (generate-fontset-menu)))))))
1276 (setq font (car fonts))
1277 (set-default-font font)
1279 (error (setq fonts (cdr fonts)))))
1281 (error "Font not found")))))
1283 ;;; w32-win.el ends here