home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1993 #1 / NN_1993_1.iso / spool / gnu / emacs / sources / 923 < prev    next >
Encoding:
Text File  |  1993-01-07  |  23.4 KB  |  605 lines

  1. Path: sparky!uunet!usc!zaphod.mps.ohio-state.edu!darwin.sura.net!spool.mu.edu!agate!agate.berkeley.edu!dodd
  2. From: dodd@mycenae.cchem.berkeley.edu (Lawrence R. Dodd)
  3. Newsgroups: gnu.emacs.sources
  4. Subject: merged version of crypt.el
  5. Date: 7 Jan 93 14:56:41
  6. Organization: Dept of Chemical Engineering, Polytechnic Univ, NY, USA
  7. Lines: 590
  8. Distribution: gnu
  9. Message-ID: <DODD.93Jan7145641@mycenae.cchem.berkeley.edu>
  10. Reply-To: dodd@roebling.poly.edu
  11. NNTP-Posting-Host: mycenae.cchem.berkeley.edu
  12. Summary: version of crypt.el that works with GNU zip and tar-mode.el
  13. Keywords: GNU zip, crypt.el, tar-mode.el
  14.  
  15.  
  16.  Here is a version of crypt.el that merges together the correction for the
  17.  compress-magic-regexp discussed by Joerg-Cyril Hoehle
  18.  (hoehle@inf-wiss.ivp.uni-konstanz.de), the ability to work with gzip (GNU
  19.  zip) files, and the ability to work with Jamie Zawinski's tar-mode.el
  20.  (thanks to Jamie for these modifications -- and for tar-mode.el).
  21.  
  22. share and enjoy,
  23.   Larry
  24.  
  25. ..........................................................................
  26. ;;; Compaction, compression, gzip'sion, and encryption for GNU Emacs
  27. ;;; Copyright (C) 1988, 1989, 1990 Kyle E. Jones
  28. ;;; jwz: works with tar-mode.el
  29. ;;; jwz: applied patch from piet, merged with Lawrence Dodd's gzip version
  30. ;;; 
  31. ;;;
  32. ;;; This program is free software; you can redistribute it and/or modify
  33. ;;; it under the terms of the GNU General Public License as published by
  34. ;;; the Free Software Foundation; either version 1, or (at your option)
  35. ;;; any later version.
  36. ;;;
  37. ;;; This program is distributed in the hope that it will be useful,
  38. ;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
  39. ;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  40. ;;; GNU General Public License for more details.
  41. ;;;
  42. ;;; A copy of the GNU General Public License can be obtained from this
  43. ;;; program's author (send electronic mail to kyle@cs.odu.edu) or from
  44. ;;; the Free Software Foundation, Inc., 675 Mass Ave, Cambridge, MA
  45. ;;; 02139, USA.
  46. ;;;
  47. ;;; $Id: crypt.el,v 1.3 1993/01/07 22:49:43 dodd Exp $
  48. ;;; Send bug reports to dodd@roebling.poly.edu
  49. ;;; Larry Dodd <dodd@roebling.poly.edu>
  50. ;;; Dept Chemical Engineering 
  51. ;;; Polytechnic University, Brooklyn, NY
  52.  
  53. ;; To use this package, put it in a file called "crypt.el" in a Lisp
  54. ;; directory that Emacs knows about, byte-compile it, and put the line:
  55. ;;    (require 'crypt)
  56. ;; in your .emacs file or in the file default.el in the "lisp" directory
  57. ;; of the Emacs distribution.  Don't bother trying to autoload this file;
  58. ;; this package uses a find-file hook and thus should be loaded the
  59. ;; first time you visit any sort of file.
  60. ;;
  61. ;; The basic purpose of this package of Lisp functions is to automatically
  62. ;; recognize encrypted, compacted or compressed files when they are first
  63. ;; visited and decode the file's BUFFER before it is presented to the user.
  64. ;; The file itself is unchanged.  When the buffer is subsequently saved to
  65. ;; disk, a hook function re-encodes the buffer before the actual disk write
  66. ;; takes place.
  67. ;;
  68. ;; This package recognizes compacted and compressed files by a magic number at
  69. ;; the beginning of these files, but a heuristic is used to detect encrypted
  70. ;; files.  If you are asked for an encryption key for a file that is in fact
  71. ;; not encrypted, just hit RET and the file will be accepted as is, and the
  72. ;; crypt minor mode will not be entered.
  73.  
  74. (provide 'crypt)
  75.  
  76. (defvar crypt-encryption-program "des -f -e -k")
  77. (defvar crypt-decryption-program "des -f -d -k")
  78.  
  79. (defvar encryption-program "des -e -k")
  80. (defvar decryption-program "des -d -k")
  81.  
  82. (defvar auto-decode-buffer t
  83.   "*Non-nil value means that the buffers associated with encoded files will
  84. be decoded automatically, without requesting confirmation from the user.
  85. Nil means to ask before doing the decoding.")
  86.  
  87. (defvar buffer-save-encrypted nil
  88.   "*Non-nil means that when this buffer is saved it will be written out
  89. encrypted, as with the UNIX crypt(1) command.  Automatically local to all
  90. buffers.")
  91. (make-variable-buffer-local 'buffer-save-encrypted)
  92.  
  93. (defvar buffer-save-compacted nil
  94.   "*Non-nil means that when this buffer is saved it will be written out
  95. compacted, as with the UNIX compact(1) command.  Automatically local to all
  96. buffers.")
  97. (make-variable-buffer-local 'buffer-save-compacted)
  98.  
  99. (defvar buffer-save-compressed nil
  100.   "*Non-nil means that when this buffer is saved it will be written out
  101. compressed, as with the UNIX compress(1) command.  Automatically local to all
  102. buffers.")
  103. (make-variable-buffer-local 'buffer-save-compressed)
  104.  
  105. ;; gzip (GNU zip)
  106. (defvar buffer-save-gzipped nil
  107.   "*Non-nil means that when this buffer is saved it will be written out
  108. gzip'ed, as with the gzip (GNU zip) command.  Automatically local to all
  109. buffers.")
  110. (make-variable-buffer-local 'buffer-save-gzipped)
  111.  
  112. (defvar buffer-encryption-key nil
  113.   "*Key to use when encrypting the current buffer, prior to saving it.
  114. Automatically local to all buffers.")
  115. (make-variable-buffer-local 'buffer-encryption-key)
  116.  
  117. (defconst compact-magic-regexp "\377\037"
  118.   "Regexp that matches the magic number at the beginning of files created
  119. by the compact(1) command.")
  120.  
  121. (defconst compress-magic-regexp "\037\235"
  122.   "Regexp that matches the magic number at the beginning of files created
  123. by the compress(1) command.")
  124.  
  125. ;; gzip (GNU zip)
  126. (defconst gzip-magic-regexp "\037\213"
  127.   "Regexp that matches the magic number at the beginning of files created
  128. by the gzip command.")
  129.  
  130. ;; Encrypted files have no magic number, so we have to hack a way of
  131. ;; determining which new buffers start in crypt mode.  The current setup is
  132. ;; that we use only buffers that have a non-ASCII character very close to
  133. ;; beginning of buffer and that do NOT match crypt-magic-regexp-inverse.
  134. ;; Currently crypt-magic-regexp-inverse will match Sun OS, 4.x BSD, and
  135. ;; Ultrix executable magic numbers, so binaries can still be edited (heh)
  136. ;; without headaches.
  137.  
  138. (defconst crypt-magic-regexp-inverse
  139.   "\\(..\\)?\\([\007\010\013]\001\\|\001[\007\010\013]\\)"
  140.   "Regexp that must NOT match the beginning of an encrypted buffer.")
  141.  
  142. (defmacro save-point (&rest body)
  143.   "Save value of point, evalutes FORMS and restore value of point.
  144. If the saved value of point is no longer valid go to (point-max).
  145. This macro exists because, save-excursion loses track of point during
  146. some types of deletions."
  147.   (let ((var (make-symbol "saved-point")))
  148.     (list 'let (list (list var '(point)))
  149.       (list 'unwind-protect
  150.         (cons 'progn body)
  151.         (list 'goto-char var)))))
  152.  
  153. (defun find-crypt-file-hook ()
  154.   (let ((buffer-file-name buffer-file-name)
  155.     (old-buffer-file-name buffer-file-name)
  156.     (old-buffer-modified-p (buffer-modified-p))
  157.     encrypted compressed gzipped compacted case-fold-search buffer-read-only)
  158.     ;; We can reasonably assume that either compaction or compression will
  159.     ;; be used, or neither, but not both.
  160.     (save-point
  161.      (save-restriction
  162.        (widen)
  163.        (goto-char (point-min))
  164.        (cond ((and (looking-at compact-magic-regexp)
  165.            (or auto-decode-buffer
  166.                (y-or-n-p (format "Uncompact buffer %s? "
  167.                      (buffer-name)))))
  168.           (message "Uncompacting %s..." (buffer-name))
  169.           (compact-buffer (current-buffer) t)
  170.           ;; We can't actually go into compact mode yet because the major
  171.           ;; mode may change later on and blow away all local variables
  172.           ;; (and thus the minor modes).  So we make a note to go into
  173.           ;; compact mode later.
  174.           (setq compacted t)
  175.           ;; here we strip the compacted file's .C extension so that later
  176.           ;; we can set the buffer's major mode based on this modified
  177.           ;; name instead of the name with the .C extension.
  178.           (if (string-match "\\(\\.C\\)$" buffer-file-name)
  179.           (setq buffer-file-name
  180.             (substring buffer-file-name 0 (match-beginning 1))))
  181.           (if (not (input-pending-p))
  182.           (message "Uncompacting %s... done" (buffer-name))))
  183.          ;;
  184.          ;; compress
  185.          ;;
  186.          ((and (looking-at compress-magic-regexp)
  187.            (or auto-decode-buffer
  188.                (y-or-n-p (format "Uncompress buffer %s? "
  189.                      (buffer-name)))))
  190.           (message "Uncompressing %s..." (buffer-name))
  191.           (compress-buffer (current-buffer) t)
  192.           (setq compressed t)
  193.           (if (string-match "\\(\\.Z\\)$" buffer-file-name)
  194.           (setq buffer-file-name
  195.             (substring buffer-file-name 0 (match-beginning 1))))
  196.           (if (not (input-pending-p))
  197.           (message "Uncompressing %s... done" (buffer-name))))
  198.          ;; 
  199.          ;; gzip (GNU zip)
  200.          ;; 
  201.          ((and (looking-at gzip-magic-regexp)
  202.            (or auto-decode-buffer
  203.                (y-or-n-p (format "gunzip buffer %s? "
  204.                      (buffer-name)))))
  205.           (message "gunzip'ing %s..." (buffer-name))
  206.           (gzip-buffer (current-buffer) t)
  207.           (setq gzipped t)
  208.           (if (string-match "\\(\\.z\\)$" buffer-file-name)
  209.           (setq buffer-file-name
  210.             (substring buffer-file-name 0 (match-beginning 1))))
  211.           (if (not (input-pending-p))
  212.           (message "gunzip'ing %s... done" (buffer-name)))))
  213.        ;; Now peek at the file and see if it still looks like a binary file.
  214.        ;; If so, try the crypt-magic-regexp-inverse against it and if it FAILS
  215.        ;; we assume that this is an encrypted buffer.
  216.        (cond ((and (not (eobp))
  217.            (re-search-forward "[\200-\377]" (min (point-max) 15) t)
  218.            (goto-char (point-min))
  219.            (not (looking-at crypt-magic-regexp-inverse)))
  220.           (if (not buffer-encryption-key)
  221.           (call-interactively 'set-encryption-key))
  222.           ;; if user did not enter a key, turn off crypt mode.
  223.           ;; good for binaries that crypt-magic-regexp-inverse
  224.           ;; doesn't recognize.
  225.           ;; -- thanx to Paul Dworkin (paul@media-lab.media.mit.edu)
  226.           (if (equal buffer-encryption-key "")
  227.           (message "No key given, buffer %s assumed normal."
  228.                (buffer-name))
  229.         (message "Decrypting %s..." (buffer-name))
  230.         (crypt-buffer buffer-encryption-key nil)
  231.         ;; Tuck the key away for safe keeping since setting the major
  232.         ;; mode may well blow it away.
  233.         (setq encrypted buffer-encryption-key)
  234.         (if (not (input-pending-p))
  235.             (message "Decrypting %s... done" (buffer-name))))))
  236.        ))
  237.     ;; OK, if any changes have been made to the buffer we need to rerun the
  238.     ;; code the does automatic selection of major mode.
  239.     (cond ((or compressed gzipped compacted encrypted)
  240.        (set-auto-mode)
  241.        (hack-local-variables)
  242.        ;; Now set our minor modes.
  243.        (if compressed (compress-mode 1))
  244.        (if gzipped (gzip-mode 1))
  245.        (if compacted (compact-mode 1))
  246.        (if encrypted
  247.            (progn (crypt-mode 1)
  248.               (setq buffer-encryption-key encrypted)))
  249.        ;; Restore buffer file name now, so that lock file entry is
  250.        ;; removed properly.
  251.        (setq buffer-file-name old-buffer-file-name)
  252.        ;; Restore buffer modified flag to its previous value.
  253.        ;; This will also remove the lock file entry for the buffer
  254.        ;; if the previous value was nil; this is why buffer-file-name
  255.        ;; had to be manually restored above.
  256.        (set-buffer-modified-p old-buffer-modified-p)))))
  257.  
  258. ;; This function should be called ONLY as a write-file hook.
  259. ;; Odd things will happen if it is called elsewhere.
  260. (defun write-crypt-file-hook ()
  261.   (cond
  262.    ((or buffer-save-encrypted buffer-save-compacted
  263.     buffer-save-compressed buffer-save-gzipped)
  264.     (save-excursion
  265.       (save-restriction
  266.     (let ((copy-buffer (get-buffer-create " *crypt copy buffer*"))
  267.           (selective-display selective-display)
  268.           (buffer-read-only))
  269.       (copy-to-buffer copy-buffer 1 (1+ (buffer-size)))
  270.       (narrow-to-region (point) (point))
  271.       (unwind-protect
  272.           (progn
  273.         (insert-buffer-substring copy-buffer)
  274.         (kill-buffer copy-buffer)
  275.         ;; selective-display non-nil means we must convert carriage
  276.         ;; returns to newlines now, and set selective-display
  277.         ;; temporarily to nil.
  278.         (cond (selective-display
  279.                (goto-char (point-min))
  280.                (subst-char-in-region (point-min) (point-max) ?\r ?\n)
  281.                (setq selective-display nil)))
  282.         (cond
  283.          (buffer-save-encrypted
  284.           (if (null buffer-encryption-key)
  285.               (error "No encryption key set for buffer %s"
  286.                  (buffer-name)))
  287.           (if (not (stringp buffer-encryption-key))
  288.               (error "Encryption key is not a string"))
  289.           (message "Encrypting %s..." (buffer-name))
  290.           (crypt-buffer buffer-encryption-key t)))
  291.         (cond
  292.          ((and buffer-save-compacted buffer-save-compressed)
  293.           (error "Cannot compact and compress buffer %s"
  294.              (buffer-name)))
  295.          (buffer-save-compacted
  296.           (message "Compacting %s..." (buffer-name))
  297.           (compact-buffer))
  298.          (buffer-save-compressed
  299.           (message "Compressing %s..." (buffer-name))
  300.           (compress-buffer))
  301.          ;; gzip (GNU zip)
  302.          (buffer-save-gzipped
  303.           (message "gzip'ing %s..." (buffer-name))
  304.           (gzip-buffer)))
  305.         (write-region (point-min) (point-max) buffer-file-name nil t)
  306.         (delete-region (point-min) (point-max))
  307.         (set-buffer-modified-p nil)
  308.         ;; return t so that basic-save-buffer will
  309.         ;; know that the save has already been done.
  310.         t )
  311.         ;; unwind...
  312.         ;; If the crypted stuff has already been removed
  313.         ;; then this is a no-op.
  314.         (delete-region (point-min) (point-max)))))))))
  315.               
  316. (defun crypt-region (start end encrypt key)
  317.   "Encrypt/decrypt the text in the region.
  318. >From a program, this function takes four args: START, END, ENCRYPT and KEY.
  319. When called interactively START and END default to point and mark
  320. (START being the lesser of the two), KEY is prompted for."
  321.   (interactive
  322.    (progn
  323.      (barf-if-buffer-read-only)
  324.      (list
  325.       (region-beginning)
  326.       (region-end)
  327.       (y-or-n-p "Encrypt? ")
  328.       (read-string-no-echo "Crypt region using key: "))))
  329.   (save-point
  330.    (let ((opoint-max (point-max)))
  331.      (call-process-region start end shell-file-name t t nil "-c"
  332.               (concat (if encrypt
  333.                       encryption-program decryption-program)
  334.                   " " key)))))
  335.  
  336. (defun crypt-buffer (key encrypt &optional buffer)
  337.   "Using KEY, encrypt/decrypt BUFFER.
  338. BUFFER defaults to the current buffer."
  339.   (interactive
  340.    (progn
  341.      (barf-if-buffer-read-only)
  342.      (list (read-string-no-echo "Crypt buffer using key: "))))
  343.   (or buffer (setq buffer (current-buffer)))
  344.   (save-excursion
  345.     (set-buffer buffer)
  346.     (crypt-region (point-min) (point-max) encrypt key)))
  347.  
  348. (defun compact-region (start end &optional undo)
  349.   "Compact the text in the region.
  350. >From a program, this function takes three args: START, END and UNDO.
  351. When called interactively START and END default to point and mark
  352. (START being the lesser of the two).
  353. Prefix arg (or optional second arg non-nil) UNDO means uncompact."
  354.   (interactive "*r\nP")
  355.   (save-point
  356.    (call-process-region start end shell-file-name t t nil "-c"
  357.             (if undo "uncompact" "compact"))
  358.    (cond ((not undo)
  359.       (goto-char start)
  360.       (let (case-fold-search)
  361.         (if (not (looking-at compact-magic-regexp))
  362.         (error "%s failed!" (if undo
  363.                     "Uncompaction"
  364.                       "Compaction"))))))))
  365.  
  366. (defun compact-buffer (&optional buffer undo)
  367.   "Compact BUFFER.
  368. BUFFER defaults to the current buffer.
  369. Prefix arg (or second arg non-nil from a program) UNDO means uncompact."
  370.   (interactive (list (current-buffer) current-prefix-arg))
  371.   (or buffer (setq buffer (current-buffer)))
  372.   (save-excursion
  373.     (set-buffer buffer)
  374.     (compact-region (point-min) (point-max) undo)))
  375.  
  376. (defun compress-region (start end &optional undo)
  377.   "Compress the text in the region.
  378. >From a program, this function takes three args: START, END and UNDO.
  379. When called interactively START and END default to point and mark
  380. (START being the lesser of the two).
  381. Prefix arg (or optional second arg non-nil) UNDO means uncompress."
  382.   (interactive "*r\nP")
  383.   (save-point
  384.    (call-process-region start end shell-file-name t t nil "-c"
  385.             (if undo "compress -d" "compress"))
  386.    (cond ((not undo)
  387.       (goto-char start)
  388.       (let (case-fold-search)
  389.         (if (not (looking-at compress-magic-regexp))
  390.         (error "%s failed!" (if undo
  391.                     "Uncompression"
  392.                       "Compression"))))))))
  393.  
  394. (defun compress-buffer (&optional buffer undo)
  395.   "Compress BUFFER.
  396. BUFFER defaults to the current buffer.
  397. Prefix arg (or second arg non-nil from a program) UNDO means uncompress."
  398.   (interactive (list (current-buffer) current-prefix-arg))
  399.   (or buffer (setq buffer (current-buffer)))
  400.   (save-excursion
  401.     (set-buffer buffer)
  402.     (compress-region (point-min) (point-max) undo)))
  403.  
  404. ;; gzip (GNU zip)
  405. (defun gzip-region (start end &optional undo)
  406.   "gzip the text in the region.
  407. >From a program, this function takes three args: START, END and UNDO.
  408. When called interactively START and END default to point and mark
  409. (START being the lesser of the two).
  410. Prefix arg (or optional second arg non-nil) UNDO means gunzip."
  411.   (interactive "*r\nP")
  412.   (save-point
  413.    (call-process-region start end shell-file-name t t nil "-c"
  414.             (if undo "gzip -d" "gzip"))
  415.    (cond ((not undo)
  416.       (goto-char start)
  417.       (let (case-fold-search)
  418.         (if (not (looking-at gzip-magic-regexp))
  419.         (error "%s failed!" (if undo
  420.                     "gunzip'ing"
  421.                       "gzip'ing"))))))))
  422.  
  423. ;; gzip (GNU zip)
  424. (defun gzip-buffer (&optional buffer undo)
  425.   "gzip BUFFER.
  426. BUFFER defaults to the current buffer.
  427. Prefix arg (or second arg non-nil from a program) UNDO means gunzip."
  428.   (interactive (list (current-buffer) current-prefix-arg))
  429.   (or buffer (setq buffer (current-buffer)))
  430.   (save-excursion
  431.     (set-buffer buffer)
  432.     (gzip-region (point-min) (point-max) undo)))
  433.  
  434. (defun set-encryption-key (key &optional buffer)
  435.   "Set the encryption KEY for BUFFER.
  436. KEY should be a string.
  437. BUFFER should be a buffer or the name of one;
  438. it defaults to the current buffer.  If BUFFER is in crypt mode, then it is
  439. also marked as modified, since it needs to be saved with the new key."
  440.   (interactive
  441.    (progn
  442.      (barf-if-buffer-read-only)
  443.      (list
  444.       (read-string-no-echo
  445.        (format "Set encryption key for buffer %s: " (buffer-name))))))
  446.   (or buffer (setq buffer (current-buffer)))
  447.   (save-excursion
  448.     (set-buffer buffer)
  449.     (if (equal key buffer-encryption-key)
  450.     (message "Key is identical to original, no change.")
  451.       (setq buffer-encryption-key key)
  452.       ;; don't touch the modify flag unless we're in crypt-mode.
  453.       (if buffer-save-encrypted
  454.       (set-buffer-modified-p t)))))
  455.  
  456. (defun crypt-mode (&optional arg)
  457.   "Toggle crypt mode.
  458. With arg, turn crypt mode on iff arg is positive, otherwise turn it off.
  459. In crypt mode, buffers are automatically encrypted before being written.
  460. If crypt mode is toggled and a key has been set for the current buffer, then
  461. the current buffer is marked modified, since it needs to be rewritten
  462. with (or without) encryption.
  463.  
  464. Use \\[set-encryption-key] to set the encryption key for the current buffer.
  465.  
  466. Entering crypt mode causes auto-saving to be turned off in the current buffer,
  467. as there is no way (in Emacs Lisp) to force auto save files to be encrypted."
  468.   (interactive "P")
  469.   (let ((oldval buffer-save-encrypted))
  470.     (setq buffer-save-encrypted
  471.       (if arg (> arg 0) (not buffer-save-encrypted)))
  472.     (if buffer-save-encrypted
  473.     (auto-save-mode 0)
  474.       (auto-save-mode (if (and auto-save-default buffer-file-name) 1 0)))
  475.     (if buffer-encryption-key
  476.     (set-buffer-modified-p
  477.      (not (eq oldval buffer-save-encrypted))))))
  478.  
  479. (defun compact-mode (&optional arg)
  480.   "Toggle compact mode.
  481. With arg, turn compact mode on iff arg is positive, otherwise turn it off.
  482. In compact mode, buffers are automatically compacted before being written.
  483. If compact mode is toggled, the current buffer is marked modified, since
  484. it needs to be written with (or without) compaction.
  485.  
  486. Entering compact mode causes auto-saving to be turned off in the current
  487. buffer, as there is no way (in Emacs Lisp) to force auto save files to be
  488. compacted."
  489.   (interactive "P")
  490.   (let ((oldval buffer-save-compacted))
  491.     (setq buffer-save-compacted
  492.       (if arg (> arg 0) (not buffer-save-compacted)))
  493.     (if buffer-save-compacted
  494.     (auto-save-mode 0)
  495.       (auto-save-mode (if (and auto-save-default buffer-file-name) 1 0)))
  496.     (set-buffer-modified-p (not (eq oldval buffer-save-compacted)))))
  497.  
  498. (defun compress-mode (&optional arg)
  499.   "Toggle compress mode.
  500. With arg, turn compress mode on iff arg is positive, otherwise turn it off.
  501. In compress mode, buffers are automatically compressed before being written.
  502. If compress mode is toggled, the current buffer is marked modified, since
  503. it needs to be written with (or without) compression.
  504.  
  505. Entering compress mode causes auto-saving to be turned off in the current
  506. buffer, as there is no way (in Emacs Lisp) to force auto save files to be
  507. compressed."
  508.   (interactive "P")
  509.   (let ((oldval buffer-save-compressed))
  510.     (setq buffer-save-compressed
  511.       (if arg (> arg 0) (not buffer-save-compressed)))
  512.     (if buffer-save-compressed
  513.     (auto-save-mode 0)
  514.       (auto-save-mode (if (and auto-save-default buffer-file-name) 1 0)))
  515.     (set-buffer-modified-p (not (eq oldval buffer-save-compressed)))))
  516.  
  517. ;; gzip (GNU zip)
  518. (defun gzip-mode (&optional arg)
  519.   "Toggle gzip mode.
  520. With arg, turn gzip mode on iff arg is positive, otherwise turn it off.
  521. In gzip mode, buffers are automatically gzip'ed before being written.
  522. If gzip mode is toggled, the current buffer is marked modified, since
  523. it needs to be written with \(or without\) gzipion.
  524.  
  525. Entering gzip mode causes auto-saving to be turned off in the current
  526. buffer, as there is no way \(in Emacs Lisp\) to force auto save files to be
  527. gzip'ed."
  528.   (interactive "P")
  529.   (let ((oldval buffer-save-gzipped))
  530.     (setq buffer-save-gzipped
  531.       (if arg (> arg 0) (not buffer-save-gzipped)))
  532.     (if buffer-save-gzipped
  533.     (auto-save-mode 0)
  534.       (auto-save-mode (if (and auto-save-default buffer-file-name) 1 0)))
  535.     (set-buffer-modified-p (not (eq oldval buffer-save-gzipped)))))
  536.  
  537. (defun read-string-no-echo (prompt &optional confirm)
  538.   "Read a string from the minibuffer, prompting with PROMPT.
  539. Optional second argument CONFIRM non-nil means that the user will be asked
  540.   to type the string a second time for confirmation and if there is a
  541.   mismatch, the process is repeated.
  542.  
  543. Line editing keys are:
  544.   C-h, DEL    rubout
  545.   C-u, C-x      line kill
  546.   C-q, C-v      literal next"
  547.   (catch 'return-value
  548.     (save-excursion
  549.       (let ((input-buffer (get-buffer-create " *password*"))
  550.         (cursor-in-echo-area t)
  551.         (echo-keystrokes 0)
  552.         char string help-form done kill-ring)
  553.     (set-buffer input-buffer)
  554.     (unwind-protect
  555.         (while t
  556.           (erase-buffer)
  557.           (message prompt)
  558.           (while (not (memq (setq char (read-char)) '(?\C-m ?\C-j)))
  559.         (if (setq form
  560.               (cdr
  561.                (assq char
  562.                  '((?\C-h . (delete-char -1))
  563.                    (?\C-? . (delete-char -1))
  564.                    (?\C-u . (delete-region 1 (point)))
  565.                    (?\C-x . (delete-region 1 (point)))
  566.                    (?\C-q . (quoted-insert 1))
  567.                    (?\C-v . (quoted-insert 1))))))
  568.             (condition-case error-data
  569.             (eval form)
  570.               (error t))
  571.           (insert char))
  572.         (message prompt))
  573.           (cond ((and confirm string)
  574.              (cond ((not (string= string (buffer-string)))
  575.                 (message
  576.                  (concat prompt "[Mismatch... try again.]"))
  577.                 (ding)
  578.                 (sit-for 2)
  579.                 (setq string nil))
  580.                (t (throw 'return-value string))))
  581.             (confirm
  582.              (setq string (buffer-string))
  583.              (message (concat prompt "[Retype to confirm...]"))
  584.              (sit-for 2))
  585.             (t (throw 'return-value (buffer-string)))))
  586.       (set-buffer-modified-p nil)
  587.       (kill-buffer input-buffer))))))
  588.  
  589. ;; Install the hooks, then add the mode indicators to
  590. ;; the minor mode alist.
  591. (cond
  592.  ((not (memq 'write-crypt-file-hook write-file-hooks))
  593.   ;; make this hook last on purpose
  594.   (setq write-file-hooks (append write-file-hooks
  595.                  (list 'write-crypt-file-hook))
  596.     find-file-hooks (cons 'find-crypt-file-hook find-file-hooks)
  597.     find-file-not-found-hooks (cons 'find-crypt-file-hook
  598.                     find-file-not-found-hooks)
  599.     minor-mode-alist (nconc (list '(buffer-save-encrypted " Crypt")
  600.                       '(buffer-save-compacted " Compact")
  601.                       '(buffer-save-compressed " Compress")
  602.                       '(buffer-save-gzipped " GNU-zip"))
  603.                 minor-mode-alist))))
  604. .....
  605.