* lisp/select.el: Use lexical-binding.
[emacs.git] / lisp / term / w32-win.el
blobbb9a7e481a20426c9a5d98defdc20cd8d282ec52
1 ;;; w32-win.el --- parse switches controlling interface with W32 window system -*- lexical-binding: t -*-
3 ;; Copyright (C) 1993-1994, 2001-2014 Free Software Foundation, Inc.
5 ;; Author: Kevin Gallo
6 ;; Keywords: terminals
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 3 of the License, or
13 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
23 ;;; Commentary:
25 ;; w32-win.el: this file is loaded from ../lisp/startup.el when it recognizes
26 ;; that W32 windows are to be used. Command line switches are parsed and those
27 ;; pertaining to W32 are processed and removed from the command line. The
28 ;; W32 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).
33 ;;; Code:
36 ;; These are the standard X switches from the Xt Initialize.c file of
37 ;; Release 4.
39 ;; Command line Resource Manager string
41 ;; +rv *reverseVideo
42 ;; +synchronous *synchronous
43 ;; -background *background
44 ;; -bd *borderColor
45 ;; -bg *background
46 ;; -bordercolor *borderColor
47 ;; -borderwidth .borderWidth
48 ;; -bw .borderWidth
49 ;; -display .display
50 ;; -fg *foreground
51 ;; -fn *font
52 ;; -font *font
53 ;; -foreground *foreground
54 ;; -geometry .geometry
55 ;; -i .iconType
56 ;; -itype .iconType
57 ;; -iconic .iconic
58 ;; -name .name
59 ;; -reverse *reverseVideo
60 ;; -rv *reverseVideo
61 ;; -selectionTimeout .selectionTimeout
62 ;; -synchronous *synchronous
63 ;; -xrm
65 ;; An alist of X options and the function which handles them. See
66 ;; ../startup.el.
68 ;; (if (not (eq window-system 'w32))
69 ;; (error "%s: Loading w32-win.el but not compiled for w32" (invocation-name)))
71 (eval-when-compile (require 'cl-lib))
72 (require 'frame)
73 (require 'mouse)
74 (require 'scroll-bar)
75 (require 'faces)
76 (require 'select)
77 (require 'menu-bar)
78 (require 'dnd)
79 (require 'w32-vars)
81 ;; Keep an obsolete alias for w32-focus-frame and w32-select-font in case
82 ;; they are used by code outside Emacs.
83 (define-obsolete-function-alias 'w32-focus-frame 'x-focus-frame "23.1")
84 (declare-function x-select-font "w32font.c"
85 (&optional frame exclude-proportional))
86 (define-obsolete-function-alias 'w32-select-font 'x-select-font "23.1")
88 (defvar w32-color-map) ;; defined in w32fns.c
89 (make-obsolete 'w32-default-color-map nil "24.1")
91 (declare-function w32-send-sys-command "w32fns.c")
92 (declare-function set-message-beep "w32fns.c")
94 (declare-function cygwin-convert-file-name-from-windows "cygw32.c"
95 (path &optional absolute_p))
97 ;; Conditional on new-fontset so bootstrapping works on non-GUI compiles
98 (if (fboundp 'new-fontset)
99 (require 'fontset))
101 ;; The following definition is used for debugging scroll bar events.
102 ;(defun w32-handle-scroll-bar-event (event) (interactive "e") (princ event))
104 ;; (defun w32-drag-n-drop-debug (event)
105 ;; "Print the drag-n-drop EVENT in a readable form."
106 ;; (interactive "e")
107 ;; (princ event))
109 (defun w32-handle-dropped-file (window file-name)
110 (let ((f (if (eq system-type 'cygwin)
111 (cygwin-convert-file-name-from-windows file-name t)
112 (subst-char-in-string ?\\ ?/ file-name)))
113 (coding (if (eq system-type 'windows-nt)
114 ;; Native w32 build pretends that its file names
115 ;; are encoded in UTF-8, and converts to the
116 ;; appropriate encoding internally.
117 'utf-8
118 (or file-name-coding-system
119 default-file-name-coding-system))))
121 (setq file-name
122 (mapconcat 'url-hexify-string
123 (split-string (encode-coding-string f coding)
124 "/")
125 "/")))
126 (dnd-handle-one-url window 'private
127 (concat
128 (if (eq system-type 'cygwin)
129 "file://"
130 "file:")
131 file-name)))
133 (defun w32-drag-n-drop (event &optional new-frame)
134 "Edit the files listed in the drag-n-drop EVENT.
135 Switch to a buffer editing the last file dropped."
136 (interactive "e")
137 (save-excursion
138 ;; Make sure the drop target has positive co-ords
139 ;; before setting the selected frame - otherwise it
140 ;; won't work. <skx@tardis.ed.ac.uk>
141 (let* ((window (posn-window (event-start event)))
142 (coords (posn-x-y (event-start event)))
143 (x (car coords))
144 (y (cdr coords)))
145 (if (and (> x 0) (> y 0))
146 (set-frame-selected-window nil window))
148 (when new-frame
149 (select-frame (make-frame)))
150 (raise-frame)
151 (setq window (selected-window))
153 (mapc (apply-partially #'w32-handle-dropped-file window)
154 (car (cdr (cdr event)))))))
156 (defun w32-drag-n-drop-other-frame (event)
157 "Edit the files listed in the drag-n-drop EVENT, in other frames.
158 May create new frames, or reuse existing ones. The frame editing
159 the last file dropped is selected."
160 (interactive "e")
161 (w32-drag-n-drop event t))
163 ;; Bind the drag-n-drop event.
164 (global-set-key [drag-n-drop] 'w32-drag-n-drop)
165 (global-set-key [C-drag-n-drop] 'w32-drag-n-drop-other-frame)
167 ;; Keyboard layout/language change events
168 ;; For now ignore language-change events; in the future
169 ;; we should switch the Emacs Input Method to match the
170 ;; new layout/language selected by the user.
171 (global-set-key [language-change] 'ignore)
173 (defvar x-resource-name)
176 ;;;; Function keys
178 ;;; make f10 activate the real menubar rather than the mini-buffer menu
179 ;;; navigation feature.
180 (defun w32-menu-bar-open (&optional frame)
181 "Start key navigation of the menu bar in FRAME.
183 This initially activates the first menu-bar item, and you can then navigate
184 with the arrow keys, select a menu entry with the Return key or cancel with
185 the Escape key. If FRAME has no menu bar, this function does nothing.
187 If FRAME is nil or not given, use the selected frame.
188 If FRAME does not have the menu bar enabled, display a text menu using
189 `tmm-menubar'."
190 (interactive "i")
191 (if menu-bar-mode
192 (w32-send-sys-command ?\xf100 frame)
193 (with-selected-frame (or frame (selected-frame))
194 (tmm-menubar))))
197 ;; W32 systems have different fonts than commonly found on X, so
198 ;; we define our own standard fontset here.
199 (defvar w32-standard-fontset-spec
200 "-*-Courier New-normal-r-*-*-13-*-*-*-c-*-fontset-standard"
201 "String of fontset spec of the standard fontset.
202 This defines a fontset consisting of the Courier New variations for
203 European languages which are distributed with Windows as
204 \"Multilanguage Support\".
206 See the documentation of `create-fontset-from-fontset-spec' for the format.")
208 (defun w32-win-suspend-error ()
209 "Report an error when a suspend is attempted."
210 (error "Suspending an Emacs running under W32 makes no sense"))
212 (defvar dynamic-library-alist)
213 (defvar libpng-version) ; image.c #ifdef HAVE_NTGUI
214 (defvar libgif-version)
215 (defvar libjpeg-version)
217 ;;; Set default known names for external libraries
218 (setq dynamic-library-alist
219 (list
220 '(xpm "libxpm.dll" "xpm4.dll" "libXpm-nox4.dll")
221 ;; Versions of libpng 1.4.x and later are incompatible with
222 ;; earlier versions. Set up the list of libraries according to
223 ;; the version we were compiled against. (If we were compiled
224 ;; without PNG support, libpng-version's value is -1.)
225 (if (>= libpng-version 10400)
226 (let ((major (/ libpng-version 10000))
227 (minor (mod (/ libpng-version 100) 10)))
228 (list 'png
229 ;; libpngXY.dll is the default name when building
230 ;; with CMake or from a lpngXYY tarball on w32,
231 ;; libpngXY-XY.dll is the DLL name when building
232 ;; with libtool / autotools
233 (format "libpng%d%d.dll" major minor)
234 (format "libpng%d%d-%d%d.dll" major minor major minor)))
235 '(png "libpng12d.dll" "libpng12.dll" "libpng3.dll" "libpng.dll"
236 ;; these are libpng 1.2.8 from GTK+
237 "libpng13d.dll" "libpng13.dll"))
238 '(tiff "libtiff-5.dll" "libtiff3.dll" "libtiff.dll")
239 (if (> libjpeg-version 62)
240 ;; Versions of libjpeg after 6b are incompatible with
241 ;; earlier versions, and each of versions 7, 8, and 9 is
242 ;; also incompatible with the preceding ones (the core data
243 ;; structures used for communications with the library
244 ;; gained additional members with each new version). So we
245 ;; must use only the version of the library which Emacs was
246 ;; compiled against.
247 (list 'jpeg (format "libjpeg-%d.dll" (/ libjpeg-version 10)))
248 '(jpeg "jpeg62.dll" "libjpeg.dll" "jpeg-62.dll" "jpeg.dll"))
249 ;; Versions of giflib 5.0.0 and later changed signatures of
250 ;; several functions used by Emacs, which makes those versions
251 ;; incompatible with previous ones. We select the correct
252 ;; libraries according to the version of giflib we were
253 ;; compiled against. (If we were compiled without GIF support,
254 ;; libgif-version's value is -1.)
255 (if (>= libgif-version 50100)
256 ;; Yes, giflib 5.0 uses 6 as the major version of the API,
257 ;; and giflib 5.1 uses 7, thus "libgif-7.dll" and
258 ;; "libgif-6.dll" below (giflib 4.x used 5 as the major API
259 ;; version). giflib5.dll is from the lua-files project,
260 ;; and gif.dll is from luapower.
261 '(gif "libgif-7.dll")
262 (if (>= libgif-version 50000)
263 '(gif "libgif-6.dll" "giflib5.dll" "gif.dll")
264 '(gif "libgif-5.dll" "giflib4.dll" "libungif4.dll" "libungif.dll")))
265 '(svg "librsvg-2-2.dll")
266 '(gdk-pixbuf "libgdk_pixbuf-2.0-0.dll")
267 '(glib "libglib-2.0-0.dll")
268 '(gobject "libgobject-2.0-0.dll")
269 '(gnutls "libgnutls-28.dll" "libgnutls-26.dll")
270 '(libxml2 "libxml2-2.dll" "libxml2.dll")
271 '(zlib "zlib1.dll" "libz-1.dll")))
273 ;;; multi-tty support
274 (defvar w32-initialized nil
275 "Non-nil if the w32 window system has been initialized.")
277 (declare-function x-open-connection "w32fns.c"
278 (display &optional xrm-string must-succeed))
279 (declare-function create-default-fontset "fontset" ())
280 (declare-function create-fontset-from-fontset-spec "fontset"
281 (fontset-spec &optional style-variant noerror))
282 (declare-function create-fontset-from-x-resource "fontset" ())
283 (declare-function x-get-resource "frame.c"
284 (attribute class &optional component subclass))
285 (declare-function x-handle-args "common-win" (args))
286 (declare-function x-parse-geometry "frame.c" (string))
287 (defvar x-command-line-resources)
289 (defun w32-initialize-window-system (&optional _display)
290 "Initialize Emacs for W32 GUI frames."
291 (cl-assert (not w32-initialized))
293 ;; Do the actual Windows setup here; the above code just defines
294 ;; functions and variables that we use now.
296 (setq command-line-args (x-handle-args command-line-args))
298 ;; Make sure we have a valid resource name.
299 (or (stringp x-resource-name)
300 (setq x-resource-name
301 ;; Change any . or * characters in x-resource-name to hyphens,
302 ;; so as not to choke when we use it in X resource queries.
303 (replace-regexp-in-string "[.*]" "-" (invocation-name))))
305 (x-open-connection "w32" x-command-line-resources
306 ;; Exit with a fatal error if this fails and we
307 ;; are the initial display
308 (eq initial-window-system 'w32))
310 ;; Create the default fontset.
311 (create-default-fontset)
312 ;; Create the standard fontset.
313 (condition-case err
314 (create-fontset-from-fontset-spec w32-standard-fontset-spec t)
315 (error (display-warning
316 'initialization
317 (format "Creation of the standard fontset failed: %s" err)
318 :error)))
319 ;; Create fontset specified in X resources "Fontset-N" (N is 0, 1,...).
320 (create-fontset-from-x-resource)
322 ;; Apply a geometry resource to the initial frame. Put it at the end
323 ;; of the alist, so that anything specified on the command line takes
324 ;; precedence.
325 (let* ((res-geometry (x-get-resource "geometry" "Geometry"))
326 parsed)
327 (if res-geometry
328 (progn
329 (setq parsed (x-parse-geometry res-geometry))
330 ;; If the resource specifies a position,
331 ;; call the position and size "user-specified".
332 (if (or (assq 'top parsed) (assq 'left parsed))
333 (setq parsed (cons '(user-position . t)
334 (cons '(user-size . t) parsed))))
335 ;; All geometry parms apply to the initial frame.
336 (setq initial-frame-alist (append initial-frame-alist parsed))
337 ;; The size parms apply to all frames.
338 (if (and (assq 'height parsed)
339 (not (assq 'height default-frame-alist)))
340 (setq default-frame-alist
341 (cons (cons 'height (cdr (assq 'height parsed)))
342 default-frame-alist))
343 (if (and (assq 'width parsed)
344 (not (assq 'width default-frame-alist)))
345 (setq default-frame-alist
346 (cons (cons 'width (cdr (assq 'width parsed)))
347 default-frame-alist)))))))
349 ;; Check the reverseVideo resource.
350 (let ((case-fold-search t))
351 (let ((rv (x-get-resource "reverseVideo" "ReverseVideo")))
352 (if (and rv (string-match "^\\(true\\|yes\\|on\\)$" rv))
353 (setq default-frame-alist
354 (cons '(reverse . t) default-frame-alist)))))
356 ;; Don't let Emacs suspend under Windows.
357 (add-hook 'suspend-hook #'w32-win-suspend-error)
359 ;; Turn off window-splitting optimization; w32 is usually fast enough
360 ;; that this is only annoying.
361 (setq split-window-keep-point t)
363 ;; W32 expects the menu bar cut and paste commands to use the clipboard.
364 (menu-bar-enable-clipboard)
366 ;; Don't show the frame name; that's redundant.
367 (setq-default mode-line-frame-identification " ")
369 ;; Set to a system sound if you want a fancy bell.
370 (set-message-beep 'ok)
371 (x-apply-session-resources)
372 (setq w32-initialized t))
374 (add-to-list 'display-format-alist '("\\`w32\\'" . w32))
375 (gui-method-define handle-args-function w32 #'x-handle-args)
376 (gui-method-define frame-creation-function w32
377 #'x-create-frame-with-faces)
378 (gui-method-define window-system-initialization w32
379 #'w32-initialize-window-system)
381 ;;;; Selections
383 (declare-function w32-set-clipboard-data "w32select.c"
384 (string &optional ignored))
385 (declare-function w32-get-clipboard-data "w32select.c")
386 (declare-function w32-selection-exists-p "w32select.c")
388 ;;; Fix interface to (X-specific) mouse.el
389 (defun w32--set-selection (type value)
390 (if (eq type 'CLIPBOARD)
391 (w32-set-clipboard-data value)
392 (put 'x-selections (or type 'PRIMARY) value)))
394 (defun w32--get-selection (&optional type data-type)
395 (if (and (eq type 'CLIPBOARD)
396 (eq data-type 'STRING))
397 (with-demoted-errors "w32-get-clipboard-data:%S"
398 (w32-get-clipboard-data))
399 (get 'x-selections (or type 'PRIMARY))))
401 (defun w32--selection-owner-p (selection)
402 (and (memq selection '(nil PRIMARY SECONDARY))
403 (get 'x-selections (or selection 'PRIMARY))))
405 (gui-method-define gui-set-selection w32 #'w32--set-selection)
406 (gui-method-define gui-get-selection w32 #'w32--get-selection)
408 (gui-method-define gui-selection-owner-p w32 #'w32--selection-owner-p)
409 (gui-method-define gui-selection-exists-p w32 #'w32-selection-exists-p)
411 (when (eq system-type 'windows-nt)
412 ;; Make copy&pasting in w32's console interact with the system's clipboard!
413 (gui-method-define gui-set-selection nil #'w32--set-selection)
414 (gui-method-define gui-get-selection nil #'w32--get-selection)
415 (gui-method-define gui-selection-owner-p nil #'w32--selection-owner-p)
416 (gui-method-define gui-selection-exists-p nil #'w32-selection-exists-p))
418 ;; The "Windows" keys on newer keyboards bring up the Start menu
419 ;; whether you want it or not - make Emacs ignore these keystrokes
420 ;; rather than beep.
421 (global-set-key [lwindow] 'ignore)
422 (global-set-key [rwindow] 'ignore)
424 (declare-function x-server-version "w32fns.c" (&optional terminal))
426 (defun w32-version ()
427 "Return the MS-Windows version numbers.
428 The value is a list of three integers: the major and minor version
429 numbers, and the build number."
430 (x-server-version))
432 (defun w32-using-nt ()
433 "Return non-nil if running on a Windows NT descendant.
434 That includes all Windows systems except for 9X/Me."
435 (getenv "SystemRoot"))
437 (provide 'w32-win)
439 ;;; w32-win.el ends here