[Gnus] XEmacs 21.5 compilation fix
[emacs.git] / lisp / w32-fns.el
blob1769ee73be576d5f01c9fa01ff975124be4373a5
1 ;;; w32-fns.el --- Lisp routines for 32-bit Windows
3 ;; Copyright (C) 1994, 2001-2012 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 "w32console.c")
35 (declare-function w32-get-clipboard-data "w32select.c")
36 (declare-function w32-get-locale-info "w32proc.c")
37 (declare-function w32-get-valid-locale-ids "w32proc.c")
38 (declare-function w32-set-clipboard-data "w32select.c")
40 ;; Map all versions of a filename (8.3, longname, mixed case) to the
41 ;; same buffer.
42 (setq find-file-visit-truename t)
44 (declare-function x-server-version "w32fns.c" (&optional display))
46 (defun w32-version ()
47 "Return the MS-Windows version numbers.
48 The value is a list of three integers: the major and minor version
49 numbers, and the build number."
50 (x-server-version))
52 (defun w32-using-nt ()
53 "Return non-nil if running on a Windows NT descendant.
54 That includes all Windows systems except for 9X/Me."
55 (and (eq system-type 'windows-nt) (getenv "SystemRoot")))
57 (defun w32-shell-name ()
58 "Return the name of the shell being used."
59 (or (bound-and-true-p shell-file-name)
60 (getenv "ESHELL")
61 (getenv "SHELL")
62 (and (w32-using-nt) "cmd.exe")
63 "command.com"))
65 (defun w32-system-shell-p (shell-name)
66 (and shell-name
67 (member (downcase (file-name-nondirectory shell-name))
68 w32-system-shells)))
70 (defun w32-shell-dos-semantics ()
71 "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
72 (or (w32-system-shell-p (w32-shell-name))
73 (and (member (downcase (file-name-nondirectory (w32-shell-name)))
74 '("cmdproxy" "cmdproxy.exe"))
75 (w32-system-shell-p (getenv "COMSPEC")))))
77 (defvar w32-quote-process-args) ;; defined in w32proc.c
79 (defun w32-check-shell-configuration ()
80 "Check the configuration of shell variables on Windows.
81 This function is invoked after loading the init files and processing
82 the command line arguments. It issues a warning if the user or site
83 has configured the shell with inappropriate settings."
84 (interactive)
85 (let ((prev-buffer (current-buffer))
86 (buffer (get-buffer-create "*Shell Configuration*"))
87 (system-shell))
88 (set-buffer buffer)
89 (erase-buffer)
90 (if (w32-system-shell-p (getenv "ESHELL"))
91 (insert (format "Warning! The ESHELL environment variable uses %s.
92 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
93 (getenv "ESHELL"))))
94 (if (w32-system-shell-p (getenv "SHELL"))
95 (insert (format "Warning! The SHELL environment variable uses %s.
96 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
97 (getenv "SHELL"))))
98 (if (w32-system-shell-p shell-file-name)
99 (insert (format "Warning! shell-file-name uses %s.
100 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
101 shell-file-name)))
102 (if (and (boundp 'explicit-shell-file-name)
103 (w32-system-shell-p explicit-shell-file-name))
104 (insert (format "Warning! explicit-shell-file-name uses %s.
105 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
106 explicit-shell-file-name)))
107 (setq system-shell (> (buffer-size) 0))
109 ;; Allow user to specify that they really do want to use one of the
110 ;; "system" shells, despite the drawbacks, but still warn if
111 ;; shell-command-switch doesn't match.
112 (if w32-allow-system-shell
113 (erase-buffer))
115 (cond (system-shell
116 ;; System shells.
117 (if (string-equal "-c" shell-command-switch)
118 (insert "Warning! shell-command-switch is \"-c\".
119 You should set this to \"/c\" when using a system shell.\n\n"))
120 (if w32-quote-process-args
121 (insert "Warning! w32-quote-process-args is t.
122 You should set this to nil when using a system shell.\n\n")))
123 ;; Non-system shells.
125 (if (string-equal "/c" shell-command-switch)
126 (insert "Warning! shell-command-switch is \"/c\".
127 You should set this to \"-c\" when using a non-system shell.\n\n"))
128 (if (not w32-quote-process-args)
129 (insert "Warning! w32-quote-process-args is nil.
130 You should set this to t when using a non-system shell.\n\n"))))
131 (if (> (buffer-size) 0)
132 (display-buffer buffer)
133 (kill-buffer buffer))
134 (set-buffer prev-buffer)))
136 (add-hook 'after-init-hook 'w32-check-shell-configuration)
138 ;; Override setting chosen at startup.
139 (defun set-default-process-coding-system ()
140 ;; Most programs on Windows will accept Unix line endings on input
141 ;; (and some programs ported from Unix require it) but most will
142 ;; produce DOS line endings on output.
143 (setq default-process-coding-system
144 (if (default-value 'enable-multibyte-characters)
145 '(undecided-dos . undecided-unix)
146 '(raw-text-dos . raw-text-unix)))
147 ;; Make cmdproxy default to using DOS line endings for input,
148 ;; because some Windows programs (including command.com) require it.
149 (add-to-list 'process-coding-system-alist
150 `("[cC][mM][dD][pP][rR][oO][xX][yY]"
151 . ,(if (default-value 'enable-multibyte-characters)
152 '(undecided-dos . undecided-dos)
153 '(raw-text-dos . raw-text-dos))))
154 ;; plink needs DOS input when entering the password.
155 (add-to-list 'process-coding-system-alist
156 `("[pP][lL][iI][nN][kK]"
157 . ,(if (default-value 'enable-multibyte-characters)
158 '(undecided-dos . undecided-dos)
159 '(raw-text-dos . raw-text-dos)))))
161 (add-hook 'before-init-hook 'set-default-process-coding-system)
164 ;;; Basic support functions for managing Emacs's locale setting
166 (defvar w32-valid-locales nil
167 "List of locale ids known to be supported.")
169 ;; This is the brute-force version; an efficient version is now
170 ;; built-in though.
171 (if (not (fboundp 'w32-get-valid-locale-ids))
172 (defun w32-get-valid-locale-ids ()
173 "Return list of all valid Windows locale ids."
174 (let ((i 65535)
175 locales)
176 (while (> i 0)
177 (if (w32-get-locale-info i)
178 (setq locales (cons i locales)))
179 (setq i (1- i)))
180 locales)))
182 (defun w32-list-locales ()
183 "List the name and id of all locales supported by Windows."
184 (interactive)
185 (when (null w32-valid-locales)
186 (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
187 (with-output-to-temp-buffer "*Supported Locales*"
188 (princ "LCID\tAbbrev\tFull name\n\n")
189 (dolist (locale w32-valid-locales)
190 (princ (format "%d\t%s\t%s\n"
191 locale
192 (w32-get-locale-info locale)
193 (w32-get-locale-info locale t))))))
195 ;; The variable source-directory is used to initialize Info-directory-list.
196 ;; However, the common case is that Emacs is being used from a binary
197 ;; distribution, and the value of source-directory is meaningless in that
198 ;; case. Even worse, source-directory can refer to a directory on a drive
199 ;; on the build machine that happens to be a removable drive on the user's
200 ;; machine. When this happens, Emacs tries to access the removable drive
201 ;; and produces the abort/retry/ignore dialog. Since we do not use
202 ;; source-directory, set it to something that is a reasonable approximation
203 ;; on the user's machine.
205 ;;(add-hook 'before-init-hook
206 ;; (lambda ()
207 ;; (setq source-directory (file-name-as-directory
208 ;; (expand-file-name ".." exec-directory)))))
210 (defun w32-convert-standard-filename (filename)
211 "Convert a standard file's name to something suitable for MS-Windows.
212 This means to guarantee valid names and perhaps to canonicalize
213 certain patterns.
215 This function is called by `convert-standard-filename'.
217 Replace invalid characters and turn Cygwin names into native
218 names, and also turn slashes into backslashes if the shell
219 requires it (see `w32-shell-dos-semantics')."
220 (save-match-data
221 (let ((name
222 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
223 (replace-match "\\1:/" t nil filename)
224 (copy-sequence filename)))
225 (start 0))
226 ;; leave ':' if part of drive specifier
227 (if (and (> (length name) 1)
228 (eq (aref name 1) ?:))
229 (setq start 2))
230 ;; destructively replace invalid filename characters with !
231 (while (string-match "[?*:<>|\"\000-\037]" name start)
232 (aset name (match-beginning 0) ?!)
233 (setq start (match-end 0)))
234 ;; convert directory separators to Windows format
235 ;; (but only if the shell in use requires it)
236 (when (w32-shell-dos-semantics)
237 (setq start 0)
238 (while (string-match "/" name start)
239 (aset name (match-beginning 0) ?\\)
240 (setq start (match-end 0))))
241 name)))
243 ;;; Fix interface to (X-specific) mouse.el
244 (defun x-set-selection (type data)
245 "Make an X selection of type TYPE and value DATA.
246 The argument TYPE (nil means `PRIMARY') says which selection, and
247 DATA specifies the contents. TYPE must be a symbol. \(It can also
248 be a string, which stands for the symbol with that name, but this
249 is considered obsolete.) DATA may be a string, a symbol, an
250 integer (or a cons of two integers or list of two integers).
252 The selection may also be a cons of two markers pointing to the same buffer,
253 or an overlay. In these cases, the selection is considered to be the text
254 between the markers *at whatever time the selection is examined*.
255 Thus, editing done in the buffer after you specify the selection
256 can alter the effective value of the selection.
258 The data may also be a vector of valid non-vector selection values.
260 The return value is DATA.
262 Interactively, this command sets the primary selection. Without
263 prefix argument, it reads the selection in the minibuffer. With
264 prefix argument, it uses the text of the region as the selection value.
266 Note that on MS-Windows, primary and secondary selections set by Emacs
267 are not available to other programs."
268 (put 'x-selections (or type 'PRIMARY) data))
270 (defun x-get-selection (&optional type _data-type)
271 "Return the value of an X Windows selection.
272 The argument TYPE (default `PRIMARY') says which selection,
273 and the argument DATA-TYPE (default `STRING') says
274 how to convert the data.
276 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
277 only a few symbols are commonly used. They conventionally have
278 all upper-case names. The most often used ones, in addition to
279 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
281 DATA-TYPE is usually `STRING', but can also be one of the symbols
282 in `selection-converter-alist', which see."
283 (get 'x-selections (or type 'PRIMARY)))
285 ;; x-selection-owner-p is used in simple.el
286 (defun x-selection-owner-p (&optional type)
287 (and (memq type '(nil PRIMARY SECONDARY))
288 (get 'x-selections (or type 'PRIMARY))))
290 (defun set-w32-system-coding-system (coding-system)
291 "Set the coding system used by the Windows system to CODING-SYSTEM.
292 This is used for things like passing font names with non-ASCII
293 characters in them to the system. For a list of possible values of
294 CODING-SYSTEM, use \\[list-coding-systems].
296 This function is provided for backward compatibility, since
297 `w32-system-coding-system' is now an alias for `locale-coding-system'."
298 (interactive
299 (list (let ((default locale-coding-system))
300 (read-coding-system
301 (format "Coding system for system calls (default %s): "
302 default)
303 default))))
304 (check-coding-system coding-system)
305 (setq locale-coding-system coding-system))
307 ;; locale-coding-system was introduced to do the same thing as
308 ;; w32-system-coding-system. Use that instead.
309 (defvaralias 'w32-system-coding-system 'locale-coding-system)
311 ;; Set to a system sound if you want a fancy bell.
312 (set-message-beep nil)
314 ;; The "Windows" keys on newer keyboards bring up the Start menu
315 ;; whether you want it or not - make Emacs ignore these keystrokes
316 ;; rather than beep.
317 (global-set-key [lwindow] 'ignore)
318 (global-set-key [rwindow] 'ignore)
320 (defvar w32-charset-info-alist) ; w32font.c
322 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
323 "Function to add character sets to display with Windows fonts.
324 Creates entries in `w32-charset-info-alist'.
325 XLFD-CHARSET is a string which will appear in the XLFD font name to
326 identify the character set. WINDOWS-CHARSET is a symbol identifying
327 the Windows character set this maps to. For the list of possible
328 values, see the documentation for `w32-charset-info-alist'. CODEPAGE
329 can be a numeric codepage that Windows uses to display the character
330 set, t for Unicode output with no codepage translation or nil for 8
331 bit output with no translation."
332 (add-to-list 'w32-charset-info-alist
333 (cons xlfd-charset (cons windows-charset codepage))))
335 ;; The last charset we add becomes the "preferred" charset for the return
336 ;; value from w32-select-font etc, so list the most important charsets last.
337 (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
338 (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
339 ;; The following two are included for pattern matching.
340 (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
341 (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
342 (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
343 (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
344 (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
345 (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
346 (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
347 (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
348 (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
349 (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
350 (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
351 (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
352 (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
353 (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
354 (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
355 (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
356 (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
357 (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
358 (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
359 (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
360 (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
361 (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
362 (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
363 (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
364 (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
366 ;; ;; If Unicode Windows charset is not defined, use ansi fonts.
367 ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
369 ;; Preferred names
370 (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
371 (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
372 (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
373 (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
374 (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
375 (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
377 (make-obsolete-variable 'w32-enable-italics
378 'w32-enable-synthesized-fonts "21.1")
379 (make-obsolete-variable 'w32-charset-to-codepage-alist
380 'w32-charset-info-alist "21.1")
383 ;;;; Selections
385 ;; We keep track of the last text selected here, so we can check the
386 ;; current selection against it, and avoid passing back our own text
387 ;; from x-selection-value.
388 (defvar x-last-selected-text nil)
390 (defun x-get-selection-value ()
391 "Return the value of the current selection.
392 Consult the selection. Treat empty strings as if they were unset."
393 (if x-select-enable-clipboard
394 (let (text)
395 ;; Don't die if x-get-selection signals an error.
396 (condition-case c
397 (setq text (w32-get-clipboard-data))
398 (error (message "w32-get-clipboard-data:%s" c)))
399 (if (string= text "") (setq text nil))
400 (cond
401 ((not text) nil)
402 ((eq text x-last-selected-text) nil)
403 ((string= text x-last-selected-text)
404 ;; Record the newer string, so subsequent calls can use the 'eq' test.
405 (setq x-last-selected-text text)
406 nil)
408 (setq x-last-selected-text text))))))
410 (defalias 'x-selection-value 'x-get-selection-value)
412 ;; Arrange for the kill and yank functions to set and check the clipboard.
413 (setq interprogram-cut-function 'x-select-text)
414 (setq interprogram-paste-function 'x-get-selection-value)
417 ;;;; Support for build process
419 ;; From autoload.el
420 (defvar autoload-make-program)
421 (defvar generated-autoload-file)
423 (defun w32-batch-update-autoloads ()
424 "Like `batch-update-autoloads', but takes the name of the autoloads file
425 from the command line.
427 This is required because some Windows build environments, such as MSYS,
428 munge command-line arguments that include file names to a horrible mess
429 that Emacs is unable to cope with."
430 (let ((generated-autoload-file
431 (expand-file-name (pop command-line-args-left)))
432 ;; I can only assume the same considerations may apply here...
433 (autoload-make-program (pop command-line-args-left)))
434 (batch-update-autoloads)))
436 (defun w32-append-code-lines (orig extra)
437 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
439 This function saves all buffers and kills the Emacs session, without asking
440 for any permissions.
442 This is required because the Windows build environment is not required
443 to include Sed, which is used by leim/Makefile.in to do the job."
444 (find-file orig)
445 (goto-char (point-max))
446 (insert-file-contents extra)
447 (delete-matching-lines "^$\\|^;")
448 (save-buffers-kill-emacs t))
450 ;;; w32-fns.el ends here