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