w32-fns.el 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339
  1. ;;; w32-fns.el --- Lisp routines for 32-bit Windows
  2. ;; Copyright (C) 1994, 2001-2017 Free Software Foundation, Inc.
  3. ;; Author: Geoff Voelker <voelker@cs.washington.edu>
  4. ;; Keywords: internal
  5. ;; Package: emacs
  6. ;; This file is part of GNU Emacs.
  7. ;; GNU Emacs is free software: you can redistribute it and/or modify
  8. ;; it under the terms of the GNU General Public License as published by
  9. ;; the Free Software Foundation, either version 3 of the License, or
  10. ;; (at your option) any later version.
  11. ;; GNU Emacs is distributed in the hope that it will be useful,
  12. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  14. ;; GNU General Public License for more details.
  15. ;; You should have received a copy of the GNU General Public License
  16. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  17. ;;; Commentary:
  18. ;;; Code:
  19. (require 'w32-vars)
  20. (defvar explicit-shell-file-name)
  21. ;;;; Function keys
  22. (declare-function set-message-beep "w32fns.c" (sound))
  23. (declare-function w32-get-locale-info "w32proc.c" (lcid &optional longform))
  24. (declare-function w32-get-valid-locale-ids "w32proc.c" ())
  25. ;; Map all versions of a filename (8.3, longname, mixed case) to the
  26. ;; same buffer.
  27. (setq find-file-visit-truename t)
  28. (defun w32-shell-name ()
  29. "Return the name of the shell being used."
  30. (or (bound-and-true-p shell-file-name)
  31. (getenv "ESHELL")
  32. (getenv "SHELL")
  33. (and (fboundp 'w32-using-nt) (w32-using-nt) "cmd.exe")
  34. "command.com"))
  35. (defun w32-system-shell-p (shell-name)
  36. (and shell-name
  37. (member (downcase (file-name-nondirectory shell-name))
  38. w32-system-shells)))
  39. (defun w32-shell-dos-semantics ()
  40. "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
  41. (or (w32-system-shell-p (w32-shell-name))
  42. (and (member (downcase (file-name-nondirectory (w32-shell-name)))
  43. '("cmdproxy" "cmdproxy.exe"))
  44. (w32-system-shell-p (getenv "COMSPEC")))))
  45. (defvar w32-quote-process-args) ;; defined in w32proc.c
  46. (defun w32-check-shell-configuration ()
  47. "Check the configuration of shell variables on Windows.
  48. This function is invoked after loading the init files and processing
  49. the command line arguments. It issues a warning if the user or site
  50. has configured the shell with inappropriate settings."
  51. (interactive)
  52. (let ((prev-buffer (current-buffer))
  53. (buffer (get-buffer-create "*Shell Configuration*"))
  54. (system-shell))
  55. (set-buffer buffer)
  56. (erase-buffer)
  57. (if (w32-system-shell-p (getenv "ESHELL"))
  58. (insert (format "Warning! The ESHELL environment variable uses %s.
  59. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  60. (getenv "ESHELL"))))
  61. (if (w32-system-shell-p (getenv "SHELL"))
  62. (insert (format "Warning! The SHELL environment variable uses %s.
  63. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  64. (getenv "SHELL"))))
  65. (if (w32-system-shell-p shell-file-name)
  66. (insert (format "Warning! shell-file-name uses %s.
  67. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  68. shell-file-name)))
  69. (if (and (boundp 'explicit-shell-file-name)
  70. (w32-system-shell-p explicit-shell-file-name))
  71. (insert (format "Warning! explicit-shell-file-name uses %s.
  72. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  73. explicit-shell-file-name)))
  74. (setq system-shell (> (buffer-size) 0))
  75. ;; Allow user to specify that they really do want to use one of the
  76. ;; "system" shells, despite the drawbacks, but still warn if
  77. ;; shell-command-switch doesn't match.
  78. (if w32-allow-system-shell
  79. (erase-buffer))
  80. (cond (system-shell
  81. ;; System shells.
  82. (if (string-equal "-c" shell-command-switch)
  83. (insert "Warning! shell-command-switch is \"-c\".
  84. You should set this to \"/c\" when using a system shell.\n\n"))
  85. (if w32-quote-process-args
  86. (insert "Warning! w32-quote-process-args is t.
  87. You should set this to nil when using a system shell.\n\n")))
  88. ;; Non-system shells.
  89. (t
  90. (if (string-equal "/c" shell-command-switch)
  91. (insert "Warning! shell-command-switch is \"/c\".
  92. You should set this to \"-c\" when using a non-system shell.\n\n"))
  93. (if (not w32-quote-process-args)
  94. (insert "Warning! w32-quote-process-args is nil.
  95. You should set this to t when using a non-system shell.\n\n"))))
  96. (if (> (buffer-size) 0)
  97. (display-buffer buffer)
  98. (kill-buffer buffer))
  99. (set-buffer prev-buffer)))
  100. (add-hook 'after-init-hook 'w32-check-shell-configuration)
  101. ;; Override setting chosen at startup.
  102. (defun w32-set-default-process-coding-system ()
  103. ;; Most programs on Windows will accept Unix line endings on input
  104. ;; (and some programs ported from Unix require it) but most will
  105. ;; produce DOS line endings on output.
  106. (setq default-process-coding-system
  107. (if (default-value 'enable-multibyte-characters)
  108. '(undecided-dos . undecided-unix)
  109. '(raw-text-dos . raw-text-unix)))
  110. ;; Make cmdproxy default to using DOS line endings for input,
  111. ;; because some Windows programs (including command.com) require it.
  112. (add-to-list 'process-coding-system-alist
  113. `("[cC][mM][dD][pP][rR][oO][xX][yY]"
  114. . ,(if (default-value 'enable-multibyte-characters)
  115. '(undecided-dos . undecided-dos)
  116. '(raw-text-dos . raw-text-dos))))
  117. ;; plink needs DOS input when entering the password.
  118. (add-to-list 'process-coding-system-alist
  119. `("[pP][lL][iI][nN][kK]"
  120. . ,(if (default-value 'enable-multibyte-characters)
  121. '(undecided-dos . undecided-dos)
  122. '(raw-text-dos . raw-text-dos)))))
  123. (define-obsolete-function-alias 'set-default-process-coding-system
  124. #'w32-set-default-process-coding-system "26.1")
  125. (add-hook 'before-init-hook #'w32-set-default-process-coding-system)
  126. ;;; Basic support functions for managing Emacs's locale setting
  127. (defvar w32-valid-locales nil
  128. "List of locale ids known to be supported.")
  129. ;; This is the brute-force version; an efficient version is now
  130. ;; built-in though.
  131. (if (not (fboundp 'w32-get-valid-locale-ids))
  132. (defun w32-get-valid-locale-ids ()
  133. "Return list of all valid Windows locale ids."
  134. (let ((i 65535)
  135. locales)
  136. (while (> i 0)
  137. (if (w32-get-locale-info i)
  138. (setq locales (cons i locales)))
  139. (setq i (1- i)))
  140. locales)))
  141. (defun w32-list-locales ()
  142. "List the name and id of all locales supported by Windows."
  143. (interactive)
  144. (when (null w32-valid-locales)
  145. (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
  146. (with-output-to-temp-buffer "*Supported Locales*"
  147. (princ "LCID\tAbbrev\tFull name\n\n")
  148. (dolist (locale w32-valid-locales)
  149. (princ (format "%d\t%s\t%s\n"
  150. locale
  151. (w32-get-locale-info locale)
  152. (w32-get-locale-info locale t))))))
  153. ;; The variable source-directory is used to initialize Info-directory-list.
  154. ;; However, the common case is that Emacs is being used from a binary
  155. ;; distribution, and the value of source-directory is meaningless in that
  156. ;; case. Even worse, source-directory can refer to a directory on a drive
  157. ;; on the build machine that happens to be a removable drive on the user's
  158. ;; machine. When this happens, Emacs tries to access the removable drive
  159. ;; and produces the abort/retry/ignore dialog. Since we do not use
  160. ;; source-directory, set it to something that is a reasonable approximation
  161. ;; on the user's machine.
  162. ;;(add-hook 'before-init-hook
  163. ;; (lambda ()
  164. ;; (setq source-directory (file-name-as-directory
  165. ;; (expand-file-name ".." exec-directory)))))
  166. (defun w32-convert-standard-filename (filename)
  167. "Convert a standard file's name to something suitable for MS-Windows.
  168. This means to guarantee valid names and perhaps to canonicalize
  169. certain patterns.
  170. This function is called by `convert-standard-filename'.
  171. Replace invalid characters and turn Cygwin names into native
  172. names."
  173. (save-match-data
  174. (let ((name
  175. (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
  176. (replace-match "\\1:/" t nil filename)
  177. (copy-sequence filename)))
  178. (start 0))
  179. ;; leave ':' if part of drive specifier
  180. (if (and (> (length name) 1)
  181. (eq (aref name 1) ?:))
  182. (setq start 2))
  183. ;; destructively replace invalid filename characters with !
  184. (while (string-match "[?*:<>|\"\000-\037]" name start)
  185. (aset name (match-beginning 0) ?!)
  186. (setq start (match-end 0)))
  187. name)))
  188. (defun w32-set-system-coding-system (coding-system)
  189. "Set the coding system used by the Windows system to CODING-SYSTEM.
  190. This is used for things like passing font names with non-ASCII
  191. characters in them to the system. For a list of possible values of
  192. CODING-SYSTEM, use \\[list-coding-systems].
  193. This function is provided for backward compatibility, since
  194. `w32-system-coding-system' is now an alias for `locale-coding-system'."
  195. (interactive
  196. (list (let ((default locale-coding-system))
  197. (read-coding-system
  198. (format "Coding system for system calls (default %s): "
  199. default)
  200. default))))
  201. (check-coding-system coding-system)
  202. (setq locale-coding-system coding-system))
  203. (define-obsolete-function-alias 'set-w32-system-coding-system
  204. #'w32-set-system-coding-system "26.1")
  205. ;; locale-coding-system was introduced to do the same thing as
  206. ;; w32-system-coding-system. Use that instead.
  207. (defvaralias 'w32-system-coding-system 'locale-coding-system)
  208. ;; Set to a system sound if you want a fancy bell.
  209. (set-message-beep nil)
  210. (defvar w32-charset-info-alist) ; w32font.c
  211. (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
  212. "Function to add character sets to display with Windows fonts.
  213. Creates entries in `w32-charset-info-alist'.
  214. XLFD-CHARSET is a string which will appear in the XLFD font name to
  215. identify the character set. WINDOWS-CHARSET is a symbol identifying
  216. the Windows character set this maps to. For the list of possible
  217. values, see the documentation for `w32-charset-info-alist'. CODEPAGE
  218. can be a numeric codepage that Windows uses to display the character
  219. set, t for Unicode output with no codepage translation or nil for 8
  220. bit output with no translation."
  221. (add-to-list 'w32-charset-info-alist
  222. (cons xlfd-charset (cons windows-charset codepage))))
  223. ;; The last charset we add becomes the "preferred" charset for the return
  224. ;; value from w32-select-font etc, so list the most important charsets last.
  225. (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
  226. (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
  227. ;; The following two are included for pattern matching.
  228. (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
  229. (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
  230. (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
  231. (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
  232. (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
  233. (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
  234. (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
  235. (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
  236. (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
  237. (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
  238. (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
  239. (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
  240. (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
  241. (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
  242. (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
  243. (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
  244. (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
  245. (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
  246. (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
  247. (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
  248. (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
  249. (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
  250. (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
  251. (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
  252. (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
  253. ;; ;; If Unicode Windows charset is not defined, use ansi fonts.
  254. ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
  255. ;; Preferred names
  256. (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
  257. (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
  258. (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
  259. (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
  260. (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
  261. (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
  262. ;;;; Support for build process
  263. ;; From autoload.el
  264. (defvar autoload-make-program)
  265. (defvar generated-autoload-file)
  266. (defun w32-batch-update-autoloads ()
  267. "Like `batch-update-autoloads', but takes the name of the autoloads file
  268. from the command line.
  269. This is required because some Windows build environments, such as MSYS,
  270. munge command-line arguments that include file names to a horrible mess
  271. that Emacs is unable to cope with."
  272. (let ((generated-autoload-file
  273. (expand-file-name (pop command-line-args-left)))
  274. ;; I can only assume the same considerations may apply here...
  275. (autoload-make-program (pop command-line-args-left)))
  276. (batch-update-autoloads)))
  277. (defun w32-append-code-lines (orig extra)
  278. "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
  279. This function saves all buffers and kills the Emacs session, without asking
  280. for any permissions.
  281. This is required because the Windows build environment is not required
  282. to include Sed, which is used by leim/Makefile.in to do the job."
  283. (find-file orig)
  284. (goto-char (point-max))
  285. (insert-file-contents extra)
  286. (delete-matching-lines "^$\\|^;")
  287. (save-buffers-kill-emacs t))
  288. ;;; w32-fns.el ends here