dos-fns.el 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317
  1. ;;; dos-fns.el --- MS-Dos specific functions
  2. ;; Copyright (C) 1991, 1993, 1995-1996, 2001-2012
  3. ;; Free Software Foundation, Inc.
  4. ;; Maintainer: Morten Welinder <terra@diku.dk>
  5. ;; Keywords: internal
  6. ;; Package: emacs
  7. ;; This file is part of GNU Emacs.
  8. ;; GNU Emacs is free software: you can redistribute it and/or modify
  9. ;; it under the terms of the GNU General Public License as published by
  10. ;; the Free Software Foundation, either version 3 of the License, or
  11. ;; (at your option) any later version.
  12. ;; GNU Emacs is distributed in the hope that it will be useful,
  13. ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. ;; GNU General Public License for more details.
  16. ;; You should have received a copy of the GNU General Public License
  17. ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
  18. ;;; Commentary:
  19. ;; Part of this code is taken from (or derived from) demacs.
  20. ;;; Code:
  21. (declare-function int86 "dosfns.c")
  22. (declare-function msdos-long-file-names "msdos.c")
  23. ;; See convert-standard-filename in files.el.
  24. (defun dos-convert-standard-filename (filename)
  25. "Convert a standard file's name to something suitable for MS-DOS.
  26. This means to guarantee valid names and perhaps to canonicalize
  27. certain patterns.
  28. This function is called by `convert-standard-filename'.
  29. On Windows and DOS, replace invalid characters. On DOS, make
  30. sure to obey the 8.3 limitations."
  31. (if (or (not (stringp filename))
  32. ;; This catches the case where FILENAME is "x:" or "x:/" or
  33. ;; "/", thus preventing infinite recursion.
  34. (string-match "\\`\\([a-zA-Z]:\\)?[/\\]?\\'" filename))
  35. filename
  36. (let ((flen (length filename)))
  37. ;; If FILENAME has a trailing slash, remove it and recurse.
  38. (if (memq (aref filename (1- flen)) '(?/ ?\\))
  39. (concat (dos-convert-standard-filename
  40. (substring filename 0 (1- flen)))
  41. "/")
  42. (let* (;; ange-ftp gets in the way for names like "/foo:bar".
  43. ;; We need to inhibit all magic file names, because
  44. ;; remote file names should never be passed through
  45. ;; this function, as they are not meant for the local
  46. ;; filesystem!
  47. (file-name-handler-alist nil)
  48. (dir
  49. ;; If FILENAME is "x:foo", file-name-directory returns
  50. ;; "x:/bar/baz", substituting the current working
  51. ;; directory on drive x:. We want to be left with "x:"
  52. ;; instead.
  53. (if (and (< 1 flen)
  54. (eq (aref filename 1) ?:)
  55. (null (string-match "[/\\]" filename)))
  56. (substring filename 0 2)
  57. (file-name-directory filename)))
  58. (dlen-m-1 (1- (length dir)))
  59. (string (copy-sequence (file-name-nondirectory filename)))
  60. (lastchar (aref string (1- (length string))))
  61. i firstdot)
  62. (cond
  63. ((msdos-long-file-names)
  64. ;; Replace characters that are invalid even on Windows.
  65. (while (setq i (string-match "[?*:<>|\"\000-\037]" string))
  66. (aset string i ?!)))
  67. ((not (member string '("" "." "..")))
  68. ;; Change a leading period to a leading underscore.
  69. (if (= (aref string 0) ?.)
  70. (aset string 0 ?_))
  71. ;; If the name is longer than 8 chars, and doesn't have a
  72. ;; period, and we have a dash or underscore that isn't too
  73. ;; close to the beginning, change that to a period. This
  74. ;; is so we could salvage more characters of the original
  75. ;; name by pushing them into the extension.
  76. (if (and (not (string-match "\\." string))
  77. (> (length string) 8)
  78. ;; We don't gain anything if we put the period closer
  79. ;; than 5 chars from the beginning (5 + 3 = 8).
  80. (setq i (string-match "[-_]" string 5)))
  81. (aset string i ?\.))
  82. ;; Get rid of invalid characters.
  83. (while (setq i (string-match
  84. "[^-a-zA-Z0-9_.%~^$!#&{}@`'()\200-\376]"
  85. string))
  86. (aset string i ?_))
  87. ;; If we don't have a period in the first 8 chars, insert one.
  88. ;; This enables to have 3 more characters from the original
  89. ;; name in the extension.
  90. (if (> (or (string-match "\\." string) (length string))
  91. 8)
  92. (setq string
  93. (concat (substring string 0 8)
  94. "."
  95. (substring string 8))))
  96. (setq firstdot (or (string-match "\\." string)
  97. (1- (length string))))
  98. ;; Truncate to 3 chars after the first period.
  99. (if (> (length string) (+ firstdot 4))
  100. (setq string (substring string 0 (+ firstdot 4))))
  101. ;; Change all periods except the first one into underscores.
  102. ;; (DOS doesn't allow more than one period.)
  103. (while (string-match "\\." string (1+ firstdot))
  104. (setq i (string-match "\\." string (1+ firstdot)))
  105. (aset string i ?_))
  106. ;; If the last character of the original filename was `~' or `#',
  107. ;; make sure the munged name ends with it also. This is so that
  108. ;; backup and auto-save files retain their telltale form.
  109. (if (memq lastchar '(?~ ?#))
  110. (aset string (1- (length string)) lastchar))))
  111. (concat (if (and (stringp dir)
  112. (memq (aref dir dlen-m-1) '(?/ ?\\)))
  113. (concat (dos-convert-standard-filename
  114. (substring dir 0 dlen-m-1))
  115. "/")
  116. (dos-convert-standard-filename dir))
  117. string))))))
  118. (defun dos-8+3-filename (filename)
  119. "Truncate FILENAME to DOS 8+3 limits."
  120. (if (or (not (stringp filename))
  121. (< (length filename) 5)) ; too short to give any trouble
  122. filename
  123. (let ((flen (length filename)))
  124. ;; If FILENAME has a trailing slash, remove it and recurse.
  125. (if (memq (aref filename (1- flen)) '(?/ ?\\))
  126. (concat (dos-8+3-filename (substring filename 0 (1- flen)))
  127. "/")
  128. (let* (;; ange-ftp gets in the way for names like "/foo:bar".
  129. ;; We need to inhibit all magic file names, because
  130. ;; remote file names should never be passed through
  131. ;; this function, as they are not meant for the local
  132. ;; filesystem!
  133. (file-name-handler-alist nil)
  134. (dir
  135. ;; If FILENAME is "x:foo", file-name-directory returns
  136. ;; "x:/bar/baz", substituting the current working
  137. ;; directory on drive x:. We want to be left with "x:"
  138. ;; instead.
  139. (if (and (< 1 flen)
  140. (eq (aref filename 1) ?:)
  141. (null (string-match "[/\\]" filename)))
  142. (substring filename 0 2)
  143. (file-name-directory filename)))
  144. (dlen-m-1 (1- (length dir)))
  145. (string (copy-sequence (file-name-nondirectory filename)))
  146. (strlen (length string))
  147. (lastchar (aref string (1- strlen)))
  148. firstdot)
  149. (setq firstdot (string-match "\\." string))
  150. (cond
  151. (firstdot
  152. ;; Truncate the extension to 3 characters.
  153. (if (> strlen (+ firstdot 4))
  154. (setq string (substring string 0 (+ firstdot 4))))
  155. ;; Truncate the basename to 8 characters.
  156. (if (> firstdot 8)
  157. (setq string (concat (substring string 0 8)
  158. "."
  159. (substring string (1+ firstdot))))))
  160. ((> strlen 8)
  161. ;; No dot; truncate file name to 8 characters.
  162. (setq string (substring string 0 8))))
  163. ;; If the last character of the original filename was `~',
  164. ;; make sure the munged name ends with it also. This is so
  165. ;; a backup file retains its final `~'.
  166. (if (equal lastchar ?~)
  167. (aset string (1- (length string)) lastchar))
  168. (concat (if (and (stringp dir)
  169. (memq (aref dir dlen-m-1) '(?/ ?\\)))
  170. (concat (dos-8+3-filename (substring dir 0 dlen-m-1))
  171. "/")
  172. ;; Recurse to truncate the leading directories.
  173. (dos-8+3-filename dir))
  174. string))))))
  175. ;; This is for the sake of standard file names elsewhere in Emacs that
  176. ;; are defined as constant strings or via defconst, and whose
  177. ;; conversion via `dos-convert-standard-filename' does not give good
  178. ;; enough results.
  179. (defun dosified-file-name (file-name)
  180. "Return a variant of FILE-NAME that is valid on MS-DOS filesystems.
  181. This function is for those rare cases where `dos-convert-standard-filename'
  182. does not do a job that is good enough, e.g. if you need to preserve the
  183. file-name extension. It recognizes only certain specific file names
  184. that are used in Emacs Lisp sources; any other file name will be
  185. returned unaltered."
  186. (cond
  187. ;; See files.el:dir-locals-file.
  188. ((string= file-name ".dir-locals.el")
  189. "_dir-locals.el")
  190. (t
  191. file-name)))
  192. ;; See dos-vars.el for defcustom.
  193. (defvar msdos-shells)
  194. ;; Override settings chosen at startup.
  195. (defun dos-set-default-process-coding-system ()
  196. (setq default-process-coding-system
  197. (if (default-value 'enable-multibyte-characters)
  198. '(undecided-dos . undecided-dos)
  199. '(raw-text-dos . raw-text-dos))))
  200. (add-hook 'before-init-hook 'dos-set-default-process-coding-system)
  201. ;; File names defined in preloaded packages can be incorrect or
  202. ;; invalid if long file names were available during dumping, but not
  203. ;; at runtime, or vice versa, and if the default file name begins with
  204. ;; a period. Their defcustom's need to be reevaluated at startup. To
  205. ;; see if the list of defcustom's below is up to date, run the command
  206. ;; "M-x apropos-value RET ~/\. RET".
  207. (defun dos-reevaluate-defcustoms ()
  208. ;; This is not needed in Emacs 23.2 and later, as trash-directory is
  209. ;; initialized as nil. But something like this might become
  210. ;; necessary in the future, so I'm keeping it here as a reminder.
  211. ;(custom-reevaluate-setting 'trash-directory)
  212. )
  213. (add-hook 'before-init-hook 'dos-reevaluate-defcustoms)
  214. (defvar dos-register-name-alist
  215. '((ax . 0) (bx . 1) (cx . 2) (dx . 3) (si . 4) (di . 5)
  216. (cflag . 6) (flags . 7)
  217. (al . (0 . 0)) (bl . (1 . 0)) (cl . (2 . 0)) (dl . (3 . 0))
  218. (ah . (0 . 1)) (bh . (1 . 1)) (ch . (2 . 1)) (dh . (3 . 1))))
  219. (define-obsolete-variable-alias
  220. 'register-name-alist 'dos-register-name-alist "24.1")
  221. (defun dos-make-register ()
  222. (make-vector 8 0))
  223. (define-obsolete-function-alias 'make-register 'dos-make-register "24.1")
  224. (defun dos-register-value (regs name)
  225. (let ((where (cdr (assoc name dos-register-name-alist))))
  226. (cond ((consp where)
  227. (let ((tem (aref regs (car where))))
  228. (if (zerop (cdr where))
  229. (% tem 256)
  230. (/ tem 256))))
  231. ((numberp where)
  232. (aref regs where))
  233. (t nil))))
  234. (define-obsolete-function-alias 'register-value 'dos-register-value "24.1")
  235. (defun dos-set-register-value (regs name value)
  236. (and (numberp value)
  237. (>= value 0)
  238. (let ((where (cdr (assoc name dos-register-name-alist))))
  239. (cond ((consp where)
  240. (let ((tem (aref regs (car where)))
  241. (value (logand value 255)))
  242. (aset regs
  243. (car where)
  244. (if (zerop (cdr where))
  245. (logior (logand tem 65280) value)
  246. (logior (logand tem 255) (lsh value 8))))))
  247. ((numberp where)
  248. (aset regs where (logand value 65535))))))
  249. regs)
  250. (define-obsolete-function-alias
  251. 'set-register-value 'dos-set-register-value "24.1")
  252. (defsubst dos-intdos (regs)
  253. "Issue the DOS Int 21h with registers REGS.
  254. REGS should be a vector produced by `dos-make-register'
  255. and `dos-set-register-value', which see."
  256. (int86 33 regs))
  257. (define-obsolete-function-alias 'intdos 'dos-intdos "24.1")
  258. ;; Backward compatibility for obsolescent functions which
  259. ;; set screen size.
  260. (defun dos-mode25 ()
  261. "Changes the number of screen rows to 25."
  262. (interactive)
  263. (set-frame-size (selected-frame) 80 25))
  264. (define-obsolete-function-alias 'mode25 'dos-mode25 "24.1")
  265. (defun dos-mode4350 ()
  266. "Changes the number of rows to 43 or 50.
  267. Emacs always tries to set the screen height to 50 rows first.
  268. If this fails, it will try to set it to 43 rows, on the assumption
  269. that your video hardware might not support 50-line mode."
  270. (interactive)
  271. (set-frame-size (selected-frame) 80 50)
  272. (if (eq (frame-height (selected-frame)) 50)
  273. nil ; the original built-in function returned nil
  274. (set-frame-size (selected-frame) 80 43)))
  275. (define-obsolete-function-alias 'mode4350 'dos-mode4350 "24.1")
  276. (provide 'dos-fns)
  277. ;;; dos-fns.el ends here