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

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