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 | ||
73b2c664 | 75 | ;; Don't check for untranslated file systems here. |
a750bcaa | 76 | (defun find-buffer-file-type (filename) |
73b2c664 RS |
77 | (let ((match (find-buffer-file-type-match filename)) |
78 | (code)) | |
79 | (if (not match) | |
80 | default-buffer-file-type | |
81 | (setq code (cdr match)) | |
82 | (cond ((memq code '(nil t)) code) | |
83 | ((and (symbolp code) (fboundp code)) | |
84 | (funcall code filename)))))) | |
ee425fc3 | 85 | |
99bf72f2 GV |
86 | (setq-default buffer-file-coding-system 'undecided-dos) |
87 | ||
7c621f7a | 88 | (defun find-buffer-file-type-coding-system (command) |
ee425fc3 | 89 | "Choose a coding system for a file operation. |
f473b0ca GV |
90 | If COMMAND is `insert-file-contents', the coding system is chosen based |
91 | upon the filename, the contents of `untranslated-filesystem-list' and | |
92 | `file-name-buffer-file-type-alist', and whether the file exists: | |
93 | ||
94 | If it matches in `untranslated-filesystem-list': | |
95 | If the file exists: `no-conversion' | |
96 | If the file does not exist: `undecided' | |
97 | If it matches in `file-name-buffer-file-type-alist': | |
98 | If the match is t (for binary): `no-conversion' | |
99 | If the match is nil (for dos-text): `undecided-dos' | |
ee425fc3 | 100 | Otherwise: |
f473b0ca GV |
101 | If the file exists: `undecided' |
102 | If the file does not exist: `undecided-dos' | |
ee425fc3 | 103 | |
99bf72f2 GV |
104 | If COMMAND is `write-region', the coding system is chosen based upon |
105 | the value of `buffer-file-coding-system' and `buffer-file-type'. If | |
106 | `buffer-file-coding-system' is non-nil, its value is used. If it is | |
107 | nil and `buffer-file-type' is t, the coding system is `no-conversion'. | |
108 | Otherwise, it is `undecided-dos'. | |
109 | ||
110 | The two most common situations are when DOS and Unix files are read | |
111 | and written, and their names do not match in | |
112 | `untranslated-filesystem-list' and `file-name-buffer-file-type-alist'. | |
113 | In these cases, the coding system initially will be `undecided'. As | |
114 | the file is read in the DOS case, the coding system will be changed to | |
115 | `undecided-dos' as CR/LFs are detected. As the file is read in the | |
116 | Unix case, the coding system will be changed to `undecided-unix' as | |
117 | LFs are detected. In both cases, `buffer-file-coding-system' will be | |
118 | set to the appropriate coding system, and the value of | |
119 | `buffer-file-coding-system' will be used when writing the file." | |
120 | ||
ee425fc3 RS |
121 | (let ((op (nth 0 command)) |
122 | (target) | |
dfbcdf5f | 123 | (binary nil) (text nil) |
73b2c664 | 124 | (undecided nil) (undecided-unix nil)) |
ee425fc3 RS |
125 | (cond ((eq op 'insert-file-contents) |
126 | (setq target (nth 1 command)) | |
73b2c664 RS |
127 | ;; First check for a file name that indicates |
128 | ;; it is truly binary. | |
129 | (setq binary (find-buffer-file-type target)) | |
130 | (cond (binary) | |
131 | ;; Next check for files that MUST use DOS eol conversion. | |
132 | ((find-buffer-file-type-match target) | |
133 | (setq text t)) | |
134 | ;; For any other existing file, decide based on contents. | |
135 | ((file-exists-p target) | |
136 | (setq undecided t)) | |
137 | ;; Next check for a non-DOS file system. | |
138 | ((untranslated-file-p target) | |
139 | (setq undecided-unix t))) | |
99bf72f2 GV |
140 | (cond (binary '(no-conversion . no-conversion)) |
141 | (text '(undecided-dos . undecided-dos)) | |
73b2c664 | 142 | (undecided-unix '(undecided-unix . undecided-unix)) |
99bf72f2 GV |
143 | (undecided '(undecided . undecided)) |
144 | (t '(undecided-dos . undecided-dos)))) | |
145 | ((eq op 'write-region) | |
146 | (if buffer-file-coding-system | |
147 | (cons buffer-file-coding-system | |
148 | buffer-file-coding-system) | |
73b2c664 RS |
149 | ;; Normally this is used only in a non-file-visiting |
150 | ;; buffer, because normally buffer-file-coding-system is non-nil | |
151 | ;; in a file-visiting buffer. | |
99bf72f2 GV |
152 | (if buffer-file-type |
153 | '(no-conversion . no-conversion) | |
154 | '(undecided-dos . undecided-dos))))))) | |
ee425fc3 RS |
155 | |
156 | (modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system) | |
a750bcaa RS |
157 | |
158 | (defun find-file-binary (filename) | |
159 | "Visit file FILENAME and treat it as binary." | |
160 | (interactive "FFind file binary: ") | |
161 | (let ((file-name-buffer-file-type-alist '(("" . t)))) | |
162 | (find-file filename))) | |
163 | ||
164 | (defun find-file-text (filename) | |
165 | "Visit file FILENAME and treat it as a text file." | |
166 | (interactive "FFind file text: ") | |
167 | (let ((file-name-buffer-file-type-alist '(("" . nil)))) | |
168 | (find-file filename))) | |
169 | ||
99bf72f2 | 170 | (defun find-file-not-found-set-buffer-file-coding-system () |
a750bcaa RS |
171 | (save-excursion |
172 | (set-buffer (current-buffer)) | |
99bf72f2 GV |
173 | (let* ((dummy-insert-op (list 'insert-file-contents (buffer-file-name))) |
174 | (coding-system-pair | |
175 | (find-buffer-file-type-coding-system dummy-insert-op))) | |
176 | (setq buffer-file-coding-system (car coding-system-pair)) | |
177 | (setq buffer-file-type (eq buffer-file-coding-system 'no-conversion))))) | |
178 | ||
179 | ;;; To set the default coding system on new files. | |
180 | (add-hook 'find-file-not-found-hooks | |
181 | 'find-file-not-found-set-buffer-file-coding-system) | |
a750bcaa RS |
182 | |
183 | ;;; To accomodate filesystems that do not require CR/LF translation. | |
184 | (defvar untranslated-filesystem-list nil | |
185 | "List of filesystems that require no CR/LF translation when reading | |
186 | and writing files. Each filesystem in the list is a string naming | |
187 | the directory prefix corresponding to the filesystem.") | |
188 | ||
189 | (defun untranslated-canonical-name (filename) | |
190 | "Return FILENAME in a canonicalized form for use with the functions | |
191 | dealing with untranslated filesystems." | |
192 | (if (memq system-type '(ms-dos windows-nt)) | |
b63f9ba1 | 193 | ;; The canonical form for DOS/W32 is with A-Z downcased and all |
a750bcaa RS |
194 | ;; directory separators changed to directory-sep-char. |
195 | (let ((name nil)) | |
196 | (setq name (mapconcat | |
197 | '(lambda (char) | |
198 | (if (and (<= ?A char) (<= char ?Z)) | |
199 | (char-to-string (+ (- char ?A) ?a)) | |
200 | (char-to-string char))) | |
201 | filename nil)) | |
202 | ;; Use expand-file-name to canonicalize directory separators, except | |
203 | ;; with bare drive letters (which would have the cwd appended). | |
204 | (if (string-match "^.:$" name) | |
205 | name | |
206 | (expand-file-name name))) | |
207 | filename)) | |
208 | ||
209 | (defun untranslated-file-p (filename) | |
210 | "Return t if FILENAME is on a filesystem that does not require | |
211 | CR/LF translation, and nil otherwise." | |
212 | (let ((fs (untranslated-canonical-name filename)) | |
213 | (ufs-list untranslated-filesystem-list) | |
214 | (found nil)) | |
215 | (while (and (not found) ufs-list) | |
216 | (if (string-match (concat "^" (car ufs-list)) fs) | |
217 | (setq found t) | |
218 | (setq ufs-list (cdr ufs-list)))) | |
219 | found)) | |
220 | ||
221 | (defun add-untranslated-filesystem (filesystem) | |
222 | "Add FILESYSTEM to the list of filesystems that do not require | |
223 | CR/LF translation. FILESYSTEM is a string containing the directory | |
224 | prefix corresponding to the filesystem. For example, for a Unix | |
225 | filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"." | |
60382faa | 226 | (interactive "fUntranslated file system: ") |
a750bcaa RS |
227 | (let ((fs (untranslated-canonical-name filesystem))) |
228 | (if (member fs untranslated-filesystem-list) | |
229 | untranslated-filesystem-list | |
230 | (setq untranslated-filesystem-list | |
231 | (cons fs untranslated-filesystem-list))))) | |
232 | ||
233 | (defun remove-untranslated-filesystem (filesystem) | |
234 | "Remove FILESYSTEM from the list of filesystems that do not require | |
235 | CR/LF translation. FILESYSTEM is a string containing the directory | |
236 | prefix corresponding to the filesystem. For example, for a Unix | |
237 | filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"." | |
60382faa | 238 | (interactive "fUntranslated file system: ") |
a750bcaa RS |
239 | (setq untranslated-filesystem-list |
240 | (delete (untranslated-canonical-name filesystem) | |
241 | untranslated-filesystem-list))) | |
242 | ||
ee425fc3 RS |
243 | ;; Process I/O decoding and encoding. |
244 | ||
7c621f7a | 245 | (defun find-binary-process-coding-system (command) |
ee425fc3 RS |
246 | "Choose a coding system for process I/O. |
247 | The coding system for decode is 'no-conversion' if 'binary-process-output' | |
f473b0ca | 248 | is non-nil, and 'undecided-dos' otherwise. Similarly, the coding system |
ee425fc3 | 249 | for encode is 'no-conversion' if 'binary-process-input' is non-nil, |
f473b0ca GV |
250 | and 'undecided-dos' otherwise." |
251 | (let ((decode 'undecided-dos) | |
252 | (encode 'undecided-dos)) | |
ee425fc3 RS |
253 | (if binary-process-output |
254 | (setq decode 'no-conversion)) | |
255 | (if binary-process-input | |
256 | (setq encode 'no-conversion)) | |
257 | (cons decode encode))) | |
258 | ||
259 | (modify-coding-system-alist 'process "" 'find-binary-process-coding-system) | |
260 | ||
261 | ||
b55edb63 | 262 | (provide 'dos-w32) |
a750bcaa | 263 | |
b55edb63 | 264 | ;;; dos-w32.el ends here |