[comp.emacs] Automatically uncompress files when finding them

spolsky-joel@CS.Yale.EDU (Joel Spolsky) (11/08/88)

Version: GNU-Emacs 18.52. Probably works on earlier versions.

This is a little function that replaces find-file. If the file to be
found is compressed (ends with .Z) this function will uncompress it
before finding it. I don't know if this has been done before, anyway,
here it is:

(defun find-file-with-uncompress (filename) 
  "Find a file. If it is compressed, uncompress it first. Assumes that
   any file ending in .Z is compressed. The argument should include a
   .Z to uncompress a file."
  (interactive "FFind file: ")
  (progn
    (if (equal (substring filename -2) ".Z")
	(progn
	  (let* ((buffer-read-only nil)  ; code stolen from dired.el
		 (from-file filename)
		 (to-file (substring from-file 0 -2)))
	    (message "Uncompressing %s..." from-file)
	    (call-process "uncompress" nil nil nil from-file)
	    (message "Uncompressing %s... done" from-file)
	    (switch-to-buffer (find-file-noselect to-file))))
      (switch-to-buffer (find-file-noselect filename)))))

You might bind this to ^X^F (in your .emacs for example) using:

	(global-set-key "\C-x\C-f" 'find-file-with-uncompress)


I really don't know too much about emacs-lisp, so if any of you real
hackers have any comments, I would be happy to hear them.

+----------------+---------------------------------------------------+
|  Joel Spolsky  | bitnet: spolsky@yalecs     uucp: ...!yale!spolsky |
|                | arpa:   spolsky@yale.edu   voicenet: 203-436-1483 |
+----------------+---------------------------------------------------+
                                               #include <disclaimer.h>

mad@cabbage.keio.JUNET (MAEDA Atusi) (11/09/88)

   From: spolsky-joel@CS.Yale.EDU (Joel Spolsky)
   Newsgroups: comp.emacs
   Date: 8 Nov 88 07:09:09 GMT
   Organization: Yale University Computer Science Dept, New Haven CT  06520-2158

   This is a little function that replaces find-file.

Thanks.  Yes, I *really* replaced find-file with it.  I added the
following in my .emacs file.  Changes from oridinal code are:

1) Expands file name.  So it now works on files in other directory.
2) Replaces the definition of find-file-noselect.  Many functions such 
   as dired-find-file or rmail-input are automatically improved.  (But 
   this may be dangerous...)

Similar trick I am using is delete-file which does not delete file but 
moves it to `trash' directory and flush later.

(fset 'old-find-file-noselect (symbol-function 'find-file-noselect))

(defun find-file-noselect (filename)
  "Find a file. If it is compressed, uncompress it first. Assumes that
   any file ending in .Z is compressed. The argument should include a
   .Z to uncompress a file.
   See old-find-file-noselect for details."
  (interactive "FFind file: ")
  (if (equal (substring filename -2) ".Z")
      (progn
	(let* ((buffer-read-only nil)	; code stolen from dired.el
	       (from-file (expand-file-name filename))
	       (to-file (substring from-file 0 -2)))
	  (message "Uncompressing %s..." from-file)
	  (call-process "uncompress" nil nil nil from-file)
	  (message "Uncompressing %s... done" from-file)
	  (old-find-file-noselect to-file)))
      (old-find-file-noselect filename)))

;;;  MAEDA Atusi   (In Japan we write our family names first.)
;;;  $@A0EDFX;J(J      (My name in japanese characters.)
;;;  				     mad@nlab.keio.junet :JUNET
;;;			mad%nlab.keio.junet@relay.cs.net :CSNET

mad@cabbage.keio.JUNET (MAEDA Atusi) (11/09/88)

   From: spolsky-joel@CS.Yale.EDU (Joel Spolsky)
   Newsgroups: comp.emacs
   Date: 8 Nov 88 07:09:09 GMT
   Organization: Yale University Computer Science Dept, New Haven CT  06520-2158

   This is a little function that replaces find-file.

Yes, I *really* replaced find-file with it.  I added the following in
my .emacs file.  Changes from oridinal code are:

1) Expands file name.  So it now works on files in other directory.
2) Replaces the definition of find-file-noselect.  Many functions such 
   as dired-find-file or rmail-input are automatically improved.  (But 
   this may be dangerous...)

Similar trick I am using is delete-file which does not delete file but 
moves it to `trash' directory and flush later.

