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.