installing packages

One of the great things about emacs is the availability of external packages. Whatever you want to do with emacs, there is a good chance that someone already solved the problem, and made a package out of it. Emacs comes with many packages, but there are still many more that it does not include. In the entry about modes, we already looked at various file-type specific packages that are available for emacs. Howeve, we did not discuss how to install external packages/modes. So, how does one install an external package? Assuming, of course, that you know what package you would like to install. This blog tries to help with helping you to identify useful ones.

The first step is, not surprisingy, to get the external package. Linux distributions like Fedora and Debian/Ubuntu have many of them pre-packaged, that is, you can get them from their repositories. This is often the easiest way to get the packages, so it's good to first check if your distribution offers the package. For example, in Debian/Ubuntu, there's emacs-goodies-el which includes a lot of useful things.

If your distribution does not include the desired package, there are some different options:

  • First, your emacs might include some kind of automatic package retrieval system; XEmacs does, and for GNU/Emacs there is ELPA. I have not used it, so I won't go into that here.
  • Second, there are bigger packages that come with their own installation procedure, documentation etc., and which are usually shipped as .tar.gz-files. An example of this is the VM (ViewMail) package, which provides an email client for Emacs. It comes with documentation and installation instructions. Another one is magit, for using the git with emacs. Magit is (at this moment) only available by getting it from its repository, but the same rule applies: just follow the installation instructions.
  • Finally, there are many single .el-files that you can find, for example, in the Emacs Lisp List. You can download such a file, and put in some directory where emacs can find it. I made a directory ~/.elisp/ for this. And at the top of my .emacs, there is something like:
    ;; my elisp directories
    (defvar elisp-path '("~/.elisp/" "~/.another-path")) 
    (mapcar '(lambda(p) (add-to-list 'load-path p)) elisp-path)
    This code creates a variable elisp-path with some directories, and uses mapcar to add each of them to load-path, which is the variable that emacs uses to determine where it can find packages. Emacs actually adds a lot of directories to load-path already before considering your .emacs; you can see the full load-path with describe-variable, or C-h v.

Now, in order to actually use a package, we have to ask for it in our .emacs. There are various ways:

  • Frequently, you need to require such a package, say:
    (require 'magit)
    This will load the package. Emacs will give an error if it cannot find the package (i.e., if it cannot find (provide 'magit) in the package), so if you are using your .emacs in different places, you might want to use the require-maybe function.
  • Another way is to use autoload. The idea is that we define a function, and the package is not loaded until we actually call that function. This can be helpful to speed up loading your emacs, and save some memory: we will only load packages that we actually use, and not before we need it. A useful pattern here is to use autoload together with auto-mode-alist:
    ;; post mode (used when editing mail / news)
    (autoload 'post-mode "post" "mode for e-mail" t)
    (add-to-list 'auto-mode-alist 
                    . post-mode))
    This does two things: first, we use autoload to define a function post-mode; if this function is called, it will load "post", which contains the real definition of that function. Second, we use auto-mode-alist to tell emacs that whenever we open a file which name matches some pattern, call the function post-mode.

No comments: