1 ;;; w32-fns.el --- Lisp routines for Windows NT
3 ;; Copyright (C) 1994, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008,
4 ;; 2009, 2010 Free Software Foundation, Inc.
6 ;; Author: Geoff Voelker <voelker@cs.washington.edu>
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
31 (defvar explicit-shell-file-name
)
35 (defvar x-alternatives-map
36 (let ((map (make-sparse-keymap)))
37 ;; Map certain keypad keys into ASCII characters that people usually expect.
38 (define-key map
[M-backspace
] [?\M-\d
])
39 (define-key map
[M-delete
] [?\M-\d
])
40 (define-key map
[M-tab
] [?\M-
\t])
41 (define-key map
[M-linefeed
] [?\M-
\n])
42 (define-key map
[M-clear
] [?\M-\C-l
])
43 (define-key map
[M-return
] [?\M-\C-m
])
44 (define-key map
[M-escape
] [?\M-\e
])
45 (define-key map
[iso-lefttab
] [backtab])
46 (define-key map [S-iso-lefttab] [backtab])
47 (define-key map
[S-tab
] [backtab])
49 "Keymap of possible alternative meanings for some keys.")
51 (defun x-setup-function-keys (frame)
52 "Set up `function-key-map' on the graphical frame FRAME."
53 ;; Don't do this twice on the same display, or it would break
54 ;; normal-erase-is-backspace-mode.
55 (unless (terminal-parameter frame 'x-setup-function-keys)
56 ;; Map certain keypad keys into ASCII characters that people usually expect.
57 (with-selected-frame frame
58 (let ((map (copy-keymap x-alternatives-map)))
59 (set-keymap-parent map (keymap-parent local-function-key-map))
60 (set-keymap-parent local-function-key-map map)))
61 (set-terminal-parameter frame 'x-setup-function-keys t)))
63 (declare-function set-message-beep "w32console.c")
64 (declare-function w32-get-clipboard-data "w32select.c")
65 (declare-function w32-get-locale-info "w32proc.c")
66 (declare-function w32-get-valid-locale-ids "w32proc.c")
67 (declare-function w32-set-clipboard-data "w32select.c")
69 ;; Map all versions of a filename (8.3, longname, mixed case) to the
71 (setq find-file-visit-truename t)
73 (declare-function x-server-version "w32fns.c" (&optional display))
76 "Return the MS-Windows version numbers.
77 The value is a list of three integers: the major and minor version
78 numbers, and the build number."
81 (defun w32-using-nt ()
82 "Return non-nil if running on a Windows NT descendant.
83 That includes all Windows systems except for 9X/Me."
84 (and (eq system-type 'windows-nt) (getenv "SystemRoot")))
86 (defun w32-shell-name ()
87 "Return the name of the shell being used."
88 (or (bound-and-true-p explicit-shell-file-name)
91 (and (w32-using-nt) "cmd.exe")
94 (defun w32-system-shell-p (shell-name)
96 (member (downcase (file-name-nondirectory shell-name))
99 (defun w32-shell-dos-semantics ()
100 "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
101 (or (w32-system-shell-p (w32-shell-name))
102 (and (member (downcase (file-name-nondirectory (w32-shell-name)))
103 '("cmdproxy" "cmdproxy.exe"))
104 (w32-system-shell-p (getenv "COMSPEC")))))
106 (defvar w32-quote-process-args) ;; defined in w32proc.c
108 (defun w32-check-shell-configuration ()
109 "Check the configuration of shell variables on Windows.
110 This function is invoked after loading the init files and processing
111 the command line arguments. It issues a warning if the user or site
112 has configured the shell with inappropriate settings."
114 (let ((prev-buffer (current-buffer))
115 (buffer (get-buffer-create "*Shell Configuration*"))
119 (if (w32-system-shell-p (getenv "ESHELL"))
120 (insert (format "Warning! The ESHELL environment variable uses %s.
121 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
123 (if (w32-system-shell-p (getenv "SHELL"))
124 (insert (format "Warning! The SHELL environment variable uses %s.
125 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
127 (if (w32-system-shell-p shell-file-name)
128 (insert (format "Warning! shell-file-name uses %s.
129 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
131 (if (and (boundp 'explicit-shell-file-name)
132 (w32-system-shell-p explicit-shell-file-name))
133 (insert (format "Warning! explicit-shell-file-name uses %s.
134 You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
135 explicit-shell-file-name)))
136 (setq system-shell (> (buffer-size) 0))
138 ;; Allow user to specify that they really do want to use one of the
139 ;; "system" shells, despite the drawbacks, but still warn if
140 ;; shell-command-switch doesn't match.
141 (if w32-allow-system-shell
146 (if (string-equal "-c" shell-command-switch)
147 (insert "Warning! shell-command-switch is \"-c\".
148 You should set this to \"/c\" when using a system shell.\n\n"))
149 (if w32-quote-process-args
150 (insert "Warning! w32-quote-process-args is t.
151 You should set this to nil when using a system shell.\n\n")))
152 ;; Non-system shells.
154 (if (string-equal "/c" shell-command-switch)
155 (insert "Warning! shell-command-switch is \"/c\".
156 You should set this to \"-c\" when using a non-system shell.\n\n"))
157 (if (not w32-quote-process-args)
158 (insert "Warning! w32-quote-process-args is nil.
159 You should set this to t when using a non-system shell.\n\n"))))
160 (if (> (buffer-size) 0)
161 (display-buffer buffer)
162 (kill-buffer buffer))
163 (set-buffer prev-buffer)))
165 (add-hook 'after-init-hook 'w32-check-shell-configuration)
167 ;; Override setting chosen at startup.
168 (defun set-default-process-coding-system ()
169 ;; Most programs on Windows will accept Unix line endings on input
170 ;; (and some programs ported from Unix require it) but most will
171 ;; produce DOS line endings on output.
172 (setq default-process-coding-system
173 (if (default-value 'enable-multibyte-characters)
174 '(undecided-dos . undecided-unix)
175 '(raw-text-dos . raw-text-unix)))
176 ;; Make cmdproxy default to using DOS line endings for input,
177 ;; because some Windows programs (including command.com) require it.
178 (add-to-list 'process-coding-system-alist
179 `("[cC][mM][dD][pP][rR][oO][xX][yY]"
180 . ,(if (default-value 'enable-multibyte-characters)
181 '(undecided-dos . undecided-dos)
182 '(raw-text-dos . raw-text-dos))))
183 ;; plink needs DOS input when entering the password.
184 (add-to-list 'process-coding-system-alist
185 `("[pP][lL][iI][nN][kK]"
186 . ,(if (default-value 'enable-multibyte-characters)
187 '(undecided-dos . undecided-dos)
188 '(raw-text-dos . raw-text-dos)))))
190 (add-hook 'before-init-hook 'set-default-process-coding-system)
193 ;;; Basic support functions for managing Emacs' locale setting
195 (defvar w32-valid-locales nil
196 "List of locale ids known to be supported.")
198 ;; This is the brute-force version; an efficient version is now
200 (if (not (fboundp 'w32-get-valid-locale-ids))
201 (defun w32-get-valid-locale-ids ()
202 "Return list of all valid Windows locale ids."
206 (if (w32-get-locale-info i)
207 (setq locales (cons i locales)))
211 (defun w32-list-locales ()
212 "List the name and id of all locales supported by Windows."
214 (when (null w32-valid-locales)
215 (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
216 (with-output-to-temp-buffer "*Supported Locales*"
217 (princ "LCID\tAbbrev\tFull name\n\n")
218 (dolist (locale w32-valid-locales)
219 (princ (format "%d\t%s\t%s\n"
221 (w32-get-locale-info locale)
222 (w32-get-locale-info locale t))))))
224 ;; Setup Info-default-directory-list to include the info directory
225 ;; near where Emacs executable was installed. We used to set INFOPATH,
226 ;; but when this is set Info-default-directory-list is ignored. We
227 ;; also cannot rely upon what is set in paths.el because they assume
228 ;; that configuration during build time is correct for runtime.
229 (defun w32-init-info ()
230 (let* ((instdir (file-name-directory invocation-directory))
231 (dir1 (expand-file-name "../info/" instdir))
232 (dir2 (expand-file-name "../../../info/" instdir)))
233 (if (file-exists-p dir1)
234 (setq Info-default-directory-list
235 (append Info-default-directory-list (list dir1)))
236 (if (file-exists-p dir2)
237 (setq Info-default-directory-list
238 (append Info-default-directory-list (list dir2)))))))
240 (add-hook 'before-init-hook 'w32-init-info)
242 ;; The variable source-directory is used to initialize Info-directory-list.
243 ;; However, the common case is that Emacs is being used from a binary
244 ;; distribution, and the value of source-directory is meaningless in that
245 ;; case. Even worse, source-directory can refer to a directory on a drive
246 ;; on the build machine that happens to be a removable drive on the user's
247 ;; machine. When this happens, Emacs tries to access the removable drive
248 ;; and produces the abort/retry/ignore dialog. Since we do not use
249 ;; source-directory, set it to something that is a reasonable approximation
250 ;; on the user's machine.
252 ;;(add-hook 'before-init-hook
254 ;; (setq source-directory (file-name-as-directory
255 ;; (expand-file-name ".." exec-directory)))))
257 (defun w32-convert-standard-filename (filename)
258 "Convert a standard file's name to something suitable for MS-Windows.
259 This means to guarantee valid names and perhaps to canonicalize
262 This function is called by `convert-standard-filename'.
264 Replace invalid characters and turn Cygwin names into native
265 names, and also turn slashes into backslashes if the shell
266 requires it (see `w32-shell-dos-semantics')."
269 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
270 (replace-match "\\1:/" t nil filename)
271 (copy-sequence filename)))
273 ;; leave ':' if part of drive specifier
274 (if (and (> (length name) 1)
275 (eq (aref name 1) ?:))
277 ;; destructively replace invalid filename characters with !
278 (while (string-match "[?*:<>|\"\000-\037]" name start)
279 (aset name (match-beginning 0) ?!)
280 (setq start (match-end 0)))
281 ;; convert directory separators to Windows format
282 ;; (but only if the shell in use requires it)
283 (when (w32-shell-dos-semantics)
285 (while (string-match "/" name start)
286 (aset name (match-beginning 0) ?\\)
287 (setq start (match-end 0))))
290 ;;; Fix interface to (X-specific) mouse.el
291 (defun x-set-selection (type data)
292 "Make an X selection of type TYPE and value DATA.
293 The argument TYPE (nil means `PRIMARY') says which selection, and
294 DATA specifies the contents. TYPE must be a symbol. \(It can also
295 be a string, which stands for the symbol with that name, but this
296 is considered obsolete.) DATA may be a string, a symbol, an
297 integer (or a cons of two integers or list of two integers).
299 The selection may also be a cons of two markers pointing to the same buffer,
300 or an overlay. In these cases, the selection is considered to be the text
301 between the markers *at whatever time the selection is examined*.
302 Thus, editing done in the buffer after you specify the selection
303 can alter the effective value of the selection.
305 The data may also be a vector of valid non-vector selection values.
307 The return value is DATA.
309 Interactively, this command sets the primary selection. Without
310 prefix argument, it reads the selection in the minibuffer. With
311 prefix argument, it uses the text of the region as the selection value.
313 Note that on MS-Windows, primary and secondary selections set by Emacs
314 are not available to other programs."
315 (put 'x-selections (or type 'PRIMARY) data))
317 (defun x-get-selection (&optional type data-type)
318 "Return the value of an X Windows selection.
319 The argument TYPE (default `PRIMARY') says which selection,
320 and the argument DATA-TYPE (default `STRING') says
321 how to convert the data.
323 TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
324 only a few symbols are commonly used. They conventionally have
325 all upper-case names. The most often used ones, in addition to
326 `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
328 DATA-TYPE is usually `STRING', but can also be one of the symbols
329 in `selection-converter-alist', which see."
330 (get 'x-selections (or type 'PRIMARY)))
332 ;; x-selection-owner-p is used in simple.el
333 (defun x-selection-owner-p (&optional type)
334 (and (memq type '(nil PRIMARY SECONDARY))
335 (get 'x-selections (or type 'PRIMARY))))
337 (defun set-w32-system-coding-system (coding-system)
338 "Set the coding system used by the Windows system to CODING-SYSTEM.
339 This is used for things like passing font names with non-ASCII
340 characters in them to the system. For a list of possible values of
341 CODING-SYSTEM, use \\[list-coding-systems].
343 This function is provided for backward compatibility, since
344 `w32-system-coding-system' is now an alias for `locale-coding-system'."
346 (list (let ((default locale-coding-system))
348 (format "Coding system for system calls (default %s): "
351 (check-coding-system coding-system)
352 (setq locale-coding-system coding-system))
354 ;; locale-coding-system was introduced to do the same thing as
355 ;; w32-system-coding-system. Use that instead.
356 (defvaralias 'w32-system-coding-system 'locale-coding-system)
358 ;; Set to a system sound if you want a fancy bell.
359 (set-message-beep nil)
361 ;; The "Windows" keys on newer keyboards bring up the Start menu
362 ;; whether you want it or not - make Emacs ignore these keystrokes
364 (global-set-key [lwindow] 'ignore)
365 (global-set-key [rwindow] 'ignore)
367 (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
368 "Function to add character sets to display with Windows fonts.
369 Creates entries in `w32-charset-info-alist'.
370 XLFD-CHARSET is a string which will appear in the XLFD font name to
371 identify the character set. WINDOWS-CHARSET is a symbol identifying
372 the Windows character set this maps to. For the list of possible
373 values, see the documentation for `w32-charset-info-alist'. CODEPAGE
374 can be a numeric codepage that Windows uses to display the character
375 set, t for Unicode output with no codepage translation or nil for 8
376 bit output with no translation."
377 (add-to-list 'w32-charset-info-alist
378 (cons xlfd-charset (cons windows-charset codepage))))
380 ;; The last charset we add becomes the "preferred" charset for the return
381 ;; value from w32-select-font etc, so list the most important charsets last.
382 (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
383 (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
384 ;; The following two are included for pattern matching.
385 (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
386 (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
387 (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
388 (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
389 (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
390 (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
391 (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
392 (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
393 (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
394 (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
395 (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
396 (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
397 (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
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 "iso8859-5" 'w32-charset-russian 28595)
405 (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
406 (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
407 (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
408 (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
409 (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
411 ;; ;; If unicode windows charset is not defined, use ansi fonts.
412 ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
415 (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
416 (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
417 (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
418 (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
419 (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
420 (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
422 (make-obsolete-variable 'w32-enable-italics
423 'w32-enable-synthesized-fonts "21.1")
424 (make-obsolete-variable 'w32-charset-to-codepage-alist
425 'w32-charset-info-alist "21.1")
428 ;;;; Selections and cut buffers
430 ;; We keep track of the last text selected here, so we can check the
431 ;; current selection against it, and avoid passing back our own text
432 ;; from x-cut-buffer-or-selection-value.
433 (defvar x-last-selected-text nil)
435 ;; It is said that overlarge strings are slow to put into the cut buffer.
436 ;; Note this value is overridden below.
437 (defvar x-cut-buffer-max 20000
438 "Max number of characters to put in the cut buffer.")
440 (defun x-select-text (text &optional push)
441 "Select TEXT, a string, according to the window system.
443 On X, put TEXT in the primary X selection. For backward
444 compatibility with older X applications, set the value of X cut
445 buffer 0 as well, and if the optional argument PUSH is non-nil,
446 rotate the cut buffers. If `x-select-enable-clipboard' is
447 non-nil, copy the text to the X clipboard as well.
449 On Windows, make TEXT the current selection. If
450 `x-select-enable-clipboard' is non-nil, copy the text to the
451 clipboard as well. The argument PUSH is ignored.
453 On Nextstep, put TEXT in the pasteboard; PUSH is ignored."
454 (if x-select-enable-clipboard
455 (w32-set-clipboard-data text))
456 (setq x-last-selected-text text))
458 (defun x-get-selection-value ()
459 "Return the value of the current selection.
460 Consult the selection, then the cut buffer. Treat empty strings as if
462 (if x-select-enable-clipboard
464 ;; Don't die if x-get-selection signals an error.
466 (setq text (w32-get-clipboard-data))
467 (error (message "w32-get-clipboard-data:%s" c)))
468 (if (string= text "") (setq text nil))
471 ((eq text x-last-selected-text) nil)
472 ((string= text x-last-selected-text)
473 ;; Record the newer string, so subsequent calls can use the 'eq' test.
474 (setq x-last-selected-text text)
477 (setq x-last-selected-text text))))))
479 (defalias 'x-cut-buffer-or-selection-value 'x-get-selection-value)
481 ;; Arrange for the kill and yank functions to set and check the clipboard.
482 (setq interprogram-cut-function 'x-select-text)
483 (setq interprogram-paste-function 'x-get-selection-value)
486 ;;;; Support for build process
487 (defun w32-batch-update-autoloads ()
488 "Like `batch-update-autoloads', but takes the name of the autoloads file
489 from the command line.
491 This is required because some Windows build environments, such as MSYS,
492 munge command-line arguments that include file names to a horrible mess
493 that Emacs is unable to cope with."
494 (let ((generated-autoload-file
495 (expand-file-name (pop command-line-args-left)))
496 ;; I can only assume the same considerations may apply here...
497 (autoload-make-program (pop command-line-args-left)))
498 (batch-update-autoloads)))
500 (defun w32-append-code-lines (orig extra)
501 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
503 This function saves all buffers and kills the Emacs session, without asking
506 This is required because the Windows build environment is not required
507 to include Sed, which is used by leim/Makefile.in to do the job."
509 (goto-char (point-max))
510 (insert-file-contents extra)
511 (delete-matching-lines "^$\\|^;")
512 (save-buffers-kill-emacs t))
514 ;; arch-tag: c49b48cc-0f4f-454f-a274-c2dc34815e14
515 ;;; w32-fns.el ends here