* term.c: Include stdarg.h.
[bpt/emacs.git] / lisp / dos-fns.el
CommitLineData
e8af40ee 1;;; dos-fns.el --- MS-Dos specific functions
007c61fa 2
e91081eb 3;; Copyright (C) 1991, 1993, 1995, 1996, 2001, 2002, 2003, 2004,
d7a0267c 4;; 2005, 2006, 2007 Free Software Foundation, Inc.
007c61fa 5
0acdb863 6;; Maintainer: Morten Welinder <terra@diku.dk>
007c61fa
RS
7;; Keywords: internal
8
9;; This file is part of GNU Emacs.
10
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
b4aa6026 13;; the Free Software Foundation; either version 3, or (at your option)
007c61fa
RS
14;; any later version.
15
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.
20
21;; You should have received a copy of the GNU General Public License
b578f267 22;; along with GNU Emacs; see the file COPYING. If not, write to the
086add15
LK
23;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
24;; Boston, MA 02110-1301, USA.
007c61fa
RS
25
26;;; Commentary:
27
28;; Part of this code is taken from (or derived from) demacs.
29
30;;; Code:
31
44998f5b
RS
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.
915b0bf0
JB
35This means to guarantee valid names and perhaps to canonicalize
36certain patterns.
37
38On Windows and DOS, replace invalid characters. On DOS, make
39sure to obey the 8.3 limitations. On Windows, turn Cygwin names
40into native names, and also turn slashes into backslashes if the
41shell requires it (see `w32-shell-dos-semantics')."
09def38b 42 (if (or (not (stringp filename))
9d47450f
EZ
43 ;; This catches the case where FILENAME is "x:" or "x:/" or
44 ;; "/", thus preventing infinite recursion.
45 (string-match "\\`\\([a-zA-Z]:\\)?[/\\]?\\'" filename))
9c777199 46 filename
9d47450f
EZ
47 (let ((flen (length filename)))
48 ;; If FILENAME has a trailing slash, remove it and recurse.
49 (if (memq (aref filename (1- flen)) '(?/ ?\\))
71296446 50 (concat (convert-standard-filename
9d47450f
EZ
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)
09def38b
EZ
75 ;; Replace characters that are invalid even on Windows.
76 (while (setq i (string-match "[?*:<>|\"\000-\037]" string))
9d47450f
EZ
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 ?_))
a007e4e3
EZ
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 ?\.))
9d47450f
EZ
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 ?_))
9d47450f 98 ;; If we don't have a period in the first 8 chars, insert one.
a007e4e3
EZ
99 ;; This enables to have 3 more characters from the original
100 ;; name in the extension.
9d47450f
EZ
101 (if (> (or (string-match "\\." string) (length string))
102 8)
103 (setq string
104 (concat (substring string 0 8)
105 "."
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.
a007e4e3 113 ;; (DOS doesn't allow more than one period.)
9d47450f
EZ
114 (while (string-match "\\." string (1+ firstdot))
115 (setq i (string-match "\\." string (1+ firstdot)))
116 (aset string i ?_))
a007e4e3
EZ
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 '(?~ ?#))
9d47450f
EZ
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))))))
44998f5b 129
51f32106 130(defun dos-8+3-filename (filename)
a9d36252
EZ
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)) '(?/ ?\\))
51f32106 138 (concat (dos-8+3-filename (substring filename 0 (1- flen)))
a9d36252
EZ
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)
170 "."
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) '(?/ ?\\)))
51f32106 182 (concat (dos-8+3-filename (substring dir 0 dlen-m-1))
a9d36252
EZ
183 "/")
184 ;; Recurse to truncate the leading directories.
51f32106 185 (dos-8+3-filename dir))
a9d36252
EZ
186 string))))))
187
feb65403
RS
188;; See dos-vars.el for defcustom.
189(defvar msdos-shells)
007c61fa 190
224116b8
AI
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))))
197
198(add-hook 'before-init-hook 'set-default-process-coding-system)
199
cafef6ab 200(defvar register-name-alist
007c61fa 201 '((ax . 0) (bx . 1) (cx . 2) (dx . 3) (si . 4) (di . 5)
21f2acd3
RS
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))))
007c61fa
RS
205
206(defun make-register ()
207 (make-vector 8 0))
208
209(defun register-value (regs name)
21f2acd3 210 (let ((where (cdr (assoc name register-name-alist))))
007c61fa
RS
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))))
219
220(defun set-register-value (regs name value)
221 (and (numberp value)
21f2acd3
RS
222 (>= value 0)
223 (let ((where (cdr (assoc name register-name-alist))))
007c61fa 224 (cond ((consp where)
21f2acd3
RS
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))))))
007c61fa 232 ((numberp where)
21f2acd3 233 (aset regs where (logand value 65535))))))
007c61fa
RS
234 regs)
235
236(defsubst intdos (regs)
237 (int86 33 regs))
238
70662974
RS
239;; Backward compatibility for obsolescent functions which
240;; set screen size.
241
242(defun mode25 ()
243 "Changes the number of screen rows to 25."
244 (interactive)
245 (set-frame-size (selected-frame) 80 25))
246
247(defun mode4350 ()
248 "Changes the number of rows to 43 or 50.
249Emacs always tries to set the screen height to 50 rows first.
250If this fails, it will try to set it to 43 rows, on the assumption
251that 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)))
257
868c7abd
RS
258(provide 'dos-fns)
259
ab5796a9 260;;; arch-tag: 00b03579-8ebb-4a02-8762-5c5a929774ad
e8af40ee 261;;; dos-fns.el ends here