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