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