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