A Cool Common Lisp Tip

I wrote the other day about Zach Beane’s new Common Lisp Tips blog. Yesterday, he had a really nifty tip that solves a common problem. We’ve all had situations where we want to output something like

Processed 1 file.

or

Processed 23 files.

Some people are lazy and you end up with

Processed 1 files.

which I always find jarring. In Emacs Lisp we might solve this problem with something like

(print (format "Processed %d file%s" n (if (> n 1) "s." ".")))

and something very similar involving a printf in C.

Common Lisp has a special format directive that takes care of this automatically. What do you expect, after all, from a language that even has a directive for outputting numbers in Roman Numerals? This is Beane’s tip so I’ll direct you to his post for the details. If you’re a Lisp programmer and, like me, you didn’t already know about this, you should definitely head over and take a look.

Posted in Programming | Tagged | Leave a comment

Inserting Quotation Marks In Emacs Org Mode

As I was writing yesterday’s post I started to enter \rdquo{} to get an opening double quotation mark for what seemed like the millionth time. I suddenly realized that this was really stupid and a waste of time so I cooked up a small amount of Emacs Lisp to do the job for me. Before I start, let me state for the record that I know about yasnippet and know that I can do the same thing with it. The thing is, I don’t have it installed and I don’t think I’d use it for anything other than implementing the quotation mark template so a tiny bit of elisp seemed like a better solution.

The code itself is very simple

(defun dq ()
  "Insert double quotes in an org buffer."
  (interactive)
  (insert "\\ldquo{}\\rdquo")
  (backward-char 6))

When dq is called, the string \ldquo{}\rdquo is inserted into the buffer and the point is moved backwards 6 characters so that it is between the two quote commands. All I need do then is type the material to be quoted and do a 【Meta+f】 to jump over the \rdquo.

I thought about just having dq insert “” so that I’d have a more general solution but when I tested that idea the Org mode exporter (or Emacs, I’m not sure which) got confused about the character encoding and asked me some questions. Rather than deal with that every time I wanted to insert quotation marks, I just stuck with what I’ve always done but with a little automation.

I’m sure there are other ways of addressing this problem in Emacs but I’m a programmer and the problem looked like a nail to me so I hit it with my favorite hammer. Feel free to cure me of my ignorance in the comments.

Update: Added missing “I”

Posted in Programming | Tagged , | 2 Comments

PostScript And PDF

I’ve read several times that PDF is “just compressed PostScript” but I recently learned that that’s not true. David Evans has a nice article over at Adobe on PostScript vs. PDF.

Evans starts off by describing what PostScript is and how it is used. Like most of my readers, none of that came as a surprise—I’ve even written some simple PostScript programs so I’m pretty familiar with it. The interesting part for me was his description of PDF and how it differs from PostScript. In addition to describing the words on a page, PDF contains other information such as fonts, images, hyperlinks, keywords, and so on. More important, however, is that the PDF file has already been processed by a Raster Image Processor and made into objects that can be viewed on screen. This has the added benefit that it gives a very clear indication of what the object will actually look like when it’s printed.

Evans writes that he believes more people will begin to deliver PDF files to printing services as better tools become available. That’s already happening. Between my first and second books, my publisher switched from asking for PostScript to asking for PDF to deliver to their printing vendor. I used to send all my memos, reports, and papers as PostScript and sometimes people (especially non-Unix people) would be unable to read them. Now, like everyone else, I send PDF and everyone can read them—even the printer.

Unless you’re already familiar with PostScript and PDF, this article is a useful introduction that’s worth a read.

Posted in General | Tagged | Leave a comment

Two Great New Lisp Blogs

Recently, Zach Beane and stassats have started new blogs about Common Lisp and Slime. Beane, the well-known Xach, is writing the Common Lisp Tips blog, which promises a new Common Lisp tip every day. As of today, there are only 3 tips but they are all useful and interesting. The second tip is a pointer to the second blog, Slime Tips.

Slime Tips discusses little known features of Slime. Like Common Lisp Tips, the blog has only 3 tips so far. These tips are definitely for someone who’s familiar with Slime basics and wants to explore some of the more esoteric features.

Both these blogs look promising and are worth adding to your news feed if you are a Lisp/Slime user.

Posted in Programming | Tagged , | Leave a comment

Emacs As A Lisp Machine

Xah Lee has an update to his Emacs and Unicode Tips page that I’ve written about before on my old blog. This is a great reference that tells you how to deal with Unicode and UTF-8 in Emacs. One of the new parts that I really like is how to enter a Unicode character by its decimal number. I like this not because I want to do it—I’m perfectly happy using the 【Ctrl+x 8 Entername-or-hex-number method—but because of the way it works and what it tells us about Emacs.

Lee tells us that to input a λ character by its decimal number, 955, you type (ucs-insert 955) into the text and then evaluate it by typing 【Ctrl+x Ctrl+e】 to insert the lambda character. Of course, now you have to delete the (ucs-insert 955) so the method isn’t really that practical.

What I like about it is what it tell us about Emacs. It’s easy, and nominally correct, to think of Emacs as an editor that has elisp as an extension language but Emacs is really more. It’s more accurate to think of it as a lisp machine that’s specialized for editing functions. With that view, you’re always typing into a REPL of sorts where each “normal” character that you type runs the command self-insert-command. You can run other commands by using various control sequences or other special keys. Lee’s method shows both these functions. First you type in the string (ucs-insert 955) and then you use 【Ctrl+x Ctrl+e】 to run eval-last-sexp.

With this view, elisp isn’t an extension language but an instance of a running Lisp interpreter that knows a lot of commands useful for editing and whose REPL (or more accurately reader) behaves a little differently than other Lisps. The point is that it expands your view of what Emacs is and allows you to imagine new and non-intuitive ways of doing things.

