(c-mode-base-map, c++-mode-map):
[bpt/emacs.git] / lisp / dos-w32.el
1 ;;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms
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
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
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
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
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
88 (defun find-buffer-file-type-coding-system (command)
89 "Choose a coding system for a file operation.
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'
100 Otherwise:
101 If the file exists: `undecided'
102 If the file does not exist: `undecided-dos'
103
104 If COMMAND is `write-region', the coding system is chosen based
105 upon the value of `buffer-file-type': If t, the coding system is
106 `no-conversion', otherwise it is `undecided-dos'."
107 (let ((op (nth 0 command))
108 (target)
109 (binary nil) (text nil)
110 (undecided nil))
111 (cond ((eq op 'insert-file-contents)
112 (setq target (nth 1 command))
113 (if (untranslated-file-p target)
114 (if (file-exists-p target)
115 (setq undecided t)
116 (setq binary t))
117 (setq binary (find-buffer-file-type target))
118 (unless binary
119 (if (find-buffer-file-type-match target)
120 (setq text t)
121 (setq undecided (file-exists-p target))))))
122 ((eq op 'write-region)
123 (setq binary buffer-file-type)))
124 (cond (binary '(no-conversion . no-conversion))
125 (text '(undecided-dos . undecided-dos))
126 (undecided '(undecided . undecided))
127 (t '(undecided-dos . undecided-dos)))))
128
129 (modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system)
130
131 (defun find-file-binary (filename)
132 "Visit file FILENAME and treat it as binary."
133 (interactive "FFind file binary: ")
134 (let ((file-name-buffer-file-type-alist '(("" . t))))
135 (find-file filename)))
136
137 (defun find-file-text (filename)
138 "Visit file FILENAME and treat it as a text file."
139 (interactive "FFind file text: ")
140 (let ((file-name-buffer-file-type-alist '(("" . nil))))
141 (find-file filename)))
142
143 (defun find-file-not-found-set-buffer-file-type ()
144 (save-excursion
145 (set-buffer (current-buffer))
146 (setq buffer-file-type (find-buffer-file-type (buffer-file-name))))
147 nil)
148
149 ;;; To set the default file type on new files.
150 (add-hook 'find-file-not-found-hooks 'find-file-not-found-set-buffer-file-type)
151
152
153 ;;; To accomodate filesystems that do not require CR/LF translation.
154 (defvar untranslated-filesystem-list nil
155 "List of filesystems that require no CR/LF translation when reading
156 and writing files. Each filesystem in the list is a string naming
157 the directory prefix corresponding to the filesystem.")
158
159 (defun untranslated-canonical-name (filename)
160 "Return FILENAME in a canonicalized form for use with the functions
161 dealing with untranslated filesystems."
162 (if (memq system-type '(ms-dos windows-nt))
163 ;; The canonical form for DOS/W32 is with A-Z downcased and all
164 ;; directory separators changed to directory-sep-char.
165 (let ((name nil))
166 (setq name (mapconcat
167 '(lambda (char)
168 (if (and (<= ?A char) (<= char ?Z))
169 (char-to-string (+ (- char ?A) ?a))
170 (char-to-string char)))
171 filename nil))
172 ;; Use expand-file-name to canonicalize directory separators, except
173 ;; with bare drive letters (which would have the cwd appended).
174 (if (string-match "^.:$" name)
175 name
176 (expand-file-name name)))
177 filename))
178
179 (defun untranslated-file-p (filename)
180 "Return t if FILENAME is on a filesystem that does not require
181 CR/LF translation, and nil otherwise."
182 (let ((fs (untranslated-canonical-name filename))
183 (ufs-list untranslated-filesystem-list)
184 (found nil))
185 (while (and (not found) ufs-list)
186 (if (string-match (concat "^" (car ufs-list)) fs)
187 (setq found t)
188 (setq ufs-list (cdr ufs-list))))
189 found))
190
191 (defun add-untranslated-filesystem (filesystem)
192 "Add FILESYSTEM to the list of filesystems that do not require
193 CR/LF translation. FILESYSTEM is a string containing the directory
194 prefix corresponding to the filesystem. For example, for a Unix
195 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
196 (interactive "fUntranslated file system: ")
197 (let ((fs (untranslated-canonical-name filesystem)))
198 (if (member fs untranslated-filesystem-list)
199 untranslated-filesystem-list
200 (setq untranslated-filesystem-list
201 (cons fs untranslated-filesystem-list)))))
202
203 (defun remove-untranslated-filesystem (filesystem)
204 "Remove FILESYSTEM from the list of filesystems that do not require
205 CR/LF translation. FILESYSTEM is a string containing the directory
206 prefix corresponding to the filesystem. For example, for a Unix
207 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
208 (interactive "fUntranslated file system: ")
209 (setq untranslated-filesystem-list
210 (delete (untranslated-canonical-name filesystem)
211 untranslated-filesystem-list)))
212
213 ;; Process I/O decoding and encoding.
214
215 (defun find-binary-process-coding-system (command)
216 "Choose a coding system for process I/O.
217 The coding system for decode is 'no-conversion' if 'binary-process-output'
218 is non-nil, and 'undecided-dos' otherwise. Similarly, the coding system
219 for encode is 'no-conversion' if 'binary-process-input' is non-nil,
220 and 'undecided-dos' otherwise."
221 (let ((decode 'undecided-dos)
222 (encode 'undecided-dos))
223 (if binary-process-output
224 (setq decode 'no-conversion))
225 (if binary-process-input
226 (setq encode 'no-conversion))
227 (cons decode encode)))
228
229 (modify-coding-system-alist 'process "" 'find-binary-process-coding-system)
230
231
232 (provide 'dos-w32)
233
234 ;;; dos-w32.el ends here