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