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