(bookmark-write): Add numbered backups for bookmark file.
[emacs.git] / lisp / uncompress.el
bloba44590a3afdabd4fcb9b0bea579ea5c02f88d204
1 ;;; uncompress.el --- auto-decompression hook for visiting .Z files
3 ;; Copyright (C) 1992 Free Software Foundation, Inc.
5 ;; Maintainer: FSF
7 ;; This file is part of GNU Emacs.
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.
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.
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.
23 ;;; Commentary:
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.
30 ;;; Code:
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.
37 (defun uncompress-backup-file ()
38 (and buffer-file-name make-backup-files (not buffer-backed-up)
39 (not (file-exists-p buffer-file-name))
40 (call-process "uncompress" nil nil nil buffer-file-name))
41 nil)
43 (or (assoc "\\.Z$" auto-mode-alist)
44 (setq auto-mode-alist
45 (cons '("\\.Z$" . uncompress-while-visiting) auto-mode-alist)))
47 (defun uncompress-while-visiting ()
48 "Temporary \"major mode\" used for .Z files, to uncompress the contents.
49 It then selects a major mode from the uncompressed file name and contents."
50 (if (and (not (null buffer-file-name))
51 (string-match "\\.Z$" buffer-file-name))
52 (set-visited-file-name
53 (substring buffer-file-name 0 (match-beginning 0))))
54 (message "Uncompressing...")
55 (let ((buffer-read-only nil))
56 (shell-command-on-region (point-min) (point-max) "uncompress" t))
57 (message "Uncompressing...done")
58 (set-buffer-modified-p nil)
59 (make-local-variable 'write-file-hooks)
60 (or (memq 'uncompress-backup-file write-file-hooks)
61 (setq write-file-hooks (cons 'uncompress-backup-file write-file-hooks)))
62 (normal-mode))
64 (or (memq 'find-compressed-version find-file-not-found-hooks)
65 (setq find-file-not-found-hooks
66 (cons 'find-compressed-version find-file-not-found-hooks)))
68 (defun find-compressed-version ()
69 "Hook to read and uncompress the compressed version of a file."
70 ;; Just pretend we had visited the compressed file,
71 ;; and uncompress-while-visiting will do the rest.
72 (if (file-exists-p (concat buffer-file-name ".Z"))
73 (progn
74 (setq buffer-file-name (concat buffer-file-name ".Z"))
75 (insert-file-contents buffer-file-name t)
76 (goto-char (point-min))
77 (setq error nil)
78 t)))
80 (provide 'uncompress)
82 ;;; uncompress.el ends here