EMACSulation

by Eric Marsden, emarsden@mail.dotcom.fr


This column is devoted to making the best use of Emacs, text editor extraordinaire. Each issue I plan to present an Emacs extension which can improve your productivity, make the sun shine more brightly and the grass greener.

Jka-compr is a package written by Jay K. Adams which allows Emacs to handle compressed files transparently. When you open a compressed file, Emacs will automatically decompress it before displaying it. If you make changes and save the file, it will be compressed transparently before being written to the disk. To enable jka-compr, just add the following lines to your emacs configuration file (normally called ~/.emacs) :

    (require 'jka-compr)
    (toggle-auto-compression 1 t)

jka-compr works by looking at the filename extension, and in its default configuration recognizes .gz (gzip), and .Z (compress) files. It also recognizes the extension .tgz and unzips tarballs before passing them to tar-mode, which lets you look inside tar files. If you use other compression programs you can tell Emacs about them too, for example to use Julian Seward's bzip2 (faster and slightly better compression than gzip, under GPL) you could add the following to your .emacs (before loading jka-compr)

    (setq jka-compr-compression-info-list
      '(["\\.Z\\(~\\|\\.~[0-9]+~\\)?\\'"
         "compressing"    "compress"     ("-c")
         "uncompressing"  "uncompress"   ("-c")
         nil t]
        ["\\.tgz\\'"
         "zipping"        "gzip"         ("-c" "-q")
         "unzipping"      "gzip"         ("-c" "-q" "-d")
         t nil]
        ["\\.gz\\(~\\|\\.~[0-9]+~\\)?\\'"
         "zipping"        "gzip"         ("-c" "-q")
         "unzipping"      "gzip"         ("-c" "-q" "-d")
         t t]
         ["\\.bz2\\(~\\|\\.~[0-9]+~\\)?\\'"
          "bzipping"      "bzip2"        ()
          "bunzipping"    "bzip2"        ("-d")
          nil t]))

How does it work?

Packages like jka-compr are written in Emacs Lisp; you can read the source code in the directory /usr/local/lib/emacs/${VERSION}/lisp/jka-compr.el for GNU Emacs, or /usr/local/lib/xemacs-${VERSION}/lisp/packages/jka-compr.el for XEmacs users (if you are using a Red Hat Linux distribution, you need to install the emacs-el package to see the source files). How can they change the behaviour of Emacs at such a low level as reading and writing files? The answer comes from the concept of hooks.

Most of Emacs' low-level functions (which are written in C) have an associated hook, to which user-level functions (written in Emacs Lisp) can be attached. Hooks are fundamental to the customizability of Emacs, allowing users to override default behaviour in ways that its developers could not have imagined. Hooks are explained in the Emacs and Elisp manuals, which are available online from within Emacs by typing C-h i (or from the Help menubar or (blech!) the XEmacs toolbar).

As an example of using a hook, the after-init-hook is run right after Emacs is lauched and has loaded your initialization file. Let's say you want Emacs to tell your fortune each time you start it. Just add the following lines to your .emacs :

    
   (add-hook 'after-init-hook
      (function
       (lambda ()
         (pop-to-buffer (get-buffer-create " *Fortune*"))
         (shell-command "fortune -a" t))))

Next time ...

In the next issue I'll discuss ange-ftp, which lets Emacs see the Internet as a huge virtual filesystem. Please contact me at <emarsden@mail.dotcom.fr> with comments, corrections or suggestions. C-u 1000 M-x hail-emacs !

PS : Emacs isn't in any way limited to Linux, since implementations exist for many other operating systems. However, as one of the leading bits of free software, one of the most powerful, complex and customizable, I feel it has its place in the Linux Gazette. Don't forget, Emacs makes all computing simple :-)


Copyright © 1998, Eric Marsden