Update FSF's address.
[bpt/emacs.git] / lisp / 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
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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
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.
23
24 ;;; Commentary:
25
26 ;; This package can be used to arrange for automatic uncompress of
27 ;; files packed with the UNIX compress(1) utility when they are visited.
28 ;; All that's necessary is to load it. This can conveniently be done from
29 ;; your .emacs file.
30
31 ;;; Code:
32
33 ;; When we are about to make a backup file,
34 ;; uncompress the file we visited
35 ;; so that making the backup can work properly.
36 ;; This is used as a write-file-hook.
37
38 (defvar uncompress-program "gunzip"
39 "Program to use for uncompression.")
40
41 (defun uncompress-backup-file ()
42 (and buffer-file-name make-backup-files (not buffer-backed-up)
43 (not (file-exists-p buffer-file-name))
44 (call-process uncompress-program nil nil nil buffer-file-name))
45 nil)
46
47 (or (assoc "\\.Z$" auto-mode-alist)
48 (setq auto-mode-alist
49 (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
50 (or (assoc "\\.gz$" auto-mode-alist)
51 (setq auto-mode-alist
52 (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
53
54 (defun uncompress-while-visiting ()
55 "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
56 It then selects a major mode from the uncompressed file name and contents."
57 (if (and (not (null buffer-file-name))
58 (string-match "\\.Z$" buffer-file-name))
59 (set-visited-file-name
60 (substring buffer-file-name 0 (match-beginning 0)))
61 (if (and (not (null buffer-file-name))
62 (string-match "\\.gz$" buffer-file-name))
63 (set-visited-file-name
64 (substring buffer-file-name 0 (match-beginning 0)))))
65 (message "Uncompressing...")
66 (let ((buffer-read-only nil))
67 (shell-command-on-region (point-min) (point-max) uncompress-program t))
68 (message "Uncompressing...done")
69 (set-buffer-modified-p nil)
70 (make-local-variable 'write-file-hooks)
71 (or (memq 'uncompress-backup-file write-file-hooks)
72 (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
73 (normal-mode))
74
75 (or (memq 'find-compressed-version find-file-not-found-hooks)
76 (setq find-file-not-found-hooks
77 (cons 'find-compressed-version find-file-not-found-hooks)))
78
79 (defun find-compressed-version ()
80 "Hook to read and uncompress the compressed version of a file."
81 ;; Just pretend we had visited the compressed file,
82 ;; and uncompress-while-visiting will do the rest.
83 (let (name)
84 (if (file-exists-p (setq name (concat buffer-file-name ".Z")))
85 (setq buffer-file-name name)
86 (if (file-exists-p (setq name (concat buffer-file-name ".gz")))
87 (setq buffer-file-name name)))
88 (if (eq name buffer-file-name)
89 (progn
90 (insert-file-contents buffer-file-name t)
91 (goto-char (point-min))
92 (setq error nil)
93 t))))
94
95 (provide 'uncompress)
96
97 ;;; uncompress.el ends here