w32-fns.el 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451
  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. ;; The variable source-directory is used to initialize Info-directory-list.
  164. ;; However, the common case is that Emacs is being used from a binary
  165. ;; distribution, and the value of source-directory is meaningless in that
  166. ;; case. Even worse, source-directory can refer to a directory on a drive
  167. ;; on the build machine that happens to be a removable drive on the user's
  168. ;; machine. When this happens, Emacs tries to access the removable drive
  169. ;; and produces the abort/retry/ignore dialog. Since we do not use
  170. ;; source-directory, set it to something that is a reasonable approximation
  171. ;; on the user's machine.
  172. ;;(add-hook 'before-init-hook
  173. ;; (lambda ()
  174. ;; (setq source-directory (file-name-as-directory
  175. ;; (expand-file-name ".." exec-directory)))))
  176. (defun w32-convert-standard-filename (filename)
  177. "Convert a standard file's name to something suitable for MS-Windows.
  178. This means to guarantee valid names and perhaps to canonicalize
  179. certain patterns.
  180. This function is called by `convert-standard-filename'.
  181. Replace invalid characters and turn Cygwin names into native
  182. names, and also turn slashes into backslashes if the shell
  183. requires it (see `w32-shell-dos-semantics')."
  184. (save-match-data
  185. (let ((name
  186. (if (string-match "\\`/cygdrive/\\([a-zA-Z]\\)/" filename)
  187. (replace-match "\\1:/" t nil filename)
  188. (copy-sequence filename)))
  189. (start 0))
  190. ;; leave ':' if part of drive specifier
  191. (if (and (> (length name) 1)
  192. (eq (aref name 1) ?:))
  193. (setq start 2))
  194. ;; destructively replace invalid filename characters with !
  195. (while (string-match "[?*:<>|\"\000-\037]" name start)
  196. (aset name (match-beginning 0) ?!)
  197. (setq start (match-end 0)))
  198. ;; convert directory separators to Windows format
  199. ;; (but only if the shell in use requires it)
  200. (when (w32-shell-dos-semantics)
  201. (setq start 0)
  202. (while (string-match "/" name start)
  203. (aset name (match-beginning 0) ?\\)
  204. (setq start (match-end 0))))
  205. name)))
  206. ;;; Fix interface to (X-specific) mouse.el
  207. (defun x-set-selection (type data)
  208. "Make an X selection of type TYPE and value DATA.
  209. The argument TYPE (nil means `PRIMARY') says which selection, and
  210. DATA specifies the contents. TYPE must be a symbol. \(It can also
  211. be a string, which stands for the symbol with that name, but this
  212. is considered obsolete.) DATA may be a string, a symbol, an
  213. integer (or a cons of two integers or list of two integers).
  214. The selection may also be a cons of two markers pointing to the same buffer,
  215. or an overlay. In these cases, the selection is considered to be the text
  216. between the markers *at whatever time the selection is examined*.
  217. Thus, editing done in the buffer after you specify the selection
  218. can alter the effective value of the selection.
  219. The data may also be a vector of valid non-vector selection values.
  220. The return value is DATA.
  221. Interactively, this command sets the primary selection. Without
  222. prefix argument, it reads the selection in the minibuffer. With
  223. prefix argument, it uses the text of the region as the selection value.
  224. Note that on MS-Windows, primary and secondary selections set by Emacs
  225. are not available to other programs."
  226. (put 'x-selections (or type 'PRIMARY) data))
  227. (defun x-get-selection (&optional type _data-type)
  228. "Return the value of an X Windows selection.
  229. The argument TYPE (default `PRIMARY') says which selection,
  230. and the argument DATA-TYPE (default `STRING') says
  231. how to convert the data.
  232. TYPE may be any symbol \(but nil stands for `PRIMARY'). However,
  233. only a few symbols are commonly used. They conventionally have
  234. all upper-case names. The most often used ones, in addition to
  235. `PRIMARY', are `SECONDARY' and `CLIPBOARD'.
  236. DATA-TYPE is usually `STRING', but can also be one of the symbols
  237. in `selection-converter-alist', which see."
  238. (get 'x-selections (or type 'PRIMARY)))
  239. ;; x-selection-owner-p is used in simple.el
  240. (defun x-selection-owner-p (&optional type)
  241. (and (memq type '(nil PRIMARY SECONDARY))
  242. (get 'x-selections (or type 'PRIMARY))))
  243. (defun set-w32-system-coding-system (coding-system)
  244. "Set the coding system used by the Windows system to CODING-SYSTEM.
  245. This is used for things like passing font names with non-ASCII
  246. characters in them to the system. For a list of possible values of
  247. CODING-SYSTEM, use \\[list-coding-systems].
  248. This function is provided for backward compatibility, since
  249. `w32-system-coding-system' is now an alias for `locale-coding-system'."
  250. (interactive
  251. (list (let ((default locale-coding-system))
  252. (read-coding-system
  253. (format "Coding system for system calls (default %s): "
  254. default)
  255. default))))
  256. (check-coding-system coding-system)
  257. (setq locale-coding-system coding-system))
  258. ;; locale-coding-system was introduced to do the same thing as
  259. ;; w32-system-coding-system. Use that instead.
  260. (defvaralias 'w32-system-coding-system 'locale-coding-system)
  261. ;; Set to a system sound if you want a fancy bell.
  262. (set-message-beep nil)
  263. ;; The "Windows" keys on newer keyboards bring up the Start menu
  264. ;; whether you want it or not - make Emacs ignore these keystrokes
  265. ;; rather than beep.
  266. (global-set-key [lwindow] 'ignore)
  267. (global-set-key [rwindow] 'ignore)
  268. (defvar w32-charset-info-alist) ; w32font.c
  269. (defun w32-add-charset-info (xlfd-charset windows-charset codepage)
  270. "Function to add character sets to display with Windows fonts.
  271. Creates entries in `w32-charset-info-alist'.
  272. XLFD-CHARSET is a string which will appear in the XLFD font name to
  273. identify the character set. WINDOWS-CHARSET is a symbol identifying
  274. the Windows character set this maps to. For the list of possible
  275. values, see the documentation for `w32-charset-info-alist'. CODEPAGE
  276. can be a numeric codepage that Windows uses to display the character
  277. set, t for Unicode output with no codepage translation or nil for 8
  278. bit output with no translation."
  279. (add-to-list 'w32-charset-info-alist
  280. (cons xlfd-charset (cons windows-charset codepage))))
  281. ;; The last charset we add becomes the "preferred" charset for the return
  282. ;; value from w32-select-font etc, so list the most important charsets last.
  283. (w32-add-charset-info "iso8859-14" 'w32-charset-ansi 28604)
  284. (w32-add-charset-info "iso8859-15" 'w32-charset-ansi 28605)
  285. ;; The following two are included for pattern matching.
  286. (w32-add-charset-info "jisx0201" 'w32-charset-shiftjis 932)
  287. (w32-add-charset-info "jisx0208" 'w32-charset-shiftjis 932)
  288. (w32-add-charset-info "jisx0201-latin" 'w32-charset-shiftjis 932)
  289. (w32-add-charset-info "jisx0201-katakana" 'w32-charset-shiftjis 932)
  290. (w32-add-charset-info "ksc5601.1989" 'w32-charset-hangeul 949)
  291. (w32-add-charset-info "big5" 'w32-charset-chinesebig5 950)
  292. (w32-add-charset-info "gb2312.1980" 'w32-charset-gb2312 936)
  293. (w32-add-charset-info "ms-symbol" 'w32-charset-symbol nil)
  294. (w32-add-charset-info "ms-oem" 'w32-charset-oem 437)
  295. (w32-add-charset-info "ms-oemlatin" 'w32-charset-oem 850)
  296. (w32-add-charset-info "iso8859-2" 'w32-charset-easteurope 28592)
  297. (w32-add-charset-info "iso8859-3" 'w32-charset-turkish 28593)
  298. (w32-add-charset-info "iso8859-4" 'w32-charset-baltic 28594)
  299. (w32-add-charset-info "iso8859-6" 'w32-charset-arabic 28596)
  300. (w32-add-charset-info "iso8859-7" 'w32-charset-greek 28597)
  301. (w32-add-charset-info "iso8859-8" 'w32-charset-hebrew 1255)
  302. (w32-add-charset-info "iso8859-9" 'w32-charset-turkish 1254)
  303. (w32-add-charset-info "iso8859-13" 'w32-charset-baltic 1257)
  304. (w32-add-charset-info "koi8-r" 'w32-charset-russian 20866)
  305. (w32-add-charset-info "iso8859-5" 'w32-charset-russian 28595)
  306. (w32-add-charset-info "tis620-2533" 'w32-charset-thai 874)
  307. (w32-add-charset-info "windows-1258" 'w32-charset-vietnamese 1258)
  308. (w32-add-charset-info "ksc5601.1992" 'w32-charset-johab 1361)
  309. (w32-add-charset-info "mac-roman" 'w32-charset-mac 10000)
  310. (w32-add-charset-info "iso10646-1" 'w32-charset-default t)
  311. ;; ;; If Unicode Windows charset is not defined, use ansi fonts.
  312. ;; (w32-add-charset-info "iso10646-1" 'w32-charset-ansi t))
  313. ;; Preferred names
  314. (w32-add-charset-info "big5-0" 'w32-charset-chinesebig5 950)
  315. (w32-add-charset-info "gb2312.1980-0" 'w32-charset-gb2312 936)
  316. (w32-add-charset-info "jisx0208-sjis" 'w32-charset-shiftjis 932)
  317. (w32-add-charset-info "ksc5601.1987-0" 'w32-charset-hangeul 949)
  318. (w32-add-charset-info "tis620-0" 'w32-charset-thai 874)
  319. (w32-add-charset-info "iso8859-1" 'w32-charset-ansi 1252)
  320. (make-obsolete-variable 'w32-enable-italics
  321. 'w32-enable-synthesized-fonts "21.1")
  322. (make-obsolete-variable 'w32-charset-to-codepage-alist
  323. 'w32-charset-info-alist "21.1")
  324. ;;;; Selections
  325. ;; We keep track of the last text selected here, so we can check the
  326. ;; current selection against it, and avoid passing back our own text
  327. ;; from x-selection-value.
  328. (defvar x-last-selected-text nil)
  329. (defun x-get-selection-value ()
  330. "Return the value of the current selection.
  331. Consult the selection. Treat empty strings as if they were unset."
  332. (if x-select-enable-clipboard
  333. (let (text)
  334. ;; Don't die if x-get-selection signals an error.
  335. (condition-case c
  336. (setq text (w32-get-clipboard-data))
  337. (error (message "w32-get-clipboard-data:%s" c)))
  338. (if (string= text "") (setq text nil))
  339. (cond
  340. ((not text) nil)
  341. ((eq text x-last-selected-text) nil)
  342. ((string= text x-last-selected-text)
  343. ;; Record the newer string, so subsequent calls can use the 'eq' test.
  344. (setq x-last-selected-text text)
  345. nil)
  346. (t
  347. (setq x-last-selected-text text))))))
  348. (defalias 'x-selection-value 'x-get-selection-value)
  349. ;; Arrange for the kill and yank functions to set and check the clipboard.
  350. (setq interprogram-cut-function 'x-select-text)
  351. (setq interprogram-paste-function 'x-get-selection-value)
  352. ;;;; Support for build process
  353. ;; From autoload.el
  354. (defvar autoload-make-program)
  355. (defvar generated-autoload-file)
  356. (defun w32-batch-update-autoloads ()
  357. "Like `batch-update-autoloads', but takes the name of the autoloads file
  358. from the command line.
  359. This is required because some Windows build environments, such as MSYS,
  360. munge command-line arguments that include file names to a horrible mess
  361. that Emacs is unable to cope with."
  362. (let ((generated-autoload-file
  363. (expand-file-name (pop command-line-args-left)))
  364. ;; I can only assume the same considerations may apply here...
  365. (autoload-make-program (pop command-line-args-left)))
  366. (batch-update-autoloads)))
  367. (defun w32-append-code-lines (orig extra)
  368. "Append non-empty non-comment lines in the file EXTRA to the file ORIG.
  369. This function saves all buffers and kills the Emacs session, without asking
  370. for any permissions.
  371. This is required because the Windows build environment is not required
  372. to include Sed, which is used by leim/Makefile.in to do the job."
  373. (find-file orig)
  374. (goto-char (point-max))
  375. (insert-file-contents extra)
  376. (delete-matching-lines "^$\\|^;")
  377. (save-buffers-kill-emacs t))
  378. ;;; w32-fns.el ends here