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

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