* files.el (directory-files-no-dot-files-regexp): Doc fix (bug#6298).
[emacs.git] / lisp / w32-fns.el
blob0b97b184d22638eebc7e60015b682c851156827f
1 ;;; w32-fns.el --- Lisp routines for Windows NT
3 ;; Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 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 w32-convert-standard-filename (filename)
257 "Convert a standard file's name to something suitable for the MS-Windows.
258 This means to guarantee valid names and perhaps to canonicalize
259 certain patterns.
261 This function is called by `convert-standard-filename'.
263 Replace invalid characters and turn Cygwin names into native
264 names, and also turn slashes into backslashes if the shell
265 requires it (see `w32-shell-dos-semantics')."
266 (save-match-data
267 (let ((name
268 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
269 (replace-match "\\1:/" t nil filename)
270 (copy-sequence filename)))
271 (start 0))
272 ;; leave ':' if part of drive specifier
273 (if (and (> (length name) 1)
274 (eq (aref name 1) ?:))
275 (setq start 2))
276 ;; destructively replace invalid filename characters with !
277 (while (string-match "[?*:<>|\"\000-\037]" name start)
278 (aset name (match-beginning 0) ?!)
279 (setq start (match-end 0)))
280 ;; convert directory separators to Windows format
281 ;; (but only if the shell in use requires it)
282 (when (w32-shell-dos-semantics)
283 (setq start 0)
284 (while (string-match "/" name start)
285 (aset name (match-beginning 0) ?\\)
286 (setq start (match-end 0))))
287 name)))
289 ;;; Fix interface to (X-specific) mouse.el
290 (defun x-set-selection (type data)
291 "Make an X selection of type TYPE and value DATA.
292 The argument TYPE (nil means `PRIMARY') says which selection, and
293 DATA specifies the contents. TYPE must be a symbol. \(It can also
294 be a string, which stands for the symbol with that name, but this
295 is considered obsolete.) DATA may be a string, a symbol, an
296 integer (or a cons of two integers or list of two integers).
298 The selection may also be a cons of two markers pointing to the same buffer,
299 or an overlay. In these cases, the selection is considered to be the text
300 between the markers *at whatever time the selection is examined*.
301 Thus, editing done in the buffer after you specify the selection
302 can alter the effective value of the selection.
304 The data may also be a vector of valid non-vector selection values.
306 The return value is DATA.
308 Interactively, this command sets the primary selection. Without
309 prefix argument, it reads the selection in the minibuffer. With
310 prefix argument, it uses the text of the region as the selection value.
312 Note that on MS-Windows, primary and secondary selections set by Emacs
313 are not available to other programs."
314 (put 'x-selections (or type 'PRIMARY) data))
316 (defun x-get-selection (&optional type data-type)
317 "Return the value of an X Windows selection.
318 The argument TYPE (default `PRIMARY') says which selection,
319 and the argument DATA-TYPE (default `STRING') says
320 how to convert the data.
322 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
323 only a few symbols are commonly used. They conventionally have
324 all upper-case names. The most often used ones, in addition to
325 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
327 DATA-TYPE is usually `STRING', but can also be one of the symbols
328 in `selection-converter-alist', which see."
329 (get 'x-selections (or type 'PRIMARY)))
331 ;; x-selection-owner-p is used in simple.el
332 (defun x-selection-owner-p (&optional type)
333 (and (memq type '(nil PRIMARY SECONDARY))
334 (get 'x-selections (or type 'PRIMARY))))
336 (defun set-w32-system-coding-system (coding-system)
337 "Set the coding system used by the Windows system to CODING-SYSTEM.
338 This is used for things like passing font names with non-ASCII
339 characters in them to the system. For a list of possible values of
340 CODING-SYSTEM, use \\[list-coding-systems].
342 This function is provided for backward compatibility, since
343 `w32-system-coding-system' is now an alias for `locale-coding-system'."
344 (interactive
345 (list (let ((default locale-coding-system))
346 (read-coding-system
347 (format "Coding system for system calls (default %s): "
348 default)
349 default))))
350 (check-coding-system coding-system)
351 (setq locale-coding-system coding-system))
353 ;; locale-coding-system was introduced to do the same thing as
354 ;; w32-system-coding-system. Use that instead.
355 (defvaralias 'w32-system-coding-system 'locale-coding-system)
357 ;; Set to a system sound if you want a fancy bell.
358 (set-message-beep nil)
360 ;; The "Windows" keys on newer keyboards bring up the Start menu
361 ;; whether you want it or not - make Emacs ignore these keystrokes
362 ;; rather than beep.
363 (global-set-key [lwindow] 'ignore)
364 (global-set-key [rwindow] 'ignore)
366 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
367 "Function to add character sets to display with Windows fonts.
368 Creates entries in `w32-charset-info-alist'.
369 XLFD-CHARSET is a string which will appear in the XLFD font name to
370 identify the character set. WINDOWS-CHARSET is a symbol identifying
371 the Windows character set this maps to. For the list of possible
372 values, see the documentation for `w32-charset-info-alist'. CODEPAGE
373 can be a numeric codepage that Windows uses to display the character
374 set, t for Unicode output with no codepage translation or nil for 8
375 bit output with no translation."
376 (add-to-list 'w32-charset-info-alist
377 (cons xlfd-charset (cons windows-charset codepage))))
379 ;; The last charset we add becomes the "preferred" charset for the return
380 ;; value from w32-select-font etc, so list the most important charsets last.
381 (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
382 (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
383 ;; The following two are included for pattern matching.
384 (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
385 (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
386 (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
387 (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
388 (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
389 (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
390 (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
391 (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
392 (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
393 (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
394 (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
395 (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
396 (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
397 (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
398 (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
399 (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
400 (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
401 (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
402 (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
403 (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
404 (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
405 (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
406 (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
407 (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
408 (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
410 ;; ;; If unicode windows charset is not defined, use ansi fonts.
411 ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
413 ;; Prefered names
414 (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
415 (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
416 (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
417 (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
418 (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
419 (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
421 (make-obsolete-variable 'w32-enable-italics
422 'w32-enable-synthesized-fonts "21.1")
423 (make-obsolete-variable 'w32-charset-to-codepage-alist
424 'w32-charset-info-alist "21.1")
427 ;;;; Selections and cut buffers
429 ;; We keep track of the last text selected here, so we can check the
430 ;; current selection against it, and avoid passing back our own text
431 ;; from x-cut-buffer-or-selection-value.
432 (defvar x-last-selected-text nil)
434 ;; It is said that overlarge strings are slow to put into the cut buffer.
435 ;; Note this value is overridden below.
436 (defvar x-cut-buffer-max 20000
437 "Max number of characters to put in the cut buffer.")
439 (defun x-select-text (text &optional push)
440 "Select TEXT, a string, according to the window system.
442 On X, put TEXT in the primary X selection. For backward
443 compatibility with older X applications, set the value of X cut
444 buffer 0 as well, and if the optional argument PUSH is non-nil,
445 rotate the cut buffers. If `x-select-enable-clipboard' is
446 non-nil, copy the text to the X clipboard as well.
448 On Windows, make TEXT the current selection. If
449 `x-select-enable-clipboard' is non-nil, copy the text to the
450 clipboard as well. The argument PUSH is ignored.
452 On Nextstep, put TEXT in the pasteboard; PUSH is ignored."
453 (if x-select-enable-clipboard
454 (w32-set-clipboard-data text))
455 (setq x-last-selected-text text))
457 (defun x-get-selection-value ()
458 "Return the value of the current selection.
459 Consult the selection, then the cut buffer. Treat empty strings as if
460 they were unset."
461 (if x-select-enable-clipboard
462 (let (text)
463 ;; Don't die if x-get-selection signals an error.
464 (condition-case c
465 (setq text (w32-get-clipboard-data))
466 (error (message "w32-get-clipboard-data:%s" c)))
467 (if (string= text "") (setq text nil))
468 (cond
469 ((not text) nil)
470 ((eq text x-last-selected-text) nil)
471 ((string= text x-last-selected-text)
472 ;; Record the newer string, so subsequent calls can use the 'eq' test.
473 (setq x-last-selected-text text)
474 nil)
476 (setq x-last-selected-text text))))))
478 (defalias 'x-cut-buffer-or-selection-value 'x-get-selection-value)
480 ;; Arrange for the kill and yank functions to set and check the clipboard.
481 (setq interprogram-cut-function 'x-select-text)
482 (setq interprogram-paste-function 'x-get-selection-value)
485 ;;;; Support for build process
486 (defun w32-batch-update-autoloads ()
487 "Like `batch-update-autoloads', but takes the name of the autoloads file
488 from the command line.
490 This is required because some Windows build environments, such as MSYS,
491 munge command-line arguments that include file names to a horrible mess
492 that Emacs is unable to cope with."
493 (let ((generated-autoload-file
494 (expand-file-name (pop command-line-args-left)))
495 ;; I can only assume the same considerations may apply here...
496 (autoload-make-program (pop command-line-args-left)))
497 (batch-update-autoloads)))
499 (defun w32-append-code-lines (orig extra)
500 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
502 This function saves all buffers and kills the Emacs session, without asking
503 for any permissions.
505 This is required because the Windows build environment is not required
506 to include Sed, which is used by leim/Makefile.in to do the job."
507 (find-file orig)
508 (goto-char (point-max))
509 (insert-file-contents extra)
510 (delete-matching-lines "^$\\|^;")
511 (save-buffers-kill-emacs t))
513 ;; arch-tag: c49b48cc-0f4f-454f-a274-c2dc34815e14
514 ;;; w32-fns.el ends here