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