1
0
mirror of https://git.savannah.gnu.org/git/emacs.git synced 2024-12-15 09:47:20 +00:00
emacs/lisp/dos-fns.el

315 lines
11 KiB
EmacsLisp
Raw Normal View History

;;; dos-fns.el --- MS-Dos specific functions
1994-01-05 21:11:33 +00:00
;; Copyright (C) 1991, 1993, 1995-1996, 2001-2020 Free Software
2013-01-01 09:11:05 +00:00
;; Foundation, Inc.
1994-01-05 21:11:33 +00:00
;; Maintainer: emacs-devel@gnu.org
1994-01-05 21:11:33 +00:00
;; Keywords: internal
;; Package: emacs
1994-01-05 21:11:33 +00:00
;; This file is part of GNU Emacs.
;; GNU Emacs is free software: you can redistribute it and/or modify
1994-01-05 21:11:33 +00:00
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
1994-01-05 21:11:33 +00:00
;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs. If not, see <https://www.gnu.org/licenses/>.
1994-01-05 21:11:33 +00:00
;;; Commentary:
;; Part of this code is taken from (or derived from) demacs.
;;; Code:
2007-11-22 04:15:55 +00:00
(declare-function int86 "dosfns.c")
(declare-function msdos-long-file-names "msdos.c")
;; See convert-standard-filename in files.el.
(defun dos-convert-standard-filename (filename)
"Convert a standard file's name to something suitable for MS-DOS.
This means to guarantee valid names and perhaps to canonicalize
certain patterns.
This function is called by `convert-standard-filename'.
On Windows and DOS, replace invalid characters. On DOS, make
sure to obey the 8.3 limitations."
(if (or (not (stringp filename))
;; This catches the case where FILENAME is "x:" or "x:/" or
;; "/", thus preventing infinite recursion.
(string-match "\\`\\([a-zA-Z]:\\)?[/\\]?\\'" filename))
filename
(let ((flen (length filename)))
;; If FILENAME has a trailing slash, remove it and recurse.
(if (memq (aref filename (1- flen)) '(?/ ?\\))
(concat (dos-convert-standard-filename
(substring filename 0 (1- flen)))
"/")
(let* (;; ange-ftp gets in the way for names like "/foo:bar".
;; We need to inhibit all magic file names, because
;; remote file names should never be passed through
;; this function, as they are not meant for the local
;; filesystem!
(file-name-handler-alist nil)
(dir
;; If FILENAME is "x:foo", file-name-directory returns
;; "x:/bar/baz", substituting the current working
;; directory on drive x:. We want to be left with "x:"
;; instead.
(if (and (< 1 flen)
(eq (aref filename 1) ?:)
(null (string-match "[/\\]" filename)))
(substring filename 0 2)
(file-name-directory filename)))
(dlen-m-1 (1- (length dir)))
(string (copy-sequence (file-name-nondirectory filename)))
(lastchar (aref string (1- (length string))))
i firstdot)
(cond
((msdos-long-file-names)
;; Replace characters that are invalid even on Windows.
(while (setq i (string-match "[?*:<>|\"\000-\037]" string))
(aset string i ?!)))
((not (member string '("" "." "..")))
;; Change a leading period to a leading underscore.
(if (= (aref string 0) ?.)
(aset string 0 ?_))
;; If the name is longer than 8 chars, and doesn't have a
;; period, and we have a dash or underscore that isn't too
;; close to the beginning, change that to a period. This
;; is so we could salvage more characters of the original
;; name by pushing them into the extension.
(if (and (not (string-match "\\." string))
(> (length string) 8)
;; We don't gain anything if we put the period closer
;; than 5 chars from the beginning (5 + 3 = 8).
(setq i (string-match "[-_]" string 5)))
(aset string i ?\.))
;; Get rid of invalid characters.
(while (setq i (string-match
"[^-a-zA-Z0-9_.%~^$!#&{}@`'()\200-\376]"
string))
(aset string i ?_))
;; If we don't have a period in the first 8 chars, insert one.
Expunge "allow" + infinitive without direct object from source and doc. Do the same for "permit", "enable", and "prevent". * doc/emacs/mule.texi: * doc/lispref/control.texi: * doc/lispref/display.texi: * doc/lispref/frames.texi: * doc/lispref/functions.texi: * doc/lispref/nonascii.texi: * doc/lispref/streams.texi: * doc/lispref/windows.texi: * doc/misc/dbus.texi: * doc/misc/eww.texi: * doc/misc/flymake.texi: * doc/misc/octave-mode.texi: * doc/misc/org.texi: * doc/misc/reftex.texi: * doc/misc/tramp.texi: * doc/misc/wisent.texi: * etc/NEWS: * lisp/autorevert.el: * lisp/cedet/mode-local.el: * lisp/cedet/semantic/senator.el: * lisp/cedet/semantic/wisent.el: * lisp/dos-fns.el: * lisp/frameset.el: * lisp/gnus/gnus-agent.el: * lisp/gnus/mm-util.el: * lisp/international/characters.el: * lisp/ldefs-boot.el: * lisp/mail/mailclient.el: * lisp/man.el: * lisp/mh-e/mh-search.el: * lisp/net/tramp-cmds.el: * lisp/net/tramp-gvfs.el: * lisp/org/org-crypt.el: * lisp/org/org-element.el: * lisp/org/org-feed.el: * lisp/org/org.el: * lisp/org/ox-ascii.el: * lisp/org/ox-icalendar.el: * lisp/org/ox-publish.el: * lisp/org/ox.el: * lisp/play/gamegrid.el: * lisp/play/gomoku.el: * lisp/progmodes/antlr-mode.el: * lisp/progmodes/python.el: * lisp/progmodes/vhdl-mode.el: * lisp/strokes.el: * lisp/textmodes/ispell.el: * lisp/tree-widget.el: * lisp/vc/pcvs.el: * lisp/window.el: * src/lisp.h: * src/w32.c: * src/w32heap.c: * src/w32term.c: * src/window.c: * src/xfaces.c: Replace solecisms like "This allow to do something" with a correct alternative, such as "This allow you to do something", "This allows something to be done" or "This allows the doing of something".
2016-01-24 20:30:39 +00:00
;; This enables having 3 more characters from the original
;; name in the extension.
(if (> (or (string-match "\\." string) (length string))
8)
(setq string
(concat (substring string 0 8)
"."
(substring string 8))))
(setq firstdot (or (string-match "\\." string)
(1- (length string))))
;; Truncate to 3 chars after the first period.
(if (> (length string) (+ firstdot 4))
(setq string (substring string 0 (+ firstdot 4))))
;; Change all periods except the first one into underscores.
;; (DOS doesn't allow more than one period.)
(while (string-match "\\." string (1+ firstdot))
(setq i (string-match "\\." string (1+ firstdot)))
(aset string i ?_))
;; If the last character of the original filename was `~' or `#',
;; make sure the munged name ends with it also. This is so that
;; backup and auto-save files retain their telltale form.
(if (memq lastchar '(?~ ?#))
(aset string (1- (length string)) lastchar))))
(concat (if (and (stringp dir)
(memq (aref dir dlen-m-1) '(?/ ?\\)))
(concat (dos-convert-standard-filename
(substring dir 0 dlen-m-1))
"/")
(dos-convert-standard-filename dir))
string))))))
(defun dos-8+3-filename (filename)
2001-04-06 19:03:00 +00:00
"Truncate FILENAME to DOS 8+3 limits."
(if (or (not (stringp filename))
(< (length filename) 5)) ; too short to give any trouble
filename
(let ((flen (length filename)))
;; If FILENAME has a trailing slash, remove it and recurse.
(if (memq (aref filename (1- flen)) '(?/ ?\\))
(concat (dos-8+3-filename (substring filename 0 (1- flen)))
2001-04-06 19:03:00 +00:00
"/")
(let* (;; ange-ftp gets in the way for names like "/foo:bar".
;; We need to inhibit all magic file names, because
;; remote file names should never be passed through
;; this function, as they are not meant for the local
;; filesystem!
(file-name-handler-alist nil)
(dir
;; If FILENAME is "x:foo", file-name-directory returns
;; "x:/bar/baz", substituting the current working
;; directory on drive x:. We want to be left with "x:"
;; instead.
(if (and (< 1 flen)
(eq (aref filename 1) ?:)
(null (string-match "[/\\]" filename)))
(substring filename 0 2)
(file-name-directory filename)))
(dlen-m-1 (1- (length dir)))
(string (copy-sequence (file-name-nondirectory filename)))
(strlen (length string))
(lastchar (aref string (1- strlen)))
2011-04-19 13:44:55 +00:00
firstdot)
2001-04-06 19:03:00 +00:00
(setq firstdot (string-match "\\." string))
(cond
(firstdot
;; Truncate the extension to 3 characters.
(if (> strlen (+ firstdot 4))
(setq string (substring string 0 (+ firstdot 4))))
;; Truncate the basename to 8 characters.
(if (> firstdot 8)
(setq string (concat (substring string 0 8)
"."
(substring string (1+ firstdot))))))
((> strlen 8)
;; No dot; truncate file name to 8 characters.
(setq string (substring string 0 8))))
;; If the last character of the original filename was `~',
;; make sure the munged name ends with it also. This is so
;; a backup file retains its final `~'.
(if (equal lastchar ?~)
(aset string (1- (length string)) lastchar))
(concat (if (and (stringp dir)
(memq (aref dir dlen-m-1) '(?/ ?\\)))
(concat (dos-8+3-filename (substring dir 0 dlen-m-1))
2001-04-06 19:03:00 +00:00
"/")
;; Recurse to truncate the leading directories.
(dos-8+3-filename dir))
2001-04-06 19:03:00 +00:00
string))))))
2008-12-26 15:02:47 +00:00
;; This is for the sake of standard file names elsewhere in Emacs that
;; are defined as constant strings or via defconst, and whose
;; conversion via `dos-convert-standard-filename' does not give good
2008-12-26 15:02:47 +00:00
;; enough results.
(defun dosified-file-name (file-name)
"Return a variant of FILE-NAME that is valid on MS-DOS filesystems.
This function is for those rare cases where `dos-convert-standard-filename'
2008-12-26 15:02:47 +00:00
does not do a job that is good enough, e.g. if you need to preserve the
file-name extension. It recognizes only certain specific file names
that are used in Emacs Lisp sources; any other file name will be
returned unaltered."
(cond
;; See files.el:dir-locals-file.
((string= file-name ".dir-locals")
"_dir-locals")
2008-12-26 15:02:47 +00:00
(t
file-name)))
;; See dos-vars.el for defcustom.
(defvar msdos-shells)
1994-01-05 21:11:33 +00:00
;; Override settings chosen at startup.
(defun dos-set-default-process-coding-system ()
(setq default-process-coding-system
'(undecided-dos . undecided-dos)))
(add-hook 'before-init-hook 'dos-set-default-process-coding-system)
;; File names defined in preloaded packages can be incorrect or
;; invalid if long file names were available during dumping, but not
;; at runtime, or vice versa, and if the default file name begins with
;; a period. Their defcustom's need to be reevaluated at startup. To
;; see if the list of defcustom's below is up to date, run the command
;; "M-x apropos-value RET ~/\. RET".
(defun dos-reevaluate-defcustoms ()
;; This is not needed in Emacs 23.2 and later, as trash-directory is
;; initialized as nil. But something like this might become
;; necessary in the future, so I'm keeping it here as a reminder.
;(custom-reevaluate-setting 'trash-directory)
)
(add-hook 'before-init-hook 'dos-reevaluate-defcustoms)
Move define-obsolete-variable-alias before the var's definition. * lisp/vc/log-edit.el (vc-comment-ring, vc-comment-ring-index): * lisp/tooltip.el (tooltip-hook): * lisp/textmodes/reftex-toc.el (reftex-toc-map): * lisp/textmodes/reftex-sel.el (reftex-select-label-map) (reftex-select-bib-map): * lisp/textmodes/reftex-index.el (reftex-index-map) (reftex-index-phrases-map): * lisp/speedbar.el (speedbar-syntax-table, speedbar-key-map): * lisp/progmodes/meta-mode.el (meta-mode-map): * lisp/novice.el (disabled-command-hook): * lisp/loadhist.el (unload-hook-features-list): * lisp/frame.el (blink-cursor): * lisp/files.el (find-file-not-found-hooks, write-file-hooks) (write-contents-hooks): * lisp/emulation/tpu-edt.el (GOLD-map): * lisp/emacs-lock.el (emacs-lock-from-exiting): * lisp/emacs-lisp/generic.el (generic-font-lock-defaults): * lisp/emacs-lisp/chart.el (chart-map): * lisp/dos-fns.el (register-name-alist): * lisp/dired-x.el (dired-omit-files-p): * lisp/desktop.el (desktop-enable): * lisp/cus-edit.el (custom-mode-hook): * lisp/buff-menu.el (buffer-menu-mode-hook): * lisp/bookmark.el (bookmark-read-annotation-text-func) (bookmark-exit-hooks): * lisp/allout.el (allout-mode-deactivate-hook) (allout-exposure-change-hook, allout-structure-added-hook) (allout-structure-deleted-hook, allout-structure-shifted-hook): * lisp/dirtrack.el (dirtrack-toggle, dirtrackp, dirtrack-debug-toggle) (dirtrack-debug): Move call to define-obsolete-variable-alias so it comes before the corresponding variable's definition.
2012-05-13 03:05:06 +00:00
(define-obsolete-variable-alias
'register-name-alist 'dos-register-name-alist "24.1")
(defvar dos-register-name-alist
1994-01-05 21:11:33 +00:00
'((ax . 0) (bx . 1) (cx . 2) (dx . 3) (si . 4) (di . 5)
(cflag . 6) (flags . 7)
(al . (0 . 0)) (bl . (1 . 0)) (cl . (2 . 0)) (dl . (3 . 0))
(ah . (0 . 1)) (bh . (1 . 1)) (ch . (2 . 1)) (dh . (3 . 1))))
1994-01-05 21:11:33 +00:00
(defun dos-make-register ()
1994-01-05 21:11:33 +00:00
(make-vector 8 0))
(define-obsolete-function-alias 'make-register 'dos-make-register "24.1")
(defun dos-register-value (regs name)
(let ((where (cdr (assoc name dos-register-name-alist))))
1994-01-05 21:11:33 +00:00
(cond ((consp where)
(let ((tem (aref regs (car where))))
(if (zerop (cdr where))
(% tem 256)
(/ tem 256))))
((numberp where)
(aref regs where))
(t nil))))
(define-obsolete-function-alias 'register-value 'dos-register-value "24.1")
(defun dos-set-register-value (regs name value)
1994-01-05 21:11:33 +00:00
(and (numberp value)
(>= value 0)
(let ((where (cdr (assoc name dos-register-name-alist))))
1994-01-05 21:11:33 +00:00
(cond ((consp where)
(let ((tem (aref regs (car where)))
(value (logand value 255)))
(aset regs
(car where)
(if (zerop (cdr where))
(logior (logand tem 65280) value)
Audit use of lsh and fix glitches I audited use of lsh in the Lisp source code, and fixed the glitches that I found. While I was at it, I replaced uses of lsh with ash when either will do. Replacement is OK when either argument is known to be nonnegative, or when only the low-order bits of the result matter, and is a (minor) win since ash is a bit more solid than lsh nowadays, and is a bit faster. * lisp/calc/calc-ext.el (math-check-fixnum): Prefer most-positive-fixnum to (lsh -1 -1). * lisp/vc/vc-hg.el (vc-hg-state-fast): When testing fixnum width, prefer (zerop (ash most-positive-fixnum -32)) to (zerop (lsh -1 32)) (Bug#32485#11). * lisp/emacs-lisp/bytecomp.el (byte-compile-lapcode): Tighten sanity-check for bytecode overflow, by checking that the result of (ash pc -8) is nonnegative. Formerly this check was not needed since lsh was used and the number overflowed differently. * lisp/net/dns.el (dns-write): Fix some obvious sign typos in shift counts. Evidently this part of the code has never been exercised. * lisp/progmodes/hideif.el (hif-shiftleft, hif-shiftright): * lisp/term/common-win.el (x-setup-function-keys): Simplify. * admin/unidata/unidata-gen.el, admin/unidata/uvs.el: * doc/lispref/keymaps.texi, doc/lispref/syntax.texi: * doc/misc/calc.texi, doc/misc/cl.texi, etc/NEWS.19: * lisp/arc-mode.el, lisp/calc/calc-bin.el, lisp/calc/calc-comb.el: * lisp/calc/calc-ext.el, lisp/calc/calc-math.el: * lisp/cedet/semantic/wisent/comp.el, lisp/composite.el: * lisp/disp-table.el, lisp/dos-fns.el, lisp/edmacro.el: * lisp/emacs-lisp/bindat.el, lisp/emacs-lisp/byte-opt.el: * lisp/emacs-lisp/bytecomp.el, lisp/emacs-lisp/cl-extra.el: * lisp/erc/erc-dcc.el, lisp/facemenu.el, lisp/gnus/message.el: * lisp/gnus/nndoc.el, lisp/gnus/nnmaildir.el, lisp/image.el: * lisp/international/ccl.el, lisp/international/fontset.el: * lisp/international/mule-cmds.el, lisp/international/mule.el: * lisp/json.el, lisp/mail/binhex.el, lisp/mail/rmail.el: * lisp/mail/uudecode.el, lisp/md4.el, lisp/net/dns.el: * lisp/net/ntlm.el, lisp/net/sasl.el, lisp/net/socks.el: * lisp/net/tramp.el, lisp/obsolete/levents.el: * lisp/obsolete/pgg-parse.el, lisp/org/org.el: * lisp/org/ox-publish.el, lisp/progmodes/cc-defs.el: * lisp/progmodes/ebnf2ps.el, lisp/progmodes/hideif.el: * lisp/ps-bdf.el, lisp/ps-print.el, lisp/simple.el: * lisp/tar-mode.el, lisp/term/common-win.el: * lisp/term/tty-colors.el, lisp/term/xterm.el, lisp/vc/vc-git.el: * lisp/vc/vc-hg.el, lisp/x-dnd.el, test/src/data-tests.el: Prefer ash to lsh when either will do.
2018-08-21 20:44:03 +00:00
(logior (logand tem 255) (ash value 8))))))
1994-01-05 21:11:33 +00:00
((numberp where)
(aset regs where (logand value 65535))))))
1994-01-05 21:11:33 +00:00
regs)
(define-obsolete-function-alias
'set-register-value 'dos-set-register-value "24.1")
(defsubst dos-intdos (regs)
"Issue the DOS Int 21h with registers REGS.
REGS should be a vector produced by `dos-make-register'
and `dos-set-register-value', which see."
1994-01-05 21:11:33 +00:00
(int86 33 regs))
(define-obsolete-function-alias 'intdos 'dos-intdos "24.1")
;; Backward compatibility for obsolescent functions which
;; set screen size.
(defun dos-mode25 ()
"Changes the number of screen rows to 25."
(interactive)
(set-frame-size (selected-frame) 80 25))
(define-obsolete-function-alias 'mode25 'dos-mode25 "24.1")
(defun dos-mode4350 ()
"Changes the number of rows to 43 or 50.
Emacs always tries to set the screen height to 50 rows first.
If this fails, it will try to set it to 43 rows, on the assumption
that your video hardware might not support 50-line mode."
(interactive)
(set-frame-size (selected-frame) 80 50)
(if (eq (frame-height (selected-frame)) 50)
nil ; the original built-in function returned nil
(set-frame-size (selected-frame) 80 43)))
(define-obsolete-function-alias 'mode4350 'dos-mode4350 "24.1")
(provide 'dos-fns)
;;; dos-fns.el ends here