By the way, if you must enter a Unicode character by its decimal number, an easier, but similar, way is to type 【Meta+:(ucs-insert 955) or whatever number you need.

Posted in General | Tagged | 7 Comments

Power Update

Two weeks ago I wrote about power management in electronic devices. The thrust of that post was that because battery technology hasn’t advanced very much it was necessary to work on the problem at the other end by making our electronics more efficient in their power use. Now the Lawrence Berkeley National Laboratory has announced a dramatic improvement in lithium-ion batteries. As most of us know, there are two problems with the lithium-ion batteries that power cell phones, computers, and other electronic devices: they can’t hold a lot of power—hence the inability of many smart phones to get through the day on a single charge—and they lose energy storage capacity after a number of charge-discharge cycles.

In a recent press release the Berkeley labs announced a dramatic improvement in both of those problems. The batteries store energy by attracting lithium ions onto the anode of the battery. This causes the anode to swell and then shrink again when the power is consumed. Because the current anodes are typically made of graphite, the constant swelling and shrinking breaks the electrical contacts in the anode and the battery is unable to hold a charge. The Berkeley folks are using a new polymer that hold up to 8 times the number of ions and is not damaged (as much) by the swelling and shrinking. After a year of testing and many hundreds of charge-discharge cycles the new batteries have held their increased energy capacity. As a bonus, the new polymer is economical.

The press release has lots of technical details and is well worth a read. We may, finally, be on the verge of batteries that hold their charges longer and don’t wear out as quickly. That combined with the improvements in the efficiency of the electronics may end the battery hassles that we all endure now.

Posted in General | Tagged | Leave a comment

Google Contacts And Emacs

As a dweller in the Apple ecosystem (or walled garden if you prefer) I enjoy a shared and automatically synced contacts list across my Macs, iPhone and iPad. Therefore I don’t bother with Google contacts. Many people do, though, and for the Emacs users among them Julien Danjou has put together a nice Emacs extension that allows you to access your Google contacts from within Emacs. The package also integrates with Gnus so you can use it instead of the Big Brother Database.

Danjou says that it probably needs Emacs 24 because of its use of oauth2 but as I wrote on Monday and before, Emacs 24 is now in pretest and several readers have reported that it is very stable and solid. If you were looking for an excuse to jump into Emacs 24 and you use Google Contacts, this could be your opportunity.

Update Updated link.

Posted in General | Tagged | 2 Comments

Scheme Versus Common Lisp

A lot has been written about Scheme versus Common Lisp and the subject has achieved holy war status much like that of the Emacs versus Vi crusades. I use and am happy with both. I like that Scheme is a Lisp-1 and (theoretically) like that it’s a “simple” language. Scheme’s continuations are a very powerful and useful feature. I very much prefer Common Lisp macros and I like its large and standardized library. Don’t ask me how I reconcile my love of Scheme’s simplicity with my love of the large command set of Common Lisp.

So while I’m not interested in enlisting in either side’s army, I do enjoy reading why people prefer one or the other. In that spirit, I can recommend this oldie but goodie from 1998 by Philip Greenspun on Scheme vs. Common Lisp. I’ve seen it before but was reminded of it when I saw it mentioned on one of my news feeds the other day. It’s a funny story but even better are the replies to it. If you’re a Lisp programmer of any denomination you’ll probably enjoy it. If you’re a noob trying decide which Lisp to learn I’m afraid you won’t find much enlightenment but you may enjoy the spectacle.

Posted in Programming | Tagged , | Leave a comment

First Emacs 24 Pretest Is Available

Chong Yidong is reporting that Emacs 24.0.90, the first Emacs 24 pretest, is available for download at ftp://alpha.gnu.org/gnu/emacs/pretest/emacs-24.0.90.tar.gz. As usual, the changes are listed in etc/NEWS.

In the comments to my post about Emacs 24 features, several of the commenters said that they were using the development version of Emacs 24 and that it was “rock solid.” Thus there’s no reason not to give this pretest a try if you’ve been waiting for any of the new features in Emacs 24. If you don’t know what those features are and want a nice summary, take a look at this post by Bozhidar Batsov.

Posted in General | Tagged | Leave a comment

SICP For The Kindle And iPad

Long time readers know that I am a big fan of Abelson and Sussman’s Structure and Interpretation of Computer Programs and that I consider it the best book ever written about computer programming. I have the on-line version permanently bookmarked in my browsers and, of course, I have a PDF version on my computers.

Today I came across Jonathan Patt’s sicp-kindle repository on github, so of course I downloaded it and after some Googling around I discovered how to load it into the Kindle app on my iPad1. I like the iBooks app slightly more than the Kindle app so I looked around to see if someone had SICP in ePub format.

It turns out that Ian Eure has SICP in ePub format in another github repository. From the README files it appeared that the Kindle version was in better shape but I downloaded the iBooks format too so that I could compare them. I couldn’t see a lot of difference with a quick skimming so either version should be fine. Of course, if you have a Kindle, you’ll want the .mobi version.

I’m not sure that I’ll ever need to check SICP when I’m away from my computers but if I do it’s nice to know that I’ve got a copy tucked away on my iPad. Having all those copies of SICP lying around on my computers probably sounds a little obsessive but there’s also a .info version for reading in Emacs that I don’t have so my addiction is still under control. Mostly.

Footnotes:

1 The easiest way is to email the .mobi file to yourself and then open the email with the iPad mail app. Clicking on the Kindle icon in the email will open the book in the Kindle app and save it to your Kindle library.

Posted in Programming | Tagged | 3 Comments