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