Initial Commit
[packages] / xemacs-packages / os-utils / uncompress.el
1 ;;; uncompress.el --- auto-decompression hook for visiting .Z files
2
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6 ;; Keywords: unix extensions
7
8 ;; This file is part of XEmacs.
9
10 ;; XEmacs is free software; you can redistribute it and/or modify it
11 ;; under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; XEmacs is distributed in the hope that it will be useful, but
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
18 ;; General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with XEmacs; see the file COPYING.  If not, write to the Free
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
23 ;; 02111-1307, USA.
24
25 ;;; Synched up with: 20.7.
26
27 ;;; Commentary:
28
29 ;; This package can be used to arrange for automatic uncompress of
30 ;; compressed files when they are visited.
31 ;; All that's necessary is to load it.  This can conveniently be done from
32 ;; your .emacs file.
33
34 ;; M-x auto-compression-mode is a more modern replacement for this package.
35
36 ;;; Code:
37
38 ;; When we are about to make a backup file,
39 ;; uncompress the file we visited
40 ;; so that making the backup can work properly.
41 ;; This is used as a write-file-hook.
42
43 (defvar uncompress-program "gunzip"
44   "Program to use for uncompression.")
45
46 (defun uncompress-backup-file ()
47   (and buffer-file-name make-backup-files (not buffer-backed-up)
48        (not (file-exists-p buffer-file-name))
49        (call-process uncompress-program nil nil nil buffer-file-name))
50   nil)
51
52 (or (assoc "\\.Z$" auto-mode-alist)
53     (setq auto-mode-alist
54           (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
55 (or (assoc "\\.gz$" auto-mode-alist)
56     (setq auto-mode-alist
57           (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
58 (or (assoc "\\.tgz$" auto-mode-alist)
59     (setq auto-mode-alist
60           (cons '("\\.tgz$" . uncompress-while-visiting) auto-mode-alist)))
61
62 (defun uncompress-while-visiting ()
63   "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
64 It then selects a major mode from the uncompressed file name and contents."
65   (if (and (not (null buffer-file-name))
66            (string-match "\\.Z$" buffer-file-name))
67       (set-visited-file-name
68        (substring buffer-file-name 0 (match-beginning 0)))
69     (if (and (not (null buffer-file-name))
70              (string-match "\\.gz$" buffer-file-name))
71         (set-visited-file-name
72          (substring buffer-file-name 0 (match-beginning 0)))
73       (if (and (not (null buffer-file-name))
74                (string-match "\\.tgz$" buffer-file-name))
75           (set-visited-file-name
76            (concat (substring buffer-file-name 0 (match-beginning 0)) ".tar")))))
77   (message "Uncompressing...")
78   (let ((buffer-read-only nil))
79     (shell-command-on-region (point-min) (point-max) uncompress-program t))
80   (message "Uncompressing...done")
81   (set-buffer-modified-p nil)
82   (make-local-variable 'write-file-hooks)
83   (or (memq 'uncompress-backup-file write-file-hooks)
84       (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
85   (normal-mode))
86
87 (or (memq 'find-compressed-version find-file-not-found-hooks)
88     (setq find-file-not-found-hooks
89           (cons 'find-compressed-version find-file-not-found-hooks)))
90
91 (defun find-compressed-version ()
92   "Hook to read and uncompress the compressed version of a file."
93   ;; Just pretend we had visited the compressed file,
94   ;; and uncompress-while-visiting will do the rest.
95   (let (name)
96     (if (file-exists-p (setq name (concat buffer-file-name ".Z")))
97         (setq buffer-file-name name)
98       (if (file-exists-p (setq name (concat buffer-file-name ".gz")))
99           (setq buffer-file-name name)))
100     (if (eq name buffer-file-name)
101         (progn
102           (insert-file-contents buffer-file-name t)
103           (goto-char (point-min))
104           ;; No need for this, because error won't be set to t
105           ;; if this function returns t.
106           ;; (setq error nil)
107           t))))
108
109 (provide 'uncompress)
110
111 ;;; uncompress.el ends here