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