(display-time-string-forms): Use
[emacs.git] / lisp / uncompress.el
blob53439190a1db4b708a18a8a77ab84699afbc411a
1 ;;; uncompress.el --- auto-decompression hook for visiting .Z files
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
7 ;; This file is part of GNU Emacs.
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.
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.
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.
24 ;;; Commentary:
26 ;; This package can be used to arrange for automatic uncompress of
27 ;; compressed files when they are visited.
28 ;; All that's necessary is to load it. This can conveniently be done from
29 ;; your .emacs file.
31 ;; M-x auto-compression-mode is a more modern replacement for this package.
33 ;;; Code:
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.
40 (defvar uncompress-program "gunzip"
41 "Program to use for uncompression.")
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))
46 (call-process uncompress-program nil nil nil buffer-file-name))
47 nil)
49 (or (assoc "\\.Z$" auto-mode-alist)
50 (setq auto-mode-alist
51 (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
52 (or (assoc "\\.gz$" auto-mode-alist)
53 (setq auto-mode-alist
54 (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
55 (or (assoc "\\.tgz$" auto-mode-alist)
56 (setq auto-mode-alist
57 (cons '("\\.tgz$" . uncompress-while-visiting) auto-mode-alist)))
59 (defun uncompress-while-visiting ()
60 "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
61 It 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
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
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")))))
74 (message "Uncompressing...")
75 (let ((buffer-read-only nil)
76 (coding-system-for-write 'no-conversion)
77 (coding-system-for-read
78 (car (find-operation-coding-system
79 'insert-file-contents
80 buffer-file-name t))))
81 (shell-command-on-region (point-min) (point-max) uncompress-program t))
82 (goto-char (point-min))
83 (message "Uncompressing...done")
84 (set-buffer-modified-p nil)
85 (make-local-variable 'write-file-hooks)
86 (or (memq 'uncompress-backup-file write-file-hooks)
87 (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
88 (normal-mode))
90 (or (memq 'find-compressed-version find-file-not-found-hooks)
91 (setq find-file-not-found-hooks
92 (cons 'find-compressed-version find-file-not-found-hooks)))
94 (defun find-compressed-version ()
95 "Hook to read and uncompress the compressed version of a file."
96 ;; Just pretend we had visited the compressed file,
97 ;; and uncompress-while-visiting will do the rest.
98 (let (name)
99 (if (file-exists-p (setq name (concat buffer-file-name ".Z")))
100 (setq buffer-file-name name)
101 (if (file-exists-p (setq name (concat buffer-file-name ".gz")))
102 (setq buffer-file-name name)))
103 (if (eq name buffer-file-name)
104 (progn
105 (insert-file-contents buffer-file-name t)
106 (goto-char (point-min))
107 ;; No need for this, because error won't be set to t
108 ;; if this function returns t.
109 ;; (setq error nil)
110 t))))
112 (provide 'uncompress)
114 ;;; uncompress.el ends here