Symbols in Emacs Lisp

One of the hardest things for Lisp newcomers to understand is symbols. They look tantalizingly like variables from other languages and, indeed, they are often just that. But, as the new Lisper soon discovers, they are much more.

Xah Lee just published a very helpful introduction to symbols in Emacs Lisp. His tutorial covers what symbols are and how they work in the context of Emacs Lisp. He also covers some of the functions that work with them and let you access the objects they contain. There is a companion page in which Lee shows you how to check if a symbol is bound to objects of various types.

If you’re trying to learn Emacs Lisp, these pages will help clear up some of the things that I found most confusing when I started. These pages are a welcome addition to Lee’s Emacs Lisp Tutorial series.

Posted in Programming | Tagged , | 1 Comment

C++

The funniest—or is it saddest—thing I’ve read about C++ in a long time.

Posted in Programming | Tagged | Leave a comment

Two From Phil

Regular reader and commenter Phil has made a couple of good observations in the comments of recent posts that I think are worth bringing to everyone’s attention. Since not everyone follows the comments, I decided to mention them in a separate post.

In the Naming Registers post, Phil mentions winner-mode and says that you’re really missing out if you don’t use it. When I replied that I had never understood the point of winner-mode, he explained further why it’s so useful. The basic idea is that it’s like Magnar Sveen’s Magit hack to restore your window configuration after quitting Magit but it works anyplace. I’m going to give it a try and will probably write up my impressions later.

In response to my More isearch Goodness post, Phil mentions that you can get extensive help—including all the available bindings—by typing 【Ctrl+h Ctrl+h】 while in isearch. That’s handy if you forget a key sequence or just to see what’s available.

Thanks to Phil for a couple of handy tips.

Posted in General | Tagged | 1 Comment

Publishing Code in a Blog Post

As regular Irreal readers know, I prepare all my blog posts with org2blog. One of the nice features of that is that any code I include in a post is automatically fontified in a language appropriate way. Recently, both Xah Lee and Magnar Sveen have published short pieces on how they include source code in their blog posts. They both use a strategy of calling htmlize to do the heavy lifting of fontifying the code.

Sveen’s method is specialized for his What the .emacs.d!? blog. He calls htmlize-region to mark up the selected code with appropriate <span> tags for use with CSS. The result is inserted into a template for the blog post and written to a buffer associated with a file for that post. It’s simple, direct, and purpose-built for writing What the .emacs.d!? blog posts. He can mark some code from his configuration files, call what-the-emacsd-post to htmlize it and insert the result into a post template ready to be completed and published.

Lee’s solution is more comprehensive. He writes about many languages so his strategy is different. With his code you put the cursor between the <pre> tags for code you want to include and call htmlize-pre-block. The language of the source code is extracted from the <pre> tag and then the code in the pre block is inserted into a temporary buffer that is set to the appropriate mode according to the language extracted from the <pre> tag. Next htmlize-buffer is called to mark up the code and the result replaces the original code between the <pre> tags.

This is a nice solution because it can handle a large range of languages and is reasonably flexible. Notice that Lee’s method assumes you already have the code in a pre block and that you just want to fontify it. In a sense, it’s the reverse of what Sveen does: extract the code from a source file and insert it into a new HTML file.

Both of these solutions are useful. Which one you should use depends on your work flow and whether you are dealing with one language or many. For my part, I have always written my posts in Org mode. Before I started using WordPress and org2blog, I wrote my posts in Org mode and called

(defun blog-post ()
  "Export an org buffer as HTML but only export the body."
  (interactive)
  (org-export-as-html 3 nil nil "*blog-post*" t))

to turn the whole buffer into HTML. Then I pasted that into the Blogger control panel to publish the post. A little clunky but much better than typing in posts to the control panel.

I like to see how other bloggers handle the writing and publishing of their posts so I always enjoy posts like Lee’s and Sveen’s. Besides, it’s always nice to see Emacs in action.

Posted in General | Tagged | Leave a comment

More isearch Goodness

In A Nice Emacs Trick, I wrote about how you can enter query-replace or query-replace-regexp directly from isearch and use the isearch search string as the FROM parameter to query-replace or query-replace-regexp. It turns out that there’s a lot more isearch goodness available.

Steve Purcell, who I first talked about in Zap Up To A Character, has a nice collection of isearch enhancements. One of these merely drew my attention to functionality that’s already there but hardly anyone knows about: you can call occur directly from isearch using the search string for the input to occur by calling isearch-occur. That’s bound to 【Meta+s o】 by default but Purcell binds it to the easier 【Ctrl+o】:

