1 ;;; w32-fns.el --- Lisp routines for Windows NT
3 ;; Copyright (C) 1994, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008 Free Software Foundation, Inc.
6 ;; Author: Geoff Voelker <voelker@cs.washington.edu>
9 ;; This file is part of GNU Emacs.
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 3, or (at your option)
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.
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.
31 ;; (November 21, 1994)
32 ;; [C-M-backspace] defined.
33 ;; mode-line-format defined to show buffer file type.
34 ;; audio bell initialized.
39 (defvar explicit-shell-file-name
)
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])
63 "Keymap of possible alternative meanings for some keys.")
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)))
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")
83 ;; Ignore case on file-name completion
84 (setq completion-ignore-case t)
86 ;; Map all versions of a filename (8.3, longname, mixed case) to the
88 (setq find-file-visit-truename t)
91 "Return the MS-Windows version numbers.
92 The value is a list of three integers: the major and minor version
93 numbers, and the build number."
96 (defun w32-using-nt ()
97 "Return non-nil if running on a Windows NT descendant.
98 That includes all Windows systems except for 9X/Me."
99 (and (eq system-type 'windows-nt) (getenv "SystemRoot")))
101 (defun w32-shell-name ()
102 "Return the name of the shell being used."
103 (or (bound-and-true-p explicit-shell-file-name)
106 (and (w32-using-nt) "cmd.exe")
109 (defun w32-system-shell-p (shell-name)
111 (member (downcase (file-name-nondirectory shell-name))
114 (defun w32-shell-dos-semantics ()
115 "Return non-nil if the interactive shell being used expects MSDOS shell semantics."
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")))))
121 (defvar w32-quote-process-args) ;; defined in w32proc.c
123 (defun w32-check-shell-configuration ()
124 "Check the configuration of shell variables on Windows NT/9X.
125 This function is invoked after loading the init files and processing
126 the command line arguments. It issues a warning if the user or site
127 has configured the shell with inappropriate settings."
129 (let ((prev-buffer (current-buffer))
130 (buffer (get-buffer-create "*Shell Configuration*"))
134 (if (w32-system-shell-p (getenv "ESHELL"))
135 (insert (format "Warning! The ESHELL environment variable uses %s.
136 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
138 (if (w32-system-shell-p (getenv "SHELL"))
139 (insert (format "Warning! The SHELL environment variable uses %s.
140 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
142 (if (w32-system-shell-p shell-file-name)
143 (insert (format "Warning! shell-file-name uses %s.
144 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
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.
149 You 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))
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
161 (if (string-equal "-c" shell-command-switch)
162 (insert "Warning! shell-command-switch is \"-c\".
163 You 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.
166 You should set this to nil when using a system shell.\n\n")))
167 ;; Non-system shells.
169 (if (string-equal "/c" shell-command-switch)
170 (insert "Warning! shell-command-switch is \"/c\".
171 You 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.
174 You 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)))
180 (add-hook 'after-init-hook 'w32-check-shell-configuration)
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)))
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)))))
205 (add-hook 'before-init-hook 'set-default-process-coding-system)
208 ;;; Basic support functions for managing Emacs' locale setting
210 (defvar w32-valid-locales nil
211 "List of locale ids known to be supported.")
213 ;;; This is the brute-force version; an efficient version is now
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."
221 (if (w32-get-locale-info i)
222 (setq locales (cons i locales)))
226 (defun w32-list-locales ()
227 "List the name and id of all locales supported by Windows."
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*"))
233 (insert "LCID\tAbbrev\tFull name\n\n")
238 (w32-get-locale-info x)
239 (w32-get-locale-info x t)))
240 w32-valid-locales "\n"))
242 (goto-char (point-min)))
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))
252 (dir1 (expand-file-name "../info/" instdir))
253 (dir2 (expand-file-name "../../../info/" instdir)))
254 (if (file-exists-p dir1)
255 (setq Info-default-directory-list
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)))))))
261 (add-hook 'before-init-hook 'w32-init-info)
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.
273 ;(add-hook 'before-init-hook
275 ; (setq source-directory (file-name-as-directory
276 ; (expand-file-name ".." exec-directory)))))
278 (defun convert-standard-filename (filename)
279 "Convert a standard file's name to something suitable for the current OS.
280 This means to guarantee valid names and perhaps to canonicalize
283 On Windows and DOS, replace invalid characters. On DOS, make
284 sure to obey the 8.3 limitations. On Windows, turn Cygwin names
285 into native names, and also turn slashes into backslashes if the
286 shell requires it (see `w32-shell-dos-semantics')."
289 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
290 (replace-match "\\1:/" t nil filename)
291 (copy-sequence filename)))
293 ;; leave ':' if part of drive specifier
294 (if (and (> (length name) 1)
295 (eq (aref name 1) ?:))
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)
305 (while (string-match "/" name start)
306 (aset name (match-beginning 0) ?\\)
307 (setq start (match-end 0))))
310 ;;; Fix interface to (X-specific) mouse.el
311 (defun x-set-selection (type data)
312 (or type (setq type 'PRIMARY))
313 (put 'x-selections type data))
315 (defun x-get-selection (&optional type data-type)
316 (or type (setq type 'PRIMARY))
317 (get 'x-selections type))
319 (defun set-w32-system-coding-system (coding-system)
320 "Set the coding system used by the Windows system to CODING-SYSTEM.
321 This is used for things like passing font names with non-ASCII
322 characters in them to the system. For a list of possible values of
323 CODING-SYSTEM, use \\[list-coding-systems].
325 This function is provided for backward compatibility, since
326 `w32-system-coding-system' is now an alias for `locale-coding-system'."
328 (list (let ((default locale-coding-system))
330 (format "Coding system for system calls (default %s): "
333 (check-coding-system coding-system)
334 (setq locale-coding-system coding-system))
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)
340 ;;; Set to a system sound if you want a fancy bell.
341 (set-message-beep nil)
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)
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)
359 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
360 "Function to add character sets to display with Windows fonts.
361 Creates entries in `w32-charset-info-alist'.
362 XLFD-CHARSET is a string which will appear in the XLFD font name to
363 identify the character set. WINDOWS-CHARSET is a symbol identifying
364 the Windows character set this maps to. For the list of possible
365 values, see the documentation for `w32-charset-info-alist'. CODEPAGE
366 can be a numeric codepage that Windows uses to display the character
367 set, t for Unicode output with no codepage translation or nil for 8
368 bit output with no translation."
369 (add-to-list 'w32-charset-info-alist
370 (cons xlfd-charset (cons windows-charset codepage)))
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.
375 (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
376 (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
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)
380 (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
381 (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
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)
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)
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)
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)
399 (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
400 (w32-add-charset-info "tis620" 'w32-charset-thai 874)
401 (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
402 (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)))
403 (if (boundp 'w32-unicode-charset-defined)
405 (w32-add-charset-info "iso10646-1" 'w32-charset-unicode t))
406 (w32-add-charset-info "iso10646-1" 'w32-charset-default t))
407 ;; ;; If unicode windows charset is not defined, use ansi fonts.
408 ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
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)
416 (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
418 (make-obsolete-variable 'w32-enable-italics
419 'w32-enable-synthesized-fonts "21.1")
420 (make-obsolete-variable 'w32-charset-to-codepage-alist
421 'w32-charset-info-alist "21.1")
424 ;;;; Selections and cut buffers
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)
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.")
436 (defun x-select-text (text &optional push)
437 "Make TEXT the last selected text.
438 If `x-select-enable-clipboard' is non-nil, copy the text to the system
439 clipboard as well. Optional PUSH is ignored on Windows."
440 (if x-select-enable-clipboard
441 (w32-set-clipboard-data text))
442 (setq x-last-selected-text text))
444 (defun x-get-selection-value ()
445 "Return the value of the current selection.
446 Consult the selection, then the cut buffer. Treat empty strings as if
448 (if x-select-enable-clipboard
450 ;; Don't die if x-get-selection signals an error.
452 (setq text (w32-get-clipboard-data))
453 (error (message "w32-get-clipboard-data:%s" c)))
454 (if (string= text "") (setq 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)
463 (setq x-last-selected-text text))))))
465 (defalias 'x-cut-buffer-or-selection-value 'x-get-selection-value)
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)
472 ;;;; Support for build process
473 (defun w32-batch-update-autoloads ()
474 "Like `batch-update-autoloads', but takes the name of the autoloads file
475 from the command line.
477 This is required because some Windows build environments, such as MSYS,
478 munge command-line arguments that include file names to a horrible mess
479 that Emacs is unable to cope with."
480 (let ((generated-autoload-file
481 (expand-file-name (pop command-line-args-left))))
482 (batch-update-autoloads)))
484 (defun w32-append-code-lines (orig extra)
485 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
487 This function saves all buffers and kills the Emacs session, without asking
490 This is required because the Windows build environment is not required
491 to include Sed, which is used by leim/Makefile.in to do the job."
493 (goto-char (point-max))
494 (insert-file-contents extra)
495 (delete-matching-lines "^$\\|^;")
496 (save-buffers-kill-emacs t))
498 ;;; arch-tag: c49b48cc-0f4f-454f-a274-c2dc34815e14
499 ;;; w32-fns.el ends here