2
|
1 ;;; uncompress.el --- auto-decompression hook for visiting .Z files
|
0
|
2
|
2
|
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
|
|
4
|
|
5 ;; Maintainer: FSF
|
0
|
6 ;; Keywords: unix extensions
|
|
7
|
2
|
8 ;; This file is part of XEmacs.
|
0
|
9
|
2
|
10 ;; XEmacs is free software; you can redistribute it and/or modify it
|
|
11 ;; under the terms of the GNU General Public License as published by
|
|
12 ;; the Free Software Foundation; either version 2, or (at your option)
|
|
13 ;; any later version.
|
|
14
|
|
15 ;; XEmacs is distributed in the hope that it will be useful, but
|
|
16 ;; WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
|
|
18 ;; General Public License for more details.
|
0
|
19
|
2
|
20 ;; You should have received a copy of the GNU General Public License
|
|
21 ;; along with XEmacs; see the file COPYING. If not, write to the Free
|
|
22 ;; Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
|
|
23 ;; 02111-1307, USA.
|
0
|
24
|
2
|
25 ;;; Synched up with: 19.34.
|
|
26
|
|
27 ;;; Commentary:
|
|
28
|
|
29 ;; This package can be used to arrange for automatic uncompress of
|
|
30 ;; files packed with the UNIX compress(1) utility when they are visited.
|
|
31 ;; All that's necessary is to load it. This can conveniently be done from
|
|
32 ;; your .emacs file.
|
|
33
|
|
34 ;;; Code:
|
0
|
35
|
|
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
|
2
|
41 (defvar uncompress-program "gunzip"
|
|
42 "Program to use for uncompression.")
|
|
43
|
0
|
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))
|
2
|
47 (call-process uncompress-program nil nil nil buffer-file-name))
|
0
|
48 nil)
|
|
49
|
|
50 (or (assoc "\\.Z$" auto-mode-alist)
|
|
51 (setq auto-mode-alist
|
|
52 (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
|
2
|
53 (or (assoc "\\.gz$" auto-mode-alist)
|
|
54 (setq auto-mode-alist
|
|
55 (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
|
0
|
56
|
|
57 (defun uncompress-while-visiting ()
|
2
|
58 "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
|
0
|
59 It then selects a major mode from the uncompressed file name and contents."
|
|
60 (if (and (not (null buffer-file-name))
|
|
61 (string-match "\\.Z$" buffer-file-name))
|
|
62 (set-visited-file-name
|
2
|
63 (substring buffer-file-name 0 (match-beginning 0)))
|
|
64 (if (and (not (null buffer-file-name))
|
|
65 (string-match "\\.gz$" buffer-file-name))
|
|
66 (set-visited-file-name
|
|
67 (substring buffer-file-name 0 (match-beginning 0)))))
|
0
|
68 (message "Uncompressing...")
|
|
69 (let ((buffer-read-only nil))
|
2
|
70 (shell-command-on-region (point-min) (point-max) uncompress-program t))
|
0
|
71 (message "Uncompressing...done")
|
|
72 (set-buffer-modified-p nil)
|
|
73 (make-local-variable 'write-file-hooks)
|
|
74 (or (memq 'uncompress-backup-file write-file-hooks)
|
|
75 (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
|
|
76 (normal-mode))
|
|
77
|
|
78 (or (memq 'find-compressed-version find-file-not-found-hooks)
|
|
79 (setq find-file-not-found-hooks
|
|
80 (cons 'find-compressed-version find-file-not-found-hooks)))
|
|
81
|
|
82 (defun find-compressed-version ()
|
|
83 "Hook to read and uncompress the compressed version of a file."
|
|
84 ;; Just pretend we had visited the compressed file,
|
|
85 ;; and uncompress-while-visiting will do the rest.
|
2
|
86 (let (name)
|
|
87 (if (file-exists-p (setq name (concat buffer-file-name ".Z")))
|
|
88 (setq buffer-file-name name)
|
|
89 (if (file-exists-p (setq name (concat buffer-file-name ".gz")))
|
|
90 (setq buffer-file-name name)))
|
|
91 (if (eq name buffer-file-name)
|
|
92 (progn
|
|
93 (insert-file-contents buffer-file-name t)
|
|
94 (goto-char (point-min))
|
|
95 (setq error nil)
|
|
96 t))))
|
|
97
|
|
98 (provide 'uncompress)
|
|
99
|
|
100 ;;; uncompress.el ends here
|