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