1 ;;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;; Maintainer: Geoff Voelker (voelker@cs.washington.edu)
8 ;; This file is part of GNU Emacs.
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)
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.
20 ;; You should have received a copy of the GNU General Public License
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.
27 ;; Parts of this code are duplicated functions taken from dos-fns.el
32 ;; Use ";" instead of ":" as a path separator (from files.el).
33 (setq path-separator
";")
35 ;; Set the null device (for compile.el).
36 (setq grep-null-device
"NUL")
38 ;; Set the grep regexp to match entries with drive letters.
39 (setq grep-regexp-alist
40 '(("^\\(\\([a-zA-Z]:\\)?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 3)))
42 ;; For distinguishing file types based upon suffixes.
43 (defvar file-name-buffer-file-type-alist
45 ("[:/].*config.sys$" . nil
) ; config.sys text
46 ("\\.elc$" . t
) ; emacs stuff
47 ("\\.\\(obj\\|exe\\|com\\|lib\\|sys\\|chk\\|out\\|bin\\|ico\\|pif\\)$" . t
)
49 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t
)
51 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\)$" . t
)
54 ; Borland Pascal stuff
58 "*Alist for distinguishing text files from binary files.
59 Each element has the form (REGEXP . TYPE), where REGEXP is matched
60 against the file name, and TYPE is nil for text, t for binary.")
62 ;; Return the pair matching filename on file-name-buffer-file-type-alist,
64 (defun find-buffer-file-type-match (filename)
65 (let ((alist file-name-buffer-file-type-alist
)
67 (let ((case-fold-search t
))
68 (setq filename
(file-name-sans-versions filename
))
69 (while (and (not found
) alist
)
70 (if (string-match (car (car alist
)) filename
)
71 (setq found
(car alist
)))
72 (setq alist
(cdr alist
)))
75 (defun find-buffer-file-type (filename)
76 ;; First check if file is on an untranslated filesystem, then on the alist.
77 (if (untranslated-file-p filename
)
79 (let ((match (find-buffer-file-type-match filename
))
82 default-buffer-file-type
83 (setq code
(cdr match
))
84 (cond ((memq code
'(nil t
)) code
)
85 ((and (symbolp code
) (fboundp code
))
86 (funcall code filename
)))))))
88 (setq-default buffer-file-coding-system
'undecided-dos
)
90 (defun find-buffer-file-type-coding-system (command)
91 "Choose a coding system for a file operation.
92 If COMMAND is `insert-file-contents', the coding system is chosen based
93 upon the filename, the contents of `untranslated-filesystem-list' and
94 `file-name-buffer-file-type-alist', and whether the file exists:
96 If it matches in `untranslated-filesystem-list':
97 If the file exists: `no-conversion'
98 If the file does not exist: `undecided'
99 If it matches in `file-name-buffer-file-type-alist':
100 If the match is t (for binary): `no-conversion'
101 If the match is nil (for dos-text): `undecided-dos'
103 If the file exists: `undecided'
104 If the file does not exist: `undecided-dos'
106 If COMMAND is `write-region', the coding system is chosen based upon
107 the value of `buffer-file-coding-system' and `buffer-file-type'. If
108 `buffer-file-coding-system' is non-nil, its value is used. If it is
109 nil and `buffer-file-type' is t, the coding system is `no-conversion'.
110 Otherwise, it is `undecided-dos'.
112 The two most common situations are when DOS and Unix files are read
113 and written, and their names do not match in
114 `untranslated-filesystem-list' and `file-name-buffer-file-type-alist'.
115 In these cases, the coding system initially will be `undecided'. As
116 the file is read in the DOS case, the coding system will be changed to
117 `undecided-dos' as CR/LFs are detected. As the file is read in the
118 Unix case, the coding system will be changed to `undecided-unix' as
119 LFs are detected. In both cases, `buffer-file-coding-system' will be
120 set to the appropriate coding system, and the value of
121 `buffer-file-coding-system' will be used when writing the file."
123 (let ((op (nth 0 command
))
125 (binary nil
) (text nil
)
127 (cond ((eq op
'insert-file-contents
)
128 (setq target
(nth 1 command
))
129 (if (untranslated-file-p target
)
130 (if (file-exists-p target
)
133 (setq binary
(find-buffer-file-type target
))
135 (if (find-buffer-file-type-match target
)
137 (setq undecided
(file-exists-p target
)))))
138 (cond (binary '(no-conversion . no-conversion
))
139 (text '(undecided-dos . undecided-dos
))
140 (undecided '(undecided . undecided
))
141 (t '(undecided-dos . undecided-dos
))))
142 ((eq op
'write-region
)
143 (if buffer-file-coding-system
144 (cons buffer-file-coding-system
145 buffer-file-coding-system
)
147 '(no-conversion . no-conversion
)
148 '(undecided-dos . undecided-dos
)))))))
150 (modify-coding-system-alist 'file
"" 'find-buffer-file-type-coding-system
)
152 (defun find-file-binary (filename)
153 "Visit file FILENAME and treat it as binary."
154 (interactive "FFind file binary: ")
155 (let ((file-name-buffer-file-type-alist '(("" . t
))))
156 (find-file filename
)))
158 (defun find-file-text (filename)
159 "Visit file FILENAME and treat it as a text file."
160 (interactive "FFind file text: ")
161 (let ((file-name-buffer-file-type-alist '(("" . nil
))))
162 (find-file filename
)))
164 (defun find-file-not-found-set-buffer-file-coding-system ()
166 (set-buffer (current-buffer))
167 (let* ((dummy-insert-op (list 'insert-file-contents
(buffer-file-name)))
169 (find-buffer-file-type-coding-system dummy-insert-op
)))
170 (setq buffer-file-coding-system
(car coding-system-pair
))
171 (setq buffer-file-type
(eq buffer-file-coding-system
'no-conversion
)))))
173 ;;; To set the default coding system on new files.
174 (add-hook 'find-file-not-found-hooks
175 'find-file-not-found-set-buffer-file-coding-system
)
177 ;;; To accomodate filesystems that do not require CR/LF translation.
178 (defvar untranslated-filesystem-list nil
179 "List of filesystems that require no CR/LF translation when reading
180 and writing files. Each filesystem in the list is a string naming
181 the directory prefix corresponding to the filesystem.")
183 (defun untranslated-canonical-name (filename)
184 "Return FILENAME in a canonicalized form for use with the functions
185 dealing with untranslated filesystems."
186 (if (memq system-type
'(ms-dos windows-nt
))
187 ;; The canonical form for DOS/W32 is with A-Z downcased and all
188 ;; directory separators changed to directory-sep-char.
190 (setq name
(mapconcat
192 (if (and (<= ?A char
) (<= char ?Z
))
193 (char-to-string (+ (- char ?A
) ?a
))
194 (char-to-string char
)))
196 ;; Use expand-file-name to canonicalize directory separators, except
197 ;; with bare drive letters (which would have the cwd appended).
198 (if (string-match "^.:$" name
)
200 (expand-file-name name
)))
203 (defun untranslated-file-p (filename)
204 "Return t if FILENAME is on a filesystem that does not require
205 CR/LF translation, and nil otherwise."
206 (let ((fs (untranslated-canonical-name filename
))
207 (ufs-list untranslated-filesystem-list
)
209 (while (and (not found
) ufs-list
)
210 (if (string-match (concat "^" (car ufs-list
)) fs
)
212 (setq ufs-list
(cdr ufs-list
))))
215 (defun add-untranslated-filesystem (filesystem)
216 "Add FILESYSTEM to the list of filesystems that do not require
217 CR/LF translation. FILESYSTEM is a string containing the directory
218 prefix corresponding to the filesystem. For example, for a Unix
219 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
220 (interactive "fUntranslated file system: ")
221 (let ((fs (untranslated-canonical-name filesystem
)))
222 (if (member fs untranslated-filesystem-list
)
223 untranslated-filesystem-list
224 (setq untranslated-filesystem-list
225 (cons fs untranslated-filesystem-list
)))))
227 (defun remove-untranslated-filesystem (filesystem)
228 "Remove FILESYSTEM from the list of filesystems that do not require
229 CR/LF translation. FILESYSTEM is a string containing the directory
230 prefix corresponding to the filesystem. For example, for a Unix
231 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
232 (interactive "fUntranslated file system: ")
233 (setq untranslated-filesystem-list
234 (delete (untranslated-canonical-name filesystem
)
235 untranslated-filesystem-list
)))
237 ;; Process I/O decoding and encoding.
239 (defun find-binary-process-coding-system (command)
240 "Choose a coding system for process I/O.
241 The coding system for decode is 'no-conversion' if 'binary-process-output'
242 is non-nil, and 'undecided-dos' otherwise. Similarly, the coding system
243 for encode is 'no-conversion' if 'binary-process-input' is non-nil,
244 and 'undecided-dos' otherwise."
245 (let ((decode 'undecided-dos
)
246 (encode 'undecided-dos
))
247 (if binary-process-output
248 (setq decode
'no-conversion
))
249 (if binary-process-input
250 (setq encode
'no-conversion
))
251 (cons decode encode
)))
253 (modify-coding-system-alist 'process
"" 'find-binary-process-coding-system
)
258 ;;; dos-w32.el ends here