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