Lexical Binding In Emacs

A while ago in Lexical Scoping In Emacs Lisp I wrote that Emacs 24 will (finally) introduce lexical binding. Trawling through the EmacsWiki today I came across a page, Dynamic Binding Vs Lexical Binding, that describes the differences between the dynamic binding that Emacs Lisp currently has and the (optional) lexical binding that’s available in Emacs 24.

It’s a good introduction for Elisp programmers who aren’t familiar with lexical binding. It looks at the advantages of dynamic binding and lexical binding and when it’s appropriate to use each. The big win with lexical binding is that it allows us to write closures. There’s an example of a closure on the page but I’m not sure it really makes clear how big a win they can be.

Suppose you need some counters in your program. You’d like to be able to give them an initial value and then increment them by any amount. You could write something like this (given that you have lexical binding):

(defun make-counter (initial-count)
  (let ((count initial-count))
    (lambda (n) (setq count (+ count n)))))

A call to make-counter will return a counter that you can use to track some count. If you call make-counter several times, each counter will have a separate count field so they won’t interfere with each other.

That’s a very simple example, of course, but it’s easy to imagine more complicated examples. The astute reader will notice that the counter is actually a sort object (in the OO sense) that has a single method. It’s easy to provide additional functionality–displaying the count, for example–so that you could have an object with several methods (loosely speaking). This fact is one of the reasons that I don’t use eieio (or CLOS in CL) very much. I get most of the benefits with a simple closure.

In any event, if you are an Elisp programmer and confused about lexical versus dynamic binding, this page is an excellent introduction and well worth a read.

Posted in Programming | Tagged , | 6 Comments

The Solution To Piracy

Paul Tassi has a nice aritcle in Forbes entitled You Will Never Kill Piracy, and Piracy Will Never Kill You. In it, he makes an observation that I think, while common, is right on the mark. That observation is that the vast majority of people who illegally download content do so because either the content providers will not sell it to them in the desired format or make it so hard to get it that it’s just easier to get it from Pirate Bay. The majority of people, he says, would be happy to pay for the content if the providers would just make it available. Consumers have become use to easy access to content and resent friction to their obtaining it. They don’t want, for example, to have to go to Best Buy to get a DVD; they want to be able to rent or buy a movie from their computer or home entertainment center.

He makes a pretty persuasive case that the only thing standing in the way of convenient tools for content delivery is that content providers are stuck in the last century and refuse to adjust their business models. They want to continue to continue to overcharge their customers for content in formats that their customers don’t want. To protect these business models they offer us SOPA and PIPA.

It’s a thoughtful piece and well worth a read. It’s too bad that the people who really need to understand its message will ignore it. While we’re at it, Julie over at Samurai Knitter makes much the same points but in a far more colorful way.

Posted in General | Leave a comment

Does Elisp Suck?

One of the links from the CategoryCode page that I wrote about yesterday is the Why Does Elisp Suck page. The idea is to give people a place to be grumpy about Elisp. My first thought was that, really, Elisp doesn’t suck—it’s a pretty nice language. I’d like to explore this question a bit more.

The answers to the “Why does Elisp suck?” question fall into two classes: the silly and the substantive. The silly answers are “because it’s not Scheme”, or “because it’s not Common Lisp”, or “because it’s not X” for your favorite language X. Are there better Lisps than Elisp? Of course. I’d argue that Scheme and Common Lisp are both better Lisps but that doesn’t mean Elisp sucks. If your X is something like Python or Ruby I’d wager that you aren’t really comfortable in Lisp but you’re certainly entitled to your opinion. On the other hand, preferring Python or Ruby or whatever also doesn’t mean Elisp sucks. In short, the fact that you’d rather code in a language other than Elisp doesn’t have very much to say about Elisp.

Of the substantive answers, only 3 are really of any concern to me. First and foremost is the lack of lexical scoping. That’s fixed (for certain values of fixed) in Emacs 24. The second problem is the lack of tail call elimination. Technically, Common Lisp doesn’t have this either—in the sense that it’s not in the standard—but as a practical matter all serious implementations of CL do eliminate tail calls. This matters to me because it enables a style of iteration that I prefer: see my rpl function for instance. Finally, there is no threading or concurrency. This bothers me less than the first two but it does mean that packages like gnus can slow things down.

Some of the other substantive problems with Elisp are no object system (a feature not a bug in my opinion), no module system (a pain because of name collisions but I can live with it), clumsy regular expressions (especially if what you really want are Perl regexps), and the API sucks.

Someone also noted that the Elisp implementation is slow. That’s true, I guess, but doesn’t really speak to whether or not Elisp sucks. It would be nice if things were snappier but on modern machines it seems fast enough to me. I’d guess that font locking and things like that have more to do with performance problems than the Lisp engine.

All-in-all, I find Elisp to be a comfortable language to write in. Not as powerful as Common Lisp and not as well designed as Scheme but still nice. What does everyone else think? Is Elisp something you put up with or do you like the language? I’d be surprised if very many people said it was their favorite language but I’d also be surprised if many Elisp users really do think the language sucks.

Posted in Programming | Tagged , | 12 Comments

Learning Elisp Idioms

I’ve said before that Emacs Lisp is pretty much like any other Lisp except for its library, which is specialized for editing tasks. One of the things I like about Xah Lee’s Emacs Lisp Tutorial is that it doesn’t concern itself too much with Lisp itself but presents Elisp use cases and idioms. If you already know a little of any Lisp, then Xah’s tutorial is a good place to go to come up to speed with Elisp.

Today, I found another good resource: The CategoryCode page on the Emacs Wiki. They’ve got a long list of articles that show you how to do various tasks in Emacs Lisp. One page, the Elisp Cookbook, is particularly helpful. It contains a collection of code snippets for many common chores. Other articles are more specialized; they have, for example, an article on a Password Generator. Some of the articles are quite detailed while others merely describe a library that’s good for doing some task or another.

If you’re new to Elisp but have a basic knowledge of Lisp, I’d start with Xah Lee’s tutorial and then try some of the articles on the CategoryCode page. Of course, the definitive source is the GNU Emacs Lisp Reference Manual, which is also available in the built-in documentation. If you need to know the particulars about some function or slightly broader area, that’s the place to look but I don’t find it particularly useful for learning the ins and outs of Elisp. The other outstanding resource you have for that is Emacs itself. You might try reading simple.el, for example, to see how some of the simple editing functions are implemented. It will give you a good feel for Elisp idioms.

Posted in Programming | Tagged , | Leave a comment

TSA: So Incompetent It Surprises Even Me

We here at Irreal, like almost everyone else in the world, are not fans of the Transportation Security Administration. Their ability to combine heavy-handedness with incompetence exceeds even that of the typical DMV office. Still, the sheer assclownery of their latest exploits is so astounding that even I wouldn’t have believed it possible.

I couldn’t begin to give this story justice so head on over to Schneier on Security for the sorry tale. As Schneier says, “I don’t even know where to begin.” The link at the top of Schneier’s post goes to a CNN report that gives a few more details.

Posted in General | Tagged | Leave a comment

Digital Lysenkoism

Cory Doctorow has a splendid article in Publishers Weekly about the futility of DRM in the publishing industry. In it he compares the industry’s belief in DRM to the disastrous beliefs of Trofim Lysenko, the Stalin-era Russian pseudo-geneticist, that led to mass starvation in the Soviet Union. In some sense, there’s not much point in telling this to Irreal readers: they all know the arguments and despise DRM already. Still it’s a good article and points out some interesting facts.

Most of the line staff at the publishers know, like Stalin-era geneticists knew about Lysenkoism, that DRM won’t work and that it’s really hurting the publishing industry while failing to do anything to stop illegal copying. The problem is that their leaders, like Stalin, do believe in it and refuse to consider the possibility that their faith in DRM might be misplaced.

Doctorow tells the story of how one publisher is planning on sending all its ARCs out as encrypted PDFs. ARCs are the Advanced Reader Copies that publishers send to reviewers before the book is actually published in the hopes of getting a good review. As Doctorow remarks, most reviewers get far more requests for a review than they can honor (Doctorow says 100-to-1 in his case) and that the idea a reviewer would put up with the annoyance of dealing with a “book” that couldn’t be loaded into whatever e-reader he happens to use or couldn’t be conveniently read on a plane, say, is wishful thinking. Instead, those crippled PDFs will be ignored and the publisher and author will miss out on advanced publicity.

Again, there’s nothing in the article that we don’t all know but it’s a good take on the problem with some interesting inside information. Well worth a read.

Posted in General | Leave a comment

Fontifying Code Buffers In Emacs Org Mode

I just learned a nice trick from the Org Mode Mailing List. By default code blocks in Org-mode buffers are colored gray and don’t show the fontification the way it does in a normal programming buffer. But if you add

(setq org-src-fontify-natively t)

to your .emacs or init.el file you get normal fontification for whatever language you’re using. This is especially nice when you open an editing buffer with【Ctrl+c '】 to insert code into the #+begin_src ... #+end_src area.

Posted in General | Tagged , | Leave a comment

TechCrunch: Amazon Is Out To Kill Publishers

Regular Irreal readers know that publishing and its travails are an enduring interest of mine. As I’ve said many times, I like publishers and wish them well but at the same time I recognize that they are making the same tragic mistakes that the music industry did. Unfortunately, they are less well positioned to survive them.

Case in point: TechCrunch has an article claiming that Amazon is purposely killing off rival publishers by giving authors huge advances and very favorable deals with the intention of driving the traditional publishers into bankruptcy. Although it’s not well known, Amazon is into publishing in a big way and has several of its own imprints. TechCrunch goes on to say that Amazon enjoys a number of benefits such as inexpensive print-on-demand facilities and, of course, the Kindle installed base.

All-in-all, the TechCrunch article is pretty depressing not only for the publishers but for those of us who think it a good thing for there to be more than one publishing outlet. It is, I think, dangerous to have only one source of published works but that’s the direction we’re headed in. I hope the traditional publishers can adjust to the times and improve their prospects but I have to say that I am less hopeful than ever.

Posted in General | Leave a comment

Journal Article On Org Mode

The Journal of Statistical Software has a nice paper by Carsten Dominik (the creator of Org mode), Eric Schulte, Dan Davison, and Thomas Dye about Org Mode and its use in Literate Programming and Reproducible Research.

I really like the idea of reproducible research and this paper is an excellent example of it. The authors make available, as supplementary material, everything you need to reproduce the paper, it diagrams, and examples. They also have another example in the paper of reproducible research that makes use of large external sources of data.

Although the paper is published in an academic journal (and one concerning statistics, at that) it is very approachable and anyone interested in learning some of the power of Org mode should give it a read. They cover using Babel to make calculations whose results appear in the paper automatically. So although the code blocks do not (necessarily) appear in the finished paper they are part of the “source code” of the paper and available for anyone interested in verifying the results. Not all of us are generating papers that others might want to verify but the facility is still useful to us. For example, all the diagrams and calculations that appear in my blog posts were generated with Babel and are part of the Org file for the post so I can reproduce them at will. The only exceptions to this are the occasional screen shots that I sometimes include in a post.

I’ve never been able to warm up to Literate Programming but for those who have, the paper shows how Org mode implements it using the Noweb notation. That’s very nice because it’s language agnostic—you can use it with any language. You can discuss your program by presenting code fragments in any convenient (to your exposition) order and then have Org mode export the reassembled code ready for a compiler.

There’s a lot of power in Org mode and I’m always learning new things about it (much like Emacs itself). This paper is a good introduction to some of its features and because the original Org file and supplementary files are available it serves as a laboratory for experimenting with it.

Posted in General | Tagged , | Leave a comment

Emacs Rocks Episode 9 Is Up

Magnar Sveen has another episode of his excellent Emacs Rocks up. In this video he talks about expand-region, an Emacs package that he wrote to help with marking text in an intelligent way. The video is less than 3 minutes so there’s no excuse not to take a look. As with all the Emacs Rocks videos, you won’t be sorry.

Watch the video and if you like what you see, get the code on github.

Posted in General | Tagged | 1 Comment