Path: utzoo!attcan!uunet!husc6!mailrus!uwmcsd1!bbn!bbn.com!akhanna From: akhanna@bbn.com (Atul Khanna) Newsgroups: comp.emacs Subject: Re: z-mode.el Message-ID: <27240@bbn.COM> Date: 20 Jul 88 19:28:10 GMT References: <8807200126.AA15162@EDDIE.MIT.EDU> Sender: news@bbn.COM Reply-To: akhanna@alexander.bbn.com (Atul Khanna) Organization: Bolt Beranek and Newman Inc., Cambridge MA Lines: 67 In article <8807200126.AA15162@EDDIE.MIT.EDU> aks%nowhere@HUB.UCSB.EDU (Alan Stebbens) writes: >Speaking of auto-uncompressing ".Z" files when visiting them, here >is my little Emacs hack to do this. It is invoked automatically >by including the function in the auto-mode-alist in my .emacs >startup file. > ... >(defun z-mode () "\ >Temporary major mode triggered by the \".Z\" suffix on a file, >used to automatically uncompress the file when visiting. After >running the buffer contents through \"uncompress\", the buffer >name is changed by truncating the \".Z\" (as well as the visited >file name). Also, the buffer is marked as read-only. Finally, >normal-mode is invoked to process the buffer for its normal mode." > (if (and (not (null buffer-file-name)) > (string-match "^\\(.*\\)\\.Z$" buffer-file-name)) > (let ((new (substring buffer-file-name > (match-beginning 1) (match-end 1)))) > (setq buffer-read-only nil) > (message "Uncompressing text...") > (shell-command-on-region (point-min) (point-max) > "uncompress" t) > (message "Uncompressing text...done") > (goto-char (point-min)) > (set-visited-file-name new) > (set-buffer-modified-p nil) > (setq buffer-read-only t))) > (normal-mode)) I do something similar. I have the following binding: (global-set-key "" 'find-and-uncompress-file). The function, which follows, does not uncompress the file. Instead, it uncompresses the buffer. If you edit and save the resulting buffer, you'll end up with both an up-to-date uncompressed version of the file and an out-of-date compressed version, so you have to be careful. I've chosen to do it this way since in most cases I just read compressed files without editing them. (defun find-and-uncompress-file (filename) "Find and uncompress its buffer, if necessary, file FILENAME" (interactive "FFind [uncompress] file: ") (if (string-match "\\.Z$" filename) (let* ((ascii-filename (substring filename 0 (string-match "\\.Z$" filename))) (ascii-buffer (get-file-buffer ascii-filename))) (if ascii-buffer (switch-to-buffer ascii-buffer) (progn (find-file filename) (mark-whole-buffer) (if buffer-read-only (toggle-read-only)) (shell-command-on-region (region-beginning) (region-end) "uncompress" t) (beginning-of-buffer) (set-visited-file-name ascii-filename) (setq buffer-read-only t) (not-modified)))) (find-file filename))) -------------------------------------------------------------------------- Atul C Khanna (akhanna@bbn.com) BBN Communications Corporation 150 CambridgePark Drive Cambridge, MA 02140 UUCP: ..!{decvax, wjh12, harvard}!bbnccv!akhanna