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