Try to use "gpg2" if "gpg" isn't available.
[emacs.git] / lisp / w32-fns.el
blob1abb29febc7a043d810e8e233d8ae054af5d5485
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>
7 ;; Keywords: internal
8 ;; Package: emacs
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/>.
25 ;;; Commentary:
28 ;;; Code:
29 (require 'w32-vars)
31 (defvar explicit-shell-file-name)
33 ;;;; Function keys
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])
48 map)
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
70 ;; same buffer.
71 (setq find-file-visit-truename t)
73 (declare-function x-server-version "w32fns.c" (&optional display))
75 (defun w32-version ()
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."
79 (x-server-version))
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)
89 (getenv "ESHELL")
90 (getenv "SHELL")
91 (and (w32-using-nt) "cmd.exe")
92 "command.com"))
94 (defun w32-system-shell-p (shell-name)
95 (and shell-name
96 (member (downcase (file-name-nondirectory shell-name))
97 w32-system-shells)))
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."
113 (interactive)
114 (let ((prev-buffer (current-buffer))
115 (buffer (get-buffer-create "*Shell Configuration*"))
116 (system-shell))
117 (set-buffer buffer)
118 (erase-buffer)
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"
122 (getenv "ESHELL"))))
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"
126 (getenv "SHELL"))))
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"
130 shell-file-name)))
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
142 (erase-buffer))
144 (cond (system-shell
145 ;; System shells.
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
199 ;; built-in though.
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."
203 (let ((i 65535)
204 locales)
205 (while (> i 0)
206 (if (w32-get-locale-info i)
207 (setq locales (cons i locales)))
208 (setq i (1- i)))
209 locales)))
211 (defun w32-list-locales ()
212 "List the name and id of all locales supported by Windows."
213 (interactive)
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"
220 locale
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
253 ;; (lambda ()
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
260 certain patterns.
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')."
267 (save-match-data
268 (let ((name
269 (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
270 (replace-match "\\1:/" t nil filename)
271 (copy-sequence filename)))
272 (start 0))
273 ;; leave ':' if part of drive specifier
274 (if (and (> (length name) 1)
275 (eq (aref name 1) ?:))
276 (setq start 2))
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)
284 (setq start 0)
285 (while (string-match "/" name start)
286 (aset name (match-beginning 0) ?\\)
287 (setq start (match-end 0))))
288 name)))
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'."
345 (interactive
346 (list (let ((default locale-coding-system))
347 (read-coding-system
348 (format "Coding system for system calls (default %s): "
349 default)
350 default))))
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
363 ;; rather than beep.
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))
414 ;; Prefered names
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
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-selection-value.
433 (defvar x-last-selected-text nil)
435 (defun x-select-text (text)
436 "Select TEXT, a string, according to the window system.
438 On X, if `x-select-enable-clipboard' is non-nil, copy TEXT to the
439 clipboard. If `x-select-enable-primary' is non-nil, put TEXT in
440 the primary selection.
442 On Windows, make TEXT the current selection. If
443 `x-select-enable-clipboard' is non-nil, copy the text to the
444 clipboard as well.
446 On Nextstep, put TEXT in the pasteboard."
447 (if x-select-enable-clipboard
448 (w32-set-clipboard-data text))
449 (setq x-last-selected-text text))
451 (defun x-get-selection-value ()
452 "Return the value of the current selection.
453 Consult the selection. Treat empty strings as if they were unset."
454 (if x-select-enable-clipboard
455 (let (text)
456 ;; Don't die if x-get-selection signals an error.
457 (condition-case c
458 (setq text (w32-get-clipboard-data))
459 (error (message "w32-get-clipboard-data:%s" c)))
460 (if (string= text "") (setq text nil))
461 (cond
462 ((not text) nil)
463 ((eq text x-last-selected-text) nil)
464 ((string= text x-last-selected-text)
465 ;; Record the newer string, so subsequent calls can use the 'eq' test.
466 (setq x-last-selected-text text)
467 nil)
469 (setq x-last-selected-text text))))))
471 (defalias 'x-selection-value 'x-get-selection-value)
473 ;; Arrange for the kill and yank functions to set and check the clipboard.
474 (setq interprogram-cut-function 'x-select-text)
475 (setq interprogram-paste-function 'x-get-selection-value)
478 ;;;; Support for build process
479 (defun w32-batch-update-autoloads ()
480 "Like `batch-update-autoloads', but takes the name of the autoloads file
481 from the command line.
483 This is required because some Windows build environments, such as MSYS,
484 munge command-line arguments that include file names to a horrible mess
485 that Emacs is unable to cope with."
486 (let ((generated-autoload-file
487 (expand-file-name (pop command-line-args-left)))
488 ;; I can only assume the same considerations may apply here...
489 (autoload-make-program (pop command-line-args-left)))
490 (batch-update-autoloads)))
492 (defun w32-append-code-lines (orig extra)
493 "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
495 This function saves all buffers and kills the Emacs session, without asking
496 for any permissions.
498 This is required because the Windows build environment is not required
499 to include Sed, which is used by leim/Makefile.in to do the job."
500 (find-file orig)
501 (goto-char (point-max))
502 (insert-file-contents extra)
503 (delete-matching-lines "^$\\|^;")
504 (save-buffers-kill-emacs t))
506 ;; arch-tag: c49b48cc-0f4f-454f-a274-c2dc34815e14
507 ;;; w32-fns.el ends here