2011-08-25

The mode-line is the emacs 'status bar', the bar just above the minibuffer
that shows various pieces of information, such as the buffer name, the major
mode, maybe the current line number, some indicators for active minor modes,
and so on. As I'm looking at it, it starts with 1<U:**- (which is:
input-method: latin-1-alt-postfix, buffer-coding-system: utf8-unix,
line-ending: unix-style, buffer is writable and buffer is modified – the
tooltips help).
As with just about anything in emacs, the mode-line can be customized just
the way you like. I give some example below, not because I think it is
necessarily the best way, but just to give you a bit of an example to
start with when making your own best-mode-line-ever.
I'm not going through all the details of the example, but let me highlight a
few things that make it a bit easier to understand.
First of all, the mode-line can be customized by setting the variable
mode-line-format; this variable becomes buffer-local automatically when
changed, so if you want to set it for all buffers, you'll need to use
setq-default in your .emacs (or equivalent). The format is quite similar
to the one for frame-title-format, which we discussed in setting the frame title a while back.
mode-line-format is a list of items which are evaluated, and put
together as a string which then ends up as the mode-line contents. These
properties can be any string. The following types of items can be used:

First, normal strings are just shown as-is;

Then, there are some special format parameters which will be replaced with
their value in the mode-line, from the Emacs-documentation:

Forms of the type (:eval ...) are evaluated each time the mode-line is
drawn (just like the '%'-parameters) ; so, if you have a value that
changes of the course your emacs session, you should use (:eval ...).
For example, for your emacs-uptime you could use (:eval (emacs-uptime "%hh")); while the emacs-PID does not change, so simply you could simply
use (format "PID:%d").
The format parameter mentioned above are of evaluated each time as
well. Note that you have to be a bit careful with evaluations - don't do
too heavy operations there, and be careful the updates don't recurse.

There are many others which I won't go into now - please check the Elisp
reference. It's a rather baroque format…

Now, let's put this all together in an example (tested with emacs 23 and
24). As I said, this is for demonstration purposes only; but hopefully it
gives you some inspiration. A lot of the 'magic' (colors, tooltips, faces)
happens with the propertize function; again, the Elisp documentation can
tell you a lot more about that. I'm (ab)using the various font-lock-faces to
have colors that blend in nicely with your current theme.
And it has a limitation still, namely that it does not react to mouse clicks;
how to that, I will discuss in some future article.

I think the way you've defined your mode-line is excellent for readability in the case of this tutorial, and makes for something much more understandable than the traditional list of #( forms and (var var) pairs for choosing a mode-line, but for actual use I'd strongly recommend defining each part separately and then using those to define your final mode line.

I find this makes life much easier both for the initial fiddling (do I really want those things in that order?) and when six months later I realise I actually want to add some more indicators or remove others.

Someday I hope you set your sights lower. I'd like to know about customizing the messages buffer at the very bottom. I find that it does not get my attention as much as I'd like it to. I'd like to fix up the type face, font size, and even font color if possible.

@Greg. Those are actually pretty easy to change as they're controlled by faces just like pretty much everything else. There are a few different faces to customise (echo-area, minibuffer, the various completion faces, etc.) but that's all pretty straight forward.

I think there is room for a good article on how to use a dedicated frame for the mini-buffer, and if anybody could find a way to not only auto-raise it but place it close to the point then that would be even better.

Uncommenting the minor mode part shows the modes (though it doesn't seem to always show the full list that the regular non customized version does) and it doesn't show "notifications" from things like ERC that the previous commenter asked about.

It's things like activity from channels Etc. Any idea how to fix that?

As @cb and @jpc suggested, global-mode-string has to be added; otherwise, things like the output of org-timer doesn't show on the mode-line. But does anyone know how to change the color of global-mode-string?