1 ;;; uncompress.el --- auto-decompression hook for visiting .Z files
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
6 ;; Keywords: unix extensions
8 ;; This file is part of XEmacs.
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)
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.
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
25 ;;; Synched up with: 20.7.
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
34 ;; M-x auto-compression-mode is a more modern replacement for this package.
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.
43 (defvar uncompress-program "gunzip"
44 "Program to use for uncompression.")
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))
52 (or (assoc "\\.Z$" auto-mode-alist)
54 (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
55 (or (assoc "\\.gz$" auto-mode-alist)
57 (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
58 (or (assoc "\\.tgz$" auto-mode-alist)
60 (cons '("\\.tgz$" . uncompress-while-visiting) auto-mode-alist)))
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)))
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)))
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.
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)
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.
109 (provide 'uncompress)
111 ;;; uncompress.el ends here