Allow format args for y-or-n-p and yes-or-no-p.
[bpt/emacs.git] / lisp / w32-fns.el
CommitLineData
e8af40ee 1;;; w32-fns.el --- Lisp routines for Windows NT
b578f267 2
597e2240 3;; Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
114f9c96 4;; 2009, 2010 Free Software Foundation, Inc.
95ed0025 5
68429d86 6;; Author: Geoff Voelker <voelker@cs.washington.edu>
284b3043 7;; Keywords: internal
bd78fa1d 8;; Package: emacs
95ed0025
RS
9
10;; This file is part of GNU Emacs.
11
eb3fa2cf 12;; GNU Emacs is free software: you can redistribute it and/or modify
95ed0025 13;; it under the terms of the GNU General Public License as published by
eb3fa2cf
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
95ed0025
RS
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
eb3fa2cf 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
95ed0025
RS
24
25;;; Commentary:
26
95ed0025
RS
27
28;;; Code:
2f42c75f 29(require 'w32-vars)
95ed0025 30
b5de9ae0
JB
31(defvar explicit-shell-file-name)
32
716eb574
JR
33;;;; Function keys
34
73e6adaa
DN
35(declare-function set-message-beep "w32console.c")
36(declare-function w32-get-clipboard-data "w32select.c")
37(declare-function w32-get-locale-info "w32proc.c")
38(declare-function w32-get-valid-locale-ids "w32proc.c")
39(declare-function w32-set-clipboard-data "w32select.c")
81b38822 40
bcaf1c36 41;; Map all versions of a filename (8.3, longname, mixed case) to the
d234707d
GV
42;; same buffer.
43(setq find-file-visit-truename t)
44
aa360da1
GM
45(declare-function x-server-version "w32fns.c" (&optional display))
46
161ab819
AI
47(defun w32-version ()
48 "Return the MS-Windows version numbers.
49The value is a list of three integers: the major and minor version
50numbers, and the build number."
51 (x-server-version))
52
ee82af56 53(defun w32-using-nt ()
4ff64219 54 "Return non-nil if running on a Windows NT descendant.
49bd2bfe 55That includes all Windows systems except for 9X/Me."
ee82af56
GV
56 (and (eq system-type 'windows-nt) (getenv "SystemRoot")))
57
58(defun w32-shell-name ()
d234707d 59 "Return the name of the shell being used."
551ea1bd 60 (or (bound-and-true-p explicit-shell-file-name)
ee82af56
GV
61 (getenv "ESHELL")
62 (getenv "SHELL")
63 (and (w32-using-nt) "cmd.exe")
64 "command.com"))
65
d234707d
GV
66(defun w32-system-shell-p (shell-name)
67 (and shell-name
bcaf1c36 68 (member (downcase (file-name-nondirectory shell-name))
d234707d 69 w32-system-shells)))
ee82af56 70
f3e62da2 71(defun w32-shell-dos-semantics ()
8fc29035 72 "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
f3e62da2
GV
73 (or (w32-system-shell-p (w32-shell-name))
74 (and (member (downcase (file-name-nondirectory (w32-shell-name)))
75 '("cmdproxy" "cmdproxy.exe"))
76 (w32-system-shell-p (getenv "COMSPEC")))))
77
200fe89b
DN
78(defvar w32-quote-process-args) ;; defined in w32proc.c
79
d234707d 80(defun w32-check-shell-configuration ()
72ab66eb 81 "Check the configuration of shell variables on Windows.
ee82af56 82This function is invoked after loading the init files and processing
d234707d
GV
83the command line arguments. It issues a warning if the user or site
84has configured the shell with inappropriate settings."
85f568ec 85 (interactive)
d234707d
GV
86 (let ((prev-buffer (current-buffer))
87 (buffer (get-buffer-create "*Shell Configuration*"))
88 (system-shell))
89 (set-buffer buffer)
90 (erase-buffer)
91 (if (w32-system-shell-p (getenv "ESHELL"))
92 (insert (format "Warning! The ESHELL environment variable uses %s.
bcaf1c36 93You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
d234707d
GV
94 (getenv "ESHELL"))))
95 (if (w32-system-shell-p (getenv "SHELL"))
96 (insert (format "Warning! The SHELL environment variable uses %s.
bcaf1c36 97You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
d234707d
GV
98 (getenv "SHELL"))))
99 (if (w32-system-shell-p shell-file-name)
100 (insert (format "Warning! shell-file-name uses %s.
bcaf1c36 101You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
d234707d
GV
102 shell-file-name)))
103 (if (and (boundp 'explicit-shell-file-name)
104 (w32-system-shell-p explicit-shell-file-name))
105 (insert (format "Warning! explicit-shell-file-name uses %s.
106You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
107 explicit-shell-file-name)))
108 (setq system-shell (> (buffer-size) 0))
85f568ec
GV
109
110 ;; Allow user to specify that they really do want to use one of the
111 ;; "system" shells, despite the drawbacks, but still warn if
112 ;; shell-command-switch doesn't match.
113 (if w32-allow-system-shell
114 (erase-buffer))
115
d234707d
GV
116 (cond (system-shell
117 ;; System shells.
118 (if (string-equal "-c" shell-command-switch)
119 (insert "Warning! shell-command-switch is \"-c\".
120You should set this to \"/c\" when using a system shell.\n\n"))
121 (if w32-quote-process-args
122 (insert "Warning! w32-quote-process-args is t.
123You should set this to nil when using a system shell.\n\n")))
124 ;; Non-system shells.
125 (t
126 (if (string-equal "/c" shell-command-switch)
127 (insert "Warning! shell-command-switch is \"/c\".
128You should set this to \"-c\" when using a non-system shell.\n\n"))
129 (if (not w32-quote-process-args)
130 (insert "Warning! w32-quote-process-args is nil.
131You should set this to t when using a non-system shell.\n\n"))))
132 (if (> (buffer-size) 0)
133 (display-buffer buffer)
134 (kill-buffer buffer))
135 (set-buffer prev-buffer)))
136
137(add-hook 'after-init-hook 'w32-check-shell-configuration)
138
f2b256fa 139;; Override setting chosen at startup.
b1ed8648
AI
140(defun set-default-process-coding-system ()
141 ;; Most programs on Windows will accept Unix line endings on input
142 ;; (and some programs ported from Unix require it) but most will
143 ;; produce DOS line endings on output.
144 (setq default-process-coding-system
597e2240 145 (if (default-value 'enable-multibyte-characters)
b1ed8648
AI
146 '(undecided-dos . undecided-unix)
147 '(raw-text-dos . raw-text-unix)))
a272a739
JR
148 ;; Make cmdproxy default to using DOS line endings for input,
149 ;; because some Windows programs (including command.com) require it.
150 (add-to-list 'process-coding-system-alist
151 `("[cC][mM][dD][pP][rR][oO][xX][yY]"
597e2240 152 . ,(if (default-value 'enable-multibyte-characters)
a272a739
JR
153 '(undecided-dos . undecided-dos)
154 '(raw-text-dos . raw-text-dos))))
155 ;; plink needs DOS input when entering the password.
156 (add-to-list 'process-coding-system-alist
157 `("[pP][lL][iI][nN][kK]"
597e2240 158 . ,(if (default-value 'enable-multibyte-characters)
a272a739
JR
159 '(undecided-dos . undecided-dos)
160 '(raw-text-dos . raw-text-dos)))))
b1ed8648
AI
161
162(add-hook 'before-init-hook 'set-default-process-coding-system)
163
85f568ec
GV
164
165;;; Basic support functions for managing Emacs' locale setting
166
167(defvar w32-valid-locales nil
168 "List of locale ids known to be supported.")
169
f2b256fa
SM
170;; This is the brute-force version; an efficient version is now
171;; built-in though.
85f568ec
GV
172(if (not (fboundp 'w32-get-valid-locale-ids))
173 (defun w32-get-valid-locale-ids ()
174 "Return list of all valid Windows locale ids."
175 (let ((i 65535)
176 locales)
177 (while (> i 0)
178 (if (w32-get-locale-info i)
179 (setq locales (cons i locales)))
180 (setq i (1- i)))
181 locales)))
182
183(defun w32-list-locales ()
184 "List the name and id of all locales supported by Windows."
185 (interactive)
ac62ec16
JB
186 (when (null w32-valid-locales)
187 (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
555e23cf
JB
188 (with-output-to-temp-buffer "*Supported Locales*"
189 (princ "LCID\tAbbrev\tFull name\n\n")
011a0143
JB
190 (dolist (locale w32-valid-locales)
191 (princ (format "%d\t%s\t%s\n"
192 locale
193 (w32-get-locale-info locale)
194 (w32-get-locale-info locale t))))))
85f568ec 195
f2b256fa
SM
196;; Setup Info-default-directory-list to include the info directory
197;; near where Emacs executable was installed. We used to set INFOPATH,
198;; but when this is set Info-default-directory-list is ignored. We
199;; also cannot rely upon what is set in paths.el because they assume
200;; that configuration during build time is correct for runtime.
d234707d
GV
201(defun w32-init-info ()
202 (let* ((instdir (file-name-directory invocation-directory))
85f568ec 203 (dir1 (expand-file-name "../info/" instdir))
d234707d
GV
204 (dir2 (expand-file-name "../../../info/" instdir)))
205 (if (file-exists-p dir1)
bcaf1c36 206 (setq Info-default-directory-list
d234707d
GV
207 (append Info-default-directory-list (list dir1)))
208 (if (file-exists-p dir2)
209 (setq Info-default-directory-list
210 (append Info-default-directory-list (list dir2)))))))
211
212(add-hook 'before-init-hook 'w32-init-info)
3eab6a03 213
f2b256fa
SM
214;; The variable source-directory is used to initialize Info-directory-list.
215;; However, the common case is that Emacs is being used from a binary
216;; distribution, and the value of source-directory is meaningless in that
217;; case. Even worse, source-directory can refer to a directory on a drive
218;; on the build machine that happens to be a removable drive on the user's
219;; machine. When this happens, Emacs tries to access the removable drive
220;; and produces the abort/retry/ignore dialog. Since we do not use
221;; source-directory, set it to something that is a reasonable approximation
222;; on the user's machine.
223
224;;(add-hook 'before-init-hook
225;; (lambda ()
226;; (setq source-directory (file-name-as-directory
227;; (expand-file-name ".." exec-directory)))))
8929f478 228
98d8b17e 229(defun w32-convert-standard-filename (filename)
b61dfbe2 230 "Convert a standard file's name to something suitable for MS-Windows.
915b0bf0
JB
231This means to guarantee valid names and perhaps to canonicalize
232certain patterns.
233
98d8b17e
EZ
234This function is called by `convert-standard-filename'.
235
236Replace invalid characters and turn Cygwin names into native
237names, and also turn slashes into backslashes if the shell
238requires it (see `w32-shell-dos-semantics')."
45b8a401
EZ
239 (save-match-data
240 (let ((name
241 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
bcaf1c36 242 (replace-match "\\1:/" t nil filename)
45b8a401
EZ
243 (copy-sequence filename)))
244 (start 0))
245 ;; leave ':' if part of drive specifier
246 (if (and (> (length name) 1)
247 (eq (aref name 1) ?:))
248 (setq start 2))
249 ;; destructively replace invalid filename characters with !
250 (while (string-match "[?*:<>|\"\000-\037]" name start)
251 (aset name (match-beginning 0) ?!)
252 (setq start (match-end 0)))
253 ;; convert directory separators to Windows format
254 ;; (but only if the shell in use requires it)
255 (when (w32-shell-dos-semantics)
256 (setq start 0)
257 (while (string-match "/" name start)
258 (aset name (match-beginning 0) ?\\)
259 (setq start (match-end 0))))
260 name)))
4e0cd0df 261
95ed0025 262;;; Fix interface to (X-specific) mouse.el
bffcf874 263(defun x-set-selection (type data)
d977a092 264 "Make an X selection of type TYPE and value DATA.
3077d1f6
EZ
265The argument TYPE (nil means `PRIMARY') says which selection, and
266DATA specifies the contents. TYPE must be a symbol. \(It can also
267be a string, which stands for the symbol with that name, but this
268is considered obsolete.) DATA may be a string, a symbol, an
269integer (or a cons of two integers or list of two integers).
270
271The selection may also be a cons of two markers pointing to the same buffer,
272or an overlay. In these cases, the selection is considered to be the text
273between the markers *at whatever time the selection is examined*.
274Thus, editing done in the buffer after you specify the selection
275can alter the effective value of the selection.
276
277The data may also be a vector of valid non-vector selection values.
278
279The return value is DATA.
280
281Interactively, this command sets the primary selection. Without
282prefix argument, it reads the selection in the minibuffer. With
283prefix argument, it uses the text of the region as the selection value.
284
285Note that on MS-Windows, primary and secondary selections set by Emacs
286are not available to other programs."
3f5c9cad 287 (put 'x-selections (or type 'PRIMARY) data))
bffcf874
RS
288
289(defun x-get-selection (&optional type data-type)
3077d1f6
EZ
290 "Return the value of an X Windows selection.
291The argument TYPE (default `PRIMARY') says which selection,
292and the argument DATA-TYPE (default `STRING') says
293how to convert the data.
294
295TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
296only a few symbols are commonly used. They conventionally have
297all upper-case names. The most often used ones, in addition to
298`PRIMARY', are `SECONDARY' and `CLIPBOARD'.
299
300DATA-TYPE is usually `STRING', but can also be one of the symbols
301in `selection-converter-alist', which see."
3f5c9cad
CY
302 (get 'x-selections (or type 'PRIMARY)))
303
304;; x-selection-owner-p is used in simple.el
305(defun x-selection-owner-p (&optional type)
306 (and (memq type '(nil PRIMARY SECONDARY))
307 (get 'x-selections (or type 'PRIMARY))))
bffcf874 308
1b42a753 309(defun set-w32-system-coding-system (coding-system)
49bd2bfe 310 "Set the coding system used by the Windows system to CODING-SYSTEM.
1b42a753 311This is used for things like passing font names with non-ASCII
551ea1bd 312characters in them to the system. For a list of possible values of
086ceb30
JR
313CODING-SYSTEM, use \\[list-coding-systems].
314
315This function is provided for backward compatibility, since
49bd2bfe 316`w32-system-coding-system' is now an alias for `locale-coding-system'."
1b42a753 317 (interactive
086ceb30 318 (list (let ((default locale-coding-system))
1b42a753 319 (read-coding-system
5b76833f 320 (format "Coding system for system calls (default %s): "
1b42a753
GV
321 default)
322 default))))
323 (check-coding-system coding-system)
086ceb30
JR
324 (setq locale-coding-system coding-system))
325
326;; locale-coding-system was introduced to do the same thing as
327;; w32-system-coding-system. Use that instead.
328(defvaralias 'w32-system-coding-system 'locale-coding-system)
1b42a753 329
f2b256fa 330;; Set to a system sound if you want a fancy bell.
81b38822
KH
331(set-message-beep nil)
332
f2b256fa
SM
333;; The "Windows" keys on newer keyboards bring up the Start menu
334;; whether you want it or not - make Emacs ignore these keystrokes
335;; rather than beep.
d234707d
GV
336(global-set-key [lwindow] 'ignore)
337(global-set-key [rwindow] 'ignore)
338
15fa6efb
JR
339(defun w32-add-charset-info (xlfd-charset windows-charset codepage)
340 "Function to add character sets to display with Windows fonts.
341Creates entries in `w32-charset-info-alist'.
342XLFD-CHARSET is a string which will appear in the XLFD font name to
72ab66eb 343identify the character set. WINDOWS-CHARSET is a symbol identifying
551ea1bd
JB
344the Windows character set this maps to. For the list of possible
345values, see the documentation for `w32-charset-info-alist'. CODEPAGE
15fa6efb
JR
346can be a numeric codepage that Windows uses to display the character
347set, t for Unicode output with no codepage translation or nil for 8
348bit output with no translation."
349 (add-to-list 'w32-charset-info-alist
72ab66eb 350 (cons xlfd-charset (cons windows-charset codepage))))
15fa6efb 351
4c7ec703
JR
352;; The last charset we add becomes the "preferred" charset for the return
353;; value from w32-select-font etc, so list the most important charsets last.
15fa6efb
JR
354(w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
355(w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
2005121f
JR
356;; The following two are included for pattern matching.
357(w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
358(w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
15fa6efb
JR
359(w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
360(w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
751be31e
JR
361(w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
362(w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
363(w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
15fa6efb
JR
364(w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
365(w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
366(w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
57016d37
JR
367(w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
368(w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
369(w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
370(w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
371(w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
372(w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
373(w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
374(w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
375(w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
376(w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
18885c11 377(w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
57016d37
JR
378(w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
379(w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
7992f52c
JR
380(w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
381(w32-add-charset-info "iso10646-1" 'w32-charset-default t)
57016d37 382
2005121f
JR
383;; ;; If unicode windows charset is not defined, use ansi fonts.
384;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
385
386;; Prefered names
387(w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
388(w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
389(w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
390(w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
391(w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
4c7ec703 392(w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
52f32671 393
47092217
JR
394(make-obsolete-variable 'w32-enable-italics
395 'w32-enable-synthesized-fonts "21.1")
d4b7d6b4 396(make-obsolete-variable 'w32-charset-to-codepage-alist
47092217 397 'w32-charset-info-alist "21.1")
d4b7d6b4 398
3b4cf5db 399\f
45240125 400;;;; Selections
3b4cf5db 401
f2b256fa
SM
402;; We keep track of the last text selected here, so we can check the
403;; current selection against it, and avoid passing back our own text
6d7cc563 404;; from x-selection-value.
3b4cf5db
AI
405(defvar x-last-selected-text nil)
406
3b4cf5db
AI
407(defun x-get-selection-value ()
408 "Return the value of the current selection.
45240125 409Consult the selection. Treat empty strings as if they were unset."
3b4cf5db
AI
410 (if x-select-enable-clipboard
411 (let (text)
412 ;; Don't die if x-get-selection signals an error.
413 (condition-case c
414 (setq text (w32-get-clipboard-data))
415 (error (message "w32-get-clipboard-data:%s" c)))
416 (if (string= text "") (setq text nil))
417 (cond
418 ((not text) nil)
419 ((eq text x-last-selected-text) nil)
420 ((string= text x-last-selected-text)
421 ;; Record the newer string, so subsequent calls can use the 'eq' test.
422 (setq x-last-selected-text text)
423 nil)
424 (t
425 (setq x-last-selected-text text))))))
426\f
6d7cc563 427(defalias 'x-selection-value 'x-get-selection-value)
3b4cf5db 428
f2b256fa 429;; Arrange for the kill and yank functions to set and check the clipboard.
3b4cf5db
AI
430(setq interprogram-cut-function 'x-select-text)
431(setq interprogram-paste-function 'x-get-selection-value)
432
3712a0b3
EZ
433\f
434;;;; Support for build process
435(defun w32-batch-update-autoloads ()
436 "Like `batch-update-autoloads', but takes the name of the autoloads file
437from the command line.
438
439This is required because some Windows build environments, such as MSYS,
440munge command-line arguments that include file names to a horrible mess
441that Emacs is unable to cope with."
442 (let ((generated-autoload-file
af9fb32f
GM
443 (expand-file-name (pop command-line-args-left)))
444 ;; I can only assume the same considerations may apply here...
445 (autoload-make-program (pop command-line-args-left)))
3712a0b3 446 (batch-update-autoloads)))
3b4cf5db 447
d1dcf3e7
EZ
448(defun w32-append-code-lines (orig extra)
449 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
450
451This function saves all buffers and kills the Emacs session, without asking
452for any permissions.
453
454This is required because the Windows build environment is not required
455to include Sed, which is used by leim/Makefile.in to do the job."
456 (find-file orig)
457 (goto-char (point-max))
458 (insert-file-contents extra)
459 (delete-matching-lines "^$\\|^;")
460 (save-buffers-kill-emacs t))
461
a0d14345 462;;; w32-fns.el ends here