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