1 ;;; w32-win.el --- parse switches controlling interface with W32 window system
3 ;; Copyright (C) 1993, 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
28 ;; w32-win.el: this file is loaded from ../lisp/startup.el when it recognizes
29 ;; that W32 windows are to be used. Command line switches are parsed and those
30 ;; pertaining to W32 are processed and removed from the command line. The
31 ;; W32 display is opened and hooks are set for popping up the initial window.
33 ;; startup.el will then examine startup files, and eventually call the hooks
34 ;; which create the first window (s).
39 ;; These are the standard X switches from the Xt Initialize.c file of
42 ;; Command line Resource Manager string
45 ;; +synchronous *synchronous
46 ;; -background *background
49 ;; -bordercolor *borderColor
50 ;; -borderwidth .borderWidth
56 ;; -foreground *foreground
57 ;; -geometry .geometry
62 ;; -reverse *reverseVideo
64 ;; -selectionTimeout .selectionTimeout
65 ;; -synchronous *synchronous
68 ;; An alist of X options and the function which handles them. See
71 ;; (if (not (eq window-system 'w32))
72 ;; (error "%s: Loading w32-win.el but not compiled for w32" (invocation-name)))
83 ;; Keep an obsolete alias for w32-focus-frame in case it is used by code
85 (define-obsolete-function-alias 'w32-focus-frame
'x-focus-frame
"23.1")
87 (defvar xlfd-regexp-registry-subnum
)
89 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles
90 (if (fboundp 'new-fontset
)
93 ;; The following definition is used for debugging scroll bar events.
94 ;(defun w32-handle-scroll-bar-event (event) (interactive "e") (princ event))
96 (defun w32-drag-n-drop-debug (event)
97 "Print the drag-n-drop EVENT in a readable form."
101 (defun w32-drag-n-drop (event)
102 "Edit the files listed in the drag-n-drop EVENT.
103 Switch to a buffer editing the last file dropped."
106 ;; Make sure the drop target has positive co-ords
107 ;; before setting the selected frame - otherwise it
108 ;; won't work. <skx@tardis.ed.ac.uk>
109 (let* ((window (posn-window (event-start event
)))
110 (coords (posn-x-y (event-start event
)))
113 (if (and (> x
0) (> y
0))
114 (set-frame-selected-window nil window
))
115 (mapc (lambda (file-name)
116 (let ((f (subst-char-in-string ?
\\ ?
/ file-name
))
117 (coding (or file-name-coding-system
118 default-file-name-coding-system
)))
120 (mapconcat 'url-hexify-string
121 (split-string (encode-coding-string f coding
)
124 (dnd-handle-one-url window
'private
125 (concat "file:" file-name
)))
126 (car (cdr (cdr event
)))))
129 (defun w32-drag-n-drop-other-frame (event)
130 "Edit the files listed in the drag-n-drop EVENT, in other frames.
131 May create new frames, or reuse existing ones. The frame editing
132 the last file dropped is selected."
134 (mapcar 'find-file-other-frame
(car (cdr (cdr event
)))))
136 ;; Bind the drag-n-drop event.
137 (global-set-key [drag-n-drop
] 'w32-drag-n-drop
)
138 (global-set-key [C-drag-n-drop
] 'w32-drag-n-drop-other-frame
)
140 ;; Keyboard layout/language change events
141 ;; For now ignore language-change events; in the future
142 ;; we should switch the Emacs Input Method to match the
143 ;; new layout/language selected by the user.
144 (global-set-key [language-change
] 'ignore
)
146 (defvar x-invocation-args
)
148 (defvar x-command-line-resources nil
)
150 (defun x-handle-switch (switch)
151 "Handle SWITCH of the form \"-switch value\" or \"-switch\"."
152 (let ((aelt (assoc switch command-line-x-option-alist
)))
154 (push (cons (nth 3 aelt
) (or (nth 4 aelt
) (pop x-invocation-args
)))
155 default-frame-alist
))))
157 (defun x-handle-numeric-switch (switch)
158 "Handle SWITCH of the form \"-switch n\"."
159 (let ((aelt (assoc switch command-line-x-option-alist
)))
161 (push (cons (nth 3 aelt
) (string-to-number (pop x-invocation-args
)))
162 default-frame-alist
))))
164 ;; Handle options that apply to initial frame only
165 (defun x-handle-initial-switch (switch)
166 (let ((aelt (assoc switch command-line-x-option-alist
)))
168 (push (cons (nth 3 aelt
) (or (nth 4 aelt
) (pop x-invocation-args
)))
169 initial-frame-alist
))))
171 (defun x-handle-iconic (switch)
172 "Make \"-iconic\" SWITCH apply only to the initial frame."
173 (push '(visibility . icon
) initial-frame-alist
))
175 (defun x-handle-xrm-switch (switch)
176 "Handle the \"-xrm\" SWITCH."
177 (or (consp x-invocation-args
)
178 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
179 (setq x-command-line-resources
180 (if (null x-command-line-resources
)
181 (car x-invocation-args
)
182 (concat x-command-line-resources
"\n" (car x-invocation-args
))))
183 (setq x-invocation-args
(cdr x-invocation-args
)))
185 (defun x-handle-geometry (switch)
186 "Handle the \"-geometry\" SWITCH."
187 (let* ((geo (x-parse-geometry (car x-invocation-args
)))
188 (left (assq 'left geo
))
189 (top (assq 'top geo
))
190 (height (assq 'height geo
))
191 (width (assq 'width geo
)))
192 (if (or height width
)
193 (setq default-frame-alist
194 (append default-frame-alist
196 (if height
(list height
))
197 (if width
(list width
)))
199 (append initial-frame-alist
201 (if height
(list height
))
202 (if width
(list width
)))))
204 (setq initial-frame-alist
205 (append initial-frame-alist
206 '((user-position . t
))
207 (if left
(list left
))
208 (if top
(list top
)))))
209 (setq x-invocation-args
(cdr x-invocation-args
))))
211 (defun x-handle-name-switch (switch)
212 "Handle the \"-name\" SWITCH."
213 ;; Handle the -name option. Set the variable x-resource-name
214 ;; to the option's operand; set the name of the initial frame, too.
215 (or (consp x-invocation-args
)
216 (error "%s: missing argument to `%s' option" (invocation-name) switch
))
217 (setq x-resource-name
(pop x-invocation-args
))
218 (push (cons 'name x-resource-name
) initial-frame-alist
))
220 (defvar x-display-name nil
221 "The display name specifying server and frame.")
223 (defun x-handle-display (switch)
224 "Handle the \"-display\" SWITCH."
225 (setq x-display-name
(pop x-invocation-args
)))
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."
234 ;; We use ARGS to accumulate the args that we don't handle here, to return.
235 (setq x-invocation-args args
237 (while (and x-invocation-args
238 (not (equal (car x-invocation-args
) "--")))
239 (let* ((this-switch (car x-invocation-args
))
240 (orig-this-switch this-switch
)
241 completion argval aelt handler
)
242 (setq x-invocation-args
(cdr x-invocation-args
))
243 ;; Check for long options with attached arguments
244 ;; and separate out the attached option argument into argval.
245 (if (string-match "^--[^=]*=" this-switch
)
246 (setq argval
(substring this-switch
(match-end 0))
247 this-switch
(substring this-switch
0 (1- (match-end 0)))))
248 ;; Complete names of long options.
249 (if (string-match "^--" this-switch
)
251 (setq completion
(try-completion this-switch command-line-x-option-alist
))
252 (if (eq completion t
)
253 ;; Exact match for long option.
255 (if (stringp completion
)
256 (let ((elt (assoc completion command-line-x-option-alist
)))
257 ;; Check for abbreviated long option.
259 (error "Option `%s' is ambiguous" this-switch
))
260 (setq this-switch completion
))))))
261 (setq aelt
(assoc this-switch command-line-x-option-alist
))
262 (if aelt
(setq handler
(nth 2 aelt
)))
265 (let ((x-invocation-args
266 (cons argval x-invocation-args
)))
267 (funcall handler this-switch
))
268 (funcall handler this-switch
))
269 (push orig-this-switch args
))))
270 (nconc (nreverse args
) x-invocation-args
))
276 (defvar x-colors
'("LightGreen"
875 "LightGoldenrodYellow"
876 "light goldenrod yellow"
893 "medium spring green"
1028 "The list of X colors from the `rgb.txt' file.
1029 XConsortium: rgb.txt,v 10.41 94/02/20 18:39:36 rws Exp")
1031 (defun xw-defined-colors (&optional frame
)
1032 "Internal function called by `defined-colors', which see."
1033 (or frame
(setq frame
(selected-frame)))
1034 (let ((defined-colors nil
))
1035 (dolist (this-color (or (mapcar 'car w32-color-map
) x-colors
))
1036 (and (color-supported-p this-color frame t
)
1037 (push this-color defined-colors
)))
1043 ;;; make f10 activate the real menubar rather than the mini-buffer menu
1044 ;;; navigation feature.
1045 (defun menu-bar-open (&optional frame
)
1046 "Start key navigation of the menu bar in FRAME.
1048 This initially activates the first menu-bar item, and you can then navigate
1049 with the arrow keys, select a menu entry with the Return key or cancel with
1050 the Escape key. If FRAME has no menu bar, this function does nothing.
1052 If FRAME is nil or not given, use the selected frame."
1054 (w32-send-sys-command ?
\xf100 frame
))
1056 (defun x-setup-function-keys (frame)
1057 "Setup Function Keys for w32."
1058 (with-selected-frame frame
1059 (define-key local-function-key-map
[f10] 'menu-bar-open)
1061 (substitute-key-definition 'suspend-emacs 'iconify-or-deiconify-frame
1062 local-function-key-map global-map)
1064 (define-key local-function-key-map [S-tab] [backtab]))
1065 (set-terminal-parameter frame 'x-setup-function-keys t))
1068 ;; W32 systems have different fonts than commonly found on X, so
1069 ;; we define our own standard fontset here.
1070 (defvar w32-standard-fontset-spec
1071 "-*-Courier New-normal-r-*-*-13-*-*-*-c-*-fontset-standard"
1072 "String of fontset spec of the standard fontset.
1073 This defines a fontset consisting of the Courier New variations for
1074 European languages which are distributed with Windows as
1075 \"Multilanguage Support\".
1077 See the documentation of `create-fontset-from-fontset-spec' for the format.")
1079 (defun x-win-suspend-error ()
1080 "Report an error when a suspend is attempted."
1081 (error "Suspending an Emacs running under W32 makes no sense"))
1084 ;;; Enable Japanese fonts on Windows to be used by default.
1085 (set-fontset-font nil (make-char 'katakana-jisx0201) '("*" . "JISX0208-SJIS"))
1086 (set-fontset-font nil (make-char 'latin-jisx0201) '("*" . "JISX0208-SJIS"))
1087 (set-fontset-font nil (make-char 'japanese-jisx0208) '("*" . "JISX0208-SJIS"))
1088 (set-fontset-font nil (make-char 'japanese-jisx0208-1978) '("*" . "JISX0208-SJIS"))
1090 (defun mouse-set-font (&rest fonts)
1091 "Select an Emacs font from a list of known good fonts and fontsets.
1093 If `w32-use-w32-font-dialog' is non-nil (the default), use the Windows
1094 font dialog to display the list of possible fonts. Otherwise use a
1095 pop-up menu (like Emacs does on other platforms) initialized with
1096 the fonts in `w32-fixed-font-alist'.
1097 If `w32-list-proportional-fonts' is non-nil, add proportional fonts
1098 to the list in the font selection dialog (the fonts listed by the
1099 pop-up menu are unaffected by `w32-list-proportional-fonts')."
1101 (if w32-use-w32-font-dialog
1102 (let ((chosen-font (w32-select-font (selected-frame)
1103 w32-list-proportional-fonts)))
1104 (and chosen-font (list chosen-font)))
1107 ;; Append list of fontsets currently defined.
1108 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles
1109 (if (fboundp 'new-fontset)
1110 (append w32-fixed-font-alist (list (generate-fontset-menu)))))))
1116 (setq font (car fonts))
1117 (set-default-font font)
1119 (error (setq fonts (cdr fonts)))))
1121 (error "Font not found")))))
1123 ;;; Set default known names for image libraries
1124 (setq image-library-alist
1125 '((xpm "xpm4.dll" "libXpm-nox4.dll" "libxpm.dll")
1126 (png "libpng13d.dll" "libpng13.dll" "libpng12d.dll" "libpng12.dll" "libpng.dll")
1127 (jpeg "jpeg62.dll" "libjpeg.dll" "jpeg-62.dll" "jpeg.dll")
1128 (tiff "libtiff3.dll" "libtiff.dll")
1129 (gif "giflib4.dll" "libungif4.dll" "libungif.dll")
1130 (svg "librsvg-2-2.dll")
1131 (gdk-pixbuf "libgdk_pixbuf-2.0-0.dll")
1132 (glib "libglib-2.0-0.dll")))
1134 ;;; multi-tty support
1135 (defvar w32-initialized nil
1136 "Non-nil if the w32 window system has been initialized.")
1138 (defun w32-initialize-window-system ()
1139 "Initialize Emacs for W32 GUI frames."
1141 ;; Do the actual Windows setup here; the above code just defines
1142 ;; functions and variables that we use now.
1144 (setq command-line-args (x-handle-args command-line-args))
1146 ;; Make sure we have a valid resource name.
1147 (or (stringp x-resource-name)
1148 (setq x-resource-name
1149 ;; Change any . or * characters in x-resource-name to hyphens,
1150 ;; so as not to choke when we use it in X resource queries.
1151 (replace-regexp-in-string "[.*]" "-" (invocation-name))))
1153 (x-open-connection "" x-command-line-resources
1154 ;; Exit with a fatal error if this fails and we
1155 ;; are the initial display
1156 (eq initial-window-system 'w32))
1158 ;; Setup the default fontset.
1159 (setup-default-fontset)
1160 ;; Create the standard fontset.
1161 (create-fontset-from-fontset-spec w32-standard-fontset-spec t)
1162 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1,...).
1163 (create-fontset-from-x-resource)
1164 ;; Try to create a fontset from a font specification which comes
1165 ;; from initial-frame-alist, default-frame-alist, or X resource.
1166 ;; A font specification in command line argument (i.e. -fn XXXX)
1167 ;; should be already in default-frame-alist as a `font'
1168 ;; parameter. However, any font specifications in site-start
1169 ;; library, user's init file (.emacs), and default.el are not
1170 ;; yet handled here.
1172 (let ((font (or (cdr (assq 'font initial-frame-alist))
1173 (cdr (assq 'font default-frame-alist))
1174 (x-get-resource "font" "Font")))
1175 xlfd-fields resolved-name)
1177 (not (query-fontset font))
1178 (setq resolved-name (x-resolve-font-name font))
1179 (setq xlfd-fields (x-decompose-font-name font)))
1180 (if (string= "fontset"
1181 (aref xlfd-fields xlfd-regexp-registry-subnum))
1183 (x-complement-fontset-spec xlfd-fields nil))
1184 ;; Create a fontset from FONT. The fontset name is
1185 ;; generated from FONT.
1186 (create-fontset-from-ascii-font font
1187 resolved-name "startup"))))
1189 ;; Apply a geometry resource to the initial frame. Put it at the end
1190 ;; of the alist, so that anything specified on the command line takes
1192 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
1196 (setq parsed (x-parse-geometry res-geometry))
1197 ;; If the resource specifies a position,
1198 ;; call the position and size "user-specified".
1199 (if (or (assq 'top parsed) (assq 'left parsed))
1200 (setq parsed (cons '(user-position . t)
1201 (cons '(user-size . t) parsed))))
1202 ;; All geometry parms apply to the initial frame.
1203 (setq initial-frame-alist (append initial-frame-alist parsed))
1204 ;; The size parms apply to all frames.
1205 (if (assq 'height parsed)
1206 (push (cons 'height (cdr (assq 'height parsed)))
1207 default-frame-alist))
1208 (if (assq 'width parsed)
1209 (push (cons 'width (cdr (assq 'width parsed)))
1210 default-frame-alist)))))
1212 ;; Check the reverseVideo resource.
1213 (let ((case-fold-search t))
1214 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
1215 (if (and rv (string-match "^\\(true\\|yes\\|on\\)$" rv))
1216 (push '(reverse . t) default-frame-alist))))
1218 ;; Don't let Emacs suspend under w32 gui
1219 (add-hook 'suspend-hook 'x-win-suspend-error)
1221 ;; Turn off window-splitting optimization; w32 is usually fast enough
1222 ;; that this is only annoying.
1223 (setq split-window-keep-point t)
1225 ;; Turn on support for mouse wheels
1226 (mouse-wheel-mode 1)
1228 ;; W32 expects the menu bar cut and paste commands to use the clipboard.
1229 (menu-bar-enable-clipboard)
1231 ;; Don't show the frame name; that's redundant.
1232 (setq-default mode-line-frame-identification " ")
1234 ;; Set to a system sound if you want a fancy bell.
1235 (set-message-beep 'ok)
1236 (setq w32-initialized t))
1238 (add-to-list 'handle-args-function-alist '(w32 . x-handle-args))
1239 (add-to-list 'frame-creation-function-alist '(w32 . x-create-frame-with-faces))
1240 (add-to-list 'window-system-initialization-alist '(w32 . w32-initialize-window-system))
1244 ;; arch-tag: 69fb1701-28c2-4890-b351-3d1fe4b4f166
1245 ;;; w32-win.el ends here