Define your own keywords

These days i’m programming in and learning about Objective-C and Cocoa. Yesterday, i stumbled upon a blog entry providing The Cocoa Memory Management Regular Expression, that is, a simple regular expression that matchs (most of) the OpenStep functions allocating memory that must be explicitly de-allocated. Here’s the regexp in question:

^retain$|^(alloc|new)|[cC]opy

An immediate application of this regexp is, of course, providing some sort of visual warning to remind you that memory allocation is at play. In Emacs, a way to do that is to fontify the keywords in question using an special face (font-lock-warning-face, for instance). As it happens, this is very easy to do: just call font-lock-add-keywords with the appropriate parameters:

(font-lock-add-keywords 'objc-mode
   '(("\\\\<retain\\\\>" 0 font-lock-warning-face)
     ("\\\\(\\\\<\\\\(alloc\\\\|new\\)\\\\w*\\\\)[]:]" 1 font-lock-warning-face)
     ("\\\\(\\\\w*[cC]opy\\\\w*\\\\)[]:]" 1 font-lock-warning-face)))

Each entry in the association list above consists of a regular expression, the group inside it to be hightlighted (0 means the whole match) and the face used for the highlighting. C-h f font-lock-add-keywords RET for extended help, including further examples (for instance, to highlight your to-dos).

Oh my gawd, it’s full of links!

I’ve been using emacs-muse during several years now to keep a personal knowledge base in the form a local wiki. To me, the killer feature of emacs-muse is its ability to create links to other documents, mails, URLs or any other resource accessible via Emacs (which is to say, virtually any resource). Combined with planner mode, one has a pretty good PIM system that, among many other virtues, is based on plain text files (and, therefore, is easily indexable) and is as portable as Emacs itself.

Emacs-muse is pretty neat, but not perfect. My main quibble with it is that it’s a major mode. That means that, in order to create a link to other resource, you must be in a ‘muse’ document (a sort of wiki page). But wouldn’t if be nice if you could create links in any document. Say you’re writing source code an want to reference another file in the project, or an HTML page detailing an API you’re using, or a PDF with your projects requirements. Or you’re writing an essay in LaTeX and want to link to a text file of related notes, or an email, or… you get the idea. What I’m asking for is, in emacs parlance, a minor mode providing the linking functionality.

As it often happens in the Emacs universe, my prayers have been heard. Linkd is exactly the package i wanted. In the words of its author, David O’Toole, “linkd-mode is a hypertext system for organizing and interlinking all resources available to GNU Emacs. It can be used to make a “personal web” out of all your text files—regardless of file format or major mode.” In addition, linkd trims off some of the features I rarely use in emacs-muse, namely, the publishing functionality.

Although still in alpha, linkd looks already pretty good, with such niceties as proxy icons in links, as shown in this picture:

Linkd in action

Other interesting features of linkd include its ability to mark blocks inside any file that can be link targets, and its tagging functionality. In addition, the list of planned features (searching and indexing, summarize, synthesize links a la remember-mode) makes linkd a package definitely worth exploring and keeping an eye on.

The Ghost in the Lisp Machine

A friend of mine uses to say that Emacs fills our yearning for a Lisp Machine. I tend to agree with him: Emacs is not just an editor, but a full integrated environment where you can perform virtually any imaginable task; and, most importantly, the inner workings of the system are open to you to explore and extend. Using, for extra fun, Lisp. No, i don’t think that Elisp is the nicest Lisp incarnation around, but is far better than, say, C, and i still prefer it to other scripting languages. Moreover, the awesome range of libraries at your disposal makes up for many of the deficiencies in the language.

Living in Emacs is addictive. Imagine an operating system where you can switch from writing code to browsing the web or chatting without leaving a consistent environment, with the same set of commands and shortcuts. Imagine a set of integrated applications where data is seamlessly shared, where any single functionality can be tweaked, extended and adapted to your particular needs. Where everything is easily scriptable. Imagine, in additon, that the environment provides powerful and complete interactive self-documentation facilities with which the user can find out what is available. I have yet to find an operating system providing such an integrated environment. Not even Mac OS X, where AppleScript support is often lacking and system services are underused.

Of course, the key ingredient here is Emacs’ extensibility. Far from being an afterthought or simply one of its features, extensibility is the central aspect of Emacs’ architecture. Actually, the whole point of this post is to recommend you reading Richard Stallman’s 1981 essay EMACS: The Extensible, Customizable Display Editor, which explains much better than I could the strong points of Emacs design, i.e., those traits that make Emacs more, much more, than just an editor. From the horse’s mouth:

Extensibility means that the user can add new editing commands or change old ones to fit his editing needs, while he is editing. EMACS is written in a modular fashion, composed of many separate and independent functions. The user extends EMACS by adding or replacing functions, writing their definitions in the same language that was used to write the original EMACS system. We will explain below why this is the only method of extension which is practical in use: others are theoretically equally good but discourage use, or discourage nontrivial use.
[...]
User customization helps in another, subtler way, by making the whole user community into a breeding and testing ground for new ideas. Users think of small changes, try them, and give them to other users–if an idea becomes popular, it can be incorporated into the core system. When we poll users on suggested changes, they can respond on the basis of actual experience rather than thought experiments.

The article goes on explaining the organization of the Emacs system, how it depends on its interpreter, Elisp’s main features and how built-in self-documentation is provided. Also interesting is the list of related systems at the end of the essay: Lisp machines, LOGO, MacLisp and Smalltalk. We’re definitely in good company!

The Lord of Elisp

Knuth’s Tex for the Math-kings of sigma, and pi,
Unix vim for the Server-lords with their O’Reilly tomes,
Word for Mortal Men doomed to die,
Emacs from the Bearded One on his Gnu throne,
In the land of Stallman where free software lies.
One Emacs to rule them all. One Emacs to find them,
One Emacs to take commands and to the keystrokes bind them,
In the land of Stallman, where free software lies.

(Raffael Cavallaro, gnu.emacs.help)

Posted in Fun. 3 Comments »
Follow

Get every new post delivered to your Inbox.