(fset 'old-find-file-noselect (symbol-function 'find-file-noselect))

(defun find-file-noselect (filename)
  "Find a file. If it is compressed, uncompress it first. Assumes that
   any file ending in .Z is compressed. The argument should include a
   .Z to uncompress a file.
   See old-find-file-noselect for details."
  (interactive "FFind file: ")
  (if (equal (substring filename -2) ".Z")
      (progn
	(let* ((buffer-read-only nil)	; code stolen from dired.el
	       (from-file (expand-file-name filename))
	       (to-file (substring from-file 0 -2)))
	  (message "Uncompressing %s..." from-file)
	  (call-process "uncompress" nil nil nil from-file)
	  (message "Uncompressing %s... done" from-file)
	  (old-find-file-noselect to-file)))
      (old-find-file-noselect filename)))

;;;  MAEDA Atusi   (In Japan we write our family names first.)
;;;  $@A0EDFX;J(J      (My name in japanese characters.)
;;;  				     mad@nlab.keio.junet :JUNET
;;;			mad%nlab.keio.junet@relay.cs.net :CSNET

jbw@bucsb.UUCP (Joe Wells) (11/11/88)

I suggest that everyone who is interested in having files automatically
uncompressed by GNU Emacs should look in lisp/uncompress.el in the GNU
Emacs distribution.  I think that this file has been there since 18.50
and possibly before.  It uses hooks that are already in place in the
code in lisp/files.el, instead of modifying other function definitions.

Have fun!

--
Joe Wells
INTERNET: jbw%bucsf.bu.edu@bu-it.bu.edu
UUCP: ...!harvard!bu-cs!bucsf!jbw

pf@mips.csc.ti.com (Paul Fuqua) (11/11/88)

In article <2166@bucsb.UUCP> jbw@bucsf.bu.edu (Joe Wells) writes:
    I suggest that everyone who is interested in having files automatically
    uncompressed by GNU Emacs should look in lisp/uncompress.el in the GNU
    Emacs distribution.

I use it, and I like it, but it has trouble with write-protected files:  the
buffer comes up read-only, and the uncompress barfs, leaving it empty.  The
obvious first choice, binding buffer-read-only to nil around the call to
shell-command-on-region, doesn't seem to fix the problem.

I'd love to hear from anyone with a solution;  uncompress.el is perfect for
me, since it doesn't actually modify any files.

                              pf

Paul Fuqua
Texas Instruments Computer Science Center, Dallas, Texas
CSNet:  pf@csc.ti.com (ARPA too, sometimes)
UUCP:   {smu, texsun, cs.utexas.edu, im4u, rice}!ti-csl!pf

kjones@talos.UUCP (Kyle Jones) (11/16/88)

In article <63190@ti-csl.CSNET> pf@csc.ti.com (Paul Fuqua) writes:
[re: uncompress.el]
>I use it, and I like it, but it has trouble with write-protected files:  the
>buffer comes up read-only, and the uncompress barfs, leaving it empty.  The
>obvious first choice, binding buffer-read-only to nil around the call to
>shell-command-on-region, doesn't seem to fix the problem.

(let (buffer-read-only)
  (shell-command-on-region ...))

worked here.

Perhaps you meant read-protected when you wrote write-protected above.
I say this because when I tried to duplicate the error you mentioned,
the buffer did not come up empty, it contained the compressed file.
Oddly, the buffer was marked as modified, but there was no undo
information available.