Yet another illustrated primer

This one, called Living in Emacs (PDF), covers the very basics of using Emacs as your working environment:

Getting started with Emacs requires navigating a steep learning curve. Our goal is to help you past the initially unfamiliar interface so that the power and utility of Emacs become apparent. Then you’ll be ready to explore further on your own, following up on the resources and tips at the end of the tutorial.

The 25-pages long tutorial closes with a good piece of advice:

Use Emacs. Live in it for a while, learn to love it a little bit. Like any complex program, it will take time for you to fully grok it, but the effort’s worthwhile: you’ll have become fluent in one of the most common UNIX programming utilities and picked up a fundamentally marketable skill to boot!

Worth a look if you’re just getting started.

Emacs-friendly Firefox

When i use Firefox, there’s nothing more annoying than editing a textarea. On a Mac, one has at least some Emacs-like shortcuts. Incomprehensibly, on GTK+ 2.0 Emacs shortcuts are no longer the default, and one has to put something like

include "/usr/share/themes/Emacs/gtk-2.0-key/gtkrc"

in ~/.gtkrc-2.0 to restore a minimum of sanity.

It's All Text!But still, what i really want is to edit those textareas using the real thing. I just stumbled upon the answer to my prayers: It’s All Text! is a Firefox add-on that provides an edit button on any text box. You click on it and, the first time, you’re asked for your editor of choice. As you’ll notice, the default option is wrong. Change it by /usr/bin/emacsclient (Emacs 21 users may use gnuclient instead), and don’t forget to start the Emacs server with (server-start) somewhere in your initialisation file.

Better, no?

MozexUpdate: Well, things can be even better, as pointed out by Victor below. The Mozex extension lets you not only edit textareas, but also assign shortcuts, view sources or choose the editor for mailto URLs. For some reason, using just emacsclient -e '(compose-mail "%a" "%s")' didn’t work for me, so i’ve created a simple shell script, gnumail:

    #!/bin/sh
    exec emacsclient -e "(compose-mail \"$1\" \"$2\")"

and told Mozex to use it. Don’t forget to set the variable mail-user-agent to something reasonable (for instance, since i use Gnus, i’ve got (setq mail-user-agent 'gnus-user-agent) in my configuration files).

Thanks, Victor!

Tab shuffling in emacs-w3m

Emacs-w3m comes with an excellent tab mode, which you can enable with (setq w3m-use-tab t) in your .emacs. C-cC-t creates new tabs, C-cC-w closes them and you can navigate then with C-cC-[np], or, my favourite, using a tab list via C-cC-s (if you didn’t know about this one, give it a try: it’s very useful).

But i quickly find myself with lots of open tabs that happen to be in the wrong order, and i wanted something akin to Firefox’s ability to move and reorder them around. I didn’t find a way change a tab’s position in a vanilla emacs-w3m, but it wasn’t that difficult to cook up my own solution. This function:

(defun jao-w3m-switch-buffers (dist)
    (interactive "p")
    (let* ((dist (if (zerop dist) 1 dist))
           (current (current-buffer))
           (current-no (w3m-buffer-number current))
           (next (progn (w3m-next-buffer dist)
  			(current-buffer)))
           (next-no (w3m-buffer-number next)))
      (with-current-buffer current
        (rename-buffer "*w3m*<*>")
        (w3m-buffer-set-number next current-no)
	(w3m-buffer-set-number current next-no)
        (w3m-pack-buffer-numbers))
      (switch-to-buffer current)))

switches the current tab with the one to its right (cyclically), or to the n-th to its right if you provide a numerical argument (e.g. M-3). I’ve got this function bound to C-cC-f:

(define-key w3m-mode-map (kbd "C-cC-f") 'jao-w3m-switch-buffers)

Alternatively, one could define a function that moves the current tab to the right a number of times. We can take advantage of the above function for a quick hack:

(defun jao-w3m-move-buffer (dist)
  (interactive "p")
  (let ((dist (if (zerop dist) 1 dist))
    (while (> dist 0)
      (jao-w3m-switch-buffers 1)
      (setq dist (1- dist)))))

and bind that to a shortcut of your choice. There you go: easy tab shuffling.

An illustrated Emacs tutorial

MIT’s SIPB IAP activities page is a treasure trove of interesting stuff. Among the many classes programmed for 2007, there’s one that i’m sure will be interesting to many mew readers: Being Productive With Emacs. The good news is that you don’t need to travel to Boston to get this excellent illustrated introduction to Emacs, continued with a customization and an extension guide. Being the slides of three presentations (by Phil Sung), they’re sketchy at places, but make for a great introduction to Emacsland nonetheless, and there’s a trick or two for seasoned users as well. Recommended.

Elisp and the Lambda Calculus

This one is only for hardcore emacsers who are into programming languages theory: a lambda calculus interpreter written in Elisp by Michael Olson. Enjoy!

Gnus miscellanea

GnusI’ve spent part of the morning setting up Gnus on my OS X, to replace Mail.app and ease synchronization with my GNU/Linux boxes. I won’t enter in the details of basic Gnus setup (see, for instance, the Gnus tutorial), but offer some customization tips, and mention two of my favourite Gnus-enhancing packages (which are not OS X-specific, by the way).

First thing was to import my Mail.app mailboxes. Easy. Open the mailbox in Mail, select all messages and execute the menu command Save as... (under File); in the save dialog, be sure to select Raw Message Source. The saved file is in mbox format, a Gnus import friendly one. All you need to get those messages into Gnus is to press B f in the *Group* buffer, providing your mbox file when asked. A new group is created with all your messages. You can, for instance, mark all of them and B c to copy them in, say, a regular nnml group.

Setting Gnus to read and send messages via Gmail was also easy following the instructions here. A caveat: you’ll need also the file ssl.el from the Gnus CVS repository to make POP3 work (and of course gnutls, which i installed via the fink package gnutls14; be sure to tell Emacs about your fink path: (add-to-list 'exec-path "/sw/bin")).

With that, I had a functional Gnus running and could move on to more interesting stuff. I don’t know about you, but i absolutely need random mail signatures, selected from my growing collection. To that end, i’ve been using randomsig.el since i can remember. Just put the file in your load path (or, if you’re on Debian, install the gnus-bonus-el package), and add to your initialisation files something along the lines of:

(when (require 'randomsig nil t)
  (define-key message-mode-map (kbd "C-c s") 'randomsig-replace-sig)
  (define-key message-mode-map (kbd "C-c S") 'randomsig-select-sig)
  (require 'gnus-sum) ; probably required for `gnus-summary-save-map'
  (define-key gnus-summary-save-map "-" 'gnus/randomsig-summary-read-sig)
  (setq randomsig-dir "~/Library/Emacs/Gnus/")
  (setq randomsig-files '("signatures.txt"))
  ;; or (setq randomsig-files (randomsig-search-sigfiles))
  ;; or (setq randomsig-files 'randomsig-search-sigfiles)
  (setq message-signature 'randomsig-signature)
  (setq randomsig-delimiter-pattern "^--"))

As you can see, i put my signatures in a file called signatures.txt. Now, every time i start editing an email, a random signature taken from this file is inserted for me. If i don’t like it, i can request another one typing C-c s. And if i’m reading anything inside and Emacs buffer and find a new text to add to my signatures.txt, M-x randomsig-message-read-sig will add the region marked in the buffer to my collection. Perfect bliss.

Mail.app comes with a sophisticated search engine based on Spotlight. But i’ve noticed that i never use the 95% of its features, not to mention that it’s annoyingly slow. What i really need is a simple and quick keyword search, and i’ve had it for many years now with Gnus and nnir.el. This package provides an Emacs/Gnus front-end to several indexers, my preferred one being namazu. So, besides getting nnir.el, you’ll need to install it. In Debian, it’s as easy as installing the corresponding package (namazu2), but, theres no namazu package to be seen in fink. Fortunately, you can compile it from source just by following the instructions: they worked in my MacBook without a glitch. With namazu installed, you can index your extant email in a breeze: create a directory to hold the index files (i use ~/Library/Caches/namazu) and type:

cd ~/Library/Caches/namazu
mknmz --mailnews ~/Library/Emacs/Gnus/Mail

(using the appropriate directories, of course), and the index directory will get populated. Now, with nnir.el in your load path, tell Emacs how to find those index files:

(when (require 'nnir nil t)
  (setq nnir-search-engine 'namazu)
  (setq nnir-namazu-index-directory
        (expand-file-name "~/Library/Caches/namazu"))
  (setq nnir-namazu-remove-prefix
        (expand-file-name "~/Library/Emacs/Gnus/Mail/"))
  (setq nnir-mail-backend (nth 0 gnus-secondary-select-methods)))

(i put this code in my Gnus configuration file). We are all setup: go to the *Group* buffer and type G G to search your emails. A temporal group containing the messages that match your query will be created for you, for your browsing pleasure. Inside that group, G T will teletransport you to the original message, in case you need to recover its context. As you’ll notice, namazu searches are at least an order of magnitude faster than Spotlight.

The only bit left is to set an entry in your crontab running mknmz for you periodically (i’ve got it set up with a period of one hour). Indexing is incremental, i.e., fast. Yeah, it’s not that immediate indexing of Spotlight or Beagle, but, you know, it’s not like i get a mail every five seconds, or that i need engine to find a mail i read twenty minutes ago.

There are many other tricks to play with Gnus, but i’ll save them for future posts :-).