* font.c (clear_font_cache): New function, stripped from...
[emacs.git] / lisp / dos-w32.el
blob0573caa6c23c1f721662d1c50fbcf64713ca987a
1 ;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms
3 ;; Copyright (C) 1996, 2001-2013 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. DEPRECATED, DO NOT USE!
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 used in the past 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.
61 This variable is deprecated, not used anywhere, and will soon be deleted."
62 :type '(repeat (cons regexp boolean))
63 :group 'dos-fns
64 :group 'w32)
66 (make-obsolete-variable 'file-name-buffer-file-type-alist
67 'file-coding-system-alist
68 "24.4")
70 (setq-default buffer-file-coding-system 'undecided-dos)
72 (defun find-buffer-file-type-coding-system (command)
73 "Choose a coding system for a file operation in COMMAND.
74 COMMAND is a list that specifies the operation, an I/O primitive, as its
75 CAR, and the arguments that might be given to that operation as its CDR.
76 If operation is `insert-file-contents', the coding system is chosen based
77 upon the filename (the CAR of the arguments beyond the operation), the contents
78 of `untranslated-filesystem-list' and `file-name-buffer-file-type-alist',
79 and whether the file exists:
81 If it matches in `untranslated-filesystem-list':
82 If the file exists: `undecided'
83 If the file does not exist: `undecided-unix'
84 Otherwise:
85 If the file exists: `undecided'
86 If the file does not exist default value of `buffer-file-coding-system'
88 Note that the CAR of arguments to `insert-file-contents' operation could
89 be a cons cell of the form (FILENAME . BUFFER), where BUFFER is a buffer
90 into which the file's contents were already read, but not yet decoded.
92 If operation is `write-region', the coding system is chosen based
93 upon the value of `buffer-file-coding-system'. If
94 `buffer-file-coding-system' is non-nil, its value is used.
95 Otherwise, it is `undecided-dos'.
97 The most common situation is when DOS and Unix files are read and
98 written, and their names do not match in `untranslated-filesystem-list'.
99 In these cases, the coding system initially will be `undecided'.
100 As the file is read in the DOS case, the coding system will be
101 changed to `undecided-dos' as CR/LFs are detected. As the file
102 is read in the Unix case, the coding system will be changed to
103 `undecided-unix' as LFs are detected. In both cases,
104 `buffer-file-coding-system' will be set to the appropriate coding
105 system, and the value of `buffer-file-coding-system' will be used
106 when writing the file."
108 (let ((op (nth 0 command))
109 (undecided nil) (undecided-unix nil)
110 target target-buf)
111 (cond ((eq op 'insert-file-contents)
112 (setq target (nth 1 command))
113 ;; If TARGET is a cons cell, it has the form (FILENAME . BUFFER),
114 ;; where BUFFER is a buffer into which the file was already read,
115 ;; but its contents were not yet decoded. (This form of the
116 ;; arguments is used, e.g., in arc-mode.el.) This function
117 ;; doesn't care about the contents, it only looks at the file's
118 ;; name, which is the CAR of the cons cell.
119 (when (consp target)
120 (setq target-buf
121 (and (bufferp (cdr target))
122 (buffer-name (cdr target))))
123 (setq target (car target)))
124 (cond ((or
125 ;; For any existing file, decide based on contents.
126 (file-exists-p target)
127 ;; If TARGET does not exist as a file, replace its
128 ;; base name with TARGET-BUF and try again. This
129 ;; is for jka-compr's sake, which strips the
130 ;; compression (.gz etc.) extension from the
131 ;; FILENAME, but leaves it in the BUFFER's name.
132 (and (stringp target-buf)
133 (file-exists-p
134 (expand-file-name target-buf
135 (file-name-directory target)))))
136 (setq undecided t))
137 ;; Next check for a non-DOS file system.
138 ((untranslated-file-p target)
139 (setq undecided-unix t)))
140 (cond (undecided-unix '(undecided-unix . undecided-unix))
141 (undecided '(undecided . undecided))
142 (t (cons (default-value 'buffer-file-coding-system)
143 (default-value 'buffer-file-coding-system)))))
144 ((eq op 'write-region)
145 (if buffer-file-coding-system
146 (cons buffer-file-coding-system
147 buffer-file-coding-system)
148 ;; Normally this is used only in a non-file-visiting
149 ;; buffer, because normally buffer-file-coding-system is non-nil
150 ;; in a file-visiting buffer.
151 '(undecided-dos . undecided-dos))))))
153 (defun find-file-binary (filename)
154 "Visit file FILENAME and treat it as binary."
155 (interactive "FFind file binary: ")
156 (let ((coding-system-for-read 'no-conversion))
157 (find-file filename)))
159 (defun find-file-text (filename)
160 "Visit file FILENAME and treat it as a text file."
161 (interactive "FFind file text: ")
162 (let ((coding-system-for-read 'undecided-dos))
163 (find-file filename)))
165 (defun find-file-not-found-set-buffer-file-coding-system ()
166 (with-current-buffer (current-buffer)
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
174 (untranslated-file-p (buffer-file-name))))
175 (setq coding (coding-system-change-eol-conversion coding 0))
176 (setq buffer-file-coding-system coding))
177 nil)))
179 ;;; To set the default coding system on new files.
180 (add-hook 'find-file-not-found-functions
181 'find-file-not-found-set-buffer-file-coding-system)
183 ;;; To accommodate filesystems that do not require CR/LF translation.
184 (defvar untranslated-filesystem-list nil
185 "List of filesystems that require no CR/LF translation when reading
186 and writing files. Each filesystem in the list is a string naming
187 the directory prefix corresponding to the filesystem.")
189 (defun untranslated-canonical-name (filename)
190 "Return FILENAME in a canonicalized form for use with the functions
191 dealing with untranslated filesystems."
192 (if (memq system-type '(ms-dos windows-nt cygwin))
193 ;; The canonical form for DOS/W32 is with A-Z downcased and all
194 ;; directory separators changed to directory-sep-char.
195 (let ((name nil))
196 (setq name (mapconcat
197 (lambda (char)
198 (if (and (<= ?A char) (<= char ?Z))
199 (char-to-string (+ (- char ?A) ?a))
200 (char-to-string char)))
201 filename nil))
202 ;; Use expand-file-name to canonicalize directory separators, except
203 ;; with bare drive letters (which would have the cwd appended).
204 ;; Avoid expanding names that could trigger ange-ftp to prompt
205 ;; for passwords, though.
206 (if (or (string-match-p "^.:$" name)
207 (string-match-p "^/[^/:]+:" name))
208 name
209 (expand-file-name name)))
210 filename))
212 (defun untranslated-file-p (filename)
213 "Return t if FILENAME is on a filesystem that does not require
214 CR/LF translation, and nil otherwise."
215 (let ((fs (untranslated-canonical-name filename))
216 (ufs-list untranslated-filesystem-list)
217 (found nil))
218 (while (and (not found) ufs-list)
219 (if (string-match-p (concat "^" (car ufs-list)) fs)
220 (setq found t)
221 (setq ufs-list (cdr ufs-list))))
222 found))
224 (defun add-untranslated-filesystem (filesystem)
225 "Add FILESYSTEM to the list of filesystems that do not require
226 CR/LF translation. FILESYSTEM is a string containing the directory
227 prefix corresponding to the filesystem. For example, for a Unix
228 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
229 ;; We use "D", not "f", to avoid confusing the user: "f" prompts
230 ;; with a directory, but RET returns the current buffer's file, not
231 ;; its directory.
232 (interactive "DUntranslated file system: ")
233 (let ((fs (untranslated-canonical-name filesystem)))
234 (if (member fs untranslated-filesystem-list)
235 untranslated-filesystem-list
236 (setq untranslated-filesystem-list
237 (cons fs untranslated-filesystem-list)))))
239 (defun remove-untranslated-filesystem (filesystem)
240 "Remove FILESYSTEM from the list of filesystems that do not require
241 CR/LF translation. FILESYSTEM is a string containing the directory
242 prefix corresponding to the filesystem. For example, for a Unix
243 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
244 (interactive "fUntranslated file system: ")
245 (setq untranslated-filesystem-list
246 (delete (untranslated-canonical-name filesystem)
247 untranslated-filesystem-list)))
249 ;;; Support for printing under DOS/Windows, see lpr.el and ps-print.el.
251 (defcustom direct-print-region-use-command-dot-com t
252 "If non-nil, use command.com to print on Windows 9x."
253 :type 'boolean
254 :group 'dos-fns
255 :group 'w32)
257 ;; Function to actually send data to the printer port.
258 ;; Supports writing directly, and using various programs.
259 (defun direct-print-region-helper (printer
260 start end
261 lpr-prog
262 _delete-text _buf _display
263 rest)
264 (let* (;; Ignore case when matching known external program names.
265 (case-fold-search t)
266 ;; Convert / to \ in printer name, for sake of external programs.
267 (printer
268 (if (stringp printer)
269 (subst-char-in-string ?/ ?\\ printer)
270 printer))
271 ;; Find a directory that is local, to work-around Windows bug.
272 (safe-dir
273 (let ((safe-dirs (list "c:/" (getenv "windir") (getenv "TMPDIR"))))
274 (while (not (file-attributes (car safe-dirs)))
275 (setq safe-dirs (cdr safe-dirs)))
276 (car safe-dirs)))
277 (tempfile
278 (subst-char-in-string
279 ?/ ?\\
280 (make-temp-name
281 (expand-file-name "EP" temporary-file-directory))))
282 ;; capture output for diagnosis
283 (errbuf (list (get-buffer-create " *print-region-helper*") t)))
284 ;; It seems that we must be careful about the directory name that
285 ;; gets added to the printer port name by write-region when using
286 ;; the standard "PRN" or "LPTx" ports, because the write can fail if
287 ;; the directory is on a network drive. The same is true when
288 ;; asking command.com to copy the file.
289 ;; No action is needed for UNC printer names, which is just as well
290 ;; because `expand-file-name' doesn't support UNC names on MS-DOS.
291 (if (and (stringp printer) (not (string-match-p "^\\\\" printer)))
292 (setq printer
293 (subst-char-in-string ?/ ?\\ (expand-file-name printer safe-dir))))
294 ;; Handle known programs specially where necessary.
295 (unwind-protect
296 (cond
297 ;; nprint.exe is the standard print command on Netware
298 ((string-match-p "\\`nprint\\(\\.exe\\)?\\'"
299 (file-name-nondirectory lpr-prog))
300 (write-region start end tempfile nil 0)
301 (call-process lpr-prog nil errbuf nil
302 tempfile (concat "P=" printer)))
303 ;; print.exe is a standard command on NT
304 ((string-match-p "\\`print\\(\\.exe\\)?\\'"
305 (file-name-nondirectory lpr-prog))
306 ;; Be careful not to invoke print.exe on MS-DOS or Windows 9x
307 ;; though, because it is a TSR program there (hangs Emacs).
308 (or (and (eq system-type 'windows-nt)
309 (null (getenv "winbootdir")))
310 (error "Printing via print.exe is not supported on MS-DOS or Windows 9x"))
311 ;; It seems that print.exe always appends a form-feed so we
312 ;; should make sure to omit the last FF in the data.
313 (if (and (> end start)
314 (char-equal (char-before end) ?\C-l))
315 (setq end (1- end)))
316 ;; cancel out annotate function for non-PS case
317 (let ((write-region-annotate-functions nil))
318 (write-region start end tempfile nil 0))
319 (call-process lpr-prog nil errbuf nil
320 (concat "/D:" printer) tempfile))
321 ;; support lpr and similar programs for convenience, but
322 ;; supply an explicit filename because the NT version of lpr
323 ;; can't read from stdin.
324 ((> (length lpr-prog) 0)
325 (write-region start end tempfile nil 0)
326 (setq rest (append rest (list tempfile)))
327 (apply 'call-process lpr-prog nil errbuf nil rest))
328 ;; Run command.com to access printer port on Windows 9x, unless
329 ;; we are supposed to append to an existing (non-empty) file,
330 ;; to work around a bug in Windows 9x that prevents Windows
331 ;; programs from accessing LPT ports reliably.
332 ((and (eq system-type 'windows-nt)
333 (getenv "winbootdir")
334 ;; Allow cop-out so command.com isn't invoked
335 direct-print-region-use-command-dot-com
336 ;; file-attributes fails on LPT ports on Windows 9x but
337 ;; not on NT, so handle both cases for safety.
338 (eq (or (nth 7 (file-attributes printer)) 0) 0))
339 (write-region start end tempfile nil 0)
340 (let ((w32-quote-process-args nil))
341 (call-process "command.com" nil errbuf nil "/c"
342 (format "copy /b %s %s" tempfile printer))))
343 ;; write directly to the printer port
345 (write-region start end printer t 0)))
346 ;; ensure we remove the tempfile if created
347 (if (file-exists-p tempfile)
348 (delete-file tempfile)))))
350 (defvar printer-name)
352 (declare-function default-printer-name "w32fns.c")
354 (defun direct-print-region-function (start end
355 &optional lpr-prog
356 delete-text buf display
357 &rest rest)
358 "DOS/Windows-specific function to print the region on a printer.
359 Writes the region to the device or file which is a value of
360 `printer-name' (which see), unless the value of `lpr-command'
361 indicates a specific program should be invoked."
363 ;; DOS printers need the lines to end with CR-LF pairs, so make
364 ;; sure it always happens that way, unless the buffer is binary.
365 (let* ((coding coding-system-for-write)
366 (coding-base
367 (if (null coding) 'undecided (coding-system-base coding)))
368 (eol-type (coding-system-eol-type coding-base))
369 ;; Make each print-out eject the final page, but don't waste
370 ;; paper if the file ends with a form-feed already.
371 (write-region-annotate-functions
372 (cons
373 (lambda (_start end)
374 (if (not (char-equal (char-before end) ?\f))
375 `((,end . "\f"))))
376 write-region-annotate-functions))
377 (printer (or (and (boundp 'dos-printer)
378 (stringp (symbol-value 'dos-printer))
379 (symbol-value 'dos-printer))
380 printer-name
381 (default-printer-name))))
382 (or (eq coding-system-for-write 'no-conversion)
383 (setq coding-system-for-write
384 (aref eol-type 1))) ; force conversion to DOS EOLs
385 (direct-print-region-helper printer start end lpr-prog
386 delete-text buf display rest)))
388 (defvar lpr-headers-switches)
390 ;; Set this to nil if you have a port of the `pr' program
391 ;; (e.g., from GNU Textutils), or if you have an `lpr'
392 ;; program (see above) that can print page headers.
393 ;; If `lpr-headers-switches' is non-nil (the default) and
394 ;; `print-region-function' is set to `dos-print-region-function',
395 ;; then requests to print page headers will be silently
396 ;; ignored, and `print-buffer' and `print-region' produce
397 ;; the same output as `lpr-buffer' and `lpr-region', accordingly.
398 (setq lpr-headers-switches "(page headers are not supported)")
400 (defvar ps-printer-name)
402 (defun direct-ps-print-region-function (start end
403 &optional lpr-prog
404 delete-text buf display
405 &rest rest)
406 "DOS/Windows-specific function to print the region on a PostScript printer.
407 Writes the region to the device or file which is a value of
408 `ps-printer-name' (which see), unless the value of `ps-lpr-command'
409 indicates a specific program should be invoked."
411 (let ((printer (or (and (boundp 'dos-ps-printer)
412 (stringp (symbol-value 'dos-ps-printer))
413 (symbol-value 'dos-ps-printer))
414 ps-printer-name
415 (default-printer-name))))
416 (direct-print-region-helper printer start end lpr-prog
417 delete-text buf display rest)))
419 ;(setq ps-lpr-command "gs")
421 ;(setq ps-lpr-switches '("-q" "-dNOPAUSE" "-sDEVICE=epson" "-r240x60"
422 ; "-sOutputFile=LPT1"))
424 (provide 'dos-w32)
426 ;;; dos-w32.el ends here