* src/window.c (Fselect_window): Add missing return value.
[emacs.git] / lisp / w32-fns.el
blob2ece6e0c48725dac9615564c53142957001288a1
1 ;;; w32-fns.el --- Lisp routines for Windows NT
3 ;; Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010, 2011 Free Software Foundation, Inc.
6 ;; Author: Geoff Voelker <voelker@cs.washington.edu>
7 ;; Keywords: internal
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 of the License, or
14 ;; (at your option) any later version.
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. If not, see <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
27 ;;; Code:
28 (require 'w32-vars)
30 (defvar explicit-shell-file-name)
32 ;;;; Function keys
34 (defvar x-alternatives-map
35 (let ((map (make-sparse-keymap)))
36 ;; Map certain keypad keys into ASCII characters that people usually expect.
37 (define-key map [M-backspace] [?\M-\d])
38 (define-key map [M-delete] [?\M-\d])
39 (define-key map [M-tab] [?\M-\t])
40 (define-key map [M-linefeed] [?\M-\n])
41 (define-key map [M-clear] [?\M-\C-l])
42 (define-key map [M-return] [?\M-\C-m])
43 (define-key map [M-escape] [?\M-\e])
44 (define-key map [iso-lefttab] [backtab])
45 (define-key map [S-iso-lefttab] [backtab])
46 (define-key map [S-tab] [backtab])
47 map)
48 "Keymap of possible alternative meanings for some keys.")
50 (defun x-setup-function-keys (frame)
51 "Set up `function-key-map' on the graphical frame FRAME."
52 ;; Don't do this twice on the same display, or it would break
53 ;; normal-erase-is-backspace-mode.
54 (unless (terminal-parameter frame 'x-setup-function-keys)
55 ;; Map certain keypad keys into ASCII characters that people usually expect.
56 (with-selected-frame frame
57 (let ((map (copy-keymap x-alternatives-map)))
58 (set-keymap-parent map (keymap-parent local-function-key-map))
59 (set-keymap-parent local-function-key-map map)))
60 (set-terminal-parameter frame 'x-setup-function-keys t)))
62 (declare-function set-message-beep "w32console.c")
63 (declare-function w32-get-clipboard-data "w32select.c")
64 (declare-function w32-get-locale-info "w32proc.c")
65 (declare-function w32-get-valid-locale-ids "w32proc.c")
66 (declare-function w32-set-clipboard-data "w32select.c")
68 ;; Map all versions of a filename (8.3, longname, mixed case) to the
69 ;; same buffer.
70 (setq find-file-visit-truename t)
72 (declare-function x-server-version "w32fns.c" (&optional display))
74 (defun w32-version ()
75 "Return the MS-Windows version numbers.
76 The value is a list of three integers: the major and minor version
77 numbers, and the build number."
78 (x-server-version))
80 (defun w32-using-nt ()
81 "Return non-nil if running on a Windows NT descendant.
82 That includes all Windows systems except for 9X/Me."
83 (and (eq system-type 'windows-nt) (getenv "SystemRoot")))
85 (defun w32-shell-name ()
86 "Return the name of the shell being used."
87 (or (bound-and-true-p explicit-shell-file-name)
88 (getenv "ESHELL")
89 (getenv "SHELL")
90 (and (w32-using-nt) "cmd.exe")
91 "command.com"))
93 (defun w32-system-shell-p (shell-name)
94 (and shell-name
95 (member (downcase (file-name-nondirectory shell-name))
96 w32-system-shells)))
98 (defun w32-shell-dos-semantics ()
99 "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
100 (or (w32-system-shell-p (w32-shell-name))
101 (and (member (downcase (file-name-nondirectory (w32-shell-name)))
102 '("cmdproxy" "cmdproxy.exe"))
103 (w32-system-shell-p (getenv "COMSPEC")))))
105 (defvar w32-quote-process-args) ;; defined in w32proc.c
107 (defun w32-check-shell-configuration ()
108 "Check the configuration of shell variables on Windows.
109 This function is invoked after loading the init files and processing
110 the command line arguments. It issues a warning if the user or site
111 has configured the shell with inappropriate settings."
112 (interactive)
113 (let ((prev-buffer (current-buffer))
114 (buffer (get-buffer-create "*Shell Configuration*"))
115 (system-shell))
116 (set-buffer buffer)
117 (erase-buffer)
118 (if (w32-system-shell-p (getenv "ESHELL"))
119 (insert (format "Warning! The ESHELL environment variable uses %s.
120 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
121 (getenv "ESHELL"))))
122 (if (w32-system-shell-p (getenv "SHELL"))
123 (insert (format "Warning! The SHELL environment variable uses %s.
124 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
125 (getenv "SHELL"))))
126 (if (w32-system-shell-p shell-file-name)
127 (insert (format "Warning! shell-file-name uses %s.
128 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
129 shell-file-name)))
130 (if (and (boundp 'explicit-shell-file-name)
131 (w32-system-shell-p explicit-shell-file-name))
132 (insert (format "Warning! explicit-shell-file-name uses %s.
133 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
134 explicit-shell-file-name)))
135 (setq system-shell (> (buffer-size) 0))
137 ;; Allow user to specify that they really do want to use one of the
138 ;; "system" shells, despite the drawbacks, but still warn if
139 ;; shell-command-switch doesn't match.
140 (if w32-allow-system-shell
141 (erase-buffer))
143 (cond (system-shell
144 ;; System shells.
145 (if (string-equal "-c" shell-command-switch)
146 (insert "Warning! shell-command-switch is \"-c\".
147 You should set this to \"/c\" when using a system shell.\n\n"))
148 (if w32-quote-process-args
149 (insert "Warning! w32-quote-process-args is t.
150 You should set this to nil when using a system shell.\n\n")))
151 ;; Non-system shells.
153 (if (string-equal "/c" shell-command-switch)
154 (insert "Warning! shell-command-switch is \"/c\".
155 You should set this to \"-c\" when using a non-system shell.\n\n"))
156 (if (not w32-quote-process-args)
157 (insert "Warning! w32-quote-process-args is nil.
158 You should set this to t when using a non-system shell.\n\n"))))
159 (if (> (buffer-size) 0)
160 (display-buffer buffer)
161 (kill-buffer buffer))
162 (set-buffer prev-buffer)))
164 (add-hook 'after-init-hook 'w32-check-shell-configuration)
166 ;; Override setting chosen at startup.
167 (defun set-default-process-coding-system ()
168 ;; Most programs on Windows will accept Unix line endings on input
169 ;; (and some programs ported from Unix require it) but most will
170 ;; produce DOS line endings on output.
171 (setq default-process-coding-system
172 (if (default-value 'enable-multibyte-characters)
173 '(undecided-dos . undecided-unix)
174 '(raw-text-dos . raw-text-unix)))
175 ;; Make cmdproxy default to using DOS line endings for input,
176 ;; because some Windows programs (including command.com) require it.
177 (add-to-list 'process-coding-system-alist
178 `("[cC][mM][dD][pP][rR][oO][xX][yY]"
179 . ,(if (default-value 'enable-multibyte-characters)
180 '(undecided-dos . undecided-dos)
181 '(raw-text-dos . raw-text-dos))))
182 ;; plink needs DOS input when entering the password.
183 (add-to-list 'process-coding-system-alist
184 `("[pP][lL][iI][nN][kK]"
185 . ,(if (default-value 'enable-multibyte-characters)
186 '(undecided-dos . undecided-dos)
187 '(raw-text-dos . raw-text-dos)))))
189 (add-hook 'before-init-hook 'set-default-process-coding-system)
192 ;;; Basic support functions for managing Emacs' locale setting
194 (defvar w32-valid-locales nil
195 "List of locale ids known to be supported.")
197 ;; This is the brute-force version; an efficient version is now
198 ;; built-in though.
199 (if (not (fboundp 'w32-get-valid-locale-ids))
200 (defun w32-get-valid-locale-ids ()
201 "Return list of all valid Windows locale ids."
202 (let ((i 65535)
203 locales)
204 (while (> i 0)
205 (if (w32-get-locale-info i)
206 (setq locales (cons i locales)))
207 (setq i (1- i)))
208 locales)))
210 (defun w32-list-locales ()
211 "List the name and id of all locales supported by Windows."
212 (interactive)
213 (when (null w32-valid-locales)
214 (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
215 (with-output-to-temp-buffer "*Supported Locales*"
216 (princ "LCID\tAbbrev\tFull name\n\n")
217 (dolist (locale w32-valid-locales)
218 (princ (format "%d\t%s\t%s\n"
219 locale
220 (w32-get-locale-info locale)
221 (w32-get-locale-info locale t))))))
223 ;; Setup Info-default-directory-list to include the info directory
224 ;; near where Emacs executable was installed. We used to set INFOPATH,
225 ;; but when this is set Info-default-directory-list is ignored. We
226 ;; also cannot rely upon what is set in paths.el because they assume
227 ;; that configuration during build time is correct for runtime.
228 (defun w32-init-info ()
229 (let* ((instdir (file-name-directory invocation-directory))
230 (dir1 (expand-file-name "../info/" instdir))
231 (dir2 (expand-file-name "../../../info/" instdir)))
232 (if (file-exists-p dir1)
233 (setq Info-default-directory-list
234 (append Info-default-directory-list (list dir1)))
235 (if (file-exists-p dir2)
236 (setq Info-default-directory-list
237 (append Info-default-directory-list (list dir2)))))))
239 (add-hook 'before-init-hook 'w32-init-info)
241 ;; The variable source-directory is used to initialize Info-directory-list.
242 ;; However, the common case is that Emacs is being used from a binary
243 ;; distribution, and the value of source-directory is meaningless in that
244 ;; case. Even worse, source-directory can refer to a directory on a drive
245 ;; on the build machine that happens to be a removable drive on the user's
246 ;; machine. When this happens, Emacs tries to access the removable drive
247 ;; and produces the abort/retry/ignore dialog. Since we do not use
248 ;; source-directory, set it to something that is a reasonable approximation
249 ;; on the user's machine.
251 ;;(add-hook 'before-init-hook
252 ;; (lambda ()
253 ;; (setq source-directory (file-name-as-directory
254 ;; (expand-file-name ".." exec-directory)))))
256 (defun convert-standard-filename (filename)
257 "Convert a standard file's name to something suitable for the current OS.
258 This means to guarantee valid names and perhaps to canonicalize
259 certain patterns.
261 On Windows and DOS, replace invalid characters. On DOS, make
262 sure to obey the 8.3 limitations. On Windows, turn Cygwin names
263 into native names, and also turn slashes into backslashes if the
264 shell requires it (see `w32-shell-dos-semantics')."
265 (save-match-data
266 (let ((name
267 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
268 (replace-match "\\1:/" t nil filename)
269 (copy-sequence filename)))
270 (start 0))
271 ;; leave ':' if part of drive specifier
272 (if (and (> (length name) 1)
273 (eq (aref name 1) ?:))
274 (setq start 2))
275 ;; destructively replace invalid filename characters with !
276 (while (string-match "[?*:<>|\"\000-\037]" name start)
277 (aset name (match-beginning 0) ?!)
278 (setq start (match-end 0)))
279 ;; convert directory separators to Windows format
280 ;; (but only if the shell in use requires it)
281 (when (w32-shell-dos-semantics)
282 (setq start 0)
283 (while (string-match "/" name start)
284 (aset name (match-beginning 0) ?\\)
285 (setq start (match-end 0))))
286 name)))
288 ;;; Fix interface to (X-specific) mouse.el
289 (defun x-set-selection (type data)
290 "Make an X selection of type TYPE and value DATA.
291 The argument TYPE (nil means `PRIMARY') says which selection, and
292 DATA specifies the contents. TYPE must be a symbol. \(It can also
293 be a string, which stands for the symbol with that name, but this
294 is considered obsolete.) DATA may be a string, a symbol, an
295 integer (or a cons of two integers or list of two integers).
297 The selection may also be a cons of two markers pointing to the same buffer,
298 or an overlay. In these cases, the selection is considered to be the text
299 between the markers *at whatever time the selection is examined*.
300 Thus, editing done in the buffer after you specify the selection
301 can alter the effective value of the selection.
303 The data may also be a vector of valid non-vector selection values.
305 The return value is DATA.
307 Interactively, this command sets the primary selection. Without
308 prefix argument, it reads the selection in the minibuffer. With
309 prefix argument, it uses the text of the region as the selection value.
311 Note that on MS-Windows, primary and secondary selections set by Emacs
312 are not available to other programs."
313 (put 'x-selections (or type 'PRIMARY) data))
315 (defun x-get-selection (&optional type data-type)
316 "Return the value of an X Windows selection.
317 The argument TYPE (default `PRIMARY') says which selection,
318 and the argument DATA-TYPE (default `STRING') says
319 how to convert the data.
321 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
322 only a few symbols are commonly used. They conventionally have
323 all upper-case names. The most often used ones, in addition to
324 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
326 DATA-TYPE is usually `STRING', but can also be one of the symbols
327 in `selection-converter-alist', which see."
328 (get 'x-selections (or type 'PRIMARY)))
330 ;; x-selection-owner-p is used in simple.el
331 (defun x-selection-owner-p (&optional type)
332 (and (memq type '(nil PRIMARY SECONDARY))
333 (get 'x-selections (or type 'PRIMARY))))
335 (defun set-w32-system-coding-system (coding-system)
336 "Set the coding system used by the Windows system to CODING-SYSTEM.
337 This is used for things like passing font names with non-ASCII
338 characters in them to the system. For a list of possible values of
339 CODING-SYSTEM, use \\[list-coding-systems].
341 This function is provided for backward compatibility, since
342 `w32-system-coding-system' is now an alias for `locale-coding-system'."
343 (interactive
344 (list (let ((default locale-coding-system))
345 (read-coding-system
346 (format "Coding system for system calls (default %s): "
347 default)
348 default))))
349 (check-coding-system coding-system)
350 (setq locale-coding-system coding-system))
352 ;; locale-coding-system was introduced to do the same thing as
353 ;; w32-system-coding-system. Use that instead.
354 (defvaralias 'w32-system-coding-system 'locale-coding-system)
356 ;; Set to a system sound if you want a fancy bell.
357 (set-message-beep nil)
359 ;; The "Windows" keys on newer keyboards bring up the Start menu
360 ;; whether you want it or not - make Emacs ignore these keystrokes
361 ;; rather than beep.
362 (global-set-key [lwindow] 'ignore)
363 (global-set-key [rwindow] 'ignore)
365 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
366 "Function to add character sets to display with Windows fonts.
367 Creates entries in `w32-charset-info-alist'.
368 XLFD-CHARSET is a string which will appear in the XLFD font name to
369 identify the character set. WINDOWS-CHARSET is a symbol identifying
370 the Windows character set this maps to. For the list of possible
371 values, see the documentation for `w32-charset-info-alist'. CODEPAGE
372 can be a numeric codepage that Windows uses to display the character
373 set, t for Unicode output with no codepage translation or nil for 8
374 bit output with no translation."
375 (add-to-list 'w32-charset-info-alist
376 (cons xlfd-charset (cons windows-charset codepage))))
378 ;; The last charset we add becomes the "preferred" charset for the return
379 ;; value from w32-select-font etc, so list the most important charsets last.
380 (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
381 (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
382 ;; The following two are included for pattern matching.
383 (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
384 (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
385 (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
386 (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
387 (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
388 (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
389 (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
390 (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
391 (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
392 (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
393 (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
394 (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
395 (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
396 (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
397 (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
398 (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
399 (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
400 (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
401 (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
402 (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
403 (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
404 (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
405 (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
406 (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
407 (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
409 ;; ;; If unicode windows charset is not defined, use ansi fonts.
410 ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
412 ;; Prefered names
413 (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
414 (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
415 (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
416 (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
417 (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
418 (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
420 (make-obsolete-variable 'w32-enable-italics
421 'w32-enable-synthesized-fonts "21.1")
422 (make-obsolete-variable 'w32-charset-to-codepage-alist
423 'w32-charset-info-alist "21.1")
426 ;;;; Selections and cut buffers
428 ;; We keep track of the last text selected here, so we can check the
429 ;; current selection against it, and avoid passing back our own text
430 ;; from x-cut-buffer-or-selection-value.
431 (defvar x-last-selected-text nil)
433 ;; It is said that overlarge strings are slow to put into the cut buffer.
434 ;; Note this value is overridden below.
435 (defvar x-cut-buffer-max 20000
436 "Max number of characters to put in the cut buffer.")
438 (defun x-select-text (text &optional push)
439 "Select TEXT, a string, according to the window system.
441 On X, put TEXT in the primary X selection. For backward
442 compatibility with older X applications, set the value of X cut
443 buffer 0 as well, and if the optional argument PUSH is non-nil,
444 rotate the cut buffers. If `x-select-enable-clipboard' is
445 non-nil, copy the text to the X clipboard as well.
447 On Windows, make TEXT the current selection. If
448 `x-select-enable-clipboard' is non-nil, copy the text to the
449 clipboard as well. The argument PUSH is ignored.
451 On Nextstep, put TEXT in the pasteboard; PUSH is ignored."
452 (if x-select-enable-clipboard
453 (w32-set-clipboard-data text))
454 (setq x-last-selected-text text))
456 (defun x-get-selection-value ()
457 "Return the value of the current selection.
458 Consult the selection, then the cut buffer. Treat empty strings as if
459 they were unset."
460 (if x-select-enable-clipboard
461 (let (text)
462 ;; Don't die if x-get-selection signals an error.
463 (condition-case c
464 (setq text (w32-get-clipboard-data))
465 (error (message "w32-get-clipboard-data:%s" c)))
466 (if (string= text "") (setq text nil))
467 (cond
468 ((not text) nil)
469 ((eq text x-last-selected-text) nil)
470 ((string= text x-last-selected-text)
471 ;; Record the newer string, so subsequent calls can use the 'eq' test.
472 (setq x-last-selected-text text)
473 nil)
475 (setq x-last-selected-text text))))))
477 (defalias 'x-cut-buffer-or-selection-value 'x-get-selection-value)
479 ;; Arrange for the kill and yank functions to set and check the clipboard.
480 (setq interprogram-cut-function 'x-select-text)
481 (setq interprogram-paste-function 'x-get-selection-value)
484 ;;;; Support for build process
485 (defun w32-batch-update-autoloads ()
486 "Like `batch-update-autoloads', but takes the name of the autoloads file
487 from the command line.
489 This is required because some Windows build environments, such as MSYS,
490 munge command-line arguments that include file names to a horrible mess
491 that Emacs is unable to cope with."
492 (let ((generated-autoload-file
493 (expand-file-name (pop command-line-args-left)))
494 ;; I can only assume the same considerations may apply here...
495 (autoload-make-program (pop command-line-args-left)))
496 (batch-update-autoloads)))
498 (defun w32-append-code-lines (orig extra)
499 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
501 This function saves all buffers and kills the Emacs session, without asking
502 for any permissions.
504 This is required because the Windows build environment is not required
505 to include Sed, which is used by leim/Makefile.in to do the job."
506 (find-file orig)
507 (goto-char (point-max))
508 (insert-file-contents extra)
509 (delete-matching-lines "^$\\|^;")
510 (save-buffers-kill-emacs t))
512 ;; arch-tag: c49b48cc-0f4f-454f-a274-c2dc34815e14
513 ;;; w32-fns.el ends here