1 ;;; dos-w32.el --- Functions shared among MS-DOS and W32 (NT/95) platforms
3 ;; Copyright (C) 1996 Free Software Foundation, Inc.
5 ;; Maintainer: Geoff Voelker <voelker@cs.washington.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
27 ;; Parts of this code are duplicated functions taken from dos-fns.el
32 ;; Use ";" instead of ":" as a path separator (from files.el).
33 (setq path-separator
";")
35 (setq minibuffer-history-case-insensitive-variables
36 (cons 'file-name-history minibuffer-history-case-insensitive-variables
))
38 ;; Set the null device (for compile.el).
39 (setq null-device
"NUL")
41 ;; Set the grep regexp to match entries with drive letters.
42 (setq grep-regexp-alist
43 '(("^\\(\\([a-zA-Z]:\\)?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 3)))
45 ;; For distinguishing file types based upon suffixes.
46 (defvar file-name-buffer-file-type-alist
48 ("[:/].*config.sys$" . nil
) ; config.sys text
49 ("\\.elc$" . t
) ; emacs stuff
50 ("\\.\\(obj\\|exe\\|com\\|lib\\|sym\\|sys\\|chk\\|out\\|bin\\|ico\\|pif\\|class\\)$" . t
)
52 ("\\.\\(dll\\|drv\\|cpl\\|scr\\vbx\\|386\\|vxd\\|fon\\|fnt\\|fot\\|ttf\\|grp\\)$" . t
)
54 ("\\.\\(hlp\\|bmp\\|wav\\|avi\\|mpg\\|jpg\\|tif\\|mov\\|au\\)" . t
)
55 ; known binary data files
56 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t
)
58 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\|jar\\)$" . t
)
61 ; Borland Pascal stuff
65 "*Alist for distinguishing text files from binary files.
66 Each element has the form (REGEXP . TYPE), where REGEXP is matched
67 against the file name, and TYPE is nil for text, t for binary.")
69 ;; Return the pair matching filename on file-name-buffer-file-type-alist,
71 (defun find-buffer-file-type-match (filename)
72 (let ((alist file-name-buffer-file-type-alist
)
74 (let ((case-fold-search t
))
75 (setq filename
(file-name-sans-versions filename
))
76 (while (and (not found
) alist
)
77 (if (string-match (car (car alist
)) filename
)
78 (setq found
(car alist
)))
79 (setq alist
(cdr alist
)))
82 ;; Don't check for untranslated file systems here.
83 (defun find-buffer-file-type (filename)
84 (let ((match (find-buffer-file-type-match filename
))
87 default-buffer-file-type
88 (setq code
(cdr match
))
89 (cond ((memq code
'(nil t
)) code
)
90 ((and (symbolp code
) (fboundp code
))
91 (funcall code filename
))))))
93 (setq-default buffer-file-coding-system
'undecided-dos
)
95 (defun find-buffer-file-type-coding-system (command)
96 "Choose a coding system for a file operation.
97 If COMMAND is `insert-file-contents', the coding system is chosen based
98 upon the filename, the contents of `untranslated-filesystem-list' and
99 `file-name-buffer-file-type-alist', and whether the file exists:
101 If it matches in `untranslated-filesystem-list':
102 If the file exists: `no-conversion'
103 If the file does not exist: `undecided'
104 If it matches in `file-name-buffer-file-type-alist':
105 If the match is t (for binary): `no-conversion'
106 If the match is nil (for dos-text): `undecided-dos'
108 If the file exists: `undecided'
109 If the file does not exist: `undecided-dos'
111 If COMMAND is `write-region', the coding system is chosen based upon
112 the value of `buffer-file-coding-system' and `buffer-file-type'. If
113 `buffer-file-coding-system' is non-nil, its value is used. If it is
114 nil and `buffer-file-type' is t, the coding system is `no-conversion'.
115 Otherwise, it is `undecided-dos'.
117 The two most common situations are when DOS and Unix files are read
118 and written, and their names do not match in
119 `untranslated-filesystem-list' and `file-name-buffer-file-type-alist'.
120 In these cases, the coding system initially will be `undecided'. As
121 the file is read in the DOS case, the coding system will be changed to
122 `undecided-dos' as CR/LFs are detected. As the file is read in the
123 Unix case, the coding system will be changed to `undecided-unix' as
124 LFs are detected. In both cases, `buffer-file-coding-system' will be
125 set to the appropriate coding system, and the value of
126 `buffer-file-coding-system' will be used when writing the file."
128 (let ((op (nth 0 command
))
130 (binary nil
) (text nil
)
131 (undecided nil
) (undecided-unix nil
))
132 (cond ((eq op
'insert-file-contents
)
133 (setq target
(nth 1 command
))
134 ;; First check for a file name that indicates
135 ;; it is truly binary.
136 (setq binary
(find-buffer-file-type target
))
138 ;; Next check for files that MUST use DOS eol conversion.
139 ((find-buffer-file-type-match target
)
141 ;; For any other existing file, decide based on contents.
142 ((file-exists-p target
)
144 ;; Next check for a non-DOS file system.
145 ((untranslated-file-p target
)
146 (setq undecided-unix t
)))
147 (cond (binary '(no-conversion . no-conversion
))
148 (text '(undecided-dos . undecided-dos
))
149 (undecided-unix '(undecided-unix . undecided-unix
))
150 (undecided '(undecided . undecided
))
151 (t '(undecided-dos . undecided-dos
))))
152 ((eq op
'write-region
)
153 (if buffer-file-coding-system
154 (cons buffer-file-coding-system
155 buffer-file-coding-system
)
156 ;; Normally this is used only in a non-file-visiting
157 ;; buffer, because normally buffer-file-coding-system is non-nil
158 ;; in a file-visiting buffer.
160 '(no-conversion . no-conversion
)
161 '(undecided-dos . undecided-dos
)))))))
163 (modify-coding-system-alist 'file
"" 'find-buffer-file-type-coding-system
)
165 (defun find-file-binary (filename)
166 "Visit file FILENAME and treat it as binary."
167 (interactive "FFind file binary: ")
168 (let ((file-name-buffer-file-type-alist '(("" . t
))))
169 (find-file filename
)))
171 (defun find-file-text (filename)
172 "Visit file FILENAME and treat it as a text file."
173 (interactive "FFind file text: ")
174 (let ((file-name-buffer-file-type-alist '(("" . nil
))))
175 (find-file filename
)))
177 (defun find-file-not-found-set-buffer-file-coding-system ()
179 (set-buffer (current-buffer))
180 (let* ((dummy-insert-op (list 'insert-file-contents
(buffer-file-name)))
182 (find-buffer-file-type-coding-system dummy-insert-op
)))
183 (setq buffer-file-coding-system
(car coding-system-pair
))
184 (setq buffer-file-type
(eq buffer-file-coding-system
'no-conversion
)))))
186 ;;; To set the default coding system on new files.
187 (add-hook 'find-file-not-found-hooks
188 'find-file-not-found-set-buffer-file-coding-system
)
190 ;;; To accomodate filesystems that do not require CR/LF translation.
191 (defvar untranslated-filesystem-list nil
192 "List of filesystems that require no CR/LF translation when reading
193 and writing files. Each filesystem in the list is a string naming
194 the directory prefix corresponding to the filesystem.")
196 (defun untranslated-canonical-name (filename)
197 "Return FILENAME in a canonicalized form for use with the functions
198 dealing with untranslated filesystems."
199 (if (memq system-type
'(ms-dos windows-nt
))
200 ;; The canonical form for DOS/W32 is with A-Z downcased and all
201 ;; directory separators changed to directory-sep-char.
203 (setq name
(mapconcat
205 (if (and (<= ?A char
) (<= char ?Z
))
206 (char-to-string (+ (- char ?A
) ?a
))
207 (char-to-string char
)))
209 ;; Use expand-file-name to canonicalize directory separators, except
210 ;; with bare drive letters (which would have the cwd appended).
211 (if (string-match "^.:$" name
)
213 (expand-file-name name
)))
216 (defun untranslated-file-p (filename)
217 "Return t if FILENAME is on a filesystem that does not require
218 CR/LF translation, and nil otherwise."
219 (let ((fs (untranslated-canonical-name filename
))
220 (ufs-list untranslated-filesystem-list
)
222 (while (and (not found
) ufs-list
)
223 (if (string-match (concat "^" (car ufs-list
)) fs
)
225 (setq ufs-list
(cdr ufs-list
))))
228 (defun add-untranslated-filesystem (filesystem)
229 "Add FILESYSTEM to the list of filesystems that do not require
230 CR/LF translation. FILESYSTEM is a string containing the directory
231 prefix corresponding to the filesystem. For example, for a Unix
232 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
233 (interactive "fUntranslated file system: ")
234 (let ((fs (untranslated-canonical-name filesystem
)))
235 (if (member fs untranslated-filesystem-list
)
236 untranslated-filesystem-list
237 (setq untranslated-filesystem-list
238 (cons fs untranslated-filesystem-list
)))))
240 (defun remove-untranslated-filesystem (filesystem)
241 "Remove FILESYSTEM from the list of filesystems that do not require
242 CR/LF translation. FILESYSTEM is a string containing the directory
243 prefix corresponding to the filesystem. For example, for a Unix
244 filesystem mounted on drive Z:, FILESYSTEM could be \"Z:\"."
245 (interactive "fUntranslated file system: ")
246 (setq untranslated-filesystem-list
247 (delete (untranslated-canonical-name filesystem
)
248 untranslated-filesystem-list
)))
250 (setq-default default-process-coding-system
251 (if (fboundp 'start-process
)
252 '(raw-text-dos . raw-text-dos
)
253 '(undecided-dos . undecided-dos
)))
255 ;; Support for printing under DOS/Windows, see lpr.el and ps-print.el.
256 (defvar printer-name
)
258 (defun direct-print-region-function (start end
260 delete-text buf display
262 "DOS/Windows-specific function to print the region on a printer.
263 Writes the region to the device or file which is a value of
264 `printer-name' \(which see\). Ignores any arguments beyond
267 ;; DOS printers need the lines to end with CR-LF pairs, so make
268 ;; sure it always happens that way, unless the buffer is binary.
269 (let* ((coding coding-system-for-write
)
271 (if (null coding
) 'undecided
(coding-system-base coding
)))
272 (eol-type (coding-system-eol-type coding-base
)))
273 (or (eq coding-system-for-write
'no-conversion
)
274 (setq coding-system-for-write
275 (aref eol-type
1))) ; force conversion to DOS EOLs
276 (write-region start end
277 (or (and (boundp 'dos-printer
) dos-printer
)
280 ;; Make each print-out start on a new page, but don't waste
281 ;; paper if there was a form-feed at the end of this file.
282 (if (not (char-equal (char-after (1- end
)) ?\C-l
))
283 (write-region "\f" nil
284 (or (and (boundp 'dos-printer
) dos-printer
)
288 ;; Set this to nil if you have a port of the `lpr' program and
289 ;; you want to use it for printing. If the default setting is
290 ;; in effect, `lpr-command' and its switches are ignored when
291 ;; printing with `lpr-xxx' and `print-xxx'.
292 (setq print-region-function
'direct-print-region-function
)
294 ;; Set this to nil if you have a port of the `pr' program
295 ;; (e.g., from GNU Textutils), or if you have an `lpr'
296 ;; program (see above) that can print page headers.
297 ;; If `lpr-headers-switches' is non-nil (the default) and
298 ;; `print-region-function' is set to `dos-print-region-function',
299 ;; then requests to print page headers will be silently
300 ;; ignored, and `print-buffer' and `print-region' produce
301 ;; the same output as `lpr-buffer' and `lpr-region', accordingly.
302 (setq lpr-headers-switches
"(page headers are not supported)")
304 (defvar ps-printer-name
)
306 (setq ps-lpr-command
"gs")
308 (setq ps-lpr-switches
'("-q" "-dNOPAUSE" "-sDEVICE=epson" "-r240x60"
309 "-sOutputFile=LPT1" "-"))
313 ;;; dos-w32.el ends here