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