Output alists with dotted pair notation in .dir-locals.el
[emacs.git] / lisp / w32-fns.el
blob91fe5186bc936a88d2e7cd82ff635f5e6c3b5ef5
1 ;;; w32-fns.el --- Lisp routines for 32-bit Windows
3 ;; Copyright (C) 1994, 2001-2018 Free Software Foundation, Inc.
5 ;; Author: Geoff Voelker <voelker@cs.washington.edu>
6 ;; Keywords: internal
7 ;; Package: emacs
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 <https://www.gnu.org/licenses/>.
24 ;;; Commentary:
27 ;;; Code:
28 (require 'w32-vars)
30 (defvar explicit-shell-file-name)
32 ;;;; Function keys
34 (declare-function w32-get-locale-info "w32proc.c" (lcid &optional longform))
35 (declare-function w32-get-valid-locale-ids "w32proc.c" ())
37 (if (eq system-type 'windows-nt)
38 ;; Map all versions of a filename (8.3, longname, mixed case) to the
39 ;; same buffer.
40 (setq find-file-visit-truename t))
42 ;;;; Shells
44 (defun w32-shell-name ()
45 "Return the name of the shell being used."
46 (or (bound-and-true-p shell-file-name)
47 (getenv "ESHELL")
48 (getenv "SHELL")
49 (and (fboundp 'w32-using-nt) (w32-using-nt) "cmd.exe")
50 "command.com"))
52 (defun w32-system-shell-p (shell-name)
53 (and shell-name
54 (member (downcase (file-name-nondirectory shell-name))
55 w32-system-shells)))
57 (defun w32-shell-dos-semantics ()
58 "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
59 (or (w32-system-shell-p (w32-shell-name))
60 (and (member (downcase (file-name-nondirectory (w32-shell-name)))
61 '("cmdproxy" "cmdproxy.exe"))
62 (w32-system-shell-p (getenv "COMSPEC")))))
64 (defvar w32-quote-process-args) ;; defined in w32proc.c
66 (defun w32-check-shell-configuration ()
67 "Check the configuration of shell variables on Windows.
68 This function is invoked after loading the init files and processing
69 the command line arguments. It issues a warning if the user or site
70 has configured the shell with inappropriate settings."
71 (interactive)
72 (let ((prev-buffer (current-buffer))
73 (buffer (get-buffer-create "*Shell Configuration*"))
74 (system-shell))
75 (set-buffer buffer)
76 (erase-buffer)
77 (if (w32-system-shell-p (getenv "ESHELL"))
78 (insert (format "Warning! The ESHELL environment variable uses %s.
79 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
80 (getenv "ESHELL"))))
81 (if (w32-system-shell-p (getenv "SHELL"))
82 (insert (format "Warning! The SHELL environment variable uses %s.
83 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
84 (getenv "SHELL"))))
85 (if (w32-system-shell-p shell-file-name)
86 (insert (format "Warning! shell-file-name uses %s.
87 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
88 shell-file-name)))
89 (if (and (boundp 'explicit-shell-file-name)
90 (w32-system-shell-p explicit-shell-file-name))
91 (insert (format "Warning! explicit-shell-file-name uses %s.
92 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
93 explicit-shell-file-name)))
94 (setq system-shell (> (buffer-size) 0))
96 ;; Allow user to specify that they really do want to use one of the
97 ;; "system" shells, despite the drawbacks, but still warn if
98 ;; shell-command-switch doesn't match.
99 (if w32-allow-system-shell
100 (erase-buffer))
102 (cond (system-shell
103 ;; System shells.
104 (if (string-equal "-c" shell-command-switch)
105 (insert "Warning! shell-command-switch is \"-c\".
106 You should set this to \"/c\" when using a system shell.\n\n"))
107 (if w32-quote-process-args
108 (insert "Warning! w32-quote-process-args is t.
109 You should set this to nil when using a system shell.\n\n")))
110 ;; Non-system shells.
112 (if (string-equal "/c" shell-command-switch)
113 (insert "Warning! shell-command-switch is \"/c\".
114 You should set this to \"-c\" when using a non-system shell.\n\n"))
115 (if (not w32-quote-process-args)
116 (insert "Warning! w32-quote-process-args is nil.
117 You should set this to t when using a non-system shell.\n\n"))))
118 (if (> (buffer-size) 0)
119 (display-buffer buffer)
120 (kill-buffer buffer))
121 (set-buffer prev-buffer)))
123 (add-hook 'after-init-hook 'w32-check-shell-configuration)
125 ;;;; Coding-systems, locales, etc.
127 ;; Override setting chosen at startup.
128 (defun w32-set-default-process-coding-system ()
129 ;; Most programs on Windows will accept Unix line endings on input
130 ;; (and some programs ported from Unix require it) but most will
131 ;; produce DOS line endings on output.
132 (setq default-process-coding-system
133 '(undecided-dos . undecided-unix))
134 ;; Make cmdproxy default to using DOS line endings for input,
135 ;; because some Windows programs (including command.com) require it.
136 (add-to-list 'process-coding-system-alist
137 '("[cC][mM][dD][pP][rR][oO][xX][yY]"
138 . (undecided-dos . undecided-dos)))
139 ;; plink needs DOS input when entering the password.
140 (add-to-list 'process-coding-system-alist
141 '("[pP][lL][iI][nN][kK]"
142 . (undecided-dos . undecided-dos))))
143 (define-obsolete-function-alias 'set-default-process-coding-system
144 #'w32-set-default-process-coding-system "26.1")
145 (add-hook 'before-init-hook #'w32-set-default-process-coding-system)
148 ;;; Basic support functions for managing Emacs's locale setting
150 (defvar w32-valid-locales nil
151 "List of locale ids known to be supported.")
153 ;; This is the brute-force version; an efficient version is now
154 ;; built-in though.
155 (if (not (fboundp 'w32-get-valid-locale-ids))
156 (defun w32-get-valid-locale-ids ()
157 "Return list of all valid Windows locale ids."
158 (let ((i 65535)
159 locales)
160 (while (> i 0)
161 (if (w32-get-locale-info i)
162 (setq locales (cons i locales)))
163 (setq i (1- i)))
164 locales)))
166 (defun w32-list-locales ()
167 "List the name and id of all locales supported by Windows."
168 (interactive)
169 (when (null w32-valid-locales)
170 (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
171 (with-output-to-temp-buffer "*Supported Locales*"
172 (princ "LCID\tAbbrev\tFull name\n\n")
173 (dolist (locale w32-valid-locales)
174 (princ (format "%d\t%s\t%s\n"
175 locale
176 (w32-get-locale-info locale)
177 (w32-get-locale-info locale t))))))
179 ;; The variable source-directory is used to initialize Info-directory-list.
180 ;; However, the common case is that Emacs is being used from a binary
181 ;; distribution, and the value of source-directory is meaningless in that
182 ;; case. Even worse, source-directory can refer to a directory on a drive
183 ;; on the build machine that happens to be a removable drive on the user's
184 ;; machine. When this happens, Emacs tries to access the removable drive
185 ;; and produces the abort/retry/ignore dialog. Since we do not use
186 ;; source-directory, set it to something that is a reasonable approximation
187 ;; on the user's machine.
189 ;;(add-hook 'before-init-hook
190 ;; (lambda ()
191 ;; (setq source-directory (file-name-as-directory
192 ;; (expand-file-name ".." exec-directory)))))
194 (defun w32-set-system-coding-system (coding-system)
195 "Set the coding system used by the Windows system to CODING-SYSTEM.
196 This is used for things like passing font names with non-ASCII
197 characters in them to the system. For a list of possible values of
198 CODING-SYSTEM, use \\[list-coding-systems].
200 This function is provided for backward compatibility, since
201 `w32-system-coding-system' is now an alias for `locale-coding-system'."
202 (interactive
203 (list (let ((default locale-coding-system))
204 (read-coding-system
205 (format "Coding system for system calls (default %s): "
206 default)
207 default))))
208 (check-coding-system coding-system)
209 (setq locale-coding-system coding-system))
210 (define-obsolete-function-alias 'set-w32-system-coding-system
211 #'w32-set-system-coding-system "26.1")
213 ;; locale-coding-system was introduced to do the same thing as
214 ;; w32-system-coding-system. Use that instead.
215 (defvaralias 'w32-system-coding-system 'locale-coding-system)
217 ;; Set to a system sound if you want a fancy bell.
218 (if (fboundp 'set-message-beep) ; w32fns.c
219 (set-message-beep nil))
221 (defvar w32-charset-info-alist) ; w32font.c
223 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
224 "Function to add character sets to display with Windows fonts.
225 Creates entries in `w32-charset-info-alist'.
226 XLFD-CHARSET is a string which will appear in the XLFD font name to
227 identify the character set. WINDOWS-CHARSET is a symbol identifying
228 the Windows character set this maps to. For the list of possible
229 values, see the documentation for `w32-charset-info-alist'. CODEPAGE
230 can be a numeric codepage that Windows uses to display the character
231 set, t for Unicode output with no codepage translation or nil for 8
232 bit output with no translation."
233 (add-to-list 'w32-charset-info-alist
234 (cons xlfd-charset (cons windows-charset codepage))))
236 (when (boundp 'w32-charset-info-alist)
237 ;; The last charset we add becomes the "preferred" charset for the return
238 ;; value from x-select-font etc, so list the most important charsets last.
239 (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
240 (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
241 ;; The following two are included for pattern matching.
242 (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
243 (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
244 (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
245 (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
246 (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
247 (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
248 (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
249 (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
250 (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
251 (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
252 (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
253 (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
254 (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
255 (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
256 (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
257 (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
258 (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
259 (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
260 (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
261 (w32-add-charset-info "tis620-2533" 'w32-charset-russian 28595)
262 (w32-add-charset-info "iso8859-11" 'w32-charset-thai 874)
263 (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
264 (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
265 (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
266 (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
268 ;; ;; If Unicode Windows charset is not defined, use ansi fonts.
269 ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
271 ;; Preferred names
272 (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
273 (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
274 (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
275 (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
276 (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
277 (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252))
279 ;;;; Standard filenames
281 (defun w32-convert-standard-filename (filename)
282 "Convert a standard file's name to something suitable for MS-Windows.
283 This means to guarantee valid names and perhaps to canonicalize
284 certain patterns.
286 This function is called by `convert-standard-filename'.
288 Replace invalid characters and turn Cygwin names into native
289 names."
290 (save-match-data
291 (let ((name
292 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
293 (replace-match "\\1:/" t nil filename)
294 (copy-sequence filename)))
295 (start 0))
296 ;; leave ':' if part of drive specifier
297 (if (and (> (length name) 1)
298 (eq (aref name 1) ?:))
299 (setq start 2))
300 ;; destructively replace invalid filename characters with !
301 (while (string-match "[?*:<>|\"\000-\037]" name start)
302 (aset name (match-beginning 0) ?!)
303 (setq start (match-end 0)))
304 name)))
306 ;;;; System name and version for emacsbug.el
308 (defun w32--os-description ()
309 "Return a string describing the underlying OS and its version."
310 (let* ((w32ver (car (w32-version)))
311 (w9x-p (< w32ver 5))
312 (key (if w9x-p
313 "SOFTWARE/Microsoft/Windows/CurrentVersion"
314 "SOFTWARE/Microsoft/Windows NT/CurrentVersion"))
315 (os-name (w32-read-registry 'HKLM key "ProductName"))
316 (os-version (if w9x-p
317 (w32-read-registry 'HKLM key "VersionNumber")
318 (let ((vmajor
319 (w32-read-registry 'HKLM key
320 "CurrentMajorVersionNumber"))
321 (vminor
322 (w32-read-registry 'HKLM key
323 "CurrentMinorVersionNumber")))
324 (if (and vmajor vmajor)
325 (format "%d.%d" vmajor vminor)
326 (w32-read-registry 'HKLM key "CurrentVersion")))))
327 (os-csd (w32-read-registry 'HKLM key "CSDVersion"))
328 (os-rel (or (w32-read-registry 'HKLM key "ReleaseID")
329 (w32-read-registry 'HKLM key "CSDBuildNumber")
330 "0")) ; No Release ID before Windows Vista
331 (os-build (w32-read-registry 'HKLM key "CurrentBuildNumber"))
332 (os-rev (w32-read-registry 'HKLM key "UBR"))
333 (os-rev (if os-rev (format "%d" os-rev))))
334 (if w9x-p
335 (concat
336 (if (not (string-match "\\`Microsoft " os-name)) "Microsoft ")
337 os-name
338 " (v" os-version ")")
339 (concat
340 (if (not (string-match "\\`Microsoft " os-name)) "Microsoft ")
341 os-name ; Windows 7 Enterprise
343 os-csd ; Service Pack 1
344 (if (and os-csd (> (length os-csd) 0)) " " "")
345 "(v"
346 os-version "." os-rel "." os-build (if os-rev (concat "." os-rev))
347 ")"))))
350 ;;;; Support for build process
352 ;; From autoload.el
353 (defvar autoload-make-program)
354 (defvar generated-autoload-file)
356 (defun w32-batch-update-autoloads ()
357 "Like `batch-update-autoloads', but takes the name of the autoloads file
358 from the command line.
360 This is required because some Windows build environments, such as MSYS,
361 munge command-line arguments that include file names to a horrible mess
362 that Emacs is unable to cope with."
363 (let ((generated-autoload-file
364 (expand-file-name (pop command-line-args-left)))
365 ;; I can only assume the same considerations may apply here...
366 (autoload-make-program (pop command-line-args-left)))
367 (batch-update-autoloads)))
369 (defun w32-append-code-lines (orig extra)
370 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
372 This function saves all buffers and kills the Emacs session, without asking
373 for any permissions.
375 This is required because the Windows build environment is not required
376 to include Sed, which is used by leim/Makefile.in to do the job."
377 (find-file orig)
378 (goto-char (point-max))
379 (insert-file-contents extra)
380 (delete-matching-lines "^$\\|^;")
381 (save-buffers-kill-emacs t))
383 ;;; w32-fns.el ends here