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