(w32_last_error): Fix cut+paste error.
[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
68429d86 5;; Maintainer: Geoff Voelker <voelker@cs.washington.edu>
a750bcaa
RS
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
fdc4f7a0
RS
35(setq minibuffer-history-case-insensitive-variables
36 (cons 'file-name-history minibuffer-history-case-insensitive-variables))
37
a750bcaa 38;; Set the null device (for compile.el).
bfba21fc 39(setq null-device "NUL")
a750bcaa
RS
40
41;; Set the grep regexp to match entries with drive letters.
42(setq grep-regexp-alist
43 '(("^\\(\\([a-zA-Z]:\\)?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 3)))
44
45;; For distinguishing file types based upon suffixes.
46(defvar file-name-buffer-file-type-alist
47 '(
48 ("[:/].*config.sys$" . nil) ; config.sys text
49 ("\\.elc$" . t) ; emacs stuff
17e4fb34 50 ("\\.\\(obj\\|exe\\|com\\|lib\\|sym\\|sys\\|chk\\|out\\|bin\\|ico\\|pif\\|class\\)$" . t)
a750bcaa 51 ; MS-Dos stuff
f359fb79
GV
52 ("\\.\\(dll\\|drv\\|cpl\\|scr\\vbx\\|386\\|vxd\\|fon\\|fnt\\|fot\\|ttf\\|grp\\)$" . t)
53 ; Windows stuff
0009dce3 54 ("\\.\\(hlp\\|bmp\\|wav\\|avi\\|mpg\\|jpg\\|tif\\|mov\\|au\\)" . t)
f359fb79 55 ; known binary data files
a750bcaa
RS
56 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t)
57 ; Packers
f359fb79 58 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\|jar\\)$" . t)
a750bcaa
RS
59 ; Unix stuff
60 ("\\.tp[ulpw]$" . t)
61 ; Borland Pascal stuff
b2a8c84d 62 ("[:/]tags$" . nil)
a750bcaa
RS
63 ; Emacs TAGS file
64 )
65 "*Alist for distinguishing text files from binary files.
66Each element has the form (REGEXP . TYPE), where REGEXP is matched
67against the file name, and TYPE is nil for text, t for binary.")
68
ee425fc3
RS
69;; Return the pair matching filename on file-name-buffer-file-type-alist,
70;; or nil otherwise.
71(defun find-buffer-file-type-match (filename)
72 (let ((alist file-name-buffer-file-type-alist)
73 (found nil))
74 (let ((case-fold-search t))
75 (setq filename (file-name-sans-versions filename))
76 (while (and (not found) alist)
77 (if (string-match (car (car alist)) filename)
78 (setq found (car alist)))
79 (setq alist (cdr alist)))
80 found)))
81
73b2c664 82;; Don't check for untranslated file systems here.
a750bcaa 83(defun find-buffer-file-type (filename)
73b2c664
RS
84 (let ((match (find-buffer-file-type-match filename))
85 (code))
86 (if (not match)
87 default-buffer-file-type
88 (setq code (cdr match))
89 (cond ((memq code '(nil t)) code)
90 ((and (symbolp code) (fboundp code))
91 (funcall code filename))))))
ee425fc3 92
99bf72f2
GV
93(setq-default buffer-file-coding-system 'undecided-dos)
94
7c621f7a 95(defun find-buffer-file-type-coding-system (command)
ee425fc3 96 "Choose a coding system for a file operation.
f473b0ca
GV
97If COMMAND is `insert-file-contents', the coding system is chosen based
98upon the filename, the contents of `untranslated-filesystem-list' and
99`file-name-buffer-file-type-alist', and whether the file exists:
100
101 If it matches in `untranslated-filesystem-list':
102 If the file exists: `no-conversion'
103 If the file does not exist: `undecided'
104 If it matches in `file-name-buffer-file-type-alist':
105 If the match is t (for binary): `no-conversion'
106 If the match is nil (for dos-text): `undecided-dos'
ee425fc3 107 Otherwise:
f473b0ca
GV
108 If the file exists: `undecided'
109 If the file does not exist: `undecided-dos'
ee425fc3 110
99bf72f2
GV
111If COMMAND is `write-region', the coding system is chosen based upon
112the value of `buffer-file-coding-system' and `buffer-file-type'. If
113`buffer-file-coding-system' is non-nil, its value is used. If it is
114nil and `buffer-file-type' is t, the coding system is `no-conversion'.
115Otherwise, it is `undecided-dos'.
116
117The two most common situations are when DOS and Unix files are read
118and written, and their names do not match in
119`untranslated-filesystem-list' and `file-name-buffer-file-type-alist'.
120In these cases, the coding system initially will be `undecided'. As
121the file is read in the DOS case, the coding system will be changed to
122`undecided-dos' as CR/LFs are detected. As the file is read in the
123Unix case, the coding system will be changed to `undecided-unix' as
124LFs are detected. In both cases, `buffer-file-coding-system' will be
125set to the appropriate coding system, and the value of
126`buffer-file-coding-system' will be used when writing the file."
127
ee425fc3
RS
128 (let ((op (nth 0 command))
129 (target)
dfbcdf5f 130 (binary nil) (text nil)
73b2c664 131 (undecided nil) (undecided-unix nil))
ee425fc3
RS
132 (cond ((eq op 'insert-file-contents)
133 (setq target (nth 1 command))
73b2c664
RS
134 ;; First check for a file name that indicates
135 ;; it is truly binary.
136 (setq binary (find-buffer-file-type target))
137 (cond (binary)
138 ;; Next check for files that MUST use DOS eol conversion.
139 ((find-buffer-file-type-match target)
140 (setq text t))
141 ;; For any other existing file, decide based on contents.
142 ((file-exists-p target)
143 (setq undecided t))
144 ;; Next check for a non-DOS file system.
145 ((untranslated-file-p target)
146 (setq undecided-unix t)))
99bf72f2
GV
147 (cond (binary '(no-conversion . no-conversion))
148 (text '(undecided-dos . undecided-dos))
73b2c664 149 (undecided-unix '(undecided-unix . undecided-unix))
99bf72f2
GV
150 (undecided '(undecided . undecided))
151 (t '(undecided-dos . undecided-dos))))
152 ((eq op 'write-region)
153 (if buffer-file-coding-system
154 (cons buffer-file-coding-system
155 buffer-file-coding-system)
73b2c664
RS
156 ;; Normally this is used only in a non-file-visiting
157 ;; buffer, because normally buffer-file-coding-system is non-nil
158 ;; in a file-visiting buffer.
99bf72f2
GV
159 (if buffer-file-type
160 '(no-conversion . no-conversion)
161 '(undecided-dos . undecided-dos)))))))
ee425fc3
RS
162
163(modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system)
a750bcaa
RS
164
165(defun find-file-binary (filename)
166 "Visit file FILENAME and treat it as binary."
167 (interactive "FFind file binary: ")
168 (let ((file-name-buffer-file-type-alist '(("" . t))))
169 (find-file filename)))
170
171(defun find-file-text (filename)
172 "Visit file FILENAME and treat it as a text file."
173 (interactive "FFind file text: ")
174 (let ((file-name-buffer-file-type-alist '(("" . nil))))
175 (find-file filename)))
176
99bf72f2 177(defun find-file-not-found-set-buffer-file-coding-system ()
a750bcaa
RS
178 (save-excursion
179 (set-buffer (current-buffer))
99bf72f2
GV
180 (let* ((dummy-insert-op (list 'insert-file-contents (buffer-file-name)))
181 (coding-system-pair
182 (find-buffer-file-type-coding-system dummy-insert-op)))
183 (setq buffer-file-coding-system (car coding-system-pair))
184 (setq buffer-file-type (eq buffer-file-coding-system 'no-conversion)))))
185
186;;; To set the default coding system on new files.
187(add-hook 'find-file-not-found-hooks
188 'find-file-not-found-set-buffer-file-coding-system)
a750bcaa
RS
189
190;;; To accomodate filesystems that do not require CR/LF translation.
191(defvar untranslated-filesystem-list nil
192 "List of filesystems that require no CR/LF translation when reading
193and writing files. Each filesystem in the list is a string naming
194the directory prefix corresponding to the filesystem.")
195
196(defun untranslated-canonical-name (filename)
197 "Return FILENAME in a canonicalized form for use with the functions
198dealing with untranslated filesystems."
199 (if (memq system-type '(ms-dos windows-nt))
b63f9ba1 200 ;; The canonical form for DOS/W32 is with A-Z downcased and all
a750bcaa
RS
201 ;; directory separators changed to directory-sep-char.
202 (let ((name nil))
203 (setq name (mapconcat
204 '(lambda (char)
205 (if (and (<= ?A char) (<= char ?Z))
206 (char-to-string (+ (- char ?A) ?a))
207 (char-to-string char)))
208 filename nil))
209 ;; Use expand-file-name to canonicalize directory separators, except
210 ;; with bare drive letters (which would have the cwd appended).
211 (if (string-match "^.:$" name)
212 name
213 (expand-file-name name)))
214 filename))
215
216(defun untranslated-file-p (filename)
217 "Return t if FILENAME is on a filesystem that does not require
218CR/LF translation, and nil otherwise."
219 (let ((fs (untranslated-canonical-name filename))
220 (ufs-list untranslated-filesystem-list)
221 (found nil))
222 (while (and (not found) ufs-list)
223 (if (string-match (concat "^" (car ufs-list)) fs)
224 (setq found t)
225 (setq ufs-list (cdr ufs-list))))
226 found))
227
228(defun add-untranslated-filesystem (filesystem)
229 "Add FILESYSTEM to the list of filesystems that do not require
230CR/LF translation. FILESYSTEM is a string containing the directory
231prefix corresponding to the filesystem. For example, for a Unix
232filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
60382faa 233 (interactive "fUntranslated file system: ")
a750bcaa
RS
234 (let ((fs (untranslated-canonical-name filesystem)))
235 (if (member fs untranslated-filesystem-list)
236 untranslated-filesystem-list
237 (setq untranslated-filesystem-list
238 (cons fs untranslated-filesystem-list)))))
239
240(defun remove-untranslated-filesystem (filesystem)
241 "Remove FILESYSTEM from the list of filesystems that do not require
242CR/LF translation. FILESYSTEM is a string containing the directory
243prefix corresponding to the filesystem. For example, for a Unix
244filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
60382faa 245 (interactive "fUntranslated file system: ")
a750bcaa
RS
246 (setq untranslated-filesystem-list
247 (delete (untranslated-canonical-name filesystem)
248 untranslated-filesystem-list)))
249
5dbd2257
GV
250;;; Override setting chosen at startup.
251(defun set-default-process-coding-system ()
252 (setq default-process-coding-system
253 (if default-enable-multibyte-characters
254 '(undecided-dos . undecided-dos)
255 '(raw-text-dos . raw-text-dos))))
256
257(add-hook 'before-init-hook 'set-default-process-coding-system)
ee425fc3 258
5a0aa644
RS
259;; Support for printing under DOS/Windows, see lpr.el and ps-print.el.
260(defvar printer-name)
261
262(defun direct-print-region-function (start end
263 &optional lpr-prog
264 delete-text buf display
265 &rest rest)
266 "DOS/Windows-specific function to print the region on a printer.
267Writes the region to the device or file which is a value of
268`printer-name' \(which see\). Ignores any arguments beyond
269START and END."
270
271 ;; DOS printers need the lines to end with CR-LF pairs, so make
272 ;; sure it always happens that way, unless the buffer is binary.
273 (let* ((coding coding-system-for-write)
274 (coding-base
275 (if (null coding) 'undecided (coding-system-base coding)))
276 (eol-type (coding-system-eol-type coding-base)))
277 (or (eq coding-system-for-write 'no-conversion)
278 (setq coding-system-for-write
279 (aref eol-type 1))) ; force conversion to DOS EOLs
280 (write-region start end
281 (or (and (boundp 'dos-printer) dos-printer)
282 printer-name)
283 t 0)
284 ;; Make each print-out start on a new page, but don't waste
285 ;; paper if there was a form-feed at the end of this file.
286 (if (not (char-equal (char-after (1- end)) ?\C-l))
287 (write-region "\f" nil
288 (or (and (boundp 'dos-printer) dos-printer)
289 printer-name)
290 t 0))))
291
292;; Set this to nil if you have a port of the `lpr' program and
293;; you want to use it for printing. If the default setting is
294;; in effect, `lpr-command' and its switches are ignored when
295;; printing with `lpr-xxx' and `print-xxx'.
296(setq print-region-function 'direct-print-region-function)
297
298;; Set this to nil if you have a port of the `pr' program
299;; (e.g., from GNU Textutils), or if you have an `lpr'
300;; program (see above) that can print page headers.
301;; If `lpr-headers-switches' is non-nil (the default) and
302;; `print-region-function' is set to `dos-print-region-function',
303;; then requests to print page headers will be silently
304;; ignored, and `print-buffer' and `print-region' produce
305;; the same output as `lpr-buffer' and `lpr-region', accordingly.
306(setq lpr-headers-switches "(page headers are not supported)")
307
308(defvar ps-printer-name)
309
310(setq ps-lpr-command "gs")
311
312(setq ps-lpr-switches '("-q" "-dNOPAUSE" "-sDEVICE=epson" "-r240x60"
313 "-sOutputFile=LPT1" "-"))
314
b55edb63 315(provide 'dos-w32)
a750bcaa 316
b55edb63 317;;; dos-w32.el ends here