w32-fns.el 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469
  1. ;;; w32-fns.el --- Lisp routines for 32-bit Windows
  2. ;; Copyright (C) 1994, 2001-2012 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 "w32console.c")
  23. (declare-function w32-get-clipboard-data "w32select.c")
  24. (declare-function w32-get-locale-info "w32proc.c")
  25. (declare-function w32-get-valid-locale-ids "w32proc.c")
  26. (declare-function w32-set-clipboard-data "w32select.c")
  27. ;; Map all versions of a filename (8.3, longname, mixed case) to the
  28. ;; same buffer.
  29. (setq find-file-visit-truename t)
  30. (declare-function x-server-version "w32fns.c" (&optional display))
  31. (defun w32-version ()
  32. "Return the MS-Windows version numbers.
  33. The value is a list of three integers: the major and minor version
  34. numbers, and the build number."
  35. (x-server-version))
  36. (defun w32-using-nt ()
  37. "Return non-nil if running on a Windows NT descendant.
  38. That includes all Windows systems except for 9X/Me."
  39. (and (eq system-type 'windows-nt) (getenv "SystemRoot")))
  40. (defun w32-shell-name ()
  41. "Return the name of the shell being used."
  42. (or (bound-and-true-p shell-file-name)
  43. (getenv "ESHELL")
  44. (getenv "SHELL")
  45. (and (w32-using-nt) "cmd.exe")
  46. "command.com"))
  47. (defun w32-system-shell-p (shell-name)
  48. (and shell-name
  49. (member (downcase (file-name-nondirectory shell-name))
  50. w32-system-shells)))
  51. (defun w32-shell-dos-semantics ()
  52. "Return non-nil if the interactive shell being used expects MS-DOS shell semantics."
  53. (or (w32-system-shell-p (w32-shell-name))
  54. (and (member (downcase (file-name-nondirectory (w32-shell-name)))
  55. '("cmdproxy" "cmdproxy.exe"))
  56. (w32-system-shell-p (getenv "COMSPEC")))))
  57. (defvar w32-quote-process-args) ;; defined in w32proc.c
  58. (defun w32-check-shell-configuration ()
  59. "Check the configuration of shell variables on Windows.
  60. This function is invoked after loading the init files and processing
  61. the command line arguments. It issues a warning if the user or site
  62. has configured the shell with inappropriate settings."
  63. (interactive)
  64. (let ((prev-buffer (current-buffer))
  65. (buffer (get-buffer-create "*Shell Configuration*"))
  66. (system-shell))
  67. (set-buffer buffer)
  68. (erase-buffer)
  69. (if (w32-system-shell-p (getenv "ESHELL"))
  70. (insert (format "Warning! The ESHELL environment variable uses %s.
  71. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  72. (getenv "ESHELL"))))
  73. (if (w32-system-shell-p (getenv "SHELL"))
  74. (insert (format "Warning! The SHELL environment variable uses %s.
  75. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  76. (getenv "SHELL"))))
  77. (if (w32-system-shell-p shell-file-name)
  78. (insert (format "Warning! shell-file-name uses %s.
  79. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  80. shell-file-name)))
  81. (if (and (boundp 'explicit-shell-file-name)
  82. (w32-system-shell-p explicit-shell-file-name))
  83. (insert (format "Warning! explicit-shell-file-name uses %s.
  84. You probably want to change it so that it uses cmdproxy.exe instead.\n\n"
  85. explicit-shell-file-name)))
  86. (setq system-shell (> (buffer-size) 0))
  87. ;; Allow user to specify that they really do want to use one of the
  88. ;; "system" shells, despite the drawbacks, but still warn if
  89. ;; shell-command-switch doesn't match.
  90. (if w32-allow-system-shell
  91. (erase-buffer))
  92. (cond (system-shell
  93. ;; System shells.
  94. (if (string-equal "-c" shell-command-switch)
  95. (insert "Warning! shell-command-switch is \"-c\".
  96. You should set this to \"/c\" when using a system shell.\n\n"))
  97. (if w32-quote-process-args
  98. (insert "Warning! w32-quote-process-args is t.
  99. You should set this to nil when using a system shell.\n\n")))
  100. ;; Non-system shells.
  101. (t
  102. (if (string-equal "/c" shell-command-switch)
  103. (insert "Warning! shell-command-switch is \"/c\".
  104. You should set this to \"-c\" when using a non-system shell.\n\n"))
  105. (if (not w32-quote-process-args)
  106. (insert "Warning! w32-quote-process-args is nil.
  107. You should set this to t when using a non-system shell.\n\n"))))
  108. (if (> (buffer-size) 0)
  109. (display-buffer buffer)
  110. (kill-buffer buffer))
  111. (set-buffer prev-buffer)))
  112. (add-hook 'after-init-hook 'w32-check-shell-configuration)
  113. ;; Override setting chosen at startup.
  114. (defun set-default-process-coding-system ()
  115. ;; Most programs on Windows will accept Unix line endings on input
  116. ;; (and some programs ported from Unix require it) but most will
  117. ;; produce DOS line endings on output.
  118. (setq default-process-coding-system
  119. (if (default-value 'enable-multibyte-characters)
  120. '(undecided-dos . undecided-unix)
  121. '(raw-text-dos . raw-text-unix)))
  122. ;; Make cmdproxy default to using DOS line endings for input,
  123. ;; because some Windows programs (including command.com) require it.
  124. (add-to-list 'process-coding-system-alist
  125. `("[cC][mM][dD][pP][rR][oO][xX][yY]"
  126. . ,(if (default-value 'enable-multibyte-characters)
  127. '(undecided-dos . undecided-dos)
  128. '(raw-text-dos . raw-text-dos))))
  129. ;; plink needs DOS input when entering the password.
  130. (add-to-list 'process-coding-system-alist
  131. `("[pP][lL][iI][nN][kK]"
  132. . ,(if (default-value 'enable-multibyte-characters)
  133. '(undecided-dos . undecided-dos)
  134. '(raw-text-dos . raw-text-dos)))))
  135. (add-hook 'before-init-hook 'set-default-process-coding-system)
  136. ;;; Basic support functions for managing Emacs's locale setting
  137. (defvar w32-valid-locales nil
  138. "List of locale ids known to be supported.")
  139. ;; This is the brute-force version; an efficient version is now
  140. ;; built-in though.
  141. (if (not (fboundp 'w32-get-valid-locale-ids))
  142. (defun w32-get-valid-locale-ids ()
  143. "Return list of all valid Windows locale ids."
  144. (let ((i 65535)
  145. locales)
  146. (while (> i 0)
  147. (if (w32-get-locale-info i)
  148. (setq locales (cons i locales)))
  149. (setq i (1- i)))
  150. locales)))
  151. (defun w32-list-locales ()
  152. "List the name and id of all locales supported by Windows."
  153. (interactive)
  154. (when (null w32-valid-locales)
  155. (setq w32-valid-locales (sort (w32-get-valid-locale-ids) #'<)))
  156. (with-output-to-temp-buffer "*Supported Locales*"
  157. (princ "LCID\tAbbrev\tFull name\n\n")
  158. (dolist (locale w32-valid-locales)
  159. (princ (format "%d\t%s\t%s\n"
  160. locale
  161. (w32-get-locale-info locale)
  162. (w32-get-locale-info locale t))))))
  163. ;; Setup Info-default-directory-list to include the info directory
  164. ;; near where Emacs executable was installed. We used to set INFOPATH,
  165. ;; but when this is set Info-default-directory-list is ignored. We
  166. ;; also cannot rely upon what is set in paths.el because they assume
  167. ;; that configuration during build time is correct for runtime.
  168. (defun w32-init-info ()
  169. (let* ((instdir (file-name-directory invocation-directory))
  170. (dir1 (expand-file-name "../info/" instdir))
  171. (dir2 (expand-file-name "../../../info/" instdir)))
  172. (if (file-exists-p dir1)
  173. (setq Info-default-directory-list
  174. (append Info-default-directory-list (list dir1)))
  175. (if (file-exists-p dir2)
  176. (setq Info-default-directory-list
  177. (append Info-default-directory-list (list dir2)))))))
  178. (add-hook 'before-init-hook 'w32-init-info)
  179. ;; The variable source-directory is used to initialize Info-directory-list.
  180. ;; However, the common case is that Emacs is being used from a binary
  181. ;; distribution, and the value of source-directory is meaningless in that
  182. ;; case. Even worse, source-directory can refer to a directory on a drive
  183. ;; on the build machine that happens to be a removable drive on the user's
  184. ;; machine. When this happens, Emacs tries to access the removable drive
  185. ;; and produces the abort/retry/ignore dialog. Since we do not use
  186. ;; source-directory, set it to something that is a reasonable approximation
  187. ;; on the user's machine.
  188. ;;(add-hook 'before-init-hook
  189. ;; (lambda ()
  190. ;; (setq source-directory (file-name-as-directory
  191. ;; (expand-file-name ".." exec-directory)))))
  192. (defun w32-convert-standard-filename (filename)
  193. "Convert a standard file's name to something suitable for MS-Windows.
  194. This means to guarantee valid names and perhaps to canonicalize
  195. certain patterns.
  196. This function is called by `convert-standard-filename'.
  197. Replace invalid characters and turn Cygwin names into native
  198. names, and also turn slashes into backslashes if the shell
  199. requires it (see `w32-shell-dos-semantics')."
  200. (save-match-data
  201. (let ((name
  202. (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
  203. (replace-match "\\1:/" t nil filename)
  204. (copy-sequence filename)))
  205. (start 0))
  206. ;; leave ':' if part of drive specifier
  207. (if (and (> (length name) 1)
  208. (eq (aref name 1) ?:))
  209. (setq start 2))
  210. ;; destructively replace invalid filename characters with !
  211. (while (string-match "[?*:<>|\"\000-\037]" name start)
  212. (aset name (match-beginning 0) ?!)
  213. (setq start (match-end 0)))
  214. ;; convert directory separators to Windows format
  215. ;; (but only if the shell in use requires it)
  216. (when (w32-shell-dos-semantics)
  217. (setq start 0)
  218. (while (string-match "/" name start)
  219. (aset name (match-beginning 0) ?\\)
  220. (setq start (match-end 0))))
  221. name)))
  222. ;;; Fix interface to (X-specific) mouse.el
  223. (defun x-set-selection (type data)
  224. "Make an X selection of type TYPE and value DATA.
  225. The argument TYPE (nil means `PRIMARY') says which selection, and
  226. DATA specifies the contents. TYPE must be a symbol. \(It can also
  227. be a string, which stands for the symbol with that name, but this
  228. is considered obsolete.) DATA may be a string, a symbol, an
  229. integer (or a cons of two integers or list of two integers).
  230. The selection may also be a cons of two markers pointing to the same buffer,
  231. or an overlay. In these cases, the selection is considered to be the text
  232. between the markers *at whatever time the selection is examined*.
  233. Thus, editing done in the buffer after you specify the selection
  234. can alter the effective value of the selection.
  235. The data may also be a vector of valid non-vector selection values.
  236. The return value is DATA.
  237. Interactively, this command sets the primary selection. Without
  238. prefix argument, it reads the selection in the minibuffer. With
  239. prefix argument, it uses the text of the region as the selection value.
  240. Note that on MS-Windows, primary and secondary selections set by Emacs
  241. are not available to other programs."
  242. (put 'x-selections (or type 'PRIMARY) data))
  243. (defun x-get-selection (&optional type _data-type)
  244. "Return the value of an X Windows selection.
  245. The argument TYPE (default `PRIMARY') says which selection,
  246. and the argument DATA-TYPE (default `STRING') says
  247. how to convert the data.
  248. TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
  249. only a few symbols are commonly used. They conventionally have
  250. all upper-case names. The most often used ones, in addition to
  251. `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
  252. DATA-TYPE is usually `STRING', but can also be one of the symbols
  253. in `selection-converter-alist', which see."
  254. (get 'x-selections (or type 'PRIMARY)))
  255. ;; x-selection-owner-p is used in simple.el
  256. (defun x-selection-owner-p (&optional type)
  257. (and (memq type '(nil PRIMARY SECONDARY))
  258. (get 'x-selections (or type 'PRIMARY))))
  259. (defun set-w32-system-coding-system (coding-system)
  260. "Set the coding system used by the Windows system to CODING-SYSTEM.
  261. This is used for things like passing font names with non-ASCII
  262. characters in them to the system. For a list of possible values of
  263. CODING-SYSTEM, use \\[list-coding-systems].
  264. This function is provided for backward compatibility, since
  265. `w32-system-coding-system' is now an alias for `locale-coding-system'."
  266. (interactive
  267. (list (let ((default locale-coding-system))
  268. (read-coding-system
  269. (format "Coding system for system calls (default %s): "
  270. default)
  271. default))))
  272. (check-coding-system coding-system)
  273. (setq locale-coding-system coding-system))
  274. ;; locale-coding-system was introduced to do the same thing as
  275. ;; w32-system-coding-system. Use that instead.
  276. (defvaralias 'w32-system-coding-system 'locale-coding-system)
  277. ;; Set to a system sound if you want a fancy bell.
  278. (set-message-beep nil)
  279. ;; The "Windows" keys on newer keyboards bring up the Start menu
  280. ;; whether you want it or not - make Emacs ignore these keystrokes
  281. ;; rather than beep.
  282. (global-set-key [lwindow] 'ignore)
  283. (global-set-key [rwindow] 'ignore)
  284. (defvar w32-charset-info-alist) ; w32font.c
  285. (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
  286. "Function to add character sets to display with Windows fonts.
  287. Creates entries in `w32-charset-info-alist'.
  288. XLFD-CHARSET is a string which will appear in the XLFD font name to
  289. identify the character set. WINDOWS-CHARSET is a symbol identifying
  290. the Windows character set this maps to. For the list of possible
  291. values, see the documentation for `w32-charset-info-alist'. CODEPAGE
  292. can be a numeric codepage that Windows uses to display the character
  293. set, t for Unicode output with no codepage translation or nil for 8
  294. bit output with no translation."
  295. (add-to-list 'w32-charset-info-alist
  296. (cons xlfd-charset (cons windows-charset codepage))))
  297. ;; The last charset we add becomes the "preferred" charset for the return
  298. ;; value from w32-select-font etc, so list the most important charsets last.
  299. (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
  300. (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
  301. ;; The following two are included for pattern matching.
  302. (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
  303. (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
  304. (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
  305. (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
  306. (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
  307. (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
  308. (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
  309. (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
  310. (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
  311. (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
  312. (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
  313. (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
  314. (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
  315. (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
  316. (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
  317. (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
  318. (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
  319. (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
  320. (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
  321. (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
  322. (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
  323. (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
  324. (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
  325. (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
  326. (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
  327. ;; ;; If Unicode Windows charset is not defined, use ansi fonts.
  328. ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
  329. ;; Preferred names
  330. (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
  331. (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
  332. (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
  333. (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
  334. (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
  335. (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
  336. (make-obsolete-variable 'w32-enable-italics
  337. 'w32-enable-synthesized-fonts "21.1")
  338. (make-obsolete-variable 'w32-charset-to-codepage-alist
  339. 'w32-charset-info-alist "21.1")
  340. ;;;; Selections
  341. ;; We keep track of the last text selected here, so we can check the
  342. ;; current selection against it, and avoid passing back our own text
  343. ;; from x-selection-value.
  344. (defvar x-last-selected-text nil)
  345. (defun x-get-selection-value ()
  346. "Return the value of the current selection.
  347. Consult the selection. Treat empty strings as if they were unset."
  348. (if x-select-enable-clipboard
  349. (let (text)
  350. ;; Don't die if x-get-selection signals an error.
  351. (condition-case c
  352. (setq text (w32-get-clipboard-data))
  353. (error (message "w32-get-clipboard-data:%s" c)))
  354. (if (string= text "") (setq text nil))
  355. (cond
  356. ((not text) nil)
  357. ((eq text x-last-selected-text) nil)
  358. ((string= text x-last-selected-text)
  359. ;; Record the newer string, so subsequent calls can use the 'eq' test.
  360. (setq x-last-selected-text text)
  361. nil)
  362. (t
  363. (setq x-last-selected-text text))))))
  364. (defalias 'x-selection-value 'x-get-selection-value)
  365. ;; Arrange for the kill and yank functions to set and check the clipboard.
  366. (setq interprogram-cut-function 'x-select-text)
  367. (setq interprogram-paste-function 'x-get-selection-value)
  368. ;;;; Support for build process
  369. ;; From autoload.el
  370. (defvar autoload-make-program)
  371. (defvar generated-autoload-file)
  372. (defun w32-batch-update-autoloads ()
  373. "Like `batch-update-autoloads', but takes the name of the autoloads file
  374. from the command line.
  375. This is required because some Windows build environments, such as MSYS,
  376. munge command-line arguments that include file names to a horrible mess
  377. that Emacs is unable to cope with."
  378. (let ((generated-autoload-file
  379. (expand-file-name (pop command-line-args-left)))
  380. ;; I can only assume the same considerations may apply here...
  381. (autoload-make-program (pop command-line-args-left)))
  382. (batch-update-autoloads)))
  383. (defun w32-append-code-lines (orig extra)
  384. "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
  385. This function saves all buffers and kills the Emacs session, without asking
  386. for any permissions.
  387. This is required because the Windows build environment is not required
  388. to include Sed, which is used by leim/Makefile.in to do the job."
  389. (find-file orig)
  390. (goto-char (point-max))
  391. (insert-file-contents extra)
  392. (delete-matching-lines "^$\\|^;")
  393. (save-buffers-kill-emacs t))
  394. ;;; w32-fns.el ends here