(Fexpand_file_name): Collapse sequences of slashes
[emacs.git] / lisp / dos-w32.el
blob18ae57e22a7c966a1ab62d2b663f81aded09028a
1 ;;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;; Maintainer: Geoff Voelker <voelker@cs.washington.edu>
6 ;; Keywords: internal
8 ;; This file is part of GNU Emacs.
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.
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.
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.
25 ;;; Commentary:
27 ;; Parts of this code are duplicated functions taken from dos-fns.el
28 ;; and winnt.el.
30 ;;; Code:
32 ;; Use ";" instead of ":" as a path separator (from files.el).
33 (setq path-separator ";")
35 (setq minibuffer-history-case-insensitive-variables
36 (cons 'file-name-history minibuffer-history-case-insensitive-variables))
38 ;; Set the null device (for compile.el).
39 (setq null-device "NUL")
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)))
45 ;; For distinguishing file types based upon suffixes.
46 (defvar file-name-buffer-file-type-alist
48 ("[:/].*config.sys$" . nil) ; config.sys text
49 ("\\.\\(obj\\|exe\\|com\\|lib\\|sys\\|bin\\|ico\\|pif\\|class\\)$" . t)
50 ; MS-Dos stuff
51 ("\\.\\(dll\\|drv\\|386\\|vxd\\|fon\\|fnt\\|fot\\|ttf\\|grp\\)$" . t)
52 ; Windows stuff
53 ("\\.\\(bmp\\|wav\\|avi\\|mpg\\|jpg\\|tif\\|mov\\|au\\)$" . t)
54 ; known binary data files
55 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t)
56 ; Packers
57 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\|jar\\)$" . t)
58 ; Unix stuff
59 ("\\.tp[ulpw]$" . t)
60 ; Borland Pascal stuff
61 ("[:/]tags$" . nil)
62 ; Emacs TAGS file
64 "*Alist for distinguishing text files from binary files.
65 Each element has the form (REGEXP . TYPE), where REGEXP is matched
66 against the file name, and TYPE is nil for text, t for binary.")
68 ;; Return the pair matching filename on file-name-buffer-file-type-alist,
69 ;; or nil otherwise.
70 (defun find-buffer-file-type-match (filename)
71 (let ((alist file-name-buffer-file-type-alist)
72 (found nil))
73 (let ((case-fold-search t))
74 (setq filename (file-name-sans-versions filename))
75 (while (and (not found) alist)
76 (if (string-match (car (car alist)) filename)
77 (setq found (car alist)))
78 (setq alist (cdr alist)))
79 found)))
81 ;; Don't check for untranslated file systems here.
82 (defun find-buffer-file-type (filename)
83 (let ((match (find-buffer-file-type-match filename))
84 (code))
85 (if (not match)
86 default-buffer-file-type
87 (setq code (cdr match))
88 (cond ((memq code '(nil t)) code)
89 ((and (symbolp code) (fboundp code))
90 (funcall code filename))))))
92 (setq-default buffer-file-coding-system 'undecided-dos)
94 (defun find-buffer-file-type-coding-system (command)
95 "Choose a coding system for a file operation.
96 If COMMAND is `insert-file-contents', the coding system is chosen based
97 upon the filename, the contents of `untranslated-filesystem-list' and
98 `file-name-buffer-file-type-alist', and whether the file exists:
100 If it matches in `untranslated-filesystem-list':
101 If the file exists: `undecided'
102 If the file does not exist: `undecided-unix'
103 If it matches in `file-name-buffer-file-type-alist':
104 If the match is t (for binary): `no-conversion'
105 If the match is nil (for dos-text): `undecided-dos'
106 Otherwise:
107 If the file exists: `undecided'
108 If the file does not exist: default-buffer-file-coding-system
110 If COMMAND is `write-region', the coding system is chosen based upon
111 the value of `buffer-file-coding-system' and `buffer-file-type'. If
112 `buffer-file-coding-system' is non-nil, its value is used. If it is
113 nil and `buffer-file-type' is t, the coding system is `no-conversion'.
114 Otherwise, it is `undecided-dos'.
116 The two most common situations are when DOS and Unix files are read
117 and written, and their names do not match in
118 `untranslated-filesystem-list' and `file-name-buffer-file-type-alist'.
119 In these cases, the coding system initially will be `undecided'. As
120 the file is read in the DOS case, the coding system will be changed to
121 `undecided-dos' as CR/LFs are detected. As the file is read in the
122 Unix case, the coding system will be changed to `undecided-unix' as
123 LFs are detected. In both cases, `buffer-file-coding-system' will be
124 set to the appropriate coding system, and the value of
125 `buffer-file-coding-system' will be used when writing the file."
127 (let ((op (nth 0 command))
128 (target)
129 (binary nil) (text nil)
130 (undecided nil) (undecided-unix nil))
131 (cond ((eq op 'insert-file-contents)
132 (setq target (nth 1 command))
133 ;; First check for a file name that indicates
134 ;; it is truly binary.
135 (setq binary (find-buffer-file-type target))
136 (cond (binary)
137 ;; Next check for files that MUST use DOS eol conversion.
138 ((find-buffer-file-type-match target)
139 (setq text t))
140 ;; For any other existing file, decide based on contents.
141 ((file-exists-p target)
142 (setq undecided t))
143 ;; Next check for a non-DOS file system.
144 ((untranslated-file-p target)
145 (setq undecided-unix t)))
146 (cond (binary '(no-conversion . no-conversion))
147 (text '(undecided-dos . undecided-dos))
148 (undecided-unix '(undecided-unix . undecided-unix))
149 (undecided '(undecided . undecided))
150 (t (cons default-buffer-file-coding-system
151 default-buffer-file-coding-system))))
152 ((eq op 'write-region)
153 (if buffer-file-coding-system
154 (cons buffer-file-coding-system
155 buffer-file-coding-system)
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.
159 (if buffer-file-type
160 '(no-conversion . no-conversion)
161 '(undecided-dos . undecided-dos)))))))
163 (modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system)
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)))
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)))
177 (defun find-file-not-found-set-buffer-file-coding-system ()
178 (save-excursion
179 (set-buffer (current-buffer))
180 (let ((coding buffer-file-coding-system))
181 ;; buffer-file-coding-system is already set by
182 ;; find-operation-coding-system, which was called from
183 ;; insert-file-contents. All that's left is to change
184 ;; the EOL conversion, if required by the user.
185 (when (and (null coding-system-for-read)
186 (or inhibit-eol-conversion
187 (untranslated-file-p (buffer-file-name))))
188 (setq coding (coding-system-change-eol-conversion coding 0))
189 (setq buffer-file-coding-system coding))
190 (setq buffer-file-type (eq buffer-file-coding-system 'no-conversion)))))
192 ;;; To set the default coding system on new files.
193 (add-hook 'find-file-not-found-hooks
194 'find-file-not-found-set-buffer-file-coding-system)
196 ;;; To accomodate filesystems that do not require CR/LF translation.
197 (defvar untranslated-filesystem-list nil
198 "List of filesystems that require no CR/LF translation when reading
199 and writing files. Each filesystem in the list is a string naming
200 the directory prefix corresponding to the filesystem.")
202 (defun untranslated-canonical-name (filename)
203 "Return FILENAME in a canonicalized form for use with the functions
204 dealing with untranslated filesystems."
205 (if (memq system-type '(ms-dos windows-nt))
206 ;; The canonical form for DOS/W32 is with A-Z downcased and all
207 ;; directory separators changed to directory-sep-char.
208 (let ((name nil))
209 (setq name (mapconcat
210 '(lambda (char)
211 (if (and (<= ?A char) (<= char ?Z))
212 (char-to-string (+ (- char ?A) ?a))
213 (char-to-string char)))
214 filename nil))
215 ;; Use expand-file-name to canonicalize directory separators, except
216 ;; with bare drive letters (which would have the cwd appended).
217 ;; Avoid expanding names that could trigger ange-ftp to prompt
218 ;; for passwords, though.
219 (if (or (string-match "^.:$" name)
220 (string-match "^/[^/:]+:" name))
221 name
222 (expand-file-name name)))
223 filename))
225 (defun untranslated-file-p (filename)
226 "Return t if FILENAME is on a filesystem that does not require
227 CR/LF translation, and nil otherwise."
228 (let ((fs (untranslated-canonical-name filename))
229 (ufs-list untranslated-filesystem-list)
230 (found nil))
231 (while (and (not found) ufs-list)
232 (if (string-match (concat "^" (car ufs-list)) fs)
233 (setq found t)
234 (setq ufs-list (cdr ufs-list))))
235 found))
237 (defun add-untranslated-filesystem (filesystem)
238 "Add FILESYSTEM to the list of filesystems that do not require
239 CR/LF translation. FILESYSTEM is a string containing the directory
240 prefix corresponding to the filesystem. For example, for a Unix
241 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
242 ;; We use "D", not "f", to avoid confusing the user: "f" prompts
243 ;; with a directory, but RET returns the current buffer's file, not
244 ;; its directory.
245 (interactive "DUntranslated file system: ")
246 (let ((fs (untranslated-canonical-name filesystem)))
247 (if (member fs untranslated-filesystem-list)
248 untranslated-filesystem-list
249 (setq untranslated-filesystem-list
250 (cons fs untranslated-filesystem-list)))))
252 (defun remove-untranslated-filesystem (filesystem)
253 "Remove FILESYSTEM from the list of filesystems that do not require
254 CR/LF translation. FILESYSTEM is a string containing the directory
255 prefix corresponding to the filesystem. For example, for a Unix
256 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
257 (interactive "fUntranslated file system: ")
258 (setq untranslated-filesystem-list
259 (delete (untranslated-canonical-name filesystem)
260 untranslated-filesystem-list)))
262 ;;; Support for printing under DOS/Windows, see lpr.el and ps-print.el.
264 (defvar direct-print-region-use-command-dot-com t
265 "*Control whether command.com is used to print on Windows 9x.")
267 ;; Function to actually send data to the printer port.
268 ;; Supports writing directly, and using various programs.
269 (defun direct-print-region-helper (printer
270 start end
271 lpr-prog
272 delete-text buf display
273 rest)
274 (let* (;; Ignore case when matching known external program names.
275 (case-fold-search t)
276 ;; Convert / to \ in printer name, for sake of external programs.
277 (printer
278 (if (stringp printer)
279 (subst-char-in-string ?/ ?\\ printer)
280 printer))
281 ;; Find a directory that is local, to work-around Windows bug.
282 (safe-dir
283 (let ((safe-dirs (list "c:/" (getenv "windir") (getenv "TMPDIR"))))
284 (while (not (file-attributes (car safe-dirs)))
285 (setq safe-dirs (cdr safe-dirs)))
286 (car safe-dirs)))
287 (tempfile
288 (subst-char-in-string
289 ?/ ?\\
290 (make-temp-name
291 (expand-file-name "EP" temporary-file-directory))))
292 ;; capture output for diagnosis
293 (errbuf (list (get-buffer-create " *print-region-helper*") t)))
294 ;; It seems that we must be careful about the directory name that
295 ;; gets added to the printer port name by write-region when using
296 ;; the standard "PRN" or "LPTx" ports, because the write can fail if
297 ;; the directory is on a network drive. The same is true when
298 ;; asking command.com to copy the file.
299 ;; No action is needed for UNC printer names, which is just as well
300 ;; because `expand-file-name' doesn't support UNC names on MS-DOS.
301 (if (and (stringp printer) (not (string-match "^\\\\" printer)))
302 (setq printer
303 (subst-char-in-string ?/ ?\\ (expand-file-name printer safe-dir))))
304 ;; Handle known programs specially where necessary.
305 (unwind-protect
306 (cond
307 ;; nprint.exe is the standard print command on Netware
308 ((string-match "^nprint\\(\\.exe\\)?$" (file-name-nondirectory lpr-prog))
309 (write-region start end tempfile nil 0)
310 (call-process lpr-prog nil errbuf nil
311 tempfile (concat "P=" printer)))
312 ;; print.exe is a standard command on NT
313 ((string-match "^print\\(\\.exe\\)?$" (file-name-nondirectory lpr-prog))
314 ;; Be careful not to invoke print.exe on MS-DOS or Windows 9x
315 ;; though, because it is a TSR program there (hangs Emacs).
316 (or (and (eq system-type 'windows-nt)
317 (null (getenv "winbootdir")))
318 (error "Printing via print.exe is not supported on MS-DOS or Windows 9x"))
319 ;; It seems that print.exe always appends a form-feed so we
320 ;; should make sure to omit the last FF in the data.
321 (if (and (> end start)
322 (char-equal (char-before end) ?\C-l))
323 (setq end (1- end)))
324 ;; cancel out annotate function for non-PS case
325 (let ((write-region-annotate-functions nil))
326 (write-region start end tempfile nil 0))
327 (call-process lpr-prog nil errbuf nil
328 (concat "/D:" printer) tempfile))
329 ;; support lpr and similar programs for convenience, but
330 ;; supply an explicit filename because the NT version of lpr
331 ;; can't read from stdin.
332 ((> (length lpr-prog) 0)
333 (write-region start end tempfile nil 0)
334 (setq rest (append rest (list tempfile)))
335 (apply 'call-process lpr-prog nil errbuf nil rest))
336 ;; Run command.com to access printer port on Windows 9x, unless
337 ;; we are supposed to append to an existing (non-empty) file,
338 ;; to work around a bug in Windows 9x that prevents Win32
339 ;; programs from accessing LPT ports reliably.
340 ((and (eq system-type 'windows-nt)
341 (getenv "winbootdir")
342 ;; Allow cop-out so command.com isn't invoked
343 direct-print-region-use-command-dot-com
344 ;; file-attributes fails on LPT ports on Windows 9x but
345 ;; not on NT, so handle both cases for safety.
346 (eq (or (nth 7 (file-attributes printer)) 0) 0))
347 (write-region start end tempfile nil 0)
348 (let ((w32-quote-process-args nil))
349 (call-process "command.com" nil errbuf nil "/c"
350 (format "copy /b %s %s" tempfile printer))))
351 ;; write directly to the printer port
353 (write-region start end printer t 0)))
354 ;; ensure we remove the tempfile if created
355 (if (file-exists-p tempfile)
356 (delete-file tempfile)))))
358 (defvar printer-name)
360 (defun direct-print-region-function (start end
361 &optional lpr-prog
362 delete-text buf display
363 &rest rest)
364 "DOS/Windows-specific function to print the region on a printer.
365 Writes the region to the device or file which is a value of
366 `printer-name' \(which see\), unless the value of `lpr-command'
367 indicates a specific program should be invoked."
369 ;; DOS printers need the lines to end with CR-LF pairs, so make
370 ;; sure it always happens that way, unless the buffer is binary.
371 (let* ((coding coding-system-for-write)
372 (coding-base
373 (if (null coding) 'undecided (coding-system-base coding)))
374 (eol-type (coding-system-eol-type coding-base))
375 ;; Make each print-out eject the final page, but don't waste
376 ;; paper if the file ends with a form-feed already.
377 (write-region-annotate-functions
378 (cons
379 (lambda (start end)
380 (if (not (char-equal (char-before end) ?\C-l))
381 `((,end . "\f"))))
382 write-region-annotate-functions))
383 (printer (or (and (boundp 'dos-printer)
384 (stringp (symbol-value 'dos-printer))
385 (symbol-value 'dos-printer))
386 printer-name)))
387 (or (eq coding-system-for-write 'no-conversion)
388 (setq coding-system-for-write
389 (aref eol-type 1))) ; force conversion to DOS EOLs
390 (direct-print-region-helper printer start end lpr-prog
391 delete-text buf display rest)))
393 (setq print-region-function 'direct-print-region-function)
395 ;; Set this to nil if you have a port of the `pr' program
396 ;; (e.g., from GNU Textutils), or if you have an `lpr'
397 ;; program (see above) that can print page headers.
398 ;; If `lpr-headers-switches' is non-nil (the default) and
399 ;; `print-region-function' is set to `dos-print-region-function',
400 ;; then requests to print page headers will be silently
401 ;; ignored, and `print-buffer' and `print-region' produce
402 ;; the same output as `lpr-buffer' and `lpr-region', accordingly.
403 (setq lpr-headers-switches "(page headers are not supported)")
405 (defvar ps-printer-name)
407 (defun direct-ps-print-region-function (start end
408 &optional lpr-prog
409 delete-text buf display
410 &rest rest)
411 "DOS/Windows-specific function to print the region on a PostScript printer.
412 Writes the region to the device or file which is a value of
413 `ps-printer-name' \(which see\), unless the value of `ps-lpr-command'
414 indicates a specific program should be invoked."
416 (let ((printer (or (and (boundp 'dos-ps-printer)
417 (stringp (symbol-value 'dos-ps-printer))
418 (symbol-value 'dos-ps-printer))
419 ps-printer-name)))
420 (direct-print-region-helper printer start end lpr-prog
421 delete-text buf display rest)))
423 (setq ps-print-region-function 'direct-ps-print-region-function)
425 ;(setq ps-lpr-command "gs")
427 ;(setq ps-lpr-switches '("-q" "-dNOPAUSE" "-sDEVICE=epson" "-r240x60"
428 ; "-sOutputFile=LPT1"))
430 (provide 'dos-w32)
432 ;;; dos-w32.el ends here