Merge from trunk
[emacs.git] / lisp / dos-w32.el
blob0962ae5f13a67bb3faf1786326a4ee7cda1cbcaf
1 ;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms
3 ;; Copyright (C) 1996, 2001, 2002, 2003, 2004, 2005,
4 ;; 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; Maintainer: Geoff Voelker <voelker@cs.washington.edu>
7 ;; Keywords: internal
8 ;; Package: emacs
10 ;; This file is part of GNU Emacs.
12 ;; GNU Emacs is free software: you can redistribute it and/or modify
13 ;; it under the terms of the GNU General Public License as published by
14 ;; the Free Software Foundation, either version 3 of the License, or
15 ;; (at your option) any later version.
17 ;; GNU Emacs is distributed in the hope that it will be useful,
18 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 ;; GNU General Public License for more details.
22 ;; You should have received a copy of the GNU General Public License
23 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
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 ;; For distinguishing file types based upon suffixes.
42 (defvar file-name-buffer-file-type-alist
44 ("[:/].*config.sys$" . nil) ; config.sys text
45 ("\\.\\(obj\\|exe\\|com\\|lib\\|sys\\|bin\\|ico\\|pif\\|class\\)$" . t)
46 ; MS-Dos stuff
47 ("\\.\\(dll\\|drv\\|386\\|vxd\\|fon\\|fnt\\|fot\\|ttf\\|grp\\)$" . t)
48 ; Windows stuff
49 ("\\.\\(bmp\\|wav\\|avi\\|mpg\\|jpg\\|tif\\|mov\\|au\\)$" . t)
50 ; known binary data files
51 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t)
52 ; Packers
53 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\|jar\\)$" . t)
54 ; Unix stuff
55 ("\\.sx[dmicw]$" . t) ; OpenOffice.org
56 ("\\.tp[ulpw]$" . t) ; borland Pascal stuff
57 ("[:/]tags$" . nil) ; emacs TAGS file
59 "*Alist for distinguishing text files from binary files.
60 Each element has the form (REGEXP . TYPE), where REGEXP is matched
61 against the file name, and TYPE is nil for text, t for binary.")
63 ;; Return the pair matching filename on file-name-buffer-file-type-alist,
64 ;; or nil otherwise.
65 (defun find-buffer-file-type-match (filename)
66 (let ((alist file-name-buffer-file-type-alist)
67 (found nil))
68 (let ((case-fold-search t))
69 (setq filename (file-name-sans-versions filename))
70 (while (and (not found) alist)
71 (if (string-match (car (car alist)) filename)
72 (setq found (car alist)))
73 (setq alist (cdr alist)))
74 found)))
76 ;; Don't check for untranslated file systems here.
77 (defun find-buffer-file-type (filename)
78 (let ((match (find-buffer-file-type-match filename))
79 (code))
80 (if (not match)
81 (default-value 'buffer-file-type)
82 (setq code (cdr match))
83 (cond ((memq code '(nil t)) code)
84 ((and (symbolp code) (fboundp code))
85 (funcall code filename))))))
87 (setq-default buffer-file-coding-system 'undecided-dos)
89 (defun find-buffer-file-type-coding-system (command)
90 "Choose a coding system for a file operation in COMMAND.
91 COMMAND is a list that specifies the operation, an I/O primitive, as its
92 CAR, and the arguments that might be given to that operation as its CDR.
93 If operation is `insert-file-contents', the coding system is chosen based
94 upon the filename (the CAR of the arguments beyond the operation), the contents
95 of `untranslated-filesystem-list' and `file-name-buffer-file-type-alist',
96 and whether the file exists:
98 If it matches in `untranslated-filesystem-list':
99 If the file exists: `undecided'
100 If the file does not exist: `undecided-unix'
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'
104 Otherwise:
105 If the file exists: `undecided'
106 If the file does not exist default value of `buffer-file-coding-system'
108 Note that the CAR of arguments to `insert-file-contents' operation could
109 be a cons cell of the form \(FILENAME . BUFFER\), where BUFFER is a buffer
110 into which the file's contents were already read, but not yet decoded.
112 If operation is `write-region', the coding system is chosen based upon
113 the value of `buffer-file-coding-system' and `buffer-file-type'. If
114 `buffer-file-coding-system' is non-nil, its value is used. If it is
115 nil and `buffer-file-type' is t, the coding system is `no-conversion'.
116 Otherwise, it is `undecided-dos'.
118 The two most common situations are when DOS and Unix files are read
119 and written, and their names do not match in
120 `untranslated-filesystem-list' and `file-name-buffer-file-type-alist'.
121 In these cases, the coding system initially will be `undecided'. As
122 the file is read in the DOS case, the coding system will be changed to
123 `undecided-dos' as CR/LFs are detected. As the file is read in the
124 Unix case, the coding system will be changed to `undecided-unix' as
125 LFs are detected. In both cases, `buffer-file-coding-system' will be
126 set to the appropriate coding system, and the value of
127 `buffer-file-coding-system' will be used when writing the file."
129 (let ((op (nth 0 command))
130 (binary nil) (text nil)
131 (undecided nil) (undecided-unix nil)
132 target target-buf)
133 (cond ((eq op 'insert-file-contents)
134 (setq target (nth 1 command))
135 ;; If TARGET is a cons cell, it has the form (FILENAME . BUFFER),
136 ;; where BUFFER is a buffer into which the file was already read,
137 ;; but its contents were not yet decoded. (This form of the
138 ;; arguments is used, e.g., in arc-mode.el.) This function
139 ;; doesn't care about the contents, it only looks at the file's
140 ;; name, which is the CAR of the cons cell.
141 (when (consp target)
142 (setq target-buf
143 (and (bufferp (cdr target))
144 (buffer-name (cdr target))))
145 (setq target (car target)))
146 ;; First check for a file name that indicates
147 ;; it is truly binary.
148 (setq binary (find-buffer-file-type target))
149 (cond (binary)
150 ;; Next check for files that MUST use DOS eol conversion.
151 ((find-buffer-file-type-match target)
152 (setq text t))
153 ;; For any other existing file, decide based on contents.
154 ((or
155 (file-exists-p target)
156 ;; If TARGET does not exist as a file, replace its
157 ;; base name with TARGET-BUF and try again. This
158 ;; is for jka-compr's sake, which strips the
159 ;; compression (.gz etc.) extension from the
160 ;; FILENAME, but leaves it in the BUFFER's name.
161 (and (stringp target-buf)
162 (file-exists-p
163 (expand-file-name target-buf
164 (file-name-directory target)))))
165 (setq undecided t))
166 ;; Next check for a non-DOS file system.
167 ((untranslated-file-p target)
168 (setq undecided-unix t)))
169 (cond (binary '(no-conversion . no-conversion))
170 (text '(undecided-dos . undecided-dos))
171 (undecided-unix '(undecided-unix . undecided-unix))
172 (undecided '(undecided . undecided))
173 (t (cons (default-value 'buffer-file-coding-system)
174 (default-value 'buffer-file-coding-system)))))
175 ((eq op 'write-region)
176 (if buffer-file-coding-system
177 (cons buffer-file-coding-system
178 buffer-file-coding-system)
179 ;; Normally this is used only in a non-file-visiting
180 ;; buffer, because normally buffer-file-coding-system is non-nil
181 ;; in a file-visiting buffer.
182 (if buffer-file-type
183 '(no-conversion . no-conversion)
184 '(undecided-dos . undecided-dos)))))))
186 (modify-coding-system-alist 'file "" 'find-buffer-file-type-coding-system)
188 (defun find-file-binary (filename)
189 "Visit file FILENAME and treat it as binary."
190 (interactive "FFind file binary: ")
191 (let ((file-name-buffer-file-type-alist '(("" . t))))
192 (find-file filename)))
194 (defun find-file-text (filename)
195 "Visit file FILENAME and treat it as a text file."
196 (interactive "FFind file text: ")
197 (let ((file-name-buffer-file-type-alist '(("" . nil))))
198 (find-file filename)))
200 (defun find-file-not-found-set-buffer-file-coding-system ()
201 (with-current-buffer (current-buffer)
202 (let ((coding buffer-file-coding-system))
203 ;; buffer-file-coding-system is already set by
204 ;; find-operation-coding-system, which was called from
205 ;; insert-file-contents. All that's left is to change
206 ;; the EOL conversion, if required by the user.
207 (when (and (null coding-system-for-read)
208 (or inhibit-eol-conversion
209 (untranslated-file-p (buffer-file-name))))
210 (setq coding (coding-system-change-eol-conversion coding 0))
211 (setq buffer-file-coding-system coding))
212 (setq buffer-file-type (eq buffer-file-coding-system 'no-conversion)))))
214 ;;; To set the default coding system on new files.
215 (add-hook 'find-file-not-found-functions
216 'find-file-not-found-set-buffer-file-coding-system)
218 ;;; To accommodate filesystems that do not require CR/LF translation.
219 (defvar untranslated-filesystem-list nil
220 "List of filesystems that require no CR/LF translation when reading
221 and writing files. Each filesystem in the list is a string naming
222 the directory prefix corresponding to the filesystem.")
224 (defun untranslated-canonical-name (filename)
225 "Return FILENAME in a canonicalized form for use with the functions
226 dealing with untranslated filesystems."
227 (if (memq system-type '(ms-dos windows-nt cygwin))
228 ;; The canonical form for DOS/W32 is with A-Z downcased and all
229 ;; directory separators changed to directory-sep-char.
230 (let ((name nil))
231 (setq name (mapconcat
232 '(lambda (char)
233 (if (and (<= ?A char) (<= char ?Z))
234 (char-to-string (+ (- char ?A) ?a))
235 (char-to-string char)))
236 filename nil))
237 ;; Use expand-file-name to canonicalize directory separators, except
238 ;; with bare drive letters (which would have the cwd appended).
239 ;; Avoid expanding names that could trigger ange-ftp to prompt
240 ;; for passwords, though.
241 (if (or (string-match "^.:$" name)
242 (string-match "^/[^/:]+:" name))
243 name
244 (expand-file-name name)))
245 filename))
247 (defun untranslated-file-p (filename)
248 "Return t if FILENAME is on a filesystem that does not require
249 CR/LF translation, and nil otherwise."
250 (let ((fs (untranslated-canonical-name filename))
251 (ufs-list untranslated-filesystem-list)
252 (found nil))
253 (while (and (not found) ufs-list)
254 (if (string-match (concat "^" (car ufs-list)) fs)
255 (setq found t)
256 (setq ufs-list (cdr ufs-list))))
257 found))
259 (defun add-untranslated-filesystem (filesystem)
260 "Add FILESYSTEM to the list of filesystems that do not require
261 CR/LF translation. FILESYSTEM is a string containing the directory
262 prefix corresponding to the filesystem. For example, for a Unix
263 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
264 ;; We use "D", not "f", to avoid confusing the user: "f" prompts
265 ;; with a directory, but RET returns the current buffer's file, not
266 ;; its directory.
267 (interactive "DUntranslated file system: ")
268 (let ((fs (untranslated-canonical-name filesystem)))
269 (if (member fs untranslated-filesystem-list)
270 untranslated-filesystem-list
271 (setq untranslated-filesystem-list
272 (cons fs untranslated-filesystem-list)))))
274 (defun remove-untranslated-filesystem (filesystem)
275 "Remove FILESYSTEM from the list of filesystems that do not require
276 CR/LF translation. FILESYSTEM is a string containing the directory
277 prefix corresponding to the filesystem. For example, for a Unix
278 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
279 (interactive "fUntranslated file system: ")
280 (setq untranslated-filesystem-list
281 (delete (untranslated-canonical-name filesystem)
282 untranslated-filesystem-list)))
284 ;;; Support for printing under DOS/Windows, see lpr.el and ps-print.el.
286 (defvar direct-print-region-use-command-dot-com t
287 "*Control whether command.com is used to print on Windows 9x.")
289 ;; Function to actually send data to the printer port.
290 ;; Supports writing directly, and using various programs.
291 (defun direct-print-region-helper (printer
292 start end
293 lpr-prog
294 delete-text buf display
295 rest)
296 (let* (;; Ignore case when matching known external program names.
297 (case-fold-search t)
298 ;; Convert / to \ in printer name, for sake of external programs.
299 (printer
300 (if (stringp printer)
301 (subst-char-in-string ?/ ?\\ printer)
302 printer))
303 ;; Find a directory that is local, to work-around Windows bug.
304 (safe-dir
305 (let ((safe-dirs (list "c:/" (getenv "windir") (getenv "TMPDIR"))))
306 (while (not (file-attributes (car safe-dirs)))
307 (setq safe-dirs (cdr safe-dirs)))
308 (car safe-dirs)))
309 (tempfile
310 (subst-char-in-string
311 ?/ ?\\
312 (make-temp-name
313 (expand-file-name "EP" temporary-file-directory))))
314 ;; capture output for diagnosis
315 (errbuf (list (get-buffer-create " *print-region-helper*") t)))
316 ;; It seems that we must be careful about the directory name that
317 ;; gets added to the printer port name by write-region when using
318 ;; the standard "PRN" or "LPTx" ports, because the write can fail if
319 ;; the directory is on a network drive. The same is true when
320 ;; asking command.com to copy the file.
321 ;; No action is needed for UNC printer names, which is just as well
322 ;; because `expand-file-name' doesn't support UNC names on MS-DOS.
323 (if (and (stringp printer) (not (string-match "^\\\\" printer)))
324 (setq printer
325 (subst-char-in-string ?/ ?\\ (expand-file-name printer safe-dir))))
326 ;; Handle known programs specially where necessary.
327 (unwind-protect
328 (cond
329 ;; nprint.exe is the standard print command on Netware
330 ((string-match "^nprint\\(\\.exe\\)?$" (file-name-nondirectory lpr-prog))
331 (write-region start end tempfile nil 0)
332 (call-process lpr-prog nil errbuf nil
333 tempfile (concat "P=" printer)))
334 ;; print.exe is a standard command on NT
335 ((string-match "^print\\(\\.exe\\)?$" (file-name-nondirectory lpr-prog))
336 ;; Be careful not to invoke print.exe on MS-DOS or Windows 9x
337 ;; though, because it is a TSR program there (hangs Emacs).
338 (or (and (eq system-type 'windows-nt)
339 (null (getenv "winbootdir")))
340 (error "Printing via print.exe is not supported on MS-DOS or Windows 9x"))
341 ;; It seems that print.exe always appends a form-feed so we
342 ;; should make sure to omit the last FF in the data.
343 (if (and (> end start)
344 (char-equal (char-before end) ?\C-l))
345 (setq end (1- end)))
346 ;; cancel out annotate function for non-PS case
347 (let ((write-region-annotate-functions nil))
348 (write-region start end tempfile nil 0))
349 (call-process lpr-prog nil errbuf nil
350 (concat "/D:" printer) tempfile))
351 ;; support lpr and similar programs for convenience, but
352 ;; supply an explicit filename because the NT version of lpr
353 ;; can't read from stdin.
354 ((> (length lpr-prog) 0)
355 (write-region start end tempfile nil 0)
356 (setq rest (append rest (list tempfile)))
357 (apply 'call-process lpr-prog nil errbuf nil rest))
358 ;; Run command.com to access printer port on Windows 9x, unless
359 ;; we are supposed to append to an existing (non-empty) file,
360 ;; to work around a bug in Windows 9x that prevents Win32
361 ;; programs from accessing LPT ports reliably.
362 ((and (eq system-type 'windows-nt)
363 (getenv "winbootdir")
364 ;; Allow cop-out so command.com isn't invoked
365 direct-print-region-use-command-dot-com
366 ;; file-attributes fails on LPT ports on Windows 9x but
367 ;; not on NT, so handle both cases for safety.
368 (eq (or (nth 7 (file-attributes printer)) 0) 0))
369 (write-region start end tempfile nil 0)
370 (let ((w32-quote-process-args nil))
371 (call-process "command.com" nil errbuf nil "/c"
372 (format "copy /b %s %s" tempfile printer))))
373 ;; write directly to the printer port
375 (write-region start end printer t 0)))
376 ;; ensure we remove the tempfile if created
377 (if (file-exists-p tempfile)
378 (delete-file tempfile)))))
380 (defvar printer-name)
382 (declare-function default-printer-name "w32fns.c")
384 (defun direct-print-region-function (start end
385 &optional lpr-prog
386 delete-text buf display
387 &rest rest)
388 "DOS/Windows-specific function to print the region on a printer.
389 Writes the region to the device or file which is a value of
390 `printer-name' \(which see\), unless the value of `lpr-command'
391 indicates a specific program should be invoked."
393 ;; DOS printers need the lines to end with CR-LF pairs, so make
394 ;; sure it always happens that way, unless the buffer is binary.
395 (let* ((coding coding-system-for-write)
396 (coding-base
397 (if (null coding) 'undecided (coding-system-base coding)))
398 (eol-type (coding-system-eol-type coding-base))
399 ;; Make each print-out eject the final page, but don't waste
400 ;; paper if the file ends with a form-feed already.
401 (write-region-annotate-functions
402 (cons
403 (lambda (start end)
404 (if (not (char-equal (char-before end) ?\C-l))
405 `((,end . "\f"))))
406 write-region-annotate-functions))
407 (printer (or (and (boundp 'dos-printer)
408 (stringp (symbol-value 'dos-printer))
409 (symbol-value 'dos-printer))
410 printer-name
411 (default-printer-name))))
412 (or (eq coding-system-for-write 'no-conversion)
413 (setq coding-system-for-write
414 (aref eol-type 1))) ; force conversion to DOS EOLs
415 (direct-print-region-helper printer start end lpr-prog
416 delete-text buf display rest)))
418 (defvar print-region-function)
419 (defvar lpr-headers-switches)
420 (setq print-region-function 'direct-print-region-function)
422 ;; Set this to nil if you have a port of the `pr' program
423 ;; (e.g., from GNU Textutils), or if you have an `lpr'
424 ;; program (see above) that can print page headers.
425 ;; If `lpr-headers-switches' is non-nil (the default) and
426 ;; `print-region-function' is set to `dos-print-region-function',
427 ;; then requests to print page headers will be silently
428 ;; ignored, and `print-buffer' and `print-region' produce
429 ;; the same output as `lpr-buffer' and `lpr-region', accordingly.
430 (setq lpr-headers-switches "(page headers are not supported)")
432 (defvar ps-printer-name)
434 (defun direct-ps-print-region-function (start end
435 &optional lpr-prog
436 delete-text buf display
437 &rest rest)
438 "DOS/Windows-specific function to print the region on a PostScript printer.
439 Writes the region to the device or file which is a value of
440 `ps-printer-name' \(which see\), unless the value of `ps-lpr-command'
441 indicates a specific program should be invoked."
443 (let ((printer (or (and (boundp 'dos-ps-printer)
444 (stringp (symbol-value 'dos-ps-printer))
445 (symbol-value 'dos-ps-printer))
446 ps-printer-name
447 (default-printer-name))))
448 (direct-print-region-helper printer start end lpr-prog
449 delete-text buf display rest)))
451 (defvar ps-print-region-function)
452 (setq ps-print-region-function 'direct-ps-print-region-function)
454 ;(setq ps-lpr-command "gs")
456 ;(setq ps-lpr-switches '("-q" "-dNOPAUSE" "-sDEVICE=epson" "-r240x60"
457 ; "-sOutputFile=LPT1"))
459 (provide 'dos-w32)
461 ;; arch-tag: dcfefdd2-362f-4fbc-9141-9634f5f4d6a7
462 ;;; dos-w32.el ends here