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