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