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