* etags.c (CNL_SAVE_DEFINEDEF): Set linecharno for use by readline.
[bpt/emacs.git] / lisp / uncompress.el
CommitLineData
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
27;; files packed with the UNIX compress(1) utility when they are visited.
28;; All that's necessary is to load it. This can conveniently be done from
29;; your .emacs file.
30
e5167999
ER
31;;; Code:
32
0addef92
JB
33;; When we are about to make a backup file,
34;; uncompress the file we visited
35;; so that making the backup can work properly.
36;; This is used as a write-file-hook.
37
baf90b62
RS
38(defvar uncompress-program "gunzip"
39 "Program to use for uncompression.")
40
0addef92
JB
41(defun uncompress-backup-file ()
42 (and buffer-file-name make-backup-files (not buffer-backed-up)
43 (not (file-exists-p buffer-file-name))
baf90b62 44 (call-process uncompress-program nil nil nil buffer-file-name))
0addef92
JB
45 nil)
46
47(or (assoc "\\.Z$" auto-mode-alist)
48 (setq auto-mode-alist
49 (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
baf90b62
RS
50(or (assoc "\\.gz$" auto-mode-alist)
51 (setq auto-mode-alist
52 (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
0addef92
JB
53
54(defun uncompress-while-visiting ()
baf90b62 55 "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
0addef92
JB
56It then selects a major mode from the uncompressed file name and contents."
57 (if (and (not (null buffer-file-name))
58 (string-match "\\.Z$" buffer-file-name))
59 (set-visited-file-name
baf90b62
RS
60 (substring buffer-file-name 0 (match-beginning 0)))
61 (if (and (not (null buffer-file-name))
62 (string-match "\\.gz$" buffer-file-name))
63 (set-visited-file-name
64 (substring buffer-file-name 0 (match-beginning 0)))))
0addef92
JB
65 (message "Uncompressing...")
66 (let ((buffer-read-only nil))
baf90b62 67 (shell-command-on-region (point-min) (point-max) uncompress-program t))
0addef92
JB
68 (message "Uncompressing...done")
69 (set-buffer-modified-p nil)
70 (make-local-variable 'write-file-hooks)
71 (or (memq 'uncompress-backup-file write-file-hooks)
72 (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
73 (normal-mode))
74
75(or (memq 'find-compressed-version find-file-not-found-hooks)
76 (setq find-file-not-found-hooks
77 (cons 'find-compressed-version find-file-not-found-hooks)))
78
79(defun find-compressed-version ()
80 "Hook to read and uncompress the compressed version of a file."
81 ;; Just pretend we had visited the compressed file,
82 ;; and uncompress-while-visiting will do the rest.
baf90b62
RS
83 (let (name)
84 (if (file-exists-p (setq name (concat buffer-file-name ".Z")))
85 (setq buffer-file-name name)
86 (if (file-exists-p (setq name (concat buffer-file-name ".gz")))
87 (setq buffer-file-name name)))
88 (if (eq name buffer-file-name)
89 (progn
90 (insert-file-contents buffer-file-name t)
91 (goto-char (point-min))
92 (setq error nil)
93 t))))
d501f516 94
2b0739e5
RS
95(provide 'uncompress)
96
d501f516 97;;; uncompress.el ends here