(define-key isearch-mode-map (kbd "C-o") 'isearch-occur)

Another common use case is wanting to end the isearch on the other side of the match. For example, if you are searching forward but want to put point before the match instead of after it you have to do something special to get it there. With a single word for the match,【Meta+b】 does the trick but more complicated matches (multiple words, say) aren’t as easy. Purcell provides isearch-exit-other-end to do this and binds it to the very sensible【Ctrl+Return】:

(define-key isearch-mode-map [(control return)] 'isearch-exit-other-end)

I’ve already added both of these to my init.el.

There are a couple of other enhancements in Purcell’s init-isearch.el file so be sure to head over and take a look. Perhaps you’ll want to add some of the others.

Posted in General | Tagged | 2 Comments

Lispdoc

The other day I wrote about discovering Steve Purcell’s emacs.d repository on GitHub. Today, I want to talk about another wonderful thing I discovered there: Lispdoc. Bill Moorier’s Lispdoc is a documentation search engine for Common Lisp that searches

  • The Hyperspec
  • CLtL2
  • Practical Common Lisp
  • Successful Lisp
  • On Lisp
  • Paradigm of Artificial Intelligence Programming
  • ANSI Common Lisp
  • The docstrings from SBCL
  • A bunch of ASDF libraries

This is a really great documentation resource. If you’re a Lisper, go on over there right now and type in a CL symbol and see what happens. Amazing.

Purcell uses a bit of Elisp, which he got from Bill Clementson, to automatically look up a symbol from Emacs. By default, it will look up the symbol at point, but you can enter any symbol you like. Very nice. Sadly, Clementson isn’t blogging anymore but his site also has a bunch of Lisp related material.

Update: a → and

Posted in Programming | Tagged , , | Leave a comment

Naming Registers

Magnar Sveen has another great post on What the .emacs.d!?. This time it’s a nice hack to make magit run in the full frame and then restore the window configuration that existed when magit was invoked. I’ve been annoyed several times by having magit mess up my window configuration so I added Sveen’s bit of Elisp to my init.el. But then I got to thinking: Sveen’s code saves the window configuration across the call to magit-status with

(window-configuration-to-register :magit-fullscreen)

At first look this is merely the expected code to do that sort of thing but what about that :magit-fullscreen? Registers are named with a single character so what’s going on with the :magit-fullscreen?

It turns out that this works because of an implementation detail in register.el. The list of active registers is kept in an alist that is searched with assq. Since identical symbols are eq in Elisp, using a symbol for the register name works. Of course, you can’t do that interactively because the interactive declarations for the register functions specify a character input for the register name.

One could argue, I suppose, that naming a register with a symbol is taking advantage of undocumented behavior that could change in the future but that risk seems minimal. The benefit, on the other hand, is large. By using a symbol to name registers in Elisp code you avoid the risk of clobbering a register that the user may have set interactively. So Sveen’s post provides not only an improvement to the behavior of magit-status but also teaches us a useful technique in Elisp programming.

Posted in Programming | Tagged | 3 Comments

A Nice Introduction to mu4e

Over at Zmalltalker, Marius Mårnes Mathiesen has a very nice post on the mu4e email app for Emacs. Mathiesen, who’s used many different email programs says that mu4e is the first one that he’s ever been happy with. To the question, “Should I start using it?” he answers, “If you use email, the answer is yes.”

The post is a good introduction to mu4e and gives you an idea of how it works and what its interface looks like. The other two links above are to the Emacs-Fu posts about mu4e. If you’re dissatisfied with your current mail app (or even if you aren’t) you should definitely take a look at these posts. Mu4e may be just what you’re looking for.

Posted in General | Tagged | 1 Comment

Silly Arguments

It’s time, again, for me to be grumpy and start waving my cane at those damn kids playing on my lawn. Some of you, ever solicitous of my mental health and well being have advised me not to care what the unenlightened think or publish but, alas, I can’t help myself. This time it’s Shane Panter over at Foundation Code who’s raising my blood pressure.

Having read Paul Graham’s Hackers & Painters, Panter takes issue with Graham’s assertion that Lisp gave him and Robert Morris a strategic advantage with their startup Viaweb. Really, he says, it’s just that Graham was a really smart guy and would have succeeded no matter what language he used. Furthermore, “way back then1” they didn’t have modern languages like Java, Python, PHP, and Ruby so his only choices were C++, Perl, or Lisp. He couldn’t really use C++ because it hadn’t been standardized yet so that left only Perl and Lisp and since Graham2 and Morris3 were from MIT they naturally had a bias towards Lisp.

There’s no doubt that Graham is a smart guy but the rest of Panter’s assertions are nonsense. We know this because Paul Graham himself tells us. Putting aside the arrogance of telling the admittedly smart Graham that he was mistaken in his belief that Lisp provided a strategic advantage, let’s just look at the languages available to Graham and Morris when they started Viaweb. Far from being unavailable or unsuitable, C++ (and Perl) were what all Viaweb’s competitors were using. So they could have used C++ if they wanted. And, it turns out, they did use C and Perl for parts of the system so the choice of Lisp appears to be a well thought out strategy not simply all that was available. The idea that they would have used Java or PHP, if available, is snortworthy.

And why wouldn’t you want to use Lisp, even today? Well, according to Panter,

  1. It’s old technology.
  2. It has too many incompatible implementations.
  3. It has a crazy, hard to read syntax4.

I’m not sure what being old technology has to do with anything. Presumably Panter enjoys a beer now and then and beer making technology is even older than Lisp. As for having many implementations, why isn’t this a good thing? Many of those implementations are targeted at niche areas such as embedded applications or those desiring to run on the JVM. And being Common Lisp, they aren’t incompatible except in additional tools and extensions they support. Again, this provides programmers a choice within Common Lisp and should be viewed as a good thing.

And don’t get me started on crazy, hard-to-read syntax. We Lispers don’t find it the slightest bit crazy and we certainly don’t find it hard to read. It’s an—unintended, I’m sure—irony that Panter positions himself as a perfect example of Graham’s famous Blub programmer with this statement.

I don’t know Panter and his site has only two posts so it’s difficult to judge his knowledge and expertise. As always, I’m inclined to give him the benefit of the doubt and his overall point—technology decisions shouldn’t be based on religion—is one we all agree with. I do wish, though, he’d get his facts straight and not presume to tell Paul Graham that, no, he isn’t correct in believing that Lisp gave him an advantage. After all, Graham has demonstrated just that many times. Panter’s free to ignore Lisp in favor of “modern languages” but it would be nice if he’d refrain from telling those of us who prefer Lisp that we’re merely deluded by our religious beliefs.

Footnotes:

1 1995

2 Actually Graham never attended MIT. His undergraduate degree was from Cornell and he got a Masters and Ph.D. from Harvard.

3 Morris is a professor at MIT but his degrees are from Harvard. He also did some graduate work at Cornell. He joined the MIT faculty after the sale of Viaweb.

4 This assertion has been removed from the post (or perhaps I just imagined it).

Posted in General | Tagged , | Leave a comment

Zap Up To A Character

I was browsing Magnar Sveen’s excellent What the .emacs.d!? blog and came across a post on elisp-slime-nav-mode, a mode that brings a bit of Slime-like navigation to Elisp. It’s not part of the Emacs distribution and Sveen didn’t say where it comes from (it’s available on MELPA) but after a little investigation I discovered that it was written by Steve Purcell. That lead me to Purcell’s emacs.d repository on Git Hub. What a find! Purcell has a ton of really good ideas and code. Doubtless I’ll be discussing some of them later but today I want to talk about zap-to-char.

Everyone agrees that zap-to-char is really useful but at least half the commentary about it complains that it zaps up to and including the specified character. Much more useful, these complaints say, would be to zap up to but not including the character. It turns out that the stock Emacs distribution does include a function that does just that: zap-up-to-char. The problem is that it’s in misc.el, which is not loaded by default, and does not have an autoload associated with it.

Purcell fixes that by simply providing an autoload and then binding zap-up-to-char to 【Meta+z】 and zap-to-char to 【Meta+Z】 on the grounds that zap-up-to-char is more useful and should have the easier key sequence. I’m used to having 【Meta+z】 be zap-to-char so I did it the other away around:

(autoload 'zap-up-to-char "misc"
  "Kill up to, but not including ARGth occurrence of CHAR.")
(global-set-key (kbd "M-Z") 'zap-up-to-char)

Now you can have the best of both worlds. Be sure to check Purcell’s repository out. It’s full of great tips and tricks.

Posted in General | Tagged | 4 Comments