2010-05-14

emacs 23.2

Recently, emacs version 23.2 was released. It's a quick update after 23.1 came out (July 29 of 2009); it seems Chong Yidong / Stefan Monnier (interview) are doing releases more often than before they took over emacs maintainership. A welcome change, I would say.

The amount of changes is obviously also a bit smaller than in 23.1, but there are still some interesting updates. Let's go through a couple of those here; I am not striving for completeness, and I won't really go into the biggest change (inclusion of the CEDET IDE-framework), as I haven't been using that enough to say anything about it. Instead, let's look at some of the other highlights; for the full list of changes, please refer to the NEWS-file. If you have some other cool new feature that deserves mentioning, please add a comment.

Some highlights

  • Maximum file size increased to 512Mb (this used to be 256 on 32-bit machines). This may be useful for big log files etc. It does take a while to load such big files, but after that it's not too slow, at least if you have enough RAM. For 'normal' files, you're unlikely to ever hit the limit; e.g. Tolstoy's War and Peace is only 3 MB…

    Note, you can set large-file-warning-threshold to set the maximum file size after which emacs will starting asking you for confirmation when trying to open (eh, visit) files.

  • By default, the mouse cursor turns invisible when typing, so there is no more need for mouse-avoidance-mode and similar tricks. However, if you insist on seeing the mouse cursor, you can add to your .emacs:
(setq make-pointer-invisible nil)
  • On X-based systems, emacs now listens for font changes (Xft), and can automatically use the GNOME mono-spaced font (as set in the GNOME Appearance preferences dialog). Note that this may not work for all fonts/settings (at least in my tests, setting the font to italic does not seem to reflect in emacs). Anyway, to enable this, put the following in your .emacs (or the moral equivalent):
(setq font-use-system-font t)
  • On Unix, Emacs 23.2 now follows the freedesktop trash specification for file deletion; thus, the hacks we hacks we mentioned before are no longer needed.
  • Some cool additions for Tramp, allowing emacs to access files in rsync and even imap://-locations. On systems supporting GVFS, emacs can now directly use e.g. obex://-uris (Bluetooth). I need to play a bit with these things! Tramp support has also been built into eshell.
  • There are already quite some ways to do auto-completion in emacs using the TAB-key, and emacs 23.2 makes this a bit easier to set up. You can add basic auto-completion with:
(setq tab-always-indent 'complete)
  • After setting that, the TAB-key will (after trying if there's anything to indent first) provide possible completions. It works pretty well for Emacs-Lisp (I did not test other languages), although the way it shows the completions (separate *Completions*-buffer) is a bit clumsier that what for instance yasnippet or company-mode do.
  • You can also do partial completions now, by appending initials to the completion style, i.e.:
;; there must be a more elegant way...
(setq completion-styles (append completion-style '(initials)))
  • With this, you can complete on the initials of functions and variables in the minibuffer, say, typing C-h v tai and then pressing TAB will give you tab-always-indent.
  • As mentioned, the biggest change is the addition on the CEDET-framework, which contains things like smart completion, code browsing, UML diagram creation, project management – features somewhat similar to those in e.g. Eclipse. I don't know how well it works in practice, but I will give it a try. At least, inclusion in Emacs should make setting it up with all dependencies a bit easier, as there is now a guaranteed-to-work setup for Emacs 23.2 at least.

Summarizing, 23.2 provides us with some nice updates all around and brings CEDET to the masses. Chong Yidong / Stefan Monnier have done a very good job in making faster releases, while still keeping an eye on the quality. On the other hand, the previous version (23.1) is a very solid release, and if you don't need CEDET, there is no real need to hurry to 23.2.

Future releases

A lot is happening in the world of GNU/Emacs, with changes being proposed and implemented in many different places. There's Eli Zaretskii's way work on making emacs support bidirectional languages (for right-to-left writing systems such as Hebrew and Arabic; the /bi/directional part is that one should be able to mix left-to-right and right-to-left). There is Jan Djärv's work on adding UI-tabs to emacs (like e.g. Firefox has them). There is Ken Raeburn and Andy Wingo's work on adding Guile Scheme support to emacs - possibly replacing the current Emacs Lisp implementation in the future. These are just a few of the more prominent projects.

Nobody knows in which release these items will be available (if at all), but it's exciting to see all the directions people are taking emacs.

2010-05-12

cleaning up the mode-line

Emacs' version on a status-bar is called the mode-line, and contains all kind of information – the current buffer name, the cursor position and a lot of other things, depending on what major and minor modes are active.

Customizing the mode-line is, unfortunately, rather hard. One day, I'll write something about that… but for now at least we may be able to improve things a little bit, by reducing mode line pollution. Mode line pollution? Well, many parts of emacs like to announce their presence and state in the mode line. With the limited space available there, this can become a bit of an issue, the (Lisp Interaction company Yas abbrev) takes quite some space:


But there are some ways to limit the space taken by modes and minor-modes. Note, these snippets should go in your .emacs, and you need to restart emacs to make them active.

First, the minor modes (note, you can see the currently activated ones with C-h m); install the handy diminish.el (or get it using the emacs-goodies-el package when using Debian/Ubuntu) and add something like the following:
(when (require 'diminish nil 'noerror)
  (eval-after-load "company"
      '(diminish 'company-mode "Cmp"))
  (eval-after-load "abbrev"
    '(diminish 'abbrev-mode "Ab"))
  (eval-after-load "yasnippet"
    '(diminish 'yas/minor-mode "Y")))

And the major-modes, for example for Emacs Lisp mode:
(add-hook 'emacs-lisp-mode-hook 
  (lambda()
    (setq mode-name "el"))) 
This looks a bit shorter:

You can of course set these names to whatever is obvious to you.

2010-04-27

navigating the kill-ring

The kill-ring is emacs' implementation of a copy-paste-clipboard. As expected, it's more powerful than what most other editors offer - but at the same time, it may be a bit hard to use. We already discussed the kill-ring in the Emacs-Fu prehistory.

One of the more powerful features of the emacs kill-ring is that is allows for multiple entries to be saved there. You can then retrieve those older entries by using prefix arguments, that is: C-y will retrieve the last item from the kill-ring, while M- n C-y will retrieve the n th last stretch of 'killed' (cut/copied) text. For example, M-2 C-y will retrieve the second last one.

Unfortunately, for most people it's quite hard to remember what was 'killed' and when and in what order… Those people can of course use the menu (Edit/Paste from kill menu), but that is not always so convenient, requires mousing around etc.

Edit: As Anynomous mentions in the comments, one can of course use M-y to circle through the candidates. This is quite useful, esp. when you have only a few items in the ring. Note, this command only works just after a 'yank' (C-y).

browse-kill-ring

Instead, using the handy browse-kill-ring extension, you can open a buffer which lists the contents of the kill ring, and you can move your cursor to the desired item and insert it.

Installation in simple; first get the browse-kill-ring package from EmacsWiki, or, alternatively, Debian/Ubuntu users can install the emacs-goodies-el-package.

Then, add to your .emacs something like:

(when (require 'browse-kill-ring nil 'noerror)
  (browse-kill-ring-default-keybindings))

Now, the M-y key binding will activate browse-kill-ring iff the normal behavior (see above) is not available, i.e., when the last command was not a 'yank'. You can also edit the kill-ring (press C-h m when in the browse-kill-ring-buffer to see the available bindings).

a little pop-up menu

While browsing EmacsWiki, I found another trick:

(global-set-key "\C-cy" '(lambda ()
   (interactive)
   (popup-menu 'yank-menu)))

After which C-c y will show a little pop-up menu with the your kill-menu entries. It does not seem to fully synchronize with the (possibly edited) entries you get from browse-kill-ring, but it's a pretty neat way to navigate through your kill-ring-buffers – if you don't have too many of them (if so, you could customize the kill-ring-max variable).

2010-04-18

creating custom modes the easy way with generic-mode

Syntax highlighting is useful when editing configuration files, programs and so on, as it helps to prevent errors and makes it easier to quickly scan documents.

Emacs supports syntax highlighting (font locking in emacs lingo) for many different file types. For many common cases (e.g. editing for many programming languages, org-mode), emacs' support goes much further than merely colorizing keywords, and offers all kinds of 'magic' (auto-completion, 'electricity', special key bindings, …). For some other file types, at least keywords are given some different color.

Still, there are files that are not recognized by emacs as having some special format; these are displayed as plain text. This may be the case for less-common configuration files, or your own specific formats.

Defining a full 'mode' for such file types can be a lot of work. Fortunately, emacs offers a easier way: generic-mode. generic-mode defines a whole lot of mode of modes for common formats, but also defines the define-generic-mode macro to create your own modes.

Suppose we have a little language called foo; a typical foo-file might look something like:

!! this is a comment
account=foo; !! another comment
user=jimmy;
password=$3cre7;

Using define-generic-mode, we can easily define a mode for this:

(require 'generic-x) ;; we need this

(define-generic-mode 
  'foo-mode                         ;; name of the mode to create
  '("!!")                           ;; comments start with '!!'
  '("account" "user" 
    "password")                     ;; some keywords
  '(("=" . 'font-lock-operator)     ;; '=' is an operator
    (";" . 'font-lock-builtin))     ;; ';' is a a built-in 
  '("\\.foo$")                      ;; files for which to activate this mode 
   nil                              ;; other functions to call
  "A mode for foo files"            ;; doc string for this mode
)

Now, this will look something like this (if necessary, see the colorized version):

!! this is a comment
account = foo; !! another comment
user = jimmy;
password = $3cre7;

2010-04-04

the zenburn color theme

A popular way to customize emacs is changing its color scheme, as already discussed color theming. Until recently, I was using an evolved version of the color theme presented there, 'djcb-dark'. It works for me but, admittedly, it's a bit ugly.

But recently, in a post to the Wanderlust mailing list, someone mentioned a color theme called Zenburn. Zenburn started its life as a color scheme for vim, around 2002. The explicit goal was to have a pleasant theme that is light on the eyes, and allows you to stay 'in the zone' for long stretches of time. People liked it, and version for many other programs were made, including emacs.

I've been using Zenburn for the last few weeks, and I really like it. I used to think that 'low-contrast' would mean that things are not really clear; but the opposite seems true. Anyway, the screen shot says more than a thousand words I suppose…

Zenburn-for-emacs (written by Daniel Brockman) can be found at the link above. I've sent my updates to him of course, but as it may take a while for the 'official' version to be updated, I've put my version on Emacswiki: ZenburnColorTheme. The changes are the support for Wanderlust, hi-line (for highlighting the current line) , magit and elscreen; also, I made selected (eh, transiently marked regions) not loose their foreground color.

Note, the theme is not yet part of the color-theme package, but does require it.

2010-03-20

showing the buffer position in the mode-line

I do quite a bit of scrolling in emacs, but I hardly ever use the scroll bar for that. The main reason for still having the scroll bar is that it gives me some indication where I am in the buffer. Of course, there is some information in the mode-line, and you can get some more with size-indication-mode, but it's not as immediately obvious as the scroll bar.
But recently, I discovered Lennart Borgman's sml-modeline, which combines all of the scroll bar information into a nice visual indication on the modeline, and I have been happily using it, and got rid of my scroll bar.
Put you sml-modeline in your load-path, and the following fragment in your .emacs should do the trick:
(if (require 'sml-modeline nil 'noerror)    ;; use sml-modeline if available
  (progn 
    (sml-modeline-mode 1)                   ;; show buffer pos in the mode line
    (scroll-bar-mode -1))                   ;; turn off the scrollbar
  (scroll-bar-mode 1)                       ;; otherwise, show a scrollbar...
  (set-scroll-bar-mode 'right))             ;; ... on the right

Note, there is a older version available in Emacswiki which has some problems (such as conflicting with the Standard ML editing mode for emacs); thus, for now it's better to us the Launchpad version; the instructions above apply to that version.

2010-03-08

cleaning up buffers automatically

Recently, I discussed some ways to deal with large numbers of buffers. Maybe we can also take a step back and ask why we have so many buffers in the first place - do we really need all of them?

Well, the obvious answer is: probably not. After a few days of (emacs-uptime) there are all kinds of temporary output buffers, man pages and unchanged buffers you haven't touched in a long time. Let's get rid of those!

midnight

For this very purpose, emacs provides midnight-mode (as has done so for more than a decade). At midnight, it looks at all the buffers, and determines which of the buffers are no longer in use, and closes ('kills') them. Regardless of its name, this cleanup does not necessarily have to take place at midnight, but could be invoked at any time.

Setup is easy, just put the following in your .emacs:

(require 'midnight)

Clearly, the package was designed for emacs instances that are running for long times – for example, by default it clears buffers after having been inactive for 3 days. I'm not sure if that use case is very common today. Anyway, you can change it by setting clean-buffer-list-delay-general (which takes the number of days before a buffer becomes eligible for killing).

You can ask midnight-mode to clean-up unused buffers right now with M-x clean-buffer-list. Also, you can use some variables to control which buffers are to be killed, and which ones specifically not:

clean-buffer-list-kill-buffer-names
clean-buffer-list-kill-never-buffer-names
clean-buffer-list-kill-regexps
clean-buffer-list-kill-never-regexps

To run clean-buffer-list every n minutes or so, you could use run-at-time, left as an exercise to the reader.

tempbuf

Another way to accomplish roughly the same is TempbufMode.

It seems a bit better equipped for shorter cleanup interval, and you have some killed. However, that requires you to add it to the modes where you'd like to more influence on the algorithm it uses to decide whether a buffer may be use it, something like:

;; download tempbuf: http://www.emacswiki.org/emacs/tempbuf.el
(when (require 'tempbuf nil 'noerror) 
  (add-hook 'custom-mode-hook 'turn-on-tempbuf-mode)
  (add-hook 'w3-mode-hook 'turn-on-tempbuf-mode)
  (add-hook 'Man-mode-hook 'turn-on-tempbuf-mode)
  (add-hook 'view-mode-hook 'turn-on-tempbuf-mode))

Side-note: I'm using the (when (require 'tempbuf nil 'noerror) ... ) pattern here to make sure that my .emacs also works when tempbuf is not available.

Added: for cleaning-up your buffer list non-automatically, you can simply use M-x kill-some-buffers. (Thanks Susan!). Or you can use C-x C-b.