Mercurial > emacs
annotate lisp/dos-w32.el @ 18858:ad717015a2de
(find-buffer-file-type-coding-system)
(find-binary-process-coding-system, find-buffer-file-type-match):
New functions.
(find-buffer-file-type): Use find-buffer-file-type-match.
Add find-buffer-file-type-coding-system to file-coding-system-alist
as the default entry.
Add find-binary-process-coding-system to process-coding-system-alist
as the default entry.
author | Richard M. Stallman <rms@gnu.org> |
---|---|
date | Fri, 18 Jul 1997 22:54:23 +0000 |
parents | 6ea4d90cc76b |
children | 62090ffa4583 |
rev | line source |
---|---|
16591
afb15f49a0c9
Use new file name dos-w32.el
Geoff Voelker <voelker@cs.washington.edu>
parents:
16234
diff
changeset
|
1 ;;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms |
16026 | 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 ;;; Add %t: into the mode line format just after the open-paren. | |
33 (let ((tail (member " %[(" mode-line-format))) | |
34 (setcdr tail (cons (purecopy "%t:") | |
35 (cdr tail)))) | |
36 | |
37 ;; Use ";" instead of ":" as a path separator (from files.el). | |
38 (setq path-separator ";") | |
39 | |
40 ;; Set the null device (for compile.el). | |
41 (setq grep-null-device "NUL") | |
42 | |
43 ;; Set the grep regexp to match entries with drive letters. | |
44 (setq grep-regexp-alist | |
45 '(("^\\(\\([a-zA-Z]:\\)?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 3))) | |
46 | |
47 ;; For distinguishing file types based upon suffixes. | |
48 (defvar file-name-buffer-file-type-alist | |
49 '( | |
50 ("[:/].*config.sys$" . nil) ; config.sys text | |
51 ("\\.elc$" . t) ; emacs stuff | |
52 ("\\.\\(obj\\|exe\\|com\\|lib\\|sys\\|chk\\|out\\|bin\\|ico\\|pif\\)$" . t) | |
53 ; MS-Dos stuff | |
54 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t) | |
55 ; Packers | |
56 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\)$" . t) | |
57 ; Unix stuff | |
58 ("\\.tp[ulpw]$" . t) | |
59 ; Borland Pascal stuff | |
60 ("[:/]tags$" . t) | |
61 ; Emacs TAGS file | |
62 ) | |
63 "*Alist for distinguishing text files from binary files. | |
64 Each element has the form (REGEXP . TYPE), where REGEXP is matched | |
65 against the file name, and TYPE is nil for text, t for binary.") | |
66 | |
18858
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
67 ;; Return the pair matching filename on file-name-buffer-file-type-alist, |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
68 ;; or nil otherwise. |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
69 (defun find-buffer-file-type-match (filename) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
70 (let ((alist file-name-buffer-file-type-alist) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
71 (found nil)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
72 (let ((case-fold-search t)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
73 (setq filename (file-name-sans-versions filename)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
74 (while (and (not found) alist) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
75 (if (string-match (car (car alist)) filename) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
76 (setq found (car alist))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
77 (setq alist (cdr alist))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
78 found))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
79 |
16026 | 80 (defun find-buffer-file-type (filename) |
81 ;; First check if file is on an untranslated filesystem, then on the alist. | |
82 (if (untranslated-file-p filename) | |
83 t ; for binary | |
18858
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
84 (let ((match (find-buffer-file-type-match filename)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
85 (code)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
86 (if (not match) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
87 default-buffer-file-type |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
88 (setq code (cdr match)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
89 (cond ((memq code '(nil t)) code) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
90 ((and (symbolp code) (fboundp code)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
91 (funcall code filename))))))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
92 |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
93 (defun find-buffer-file-type-coding-system (command args) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
94 "Choose a coding system for a file operation. |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
95 If COMMAND is 'insert-file-contents', the coding system is chosen based |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
96 upon the filename, the contents of 'untranslated-filesystem-list' and |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
97 'file-name-buffer-file-type-alist', and whether the file exists: |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
98 |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
99 If it matches in 'untranslated-filesystem-list': 'no-conversion' |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
100 If it matches in 'file-name-buffer-file-type-alist': |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
101 If the match is t (for binary): 'no-conversion' |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
102 If the match is nil (for text): 'emacs-mule-dos' |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
103 Otherwise: |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
104 If the file exists: 'undecided' |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
105 If the file does not exist: 'emacs-mule-dos' |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
106 |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
107 If COMMAND is 'write-region', the coding system is chosen based |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
108 upon the value of 'buffer-file-type': If t, the coding system is |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
109 'no-conversion', otherwise it is 'emacs-mule-dos'." |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
110 (let ((op (nth 0 command)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
111 (target) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
112 (binary) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
113 (undecided nil)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
114 (cond ((eq op 'insert-file-contents) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
115 (setq target (nth 1 command)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
116 (setq binary (find-buffer-file-type target)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
117 (if (not binary) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
118 (setq undecided |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
119 (and (file-exists-p target) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
120 (not (find-buffer-file-type-match target)))))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
121 ((eq op 'write-region) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
122 (setq binary buffer-file-type))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
123 (cond (binary '(no-conversion . no-conversion)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
124 (undecided '(undecided . undecided)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
125 (t '(emacs-mule-dos . emacs-mule-dos))))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
126 |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
127 (modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system) |
16026 | 128 |
129 (defun find-file-binary (filename) | |
130 "Visit file FILENAME and treat it as binary." | |
131 (interactive "FFind file binary: ") | |
132 (let ((file-name-buffer-file-type-alist '(("" . t)))) | |
133 (find-file filename))) | |
134 | |
135 (defun find-file-text (filename) | |
136 "Visit file FILENAME and treat it as a text file." | |
137 (interactive "FFind file text: ") | |
138 (let ((file-name-buffer-file-type-alist '(("" . nil)))) | |
139 (find-file filename))) | |
140 | |
141 (defun find-file-not-found-set-buffer-file-type () | |
142 (save-excursion | |
143 (set-buffer (current-buffer)) | |
144 (setq buffer-file-type (find-buffer-file-type (buffer-file-name)))) | |
145 nil) | |
146 | |
147 ;;; To set the default file type on new files. | |
148 (add-hook 'find-file-not-found-hooks 'find-file-not-found-set-buffer-file-type) | |
149 | |
150 | |
151 ;;; To accomodate filesystems that do not require CR/LF translation. | |
152 (defvar untranslated-filesystem-list nil | |
153 "List of filesystems that require no CR/LF translation when reading | |
154 and writing files. Each filesystem in the list is a string naming | |
155 the directory prefix corresponding to the filesystem.") | |
156 | |
157 (defun untranslated-canonical-name (filename) | |
158 "Return FILENAME in a canonicalized form for use with the functions | |
159 dealing with untranslated filesystems." | |
160 (if (memq system-type '(ms-dos windows-nt)) | |
16889
8de32e992e4d
Change uses of win32 to w32.
Geoff Voelker <voelker@cs.washington.edu>
parents:
16591
diff
changeset
|
161 ;; The canonical form for DOS/W32 is with A-Z downcased and all |
16026 | 162 ;; directory separators changed to directory-sep-char. |
163 (let ((name nil)) | |
164 (setq name (mapconcat | |
165 '(lambda (char) | |
166 (if (and (<= ?A char) (<= char ?Z)) | |
167 (char-to-string (+ (- char ?A) ?a)) | |
168 (char-to-string char))) | |
169 filename nil)) | |
170 ;; Use expand-file-name to canonicalize directory separators, except | |
171 ;; with bare drive letters (which would have the cwd appended). | |
172 (if (string-match "^.:$" name) | |
173 name | |
174 (expand-file-name name))) | |
175 filename)) | |
176 | |
177 (defun untranslated-file-p (filename) | |
178 "Return t if FILENAME is on a filesystem that does not require | |
179 CR/LF translation, and nil otherwise." | |
180 (let ((fs (untranslated-canonical-name filename)) | |
181 (ufs-list untranslated-filesystem-list) | |
182 (found nil)) | |
183 (while (and (not found) ufs-list) | |
184 (if (string-match (concat "^" (car ufs-list)) fs) | |
185 (setq found t) | |
186 (setq ufs-list (cdr ufs-list)))) | |
187 found)) | |
188 | |
189 (defun add-untranslated-filesystem (filesystem) | |
190 "Add FILESYSTEM to the list of filesystems that do not require | |
191 CR/LF translation. FILESYSTEM is a string containing the directory | |
192 prefix corresponding to the filesystem. For example, for a Unix | |
193 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"." | |
17901
6ea4d90cc76b
(add-untranslated-filesystem)
Richard M. Stallman <rms@gnu.org>
parents:
16889
diff
changeset
|
194 (interactive "fUntranslated file system: ") |
16026 | 195 (let ((fs (untranslated-canonical-name filesystem))) |
196 (if (member fs untranslated-filesystem-list) | |
197 untranslated-filesystem-list | |
198 (setq untranslated-filesystem-list | |
199 (cons fs untranslated-filesystem-list))))) | |
200 | |
201 (defun remove-untranslated-filesystem (filesystem) | |
202 "Remove FILESYSTEM from the list of filesystems that do not require | |
203 CR/LF translation. FILESYSTEM is a string containing the directory | |
204 prefix corresponding to the filesystem. For example, for a Unix | |
205 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"." | |
17901
6ea4d90cc76b
(add-untranslated-filesystem)
Richard M. Stallman <rms@gnu.org>
parents:
16889
diff
changeset
|
206 (interactive "fUntranslated file system: ") |
16026 | 207 (setq untranslated-filesystem-list |
208 (delete (untranslated-canonical-name filesystem) | |
209 untranslated-filesystem-list))) | |
210 | |
18858
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
211 ;; Process I/O decoding and encoding. |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
212 |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
213 (defun find-binary-process-coding-system (op args) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
214 "Choose a coding system for process I/O. |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
215 The coding system for decode is 'no-conversion' if 'binary-process-output' |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
216 is non-nil, and 'emacs-mule-dos' otherwise. Similarly, the coding system |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
217 for encode is 'no-conversion' if 'binary-process-input' is non-nil, |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
218 and 'emacs-mule-dos' otherwise." |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
219 (let ((decode 'emacs-mule-dos) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
220 (encode 'emacs-mule-dos)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
221 (if binary-process-output |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
222 (setq decode 'no-conversion)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
223 (if binary-process-input |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
224 (setq encode 'no-conversion)) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
225 (cons decode encode))) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
226 |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
227 (modify-coding-system-alist 'process "" 'find-binary-process-coding-system) |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
228 |
ad717015a2de
(find-buffer-file-type-coding-system)
Richard M. Stallman <rms@gnu.org>
parents:
17901
diff
changeset
|
229 |
16591
afb15f49a0c9
Use new file name dos-w32.el
Geoff Voelker <voelker@cs.washington.edu>
parents:
16234
diff
changeset
|
230 (provide 'dos-w32) |
16026 | 231 |
16591
afb15f49a0c9
Use new file name dos-w32.el
Geoff Voelker <voelker@cs.washington.edu>
parents:
16234
diff
changeset
|
232 ;;; dos-w32.el ends here |