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