;;; uncompress.el --- auto-decompression hook for visiting .Z files

;; Copyright (C) 1992, 1994, 2000 Free Software Foundation, Inc.

;; Maintainer: FSF
;; Keywords: unix extensions

;; This file is part of XEmacs.

;; XEmacs is free software; you can redistribute it and/or modify it
;; under the terms of the GNU General Public License as published by
;; the Free Software Foundation; either version 2, or (at your option)
;; any later version.

;; XEmacs is distributed in the hope that it will be useful, but
;; WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
;; General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with XEmacs; see the file COPYING.  If not, write to the Free
;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
;; 02111-1307, USA.

;;; Synched up with: 19.34.

;;; Commentary:

;; This package can be used to arrange for automatic uncompress of
;; files packed with the UNIX compress(1) utility when they are visited.
;; All that's necessary is to load it.  This can conveniently be done from
;; your .emacs file.

;;; Requirements:

;(eval-when-compile (require 'cl-macs))

;;; Code:

(defvar uncompress-program-alist '(("\\.Z$" "gunzip" ".Z")
                                   ("\\.gz$" "gunzip" ".gz")
                                   ("\\.bz2$" "bunzip2" ".bz2"))
 "List of programs to use to uncompress files matching specific regexps.
This alist maps from a regexp matching a compressed file to a program
to decompress it, and to a string to append to filenames when a file
is not found to determine whether a compressed version of that type
exists instead.")

;; When we are about to make a backup file,
;; uncompress the file we visited
;; so that making the backup can work properly.
;; This is used as a write-file-hook.
;; (Well, no, it isn't, there's a closure that is the hook.)

(defun uncompress-backup-file (uncompress-program)
  (and buffer-file-name make-backup-files (not buffer-backed-up)
       (not (file-exists-p buffer-file-name))
       (call-process uncompress-program nil nil nil buffer-file-name))
  nil)

(mapc (function (lambda (fileprog)
                        (or (assoc (car fileprog) auto-mode-alist)
                            (setq auto-mode-alist
                                  (cons `(,(car fileprog) . uncompress-while-visiting)
                                   auto-mode-alist))))) uncompress-program-alist)

(defun uncompress-while-visiting ()
  "Temporary \"major mode\" used for .Z, .gz, and .bz2 files, to uncompress them.
It then selects a major mode from the uncompressed file name and contents."
  (lexical-let ((uncompress-program))   ; The uncompression program used for this file.
    (mapc (function (lambda (fileprog)
                      (if (and (not (null buffer-file-name))
                               (string-match (car fileprog) buffer-file-name))
                          (progn (set-visited-file-name (substring buffer-file-name 0
                                                                   (match-beginning 0)))
                                 (setq uncompress-program (car (cdr fileprog)))))))
          uncompress-program-alist)
    (message "Uncompressing...")
    (let ((buffer-read-only nil))
      (shell-command-on-region (point-min) (point-max) uncompress-program t))
    (message "Uncompressing...done")
    (set-buffer-modified-p nil)
    (make-local-hook 'write-file-hooks)
    (add-hook 'write-file-hooks
              (function (lambda ()
                          (uncompress-backup-file uncompress-program))) nil t))
  (normal-mode))

(or (memq 'find-compressed-version find-file-not-found-hooks)
    (setq find-file-not-found-hooks
	  (cons 'find-compressed-version find-file-not-found-hooks)))

(defun find-compressed-version ()
  "Hook to read and uncompress the compressed version of a file."
  ;; Just pretend we had visited the compressed file,
  ;; and uncompress-while-visiting will do the rest.
  (let (found-p)
    (mapc (function (lambda (fileprog)
                            (let (name)
                                 (if (file-exists-p (setq name (concat buffer-file-name
                                                                       (car (cdr (cdr fileprog))))))
                                     (progn (setq buffer-file-name name
                                                  found-p t))))))
          uncompress-program-alist)
    (if found-p
	(progn
	  (insert-file-contents buffer-file-name t)
	  (goto-char (point-min))
	  (setq error nil)
	  t))))

(provide 'uncompress)

;;; uncompress.el ends here