Require cl only when compiling.
[emacs.git] / lisp / dos-fns.el
blob9ac8197ab64f7ed04a371d3a3f1ef65a915a5858
1 ;;; dos-fns.el --- MS-Dos specific functions.
3 ;; Copyright (C) 1991, 1993, 1995, 1996 Free Software Foundation, Inc.
5 ;; Maintainer: Morten Welinder (terra@diku.dk)
6 ;; Keywords: internal
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)
13 ;; any later version.
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.
25 ;;; Commentary:
27 ;; Part of this code is taken from (or derived from) demacs.
29 ;;; Code:
31 ;;; Add %t: into the mode line format just after the open-paren.
32 (let ((tail (member " %[(" mode-line-format)))
33 (setcdr tail (cons (purecopy "%t:")
34 (cdr tail))))
36 ;; Use ";" instead of ":" as a path separator (from files.el).
37 (setq path-separator ";")
39 ;; Set the null device (for compile.el).
40 (setq grep-null-device "NUL")
42 ;; Set the grep regexp to match entries with drive letters.
43 (setq grep-regexp-alist
44 '(("^\\(\\([a-zA-Z]:\\)?[^:( \t\n]+\\)[:( \t]+\\([0-9]+\\)[:) \t]" 1 3)))
46 ;; This overrides a trivial definition in files.el.
47 (defun convert-standard-filename (filename)
48 "Convert a standard file's name to something suitable for the current OS.
49 This function's standard definition is trivial; it just returns the argument.
50 However, on some systems, the function is redefined
51 with a definition that really does change some file names."
52 (let* ((dir (file-name-directory filename))
53 (string (copy-sequence (file-name-nondirectory filename)))
54 (lastchar (aref string (1- (length string))))
55 i firstdot)
56 ;; If the argument is empty, just return it.
57 (if (or (not (stringp filename))
58 (string= filename "")
59 (string= string ""))
60 filename
61 (progn
62 ;; Change a leading period to a leading underscore.
63 (if (= (aref string 0) ?.)
64 (aset string 0 ?_))
65 ;; Get rid of invalid characters.
66 (while (setq i (string-match
67 "[^-a-zA-Z0-9_.%~^$!#&{}@`'()\200-\376]"
68 string))
69 (aset string i ?_))
70 ;; If we don't have a period,
71 ;; and we have a dash or underscore that isn't the first char,
72 ;; change that to a period.
73 (if (and (not (string-match "\\." string))
74 (setq i (string-match "[-_]" string 1)))
75 (aset string i ?\.))
76 ;; If we don't have a period in the first 8 chars, insert one.
77 (if (> (or (string-match "\\." string)
78 (length string))
80 (setq string
81 (concat (substring string 0 8)
82 "."
83 (substring string 8))))
84 (setq firstdot (or (string-match "\\." string) (1- (length string))))
85 ;; Truncate to 3 chars after the first period.
86 (if (> (length string) (+ firstdot 4))
87 (setq string (substring string 0 (+ firstdot 4))))
88 ;; Change all periods except the first one into underscores.
89 (while (string-match "\\." string (1+ firstdot))
90 (setq i (string-match "\\." string (1+ firstdot)))
91 (aset string i ?_))
92 ;; If the last character of the original filename was `~',
93 ;; make sure the munged name ends with it also.
94 (if (equal lastchar ?~)
95 (aset string (1- (length string)) lastchar))
96 (concat dir string)))))
98 (defvar file-name-buffer-file-type-alist
100 ("[:/].*config.sys$" . nil) ; config.sys text
101 ("\\.elc$" . t) ; emacs stuff
102 ("\\.\\(obj\\|exe\\|com\\|lib\\|sys\\|chk\\|out\\|bin\\|ico\\|pif\\)$" . t)
103 ; MS-Dos stuff
104 ("\\.\\(arc\\|zip\\|pak\\|lzh\\|zoo\\)$" . t)
105 ; Packers
106 ("\\.\\(a\\|o\\|tar\\|z\\|gz\\|taz\\)$" . t)
107 ; Unix stuff
108 ("\\.tp[ulpw]$" . t)
109 ; Borland Pascal stuff
110 ("[:/]tags$" . t)
111 ; Emacs TAGS file
113 "*Alist for distinguishing text files from binary files.
114 Each element has the form (REGEXP . TYPE), where REGEXP is matched
115 against the file name, and TYPE is nil for text, t for binary.")
117 (defun find-buffer-file-type (filename)
118 (let ((alist file-name-buffer-file-type-alist)
119 (found nil)
120 (code nil))
121 (let ((case-fold-search t))
122 (setq filename (file-name-sans-versions filename))
123 (while (and (not found) alist)
124 (if (string-match (car (car alist)) filename)
125 (setq code (cdr (car alist))
126 found t))
127 (setq alist (cdr alist))))
128 (if found
129 (cond((memq code '(nil t)) code)
130 ((and (symbolp code) (fboundp code))
131 (funcall code filename)))
132 default-buffer-file-type)))
134 (defun find-file-binary (filename)
135 "Visit file FILENAME and treat it as binary."
136 (interactive "FFind file binary: ")
137 (let ((file-name-buffer-file-type-alist '(("" . t))))
138 (find-file filename)))
140 (defun find-file-text (filename)
141 "Visit file FILENAME and treat it as a text file."
142 (interactive "FFind file text: ")
143 (let ((file-name-buffer-file-type-alist '(("" . nil))))
144 (find-file filename)))
146 (defun find-file-not-found-set-buffer-file-type ()
147 (save-excursion
148 (set-buffer (current-buffer))
149 (setq buffer-file-type (find-buffer-file-type (buffer-file-name))))
150 nil)
152 ;;; To set the default file type on new files.
153 (add-hook 'find-file-not-found-hooks 'find-file-not-found-set-buffer-file-type)
155 (defvar msdos-shells '("command.com" "4dos.com" "ndos.com")
156 "*List of shells that use `/c' instead of `-c' and a backslashed command.")
158 (defconst register-name-alist
159 '((ax . 0) (bx . 1) (cx . 2) (dx . 3) (si . 4) (di . 5)
160 (cflag . 6) (flags . 7)
161 (al . (0 . 0)) (bl . (1 . 0)) (cl . (2 . 0)) (dl . (3 . 0))
162 (ah . (0 . 1)) (bh . (1 . 1)) (ch . (2 . 1)) (dh . (3 . 1))))
164 (defun make-register ()
165 (make-vector 8 0))
167 (defun register-value (regs name)
168 (let ((where (cdr (assoc name register-name-alist))))
169 (cond ((consp where)
170 (let ((tem (aref regs (car where))))
171 (if (zerop (cdr where))
172 (% tem 256)
173 (/ tem 256))))
174 ((numberp where)
175 (aref regs where))
176 (t nil))))
178 (defun set-register-value (regs name value)
179 (and (numberp value)
180 (>= value 0)
181 (let ((where (cdr (assoc name register-name-alist))))
182 (cond ((consp where)
183 (let ((tem (aref regs (car where)))
184 (value (logand value 255)))
185 (aset regs
186 (car where)
187 (if (zerop (cdr where))
188 (logior (logand tem 65280) value)
189 (logior (logand tem 255) (lsh value 8))))))
190 ((numberp where)
191 (aset regs where (logand value 65535))))))
192 regs)
194 (defsubst intdos (regs)
195 (int86 33 regs))
197 ;; Extra stub to functions in src/frame.c
198 ;; Emacs aborts during dump if the following don't have a doc string.
199 (defun window-frame (window)
200 "Return the frame that WINDOW resides on."
201 (selected-frame))
202 (defun raise-frame (frame)
203 "Raise FRAME to the top of the desktop."
204 nil)
205 (defun select-frame (frame &optional no-enter)
206 "Select FRAME for input events."
207 (selected-frame))
209 ;; Support for printing under MS-DOS, see lpr.el and ps-print.el.
210 (defvar dos-printer "PRN"
211 "*The name of a local MS-DOS device to which data is sent for printing.
212 \(Note that PostScript files are sent to `dos-ps-printer', which see.\)
214 Typical non-default settings would be \"LPT1\" to \"LPT3\" for
215 parallel printers, or \"COM1\" to \"COM4\" or \"AUX\" for serial
216 printers. You can also set it to a name of a file, in which
217 case the output gets appended to that file.
218 If you want to discard the printed output, set this to \"NUL\".")
220 (defun dos-print-region-function (start end
221 &optional lpr-prog
222 delete-text buf display rest)
223 "MS-DOS-specific function to print the region on a printer.
224 Writes the region to the device or file which is a value of
225 `dos-printer' \(which see\). Ignores any arguments beyond
226 START and END."
228 (write-region start end dos-printer t 0)
229 ;; Make each print-out start on a new page, but don't waste
230 ;; paper if there was a form-feed at the end of this file.
231 (if (not (char-equal (char-after (1- end)) ?\C-l))
232 (write-region "\f" nil dos-printer t 0)))
234 ;; Set this to nil if you have a port of the `lpr' program and
235 ;; you want to use it for printing. If the default setting is
236 ;; in effect, `lpr-command' and its switches are ignored when
237 ;; printing with `lpr-xxx' and `print-xxx'.
238 (setq print-region-function 'dos-print-region-function)
240 ;; Set this to nil if you have a port of the `pr' program
241 ;; (e.g., from GNU Textutils), or if you have an `lpr'
242 ;; program (see above) that can print page headers.
243 ;; If `lpr-headers-switches' is non-nil (the default) and
244 ;; `print-region-function' is set to `dos-print-region-function',
245 ;; then requests to print page headers will be silently
246 ;; ignored, and `print-buffer' and `print-region' produce
247 ;; the same output as `lpr-buffer' and `lpr-region', accordingly.
248 (setq lpr-headers-switches "(page headers are not supported)")
250 (defvar dos-ps-printer "PRN"
251 "*Method for printing PostScript files under MS-DOS.
253 If the value is a string, then it is taken as the name of the
254 device to which PostScript files are written. By default it
255 is the default printer device; typical non-default settings
256 would be \"LPT1\" to \"LPT3\" for parallel printers, or \"COM1\"
257 to \"COM4\" or \"AUX\" for serial printers. You can also set it
258 to a name of a file, in which case the output gets appended
259 to that file. \(Note that `ps-print' package already has
260 facilities for printing to a file, so you might as well use
261 them instead of changing the setting of this variable.\) If
262 you want to silently discard the printed output, set this to \"NUL\".
264 If the value is anything but a string, PostScript files will be
265 piped to the program given by `ps-lpr-command', with switches
266 given by `ps-lpr-switches', which see.")
268 (setq ps-lpr-command "gs")
270 (setq ps-lpr-switches '("-q" "-dNOPAUSE" "-sDEVICE=epson" "-r240x60"
271 "-sOutputFile=LPT1" "-"))
273 ;; Backward compatibility for obsolescent functions which
274 ;; set screen size.
276 (defun mode25 ()
277 "Changes the number of screen rows to 25."
278 (interactive)
279 (set-frame-size (selected-frame) 80 25))
281 (defun mode4350 ()
282 "Changes the number of rows to 43 or 50.
283 Emacs always tries to set the screen height to 50 rows first.
284 If this fails, it will try to set it to 43 rows, on the assumption
285 that your video hardware might not support 50-line mode."
286 (interactive)
287 (set-frame-size (selected-frame) 80 50)
288 (if (eq (frame-height (selected-frame)) 50)
289 nil ; the original built-in function returned nil
290 (set-frame-size (selected-frame) 80 43)))
292 (provide 'dos-fns)
294 ; dos-fns.el ends here