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