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