(xmenu_show): Don't look in menubar for core.height if no menu bar.
[bpt/emacs.git] / lisp / uncompress.el
1 ;;; uncompress.el --- auto-decompression hook for visiting .Z files
2
3 ;; Copyright (C) 1992, 1994 Free Software Foundation, Inc.
4
5 ;; Maintainer: FSF
6
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
11 ;; the Free Software Foundation; either version 2, or (at your option)
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
20 ;; along with GNU Emacs; see the file COPYING. If not, write to
21 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
22
23 ;;; Commentary:
24
25 ;; This package can be used to arrange for automatic uncompress of
26 ;; files packed with the UNIX compress(1) utility when they are visited.
27 ;; All that's necessary is to load it. This can conveniently be done from
28 ;; your .emacs file.
29
30 ;;; Code:
31
32 ;; When we are about to make a backup file,
33 ;; uncompress the file we visited
34 ;; so that making the backup can work properly.
35 ;; This is used as a write-file-hook.
36
37 (defvar uncompress-program "gunzip"
38 "Program to use for uncompression.")
39
40 (defun uncompress-backup-file ()
41 (and buffer-file-name make-backup-files (not buffer-backed-up)
42 (not (file-exists-p buffer-file-name))
43 (call-process uncompress-program nil nil nil buffer-file-name))
44 nil)
45
46 (or (assoc "\\.Z$" auto-mode-alist)
47 (setq auto-mode-alist
48 (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
49 (or (assoc "\\.gz$" auto-mode-alist)
50 (setq auto-mode-alist
51 (cons '("\\.gz$" . uncompress-while-visiting) auto-mode-alist)))
52
53 (defun uncompress-while-visiting ()
54 "Temporary \"major mode\" used for .Z and .gz files, to uncompress them.
55 It then selects a major mode from the uncompressed file name and contents."
56 (if (and (not (null buffer-file-name))
57 (string-match "\\.Z$" buffer-file-name))
58 (set-visited-file-name
59 (substring buffer-file-name 0 (match-beginning 0)))
60 (if (and (not (null buffer-file-name))
61 (string-match "\\.gz$" buffer-file-name))
62 (set-visited-file-name
63 (substring buffer-file-name 0 (match-beginning 0)))))
64 (message "Uncompressing...")
65 (let ((buffer-read-only nil))
66 (shell-command-on-region (point-min) (point-max) uncompress-program t))
67 (message "Uncompressing...done")
68 (set-buffer-modified-p nil)
69 (make-local-variable 'write-file-hooks)
70 (or (memq 'uncompress-backup-file write-file-hooks)
71 (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
72 (normal-mode))
73
74 (or (memq 'find-compressed-version find-file-not-found-hooks)
75 (setq find-file-not-found-hooks
76 (cons 'find-compressed-version find-file-not-found-hooks)))
77
78 (defun find-compressed-version ()
79 "Hook to read and uncompress the compressed version of a file."
80 ;; Just pretend we had visited the compressed file,
81 ;; and uncompress-while-visiting will do the rest.
82 (let (name)
83 (if (file-exists-p (setq name (concat buffer-file-name ".Z")))
84 (setq buffer-file-name name)
85 (if (file-exists-p (setq name (concat buffer-file-name ".gz")))
86 (setq buffer-file-name name)))
87 (if (eq name buffer-file-name)
88 (progn
89 (insert-file-contents buffer-file-name t)
90 (goto-char (point-min))
91 (setq error nil)
92 t))))
93
94 (provide 'uncompress)
95
96 ;;; uncompress.el ends here