Commit | Line | Data |
---|---|---|
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 |
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 | |
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 | 74 | (message "Uncompressing...") |
7a784f27 RS |
75 | (let ((buffer-read-only nil) |
76 | (coding-system-for-write 'no-conversion) | |
77 | (coding-system-for-read | |
c22256a1 KH |
78 | (car (find-operation-coding-system |
79 | 'insert-file-contents | |
80 | buffer-file-name t)))) | |
baf90b62 | 81 | (shell-command-on-region (point-min) (point-max) uncompress-program t)) |
f7a7738f | 82 | (goto-char (point-min)) |
0addef92 JB |
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)) | |
89 | ||
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))) | |
93 | ||
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. | |
baf90b62 RS |
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)) | |
e81d27c2 RS |
107 | ;; No need for this, because error won't be set to t |
108 | ;; if this function returns t. | |
109 | ;; (setq error nil) | |
baf90b62 | 110 | t)))) |
d501f516 | 111 | |
2b0739e5 RS |
112 | (provide 'uncompress) |
113 | ||
d501f516 | 114 | ;;; uncompress.el ends here |