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