Commit | Line | Data |
---|---|---|
b55edb63 | 1 | ;;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms |
a750bcaa RS |
2 | |
3 | ;; Copyright (C) 1996 Free Software Foundation, Inc. | |
4 | ||
5 | ;; Maintainer: Geoff Voelker (voelker@cs.washington.edu) | |
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 | |
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. | |
24 | ||
25 | ;;; Commentary: | |
26 | ||
27 | ;; Parts of this code are duplicated functions taken from dos-fns.el | |
28 | ;; and winnt.el. | |
29 | ||
30 | ;;; Code: | |
31 | ||
a750bcaa RS |
32 | ;; Use ";" instead of ":" as a path separator (from files.el). |
33 | (setq path-separator ";") | |
34 | ||
35 | ;; Set the null device (for compile.el). | |
36 | (setq grep-null-device "NUL") | |
37 | ||
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))) | |
41 | ||
42 | ;; For distinguishing file types based upon suffixes. | |
43 | (defvar file-name-buffer-file-type-alist | |
44 | '( | |
45 | ("[:/].*config.sys$" . nil) ; config.sys text | |
46 | ("\\.elc$" . t) ; emacs stuff | |
47 | ("\\.\\(obj\\|exe\\|com\\|lib\\|sys\\|chk\\|out\\|bin\\|ico\\|pif\\)$" . t) | |
48 | ; MS-Dos stuff | |
49 | ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t) | |
50 | ; Packers | |
51 | ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\)$" . t) | |
52 | ; Unix stuff | |
53 | ("\\.tp[ulpw]$" . t) | |
54 | ; Borland Pascal stuff | |
55 | ("[:/]tags$" . t) | |
56 | ; Emacs TAGS file | |
57 | ) | |
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.") | |
61 | ||
ee425fc3 RS |
62 | ;; Return the pair matching filename on file-name-buffer-file-type-alist, |
63 | ;; or nil otherwise. | |
64 | (defun find-buffer-file-type-match (filename) | |
65 | (let ((alist file-name-buffer-file-type-alist) | |
66 | (found nil)) | |
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))) | |
73 | found))) | |
74 | ||
a750bcaa RS |
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) | |
78 | t ; for binary | |
ee425fc3 RS |
79 | (let ((match (find-buffer-file-type-match filename)) |
80 | (code)) | |
81 | (if (not match) | |
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))))))) | |
87 | ||
99bf72f2 GV |
88 | (setq-default buffer-file-coding-system 'undecided-dos) |
89 | ||
7c621f7a | 90 | (defun find-buffer-file-type-coding-system (command) |
ee425fc3 | 91 | "Choose a coding system for a file operation. |
f473b0ca GV |
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: | |
95 | ||
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' | |
ee425fc3 | 102 | Otherwise: |
f473b0ca GV |
103 | If the file exists: `undecided' |
104 | If the file does not exist: `undecided-dos' | |
ee425fc3 | 105 | |
99bf72f2 GV |
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'. | |
111 | ||
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." | |
122 | ||
ee425fc3 RS |
123 | (let ((op (nth 0 command)) |
124 | (target) | |
dfbcdf5f | 125 | (binary nil) (text nil) |
ee425fc3 RS |
126 | (undecided nil)) |
127 | (cond ((eq op 'insert-file-contents) | |
128 | (setq target (nth 1 command)) | |
f473b0ca GV |
129 | (if (untranslated-file-p target) |
130 | (if (file-exists-p target) | |
131 | (setq undecided t) | |
132 | (setq binary t)) | |
133 | (setq binary (find-buffer-file-type target)) | |
134 | (unless binary | |
135 | (if (find-buffer-file-type-match target) | |
136 | (setq text t) | |
99bf72f2 GV |
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) | |
146 | (if buffer-file-type | |
147 | '(no-conversion . no-conversion) | |
148 | '(undecided-dos . undecided-dos))))))) | |
ee425fc3 RS |
149 | |
150 | (modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system) | |
a750bcaa RS |
151 | |
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))) | |
157 | ||
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))) | |
163 | ||
99bf72f2 | 164 | (defun find-file-not-found-set-buffer-file-coding-system () |
a750bcaa RS |
165 | (save-excursion |
166 | (set-buffer (current-buffer)) | |
99bf72f2 GV |
167 | (let* ((dummy-insert-op (list 'insert-file-contents (buffer-file-name))) |
168 | (coding-system-pair | |
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))))) | |
172 | ||
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) | |
a750bcaa RS |
176 | |
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.") | |
182 | ||
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)) | |
b63f9ba1 | 187 | ;; The canonical form for DOS/W32 is with A-Z downcased and all |
a750bcaa RS |
188 | ;; directory separators changed to directory-sep-char. |
189 | (let ((name nil)) | |
190 | (setq name (mapconcat | |
191 | '(lambda (char) | |
192 | (if (and (<= ?A char) (<= char ?Z)) | |
193 | (char-to-string (+ (- char ?A) ?a)) | |
194 | (char-to-string char))) | |
195 | filename nil)) | |
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) | |
199 | name | |
200 | (expand-file-name name))) | |
201 | filename)) | |
202 | ||
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) | |
208 | (found nil)) | |
209 | (while (and (not found) ufs-list) | |
210 | (if (string-match (concat "^" (car ufs-list)) fs) | |
211 | (setq found t) | |
212 | (setq ufs-list (cdr ufs-list)))) | |
213 | found)) | |
214 | ||
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:\"." | |
60382faa | 220 | (interactive "fUntranslated file system: ") |
a750bcaa RS |
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))))) | |
226 | ||
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:\"." | |
60382faa | 232 | (interactive "fUntranslated file system: ") |
a750bcaa RS |
233 | (setq untranslated-filesystem-list |
234 | (delete (untranslated-canonical-name filesystem) | |
235 | untranslated-filesystem-list))) | |
236 | ||
ee425fc3 RS |
237 | ;; Process I/O decoding and encoding. |
238 | ||
7c621f7a | 239 | (defun find-binary-process-coding-system (command) |
ee425fc3 RS |
240 | "Choose a coding system for process I/O. |
241 | The coding system for decode is 'no-conversion' if 'binary-process-output' | |
f473b0ca | 242 | is non-nil, and 'undecided-dos' otherwise. Similarly, the coding system |
ee425fc3 | 243 | for encode is 'no-conversion' if 'binary-process-input' is non-nil, |
f473b0ca GV |
244 | and 'undecided-dos' otherwise." |
245 | (let ((decode 'undecided-dos) | |
246 | (encode 'undecided-dos)) | |
ee425fc3 RS |
247 | (if binary-process-output |
248 | (setq decode 'no-conversion)) | |
249 | (if binary-process-input | |
250 | (setq encode 'no-conversion)) | |
251 | (cons decode encode))) | |
252 | ||
253 | (modify-coding-system-alist 'process "" 'find-binary-process-coding-system) | |
254 | ||
255 | ||
b55edb63 | 256 | (provide 'dos-w32) |
a750bcaa | 257 | |
b55edb63 | 258 | ;;; dos-w32.el ends here |