Fix last fix in `display-buffer-record-window'.
[emacs.git] / lisp / w32-fns.el
blob876df875b1b4bf13496e9eddacad61fef8c86dbe
1 ;;; w32-fns.el --- Lisp routines for 32-bit Windows
3 ;; Copyright (C) 1994, 2001-2015 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 <http://www.gnu.org/licenses/>.
24 ;;; Commentary:
27 ;;; Code:
28 (require 'w32-vars)
30 (defvar explicit-shell-file-name)
32 ;;;; Function keys
34 (declare-function set-message-beep "w32fns.c")
35 (declare-function w32-get-locale-info "w32proc.c")
36 (declare-function w32-get-valid-locale-ids "w32proc.c")
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 (defun w32-shell-name ()
43 "Return the name of the shell being used."
44 (or (bound-and-true-p shell-file-name)
45 (getenv "ESHELL")
46 (getenv "SHELL")
47 (and (w32-using-nt) "cmd.exe")
48 "command.com"))
50 (defun w32-system-shell-p (shell-name)
51 (and shell-name
52 (member (downcase (file-name-nondirectory shell-name))
53 w32-system-shells)))
55 (defun w32-shell-dos-semantics ()
56 "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
57 (or (w32-system-shell-p (w32-shell-name))
58 (and (member (downcase (file-name-nondirectory (w32-shell-name)))
59 '("cmdproxy" "cmdproxy.exe"))
60 (w32-system-shell-p (getenv "COMSPEC")))))
62 (defvar w32-quote-process-args) ;; defined in w32proc.c
64 (defun w32-check-shell-configuration ()
65 "Check the configuration of shell variables on Windows.
66 This function is invoked after loading the init files and processing
67 the command line arguments. It issues a warning if the user or site
68 has configured the shell with inappropriate settings."
69 (interactive)
70 (let ((prev-buffer (current-buffer))
71 (buffer (get-buffer-create "*Shell Configuration*"))
72 (system-shell))
73 (set-buffer buffer)
74 (erase-buffer)
75 (if (w32-system-shell-p (getenv "ESHELL"))
76 (insert (format "Warning! The ESHELL environment variable uses %s.
77 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
78 (getenv "ESHELL"))))
79 (if (w32-system-shell-p (getenv "SHELL"))
80 (insert (format "Warning! The SHELL environment variable uses %s.
81 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
82 (getenv "SHELL"))))
83 (if (w32-system-shell-p shell-file-name)
84 (insert (format "Warning! shell-file-name uses %s.
85 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
86 shell-file-name)))
87 (if (and (boundp 'explicit-shell-file-name)
88 (w32-system-shell-p explicit-shell-file-name))
89 (insert (format "Warning! explicit-shell-file-name uses %s.
90 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
91 explicit-shell-file-name)))
92 (setq system-shell (> (buffer-size) 0))
94 ;; Allow user to specify that they really do want to use one of the
95 ;; "system" shells, despite the drawbacks, but still warn if
96 ;; shell-command-switch doesn't match.
97 (if w32-allow-system-shell
98 (erase-buffer))
100 (cond (system-shell
101 ;; System shells.
102 (if (string-equal "-c" shell-command-switch)
103 (insert "Warning! shell-command-switch is \"-c\".
104 You should set this to \"/c\" when using a system shell.\n\n"))
105 (if w32-quote-process-args
106 (insert "Warning! w32-quote-process-args is t.
107 You should set this to nil when using a system shell.\n\n")))
108 ;; Non-system shells.
110 (if (string-equal "/c" shell-command-switch)
111 (insert "Warning! shell-command-switch is \"/c\".
112 You should set this to \"-c\" when using a non-system shell.\n\n"))
113 (if (not w32-quote-process-args)
114 (insert "Warning! w32-quote-process-args is nil.
115 You should set this to t when using a non-system shell.\n\n"))))
116 (if (> (buffer-size) 0)
117 (display-buffer buffer)
118 (kill-buffer buffer))
119 (set-buffer prev-buffer)))
121 (add-hook 'after-init-hook 'w32-check-shell-configuration)
123 ;; Override setting chosen at startup.
124 (defun set-default-process-coding-system ()
125 ;; Most programs on Windows will accept Unix line endings on input
126 ;; (and some programs ported from Unix require it) but most will
127 ;; produce DOS line endings on output.
128 (setq default-process-coding-system
129 (if (default-value 'enable-multibyte-characters)
130 '(undecided-dos . undecided-unix)
131 '(raw-text-dos . raw-text-unix)))
132 ;; Make cmdproxy default to using DOS line endings for input,
133 ;; because some Windows programs (including command.com) require it.
134 (add-to-list 'process-coding-system-alist
135 `("[cC][mM][dD][pP][rR][oO][xX][yY]"
136 . ,(if (default-value 'enable-multibyte-characters)
137 '(undecided-dos . undecided-dos)
138 '(raw-text-dos . raw-text-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 . ,(if (default-value 'enable-multibyte-characters)
143 '(undecided-dos . undecided-dos)
144 '(raw-text-dos . raw-text-dos)))))
146 (add-hook 'before-init-hook 'set-default-process-coding-system)
149 ;;; Basic support functions for managing Emacs's locale setting
151 (defvar w32-valid-locales nil
152 "List of locale ids known to be supported.")
154 ;; This is the brute-force version; an efficient version is now
155 ;; built-in though.
156 (if (not (fboundp 'w32-get-valid-locale-ids))
157 (defun w32-get-valid-locale-ids ()
158 "Return list of all valid Windows locale ids."
159 (let ((i 65535)
160 locales)
161 (while (> i 0)
162 (if (w32-get-locale-info i)
163 (setq locales (cons i locales)))
164 (setq i (1- i)))
165 locales)))
167 (defun w32-list-locales ()
168 "List the name and id of all locales supported by Windows."
169 (interactive)
170 (when (null w32-valid-locales)
171 (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
172 (with-output-to-temp-buffer "*Supported Locales*"
173 (princ "LCID\tAbbrev\tFull name\n\n")
174 (dolist (locale w32-valid-locales)
175 (princ (format "%d\t%s\t%s\n"
176 locale
177 (w32-get-locale-info locale)
178 (w32-get-locale-info locale t))))))
180 ;; The variable source-directory is used to initialize Info-directory-list.
181 ;; However, the common case is that Emacs is being used from a binary
182 ;; distribution, and the value of source-directory is meaningless in that
183 ;; case. Even worse, source-directory can refer to a directory on a drive
184 ;; on the build machine that happens to be a removable drive on the user's
185 ;; machine. When this happens, Emacs tries to access the removable drive
186 ;; and produces the abort/retry/ignore dialog. Since we do not use
187 ;; source-directory, set it to something that is a reasonable approximation
188 ;; on the user's machine.
190 ;;(add-hook 'before-init-hook
191 ;; (lambda ()
192 ;; (setq source-directory (file-name-as-directory
193 ;; (expand-file-name ".." exec-directory)))))
195 (defun w32-convert-standard-filename (filename)
196 "Convert a standard file's name to something suitable for MS-Windows.
197 This means to guarantee valid names and perhaps to canonicalize
198 certain patterns.
200 This function is called by `convert-standard-filename'.
202 Replace invalid characters and turn Cygwin names into native
203 names, and also turn slashes into backslashes if the shell
204 requires it (see `w32-shell-dos-semantics')."
205 (save-match-data
206 (let ((name
207 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
208 (replace-match "\\1:/" t nil filename)
209 (copy-sequence filename)))
210 (start 0))
211 ;; leave ':' if part of drive specifier
212 (if (and (> (length name) 1)
213 (eq (aref name 1) ?:))
214 (setq start 2))
215 ;; destructively replace invalid filename characters with !
216 (while (string-match "[?*:<>|\"\000-\037]" name start)
217 (aset name (match-beginning 0) ?!)
218 (setq start (match-end 0)))
219 ;; convert directory separators to Windows format
220 ;; (but only if the shell in use requires it)
221 (when (w32-shell-dos-semantics)
222 (setq start 0)
223 (while (string-match "/" name start)
224 (aset name (match-beginning 0) ?\\)
225 (setq start (match-end 0))))
226 name)))
228 (defun set-w32-system-coding-system (coding-system)
229 "Set the coding system used by the Windows system to CODING-SYSTEM.
230 This is used for things like passing font names with non-ASCII
231 characters in them to the system. For a list of possible values of
232 CODING-SYSTEM, use \\[list-coding-systems].
234 This function is provided for backward compatibility, since
235 `w32-system-coding-system' is now an alias for `locale-coding-system'."
236 (interactive
237 (list (let ((default locale-coding-system))
238 (read-coding-system
239 (format "Coding system for system calls (default %s): "
240 default)
241 default))))
242 (check-coding-system coding-system)
243 (setq locale-coding-system coding-system))
245 ;; locale-coding-system was introduced to do the same thing as
246 ;; w32-system-coding-system. Use that instead.
247 (defvaralias 'w32-system-coding-system 'locale-coding-system)
249 ;; Set to a system sound if you want a fancy bell.
250 (set-message-beep nil)
252 (defvar w32-charset-info-alist) ; w32font.c
254 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
255 "Function to add character sets to display with Windows fonts.
256 Creates entries in `w32-charset-info-alist'.
257 XLFD-CHARSET is a string which will appear in the XLFD font name to
258 identify the character set. WINDOWS-CHARSET is a symbol identifying
259 the Windows character set this maps to. For the list of possible
260 values, see the documentation for `w32-charset-info-alist'. CODEPAGE
261 can be a numeric codepage that Windows uses to display the character
262 set, t for Unicode output with no codepage translation or nil for 8
263 bit output with no translation."
264 (add-to-list 'w32-charset-info-alist
265 (cons xlfd-charset (cons windows-charset codepage))))
267 ;; The last charset we add becomes the "preferred" charset for the return
268 ;; value from w32-select-font etc, so list the most important charsets last.
269 (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
270 (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
271 ;; The following two are included for pattern matching.
272 (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
273 (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
274 (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
275 (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
276 (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
277 (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
278 (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
279 (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
280 (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
281 (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
282 (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
283 (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
284 (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
285 (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
286 (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
287 (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
288 (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
289 (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
290 (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
291 (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
292 (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
293 (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
294 (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
295 (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
296 (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
298 ;; ;; If Unicode Windows charset is not defined, use ansi fonts.
299 ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
301 ;; Preferred names
302 (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
303 (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
304 (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
305 (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
306 (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
307 (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
310 ;;;; Support for build process
312 ;; From autoload.el
313 (defvar autoload-make-program)
314 (defvar generated-autoload-file)
316 (defun w32-batch-update-autoloads ()
317 "Like `batch-update-autoloads', but takes the name of the autoloads file
318 from the command line.
320 This is required because some Windows build environments, such as MSYS,
321 munge command-line arguments that include file names to a horrible mess
322 that Emacs is unable to cope with."
323 (let ((generated-autoload-file
324 (expand-file-name (pop command-line-args-left)))
325 ;; I can only assume the same considerations may apply here...
326 (autoload-make-program (pop command-line-args-left)))
327 (batch-update-autoloads)))
329 (defun w32-append-code-lines (orig extra)
330 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
332 This function saves all buffers and kills the Emacs session, without asking
333 for any permissions.
335 This is required because the Windows build environment is not required
336 to include Sed, which is used by leim/Makefile.in to do the job."
337 (find-file orig)
338 (goto-char (point-max))
339 (insert-file-contents extra)
340 (delete-matching-lines "^$\\|^;")
341 (save-buffers-kill-emacs t))
343 ;;; w32-fns.el ends here