home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-387-Vol-3of3.iso / d / dos-mode.zip / DOS-MODE.EL next >
Lisp/Scheme  |  1993-03-25  |  6KB  |  173 lines

  1. ;; -*-Emacs-Lisp-*-
  2. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  3. ;;
  4. ;; File:         dos-mode.el
  5. ;; RCS:          $Header: dos-mode.el,v 1.10 92/11/26 12:07:55 ange Exp $
  6. ;; Description:  MSDOS minor mode for GNU Emacs
  7. ;; Author:       Andy Norman, Hewlett-Packard Labs, Bristol, England.
  8. ;; Created:      Wed May  6 16:42:29 1992
  9. ;; Modified:     Thu Nov 26 12:07:10 1992 (Andy Norman) ange@hplb.hpl.hp.com
  10. ;; Language:     Emacs-Lisp
  11. ;;
  12. ;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;
  13.  
  14. ;;; Copyright (C) 1992  Andy Norman.
  15. ;;;
  16. ;;; Author: Andy Norman (ange@hplb.hpl.hp.com)
  17. ;;;
  18. ;;; This program is free software; you can redistribute it and/or modify
  19. ;;; it under the terms of the GNU General Public License as published by
  20. ;;; the Free Software Foundation; either version 1, or (at your option)
  21. ;;; any later version.
  22. ;;;
  23. ;;; This program is distributed in the hope that it will be useful,
  24. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  25. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  26. ;;; GNU General Public License for more details.
  27. ;;;
  28. ;;; A copy of the GNU General Public License can be obtained from this
  29. ;;; program's author (send electronic mail to ange@hplb.hpl.hp.com) or from
  30. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  31. ;;; 02139, USA.
  32.  
  33. ;;; Description:
  34. ;;;
  35. ;;; This package does two things:
  36. ;;;
  37. ;;; If a file being read into a buffer has the ^M character at the end of each
  38. ;;; line then these extra characters are discarded from the file's buffer.
  39. ;;; The trailing ^Z, if present, is discarded too.  The buffer is now put into
  40. ;;; the dos-mode minor-mode.
  41. ;;;
  42. ;;; Secondly, when a buffer in dos-mode is being written out, the ^M and
  43. ;;; optional ^Z characters are replaced before the write happens.
  44. ;;;
  45. ;;; dos-mode can be toggled for a buffer with M-x dos-mode.
  46.  
  47. ;;; Installation:
  48. ;;;
  49. ;;; Byte-compile dos-mode.el to dos-mode.elc and put them both in a directory
  50. ;;; on your load-path.  Load the package from your .emacs file with:
  51. ;;;
  52. ;;;   (require 'dos-mode)
  53. ;;;
  54. ;;; dos-mode can't sensibly be auto-loaded; you are either using it, or you
  55. ;;; aren't.
  56.  
  57. ;;; LISPDIR ENTRY for the Elisp Archive
  58. ;;; 
  59. ;;;    LCD Archive Entry:
  60. ;;;    dos-mode|Andy Norman|ange@hplb.hpl.hp.com
  61. ;;;    |MSDOS minor mode for GNU Emacs
  62. ;;;    |$Date: 92/11/26 12:07:55 $|$Revision: 1.10 $|
  63.  
  64. (provide 'dos-mode)
  65.  
  66. (defvar dos-mode-distance 200
  67.   "Number of characters to search for RETURN when looking for a DOS file.")
  68.  
  69. (defvar dos-mode nil
  70.   "This buffer is to be converted to/from DOS format when read/written.")
  71.  
  72. (make-variable-buffer-local 'dos-mode)
  73. (setq-default dos-mode nil)
  74.  
  75. (or (assq 'dos-mode minor-mode-alist)
  76.     (setq minor-mode-alist (cons '(dos-mode " DOS") minor-mode-alist)))
  77.  
  78. (defun dos-mode (arg)
  79.   "Toggle DOS mode.
  80. With arg, turn DOS mode on iff arg is positive.
  81. In DOS mode, when the buffer is saved, it is converted to DOS format first,
  82. and when restored it is converted to UNIX format first."
  83.   (interactive "P")
  84.   (setq dos-mode
  85.     (if (null arg) (not dos-mode)
  86.       (> (prefix-numeric-value arg) 0)))
  87.   (set-buffer-modified-p t))
  88.  
  89. (defvar dos-seen-ctl-Z nil
  90.   "Remember whether C-Z was seen at the end of this buffer.")
  91.  
  92. (make-variable-buffer-local 'dos-seen-ctl-Z)
  93. (setq-default dos-seen-ctl-Z nil)
  94.  
  95. (defun dos-convert-buffer-to-unix ()
  96.   "Converts the current buffer from DOS format to UNIX format."
  97.   (let ((mod-p (buffer-modified-p))
  98.     (buffer-read-only nil))
  99.     (save-excursion
  100.  
  101.       ;; Remove all C-m's that occur at the end of each line.
  102.       (goto-char (point-min))
  103.       (while (re-search-forward "\r$" nil t)
  104.     (replace-match ""))
  105.  
  106.       ;; see if there is a C-z near the end of the buffer.
  107.       (goto-char (point-max))
  108.       (beginning-of-line)
  109.       (forward-line -1)            ;should be safe enough
  110.       (setq dos-seen-ctl-Z (search-forward "\C-z" nil t))
  111.       (if dos-seen-ctl-Z
  112.       (replace-match "")))
  113.  
  114.     (set-buffer-modified-p mod-p)
  115.     (setq dos-mode t)))
  116.  
  117. (defun dos-convert-buffer-to-dos ()
  118.   "Converts the current buffer from UNIX format to DOS format."
  119.   (save-excursion
  120.     (goto-char (point-min))
  121.     (while (re-search-forward "\n" nil t)
  122.       (replace-match "\r\n"))
  123.     (if dos-seen-ctl-Z
  124.     (progn
  125.       (goto-char (point-max))
  126.       (insert "\C-z")))))
  127.  
  128. (defun dos-check-buffer ()
  129.   "Used as a find-file hook, if the buffer looks like a DOS format buffer
  130. then it will be converted to a UNIX format buffer."
  131.   (if (or dos-mode
  132.       (save-excursion
  133.         (goto-char (point-min))
  134.         (re-search-forward "\r$"
  135.                    (min (point-max) dos-mode-distance)
  136.                    t)))
  137.       (dos-convert-buffer-to-unix)))
  138.  
  139. (defun dos-write-buffer ()
  140.   "If the current buffer was originally a DOS format buffer, write it as such."
  141.   (if dos-mode
  142.       (let* ((buffer-read-only nil)
  143.          (window (get-buffer-window (current-buffer)))
  144.          (p (point))
  145.          (m (mark))
  146.          (ws (and window (window-start window))))
  147.     (dos-convert-buffer-to-dos)
  148.     (unwind-protect
  149.         (write-region (point-min)
  150.               (point-max)
  151.               buffer-file-name
  152.               nil
  153.               t)
  154.       (dos-convert-buffer-to-unix)    ;convert back again!
  155.       (and window
  156.            (set-window-start window
  157.                  (save-excursion (goto-char ws)
  158.                          (beginning-of-line)
  159.                          (point))))
  160.       (goto-char p)
  161.       (set-mark m))
  162.     t)))
  163.  
  164. (or (memq 'dos-write-buffer write-file-hooks)
  165.     (setq write-file-hooks
  166.       (append write-file-hooks
  167.           (list 'dos-write-buffer)))) ;stick it on the end
  168.  
  169. (or (memq 'dos-check-buffer find-file-hooks)
  170.     (setq find-file-hooks
  171.       (append find-file-hooks
  172.           (list 'dos-check-buffer))))
  173.