[comp.emacs] z-mode.el

aks%nowhere@HUB.UCSB.EDU (Alan Stebbens) (07/20/88)

Speaking of auto-uncompressing ".Z" files when visiting them, here
is my little Emacs hack to do this.  It is invoked automatically
by including the function in the auto-mode-alist in my .emacs
startup file.

My original idea was to auto-compress it upon saving, but there
are good reasons not to do this.  Generally, compressing a file is
a way of archiving it for later (usually much later) retrieval.
However, if you visit a file and make changes to it, more often
than not, you are entering a modification cycle, and will make
subsequent changes to the same file.  So, compressing a file
automatically is usually counter-productive and wastes time, since
it will likely be accessed again until the modification cycle is
complete.  Once the changes are done, though, it is easy enough to
compress the file, and you need not visit the file to do this:
"M-! compress FILE". Thus, auto-compressing the visited file was
not considered useful.

Alan Stebbens <aks@hub.ucsb.edu>

========================= z-mode.el =========================
(defun z-mode () "\
Temporary major mode triggered by the \".Z\" suffix on a file,
used to automatically uncompress the file when visiting.  After
running the buffer contents through \"uncompress\", the buffer
name is changed by truncating the \".Z\" (as well as the visited
file name).  Also, the buffer is marked as read-only.  Finally,
normal-mode is invoked to process the buffer for its normal mode."
  (if (and (not (null buffer-file-name))
	   (string-match "^\\(.*\\)\\.Z$" buffer-file-name))
      (let ((new (substring buffer-file-name
			    (match-beginning 1) (match-end 1))))
	(setq buffer-read-only nil)
	(message "Uncompressing text...")
	(shell-command-on-region (point-min) (point-max)
				 "uncompress" t)
	(message "Uncompressing text...done")
	(goto-char (point-min))
	(set-visited-file-name new)
	(set-buffer-modified-p nil)
	(setq buffer-read-only t)))
  (normal-mode))
============================================================

To use it, place the following (or equivalent) in your ~/.emacs
startup.

========================== .emacs ==========================

;; Define z-mode to auto-load when visiting a ".Z" file

(autoload 'z-mode "z-mode" "\
Mode triggered by \".Z\" suffixed files, which then get automatically
uncompressed with appropriate buffer and visited file name changes.  The
buffer containing the uncompressed source is set to read-only."
	  t)

;; Establish '.Z' as a valid mode

(setq auto-mode-alist (nconc '(("\\.Z$" . z-mode)) auto-mode-alist))

akhanna@bbn.com (Atul Khanna) (07/21/88)

In article <8807200126.AA15162@EDDIE.MIT.EDU> aks%nowhere@HUB.UCSB.EDU (Alan Stebbens) writes:
>Speaking of auto-uncompressing ".Z" files when visiting them, here
>is my little Emacs hack to do this.  It is invoked automatically
>by including the function in the auto-mode-alist in my .emacs
>startup file.
> ...
>(defun z-mode () "\
>Temporary major mode triggered by the \".Z\" suffix on a file,
>used to automatically uncompress the file when visiting.  After
>running the buffer contents through \"uncompress\", the buffer
>name is changed by truncating the \".Z\" (as well as the visited
>file name).  Also, the buffer is marked as read-only.  Finally,
>normal-mode is invoked to process the buffer for its normal mode."
>  (if (and (not (null buffer-file-name))
>	   (string-match "^\\(.*\\)\\.Z$" buffer-file-name))
>      (let ((new (substring buffer-file-name
>			    (match-beginning 1) (match-end 1))))
>	(setq buffer-read-only nil)
>	(message "Uncompressing text...")
>	(shell-command-on-region (point-min) (point-max)
>				 "uncompress" t)
>	(message "Uncompressing text...done")
>	(goto-char (point-min))
>	(set-visited-file-name new)
>	(set-buffer-modified-p nil)
>	(setq buffer-read-only t)))
>  (normal-mode))

I do something similar.  I have the following binding: (global-set-key
"" 'find-and-uncompress-file).

The function, which follows, does not uncompress the file.  Instead,
it uncompresses the buffer.  If you edit and save the resulting
buffer, you'll end up with both an up-to-date uncompressed version of
the file and an out-of-date compressed version, so you have to be
careful.  I've chosen to do it this way since in most cases I just
read compressed files without editing them.

(defun find-and-uncompress-file (filename)
  "Find and uncompress its buffer, if necessary, file FILENAME"
  (interactive "FFind [uncompress] file: ")
  (if (string-match "\\.Z$" filename)
      (let* ((ascii-filename (substring filename 0 (string-match "\\.Z$" filename)))
	     (ascii-buffer (get-file-buffer ascii-filename)))
	(if ascii-buffer
	    (switch-to-buffer ascii-buffer)
	  (progn
	    (find-file filename)
	    (mark-whole-buffer)
	    (if buffer-read-only (toggle-read-only))
	    (shell-command-on-region (region-beginning) (region-end) "uncompress" t)
	    (beginning-of-buffer)
	    (set-visited-file-name ascii-filename)
	    (setq buffer-read-only t)
	    (not-modified))))
    (find-file filename)))




--------------------------------------------------------------------------
Atul C Khanna (akhanna@bbn.com)
BBN Communications Corporation
150 CambridgePark Drive
Cambridge, MA 02140

UUCP: ..!{decvax, wjh12, harvard}!bbnccv!akhanna