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