Rob Riepel <riepel at networking.stanford.edu>
[emacs.git] / lisp / dos-fns.el
blobc66cbc7933a54462f3a993ee5e4d643a90f48777
1 ;;; dos-fns.el --- MS-Dos specific functions
3 ;; Copyright (C) 1991, 1993, 1995, 1996, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007 Free Software Foundation, Inc.
6 ;; Maintainer: Morten Welinder <terra@diku.dk>
7 ;; Keywords: internal
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 3, or (at your option)
14 ;; any later version.
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to the
23 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24 ;; Boston, MA 02110-1301, USA.
26 ;;; Commentary:
28 ;; Part of this code is taken from (or derived from) demacs.
30 ;;; Code:
32 ;; This overrides a trivial definition in files.el.
33 (defun convert-standard-filename (filename)
34 "Convert a standard file's name to something suitable for the current OS.
35 This means to guarantee valid names and perhaps to canonicalize
36 certain patterns.
38 On Windows and DOS, replace invalid characters. On DOS, make
39 sure to obey the 8.3 limitations. On Windows, turn Cygwin names
40 into native names, and also turn slashes into backslashes if the
41 shell requires it (see `w32-shell-dos-semantics')."
42 (if (or (not (stringp filename))
43 ;; This catches the case where FILENAME is "x:" or "x:/" or
44 ;; "/", thus preventing infinite recursion.
45 (string-match "\\`\\([a-zA-Z]:\\)?[/\\]?\\'" filename))
46 filename
47 (let ((flen (length filename)))
48 ;; If FILENAME has a trailing slash, remove it and recurse.
49 (if (memq (aref filename (1- flen)) '(?/ ?\\))
50 (concat (convert-standard-filename
51 (substring filename 0 (1- flen)))
52 "/")
53 (let* (;; ange-ftp gets in the way for names like "/foo:bar".
54 ;; We need to inhibit all magic file names, because
55 ;; remote file names should never be passed through
56 ;; this function, as they are not meant for the local
57 ;; filesystem!
58 (file-name-handler-alist nil)
59 (dir
60 ;; If FILENAME is "x:foo", file-name-directory returns
61 ;; "x:/bar/baz", substituting the current working
62 ;; directory on drive x:. We want to be left with "x:"
63 ;; instead.
64 (if (and (< 1 flen)
65 (eq (aref filename 1) ?:)
66 (null (string-match "[/\\]" filename)))
67 (substring filename 0 2)
68 (file-name-directory filename)))
69 (dlen-m-1 (1- (length dir)))
70 (string (copy-sequence (file-name-nondirectory filename)))
71 (lastchar (aref string (1- (length string))))
72 i firstdot)
73 (cond
74 ((msdos-long-file-names)
75 ;; Replace characters that are invalid even on Windows.
76 (while (setq i (string-match "[?*:<>|\"\000-\037]" string))
77 (aset string i ?!)))
78 ((not (member string '("" "." "..")))
79 ;; Change a leading period to a leading underscore.
80 (if (= (aref string 0) ?.)
81 (aset string 0 ?_))
82 ;; If the name is longer than 8 chars, and doesn't have a
83 ;; period, and we have a dash or underscore that isn't too
84 ;; close to the beginning, change that to a period. This
85 ;; is so we could salvage more characters of the original
86 ;; name by pushing them into the extension.
87 (if (and (not (string-match "\\." string))
88 (> (length string) 8)
89 ;; We don't gain anything if we put the period closer
90 ;; than 5 chars from the beginning (5 + 3 = 8).
91 (setq i (string-match "[-_]" string 5)))
92 (aset string i ?\.))
93 ;; Get rid of invalid characters.
94 (while (setq i (string-match
95 "[^-a-zA-Z0-9_.%~^$!#&{}@`'()\200-\376]"
96 string))
97 (aset string i ?_))
98 ;; If we don't have a period in the first 8 chars, insert one.
99 ;; This enables to have 3 more characters from the original
100 ;; name in the extension.
101 (if (> (or (string-match "\\." string) (length string))
103 (setq string
104 (concat (substring string 0 8)
106 (substring string 8))))
107 (setq firstdot (or (string-match "\\." string)
108 (1- (length string))))
109 ;; Truncate to 3 chars after the first period.
110 (if (> (length string) (+ firstdot 4))
111 (setq string (substring string 0 (+ firstdot 4))))
112 ;; Change all periods except the first one into underscores.
113 ;; (DOS doesn't allow more than one period.)
114 (while (string-match "\\." string (1+ firstdot))
115 (setq i (string-match "\\." string (1+ firstdot)))
116 (aset string i ?_))
117 ;; If the last character of the original filename was `~' or `#',
118 ;; make sure the munged name ends with it also. This is so that
119 ;; backup and auto-save files retain their telltale form.
120 (if (memq lastchar '(?~ ?#))
121 (aset string (1- (length string)) lastchar))))
122 (concat (if (and (stringp dir)
123 (memq (aref dir dlen-m-1) '(?/ ?\\)))
124 (concat (convert-standard-filename
125 (substring dir 0 dlen-m-1))
126 "/")
127 (convert-standard-filename dir))
128 string))))))
130 (defun dos-8+3-filename (filename)
131 "Truncate FILENAME to DOS 8+3 limits."
132 (if (or (not (stringp filename))
133 (< (length filename) 5)) ; too short to give any trouble
134 filename
135 (let ((flen (length filename)))
136 ;; If FILENAME has a trailing slash, remove it and recurse.
137 (if (memq (aref filename (1- flen)) '(?/ ?\\))
138 (concat (dos-8+3-filename (substring filename 0 (1- flen)))
139 "/")
140 (let* (;; ange-ftp gets in the way for names like "/foo:bar".
141 ;; We need to inhibit all magic file names, because
142 ;; remote file names should never be passed through
143 ;; this function, as they are not meant for the local
144 ;; filesystem!
145 (file-name-handler-alist nil)
146 (dir
147 ;; If FILENAME is "x:foo", file-name-directory returns
148 ;; "x:/bar/baz", substituting the current working
149 ;; directory on drive x:. We want to be left with "x:"
150 ;; instead.
151 (if (and (< 1 flen)
152 (eq (aref filename 1) ?:)
153 (null (string-match "[/\\]" filename)))
154 (substring filename 0 2)
155 (file-name-directory filename)))
156 (dlen-m-1 (1- (length dir)))
157 (string (copy-sequence (file-name-nondirectory filename)))
158 (strlen (length string))
159 (lastchar (aref string (1- strlen)))
160 i firstdot)
161 (setq firstdot (string-match "\\." string))
162 (cond
163 (firstdot
164 ;; Truncate the extension to 3 characters.
165 (if (> strlen (+ firstdot 4))
166 (setq string (substring string 0 (+ firstdot 4))))
167 ;; Truncate the basename to 8 characters.
168 (if (> firstdot 8)
169 (setq string (concat (substring string 0 8)
171 (substring string (1+ firstdot))))))
172 ((> strlen 8)
173 ;; No dot; truncate file name to 8 characters.
174 (setq string (substring string 0 8))))
175 ;; If the last character of the original filename was `~',
176 ;; make sure the munged name ends with it also. This is so
177 ;; a backup file retains its final `~'.
178 (if (equal lastchar ?~)
179 (aset string (1- (length string)) lastchar))
180 (concat (if (and (stringp dir)
181 (memq (aref dir dlen-m-1) '(?/ ?\\)))
182 (concat (dos-8+3-filename (substring dir 0 dlen-m-1))
183 "/")
184 ;; Recurse to truncate the leading directories.
185 (dos-8+3-filename dir))
186 string))))))
188 ;; See dos-vars.el for defcustom.
189 (defvar msdos-shells)
191 ;;; Override setting chosen at startup.
192 (defun set-default-process-coding-system ()
193 (setq default-process-coding-system
194 (if default-enable-multibyte-characters
195 '(undecided-dos . undecided-dos)
196 '(raw-text-dos . raw-text-dos))))
198 (add-hook 'before-init-hook 'set-default-process-coding-system)
200 (defvar register-name-alist
201 '((ax . 0) (bx . 1) (cx . 2) (dx . 3) (si . 4) (di . 5)
202 (cflag . 6) (flags . 7)
203 (al . (0 . 0)) (bl . (1 . 0)) (cl . (2 . 0)) (dl . (3 . 0))
204 (ah . (0 . 1)) (bh . (1 . 1)) (ch . (2 . 1)) (dh . (3 . 1))))
206 (defun make-register ()
207 (make-vector 8 0))
209 (defun register-value (regs name)
210 (let ((where (cdr (assoc name register-name-alist))))
211 (cond ((consp where)
212 (let ((tem (aref regs (car where))))
213 (if (zerop (cdr where))
214 (% tem 256)
215 (/ tem 256))))
216 ((numberp where)
217 (aref regs where))
218 (t nil))))
220 (defun set-register-value (regs name value)
221 (and (numberp value)
222 (>= value 0)
223 (let ((where (cdr (assoc name register-name-alist))))
224 (cond ((consp where)
225 (let ((tem (aref regs (car where)))
226 (value (logand value 255)))
227 (aset regs
228 (car where)
229 (if (zerop (cdr where))
230 (logior (logand tem 65280) value)
231 (logior (logand tem 255) (lsh value 8))))))
232 ((numberp where)
233 (aset regs where (logand value 65535))))))
234 regs)
236 (defsubst intdos (regs)
237 (int86 33 regs))
239 ;; Backward compatibility for obsolescent functions which
240 ;; set screen size.
242 (defun mode25 ()
243 "Changes the number of screen rows to 25."
244 (interactive)
245 (set-frame-size (selected-frame) 80 25))
247 (defun mode4350 ()
248 "Changes the number of rows to 43 or 50.
249 Emacs always tries to set the screen height to 50 rows first.
250 If this fails, it will try to set it to 43 rows, on the assumption
251 that your video hardware might not support 50-line mode."
252 (interactive)
253 (set-frame-size (selected-frame) 80 50)
254 (if (eq (frame-height (selected-frame)) 50)
255 nil ; the original built-in function returned nil
256 (set-frame-size (selected-frame) 80 43)))
258 (provide 'dos-fns)
260 ;;; arch-tag: 00b03579-8ebb-4a02-8762-5c5a929774ad
261 ;;; dos-fns.el ends here