(iswitchb-default-method): Remove :tag entries.
[bpt/emacs.git] / lisp / dos-w32.el
CommitLineData
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
17e4fb34 47 ("\\.\\(obj\\|exe\\|com\\|lib\\|sym\\|sys\\|chk\\|out\\|bin\\|ico\\|pif\\|class\\)$" . t)
a750bcaa 48 ; MS-Dos stuff
f359fb79
GV
49 ("\\.\\(dll\\|drv\\|cpl\\|scr\\vbx\\|386\\|vxd\\|fon\\|fnt\\|fot\\|ttf\\|grp\\)$" . t)
50 ; Windows stuff
51 ("\\.\\(hlp\\|bmp\\|wav\\|avi\\|mpg\\|jpg\\|tif\\mov\\au\\)" . t)
52 ; known binary data files
a750bcaa
RS
53 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t)
54 ; Packers
f359fb79 55 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\|jar\\)$" . t)
a750bcaa
RS
56 ; Unix stuff
57 ("\\.tp[ulpw]$" . t)
58 ; Borland Pascal stuff
59 ("[:/]tags$" . t)
60 ; Emacs TAGS file
61 )
62 "*Alist for distinguishing text files from binary files.
63Each element has the form (REGEXP . TYPE), where REGEXP is matched
64against the file name, and TYPE is nil for text, t for binary.")
65
ee425fc3
RS
66;; Return the pair matching filename on file-name-buffer-file-type-alist,
67;; or nil otherwise.
68(defun find-buffer-file-type-match (filename)
69 (let ((alist file-name-buffer-file-type-alist)
70 (found nil))
71 (let ((case-fold-search t))
72 (setq filename (file-name-sans-versions filename))
73 (while (and (not found) alist)
74 (if (string-match (car (car alist)) filename)
75 (setq found (car alist)))
76 (setq alist (cdr alist)))
77 found)))
78
73b2c664 79;; Don't check for untranslated file systems here.
a750bcaa 80(defun find-buffer-file-type (filename)
73b2c664
RS
81 (let ((match (find-buffer-file-type-match filename))
82 (code))
83 (if (not match)
84 default-buffer-file-type
85 (setq code (cdr match))
86 (cond ((memq code '(nil t)) code)
87 ((and (symbolp code) (fboundp code))
88 (funcall code filename))))))
ee425fc3 89
99bf72f2
GV
90(setq-default buffer-file-coding-system 'undecided-dos)
91
7c621f7a 92(defun find-buffer-file-type-coding-system (command)
ee425fc3 93 "Choose a coding system for a file operation.
f473b0ca
GV
94If COMMAND is `insert-file-contents', the coding system is chosen based
95upon the filename, the contents of `untranslated-filesystem-list' and
96`file-name-buffer-file-type-alist', and whether the file exists:
97
98 If it matches in `untranslated-filesystem-list':
99 If the file exists: `no-conversion'
100 If the file does not exist: `undecided'
101 If it matches in `file-name-buffer-file-type-alist':
102 If the match is t (for binary): `no-conversion'
103 If the match is nil (for dos-text): `undecided-dos'
ee425fc3 104 Otherwise:
f473b0ca
GV
105 If the file exists: `undecided'
106 If the file does not exist: `undecided-dos'
ee425fc3 107
99bf72f2
GV
108If COMMAND is `write-region', the coding system is chosen based upon
109the value of `buffer-file-coding-system' and `buffer-file-type'. If
110`buffer-file-coding-system' is non-nil, its value is used. If it is
111nil and `buffer-file-type' is t, the coding system is `no-conversion'.
112Otherwise, it is `undecided-dos'.
113
114The two most common situations are when DOS and Unix files are read
115and written, and their names do not match in
116`untranslated-filesystem-list' and `file-name-buffer-file-type-alist'.
117In these cases, the coding system initially will be `undecided'. As
118the file is read in the DOS case, the coding system will be changed to
119`undecided-dos' as CR/LFs are detected. As the file is read in the
120Unix case, the coding system will be changed to `undecided-unix' as
121LFs are detected. In both cases, `buffer-file-coding-system' will be
122set to the appropriate coding system, and the value of
123`buffer-file-coding-system' will be used when writing the file."
124
ee425fc3
RS
125 (let ((op (nth 0 command))
126 (target)
dfbcdf5f 127 (binary nil) (text nil)
73b2c664 128 (undecided nil) (undecided-unix nil))
ee425fc3
RS
129 (cond ((eq op 'insert-file-contents)
130 (setq target (nth 1 command))
73b2c664
RS
131 ;; First check for a file name that indicates
132 ;; it is truly binary.
133 (setq binary (find-buffer-file-type target))
134 (cond (binary)
135 ;; Next check for files that MUST use DOS eol conversion.
136 ((find-buffer-file-type-match target)
137 (setq text t))
138 ;; For any other existing file, decide based on contents.
139 ((file-exists-p target)
140 (setq undecided t))
141 ;; Next check for a non-DOS file system.
142 ((untranslated-file-p target)
143 (setq undecided-unix t)))
99bf72f2
GV
144 (cond (binary '(no-conversion . no-conversion))
145 (text '(undecided-dos . undecided-dos))
73b2c664 146 (undecided-unix '(undecided-unix . undecided-unix))
99bf72f2
GV
147 (undecided '(undecided . undecided))
148 (t '(undecided-dos . undecided-dos))))
149 ((eq op 'write-region)
150 (if buffer-file-coding-system
151 (cons buffer-file-coding-system
152 buffer-file-coding-system)
73b2c664
RS
153 ;; Normally this is used only in a non-file-visiting
154 ;; buffer, because normally buffer-file-coding-system is non-nil
155 ;; in a file-visiting buffer.
99bf72f2
GV
156 (if buffer-file-type
157 '(no-conversion . no-conversion)
158 '(undecided-dos . undecided-dos)))))))
ee425fc3
RS
159
160(modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system)
a750bcaa
RS
161
162(defun find-file-binary (filename)
163 "Visit file FILENAME and treat it as binary."
164 (interactive "FFind file binary: ")
165 (let ((file-name-buffer-file-type-alist '(("" . t))))
166 (find-file filename)))
167
168(defun find-file-text (filename)
169 "Visit file FILENAME and treat it as a text file."
170 (interactive "FFind file text: ")
171 (let ((file-name-buffer-file-type-alist '(("" . nil))))
172 (find-file filename)))
173
99bf72f2 174(defun find-file-not-found-set-buffer-file-coding-system ()
a750bcaa
RS
175 (save-excursion
176 (set-buffer (current-buffer))
99bf72f2
GV
177 (let* ((dummy-insert-op (list 'insert-file-contents (buffer-file-name)))
178 (coding-system-pair
179 (find-buffer-file-type-coding-system dummy-insert-op)))
180 (setq buffer-file-coding-system (car coding-system-pair))
181 (setq buffer-file-type (eq buffer-file-coding-system 'no-conversion)))))
182
183;;; To set the default coding system on new files.
184(add-hook 'find-file-not-found-hooks
185 'find-file-not-found-set-buffer-file-coding-system)
a750bcaa
RS
186
187;;; To accomodate filesystems that do not require CR/LF translation.
188(defvar untranslated-filesystem-list nil
189 "List of filesystems that require no CR/LF translation when reading
190and writing files. Each filesystem in the list is a string naming
191the directory prefix corresponding to the filesystem.")
192
193(defun untranslated-canonical-name (filename)
194 "Return FILENAME in a canonicalized form for use with the functions
195dealing with untranslated filesystems."
196 (if (memq system-type '(ms-dos windows-nt))
b63f9ba1 197 ;; The canonical form for DOS/W32 is with A-Z downcased and all
a750bcaa
RS
198 ;; directory separators changed to directory-sep-char.
199 (let ((name nil))
200 (setq name (mapconcat
201 '(lambda (char)
202 (if (and (<= ?A char) (<= char ?Z))
203 (char-to-string (+ (- char ?A) ?a))
204 (char-to-string char)))
205 filename nil))
206 ;; Use expand-file-name to canonicalize directory separators, except
207 ;; with bare drive letters (which would have the cwd appended).
208 (if (string-match "^.:$" name)
209 name
210 (expand-file-name name)))
211 filename))
212
213(defun untranslated-file-p (filename)
214 "Return t if FILENAME is on a filesystem that does not require
215CR/LF translation, and nil otherwise."
216 (let ((fs (untranslated-canonical-name filename))
217 (ufs-list untranslated-filesystem-list)
218 (found nil))
219 (while (and (not found) ufs-list)
220 (if (string-match (concat "^" (car ufs-list)) fs)
221 (setq found t)
222 (setq ufs-list (cdr ufs-list))))
223 found))
224
225(defun add-untranslated-filesystem (filesystem)
226 "Add FILESYSTEM to the list of filesystems that do not require
227CR/LF translation. FILESYSTEM is a string containing the directory
228prefix corresponding to the filesystem. For example, for a Unix
229filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
60382faa 230 (interactive "fUntranslated file system: ")
a750bcaa
RS
231 (let ((fs (untranslated-canonical-name filesystem)))
232 (if (member fs untranslated-filesystem-list)
233 untranslated-filesystem-list
234 (setq untranslated-filesystem-list
235 (cons fs untranslated-filesystem-list)))))
236
237(defun remove-untranslated-filesystem (filesystem)
238 "Remove FILESYSTEM from the list of filesystems that do not require
239CR/LF translation. FILESYSTEM is a string containing the directory
240prefix corresponding to the filesystem. For example, for a Unix
241filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
60382faa 242 (interactive "fUntranslated file system: ")
a750bcaa
RS
243 (setq untranslated-filesystem-list
244 (delete (untranslated-canonical-name filesystem)
245 untranslated-filesystem-list)))
246
ee425fc3
RS
247;; Process I/O decoding and encoding.
248
7c621f7a 249(defun find-binary-process-coding-system (command)
ee425fc3
RS
250 "Choose a coding system for process I/O.
251The coding system for decode is 'no-conversion' if 'binary-process-output'
f473b0ca 252is non-nil, and 'undecided-dos' otherwise. Similarly, the coding system
ee425fc3 253for encode is 'no-conversion' if 'binary-process-input' is non-nil,
f473b0ca
GV
254and 'undecided-dos' otherwise."
255 (let ((decode 'undecided-dos)
256 (encode 'undecided-dos))
ee425fc3
RS
257 (if binary-process-output
258 (setq decode 'no-conversion))
259 (if binary-process-input
260 (setq encode 'no-conversion))
261 (cons decode encode)))
262
263(modify-coding-system-alist 'process "" 'find-binary-process-coding-system)
264
265
b55edb63 266(provide 'dos-w32)
a750bcaa 267
b55edb63 268;;; dos-w32.el ends here