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