An Emacs Style Guide

Bozhidar Batsov, proprietor of Emacs Redux, is starting an interesting new project: The Emacs Lisp Style Guide. The idea is to create a community contributed set of rules for “good Elisp form.”

Normally, I’d hate that sort of thing. Almost always, style guides end up encoding silly ideas like

if (3 != x)  /* constants before variables in C if statements */

or even the incredible coding standard from hell. Throughout my career, I mostly ignored stuff like that. Oddly, though, I find myself in agreement with Batsov’s guide so far. That’s probably just because I already do things pretty much the way he proposes and there are a couple of good ideas that I hadn’t thought of like naming unused lexically scoped local values with a preceding underscore

(lamba (x _y) (* x x))

Old timers are going to do what they’ve always done, of course, but the guide has lots of good advice for less experienced Elispers. The rules are mostly what experienced Lispers do so using them helps promote clear, easily understood code.

Take a look at the guide and let me know what you think. And, of course, if you think something is missing or see something you think is wrong, add your voice to the discussion. It is, after all, a community effort; Batsov has just done the heavy lifting to get things rolling.

Posted in Programming | Tagged , | Leave a comment

Memory Reallocation Revisited

Back in January I wrote about a charming result concerning how much memory should be increased when reallocating. Via Artur Malabarba, I came across a tweet by Wilfred Hughes

pointing to Facebook’s implementation of the idea. The FBVector.md file at the link explains why they use a factor of 1.5 instead of 2 (it misses the beautiful result about the golden ratio, though).

Their implementation of the C++ std::vector library has other optimizations, which are also interesting. If you’re working in C++, you should definitely give it a look.

In my original post, I remarked that “none of this is of great import.” The folks at Facebook have shown that that was wrong.

Posted in Programming | Tagged | Leave a comment

Truism of the Day

Posted in General | Tagged | Leave a comment

Progress Indicator for Org-Mode Code Blocks

Via Grant Rettke, here is a nice snippet of code from John Kitchin that gives a visual indication of when an org-mode code block is executing. It’s just a defadvice around org-babel-execute-src-block so it’s easy to modify to suit your preferences.

Most of my code blocks execute more or less instantly so I would have limited use for this but if, like Kitchin, you have long running processes, it’s a nice hack to let you know the status.

This is another example of how Kitchin is automating his life with Emacs. Happily, the rest of us benefit from his work as well.

Posted in General | Tagged , | Leave a comment

Temporary Syntax Tables Revisited

The other day, I wrote about Xah Lee’s post on temporary syntax tables and the with-syntax-table macro. Subsequently, Lee discovered a nasty side effect that he wrote about on the G+ Emacs Community site. The problem was that his code

(with-syntax-table (standard-syntax-table)
  (modify-syntax-entry ?\« "(»")
  (modify-syntax-entry ?\» ")«")
  ;; 
  )

modified the original standard syntax table for all buffers.

The problem is (standard-syntax-table) doesn’t copy the standard syntax table; it just returns a pointer to the standard syntax table object. This is the same thing that happens if you assign a list to a variable:

(setq x '(1 2 3))
(setq y x)
(setf (car y) 5)
(message "x is %s" x)

returns

x is (5 2 3)

because x and y refer to the same object.

What we need to do is first make a copy of the standard syntax table. Happily, the copy-syntax-table function does just that1 so if we evaluate

(with-syntax-table (copy-syntax-table)
  (modify-syntax-entry ?‹ "(›")
  (describe-syntax))

the ‹ is described as an open bracket but if we open a new fundamental buffer and call describe-syntax it is described as punctuation, as expected.

Lee solved this problem by creating a new syntax table with make-syntax-table and added his definitions to it. You might think that Lee’s solution is incorrect because the other syntax definitions will be missing from his new table but it is correct because make-syntax-table will create a table that inherits from the standard syntax table (or whatever table you specify).

Footnotes:

1

copy-syntax-table copies standard syntax table by default but you can specify a table explicitly if you want to start with some other table.

Posted in Programming | Tagged | Leave a comment

Sacha Chats with Mickey

Sacha Chua has her latest Emacs chat up. This time it’s with Mickey Petersen proprietor of the excellent Mastering Emacs. If you follow the Emacs scene at all, you will be familiar with Mickey and his definitive articles on the ins and outs of Emacs.

Mickey came to Emacs pretty much because he was told all the cool kids use Vim. In the intervening 10 years he’s worked to master Emacs and share his knowledge with the rest of us through his blog. Watching him use Emacs you can see that mastery in action. He navigates through files so quickly and easily that it’s hard to follow the action.

One of the things that impressed and inspired me was that he tends to think in term of language constructs rather than characters. Thus, for example, he navigates by and manipulates s-expressions. That’s an attitude that I want to cultivate; it can’t help but increase my editing speed and efficiency.

The chat is about 53 and a half minutes so plan accordingly. As with all of Chua’s chats, you’re sure to learn a few things. For example, I learned the efficient way to backwards kill an s-expression.

Update: Sacha has a transcript of the chat up.

Posted in General | Tagged | Leave a comment

Transposing Words

Howard Abrams has a nice video on transposing words in Emacs. You might think that there’s not much to say about the subject: just type 【Meta+t】 and you’re done. Of course there’s also the fact that you can use it to “pull” a word towards the end of the line by repeated invocation of the command or by using a prefix number but that’s also well known.

Abrams explores an interesting edge case. What if you’re at the end of the line? He’s got an actual use for that. Suppose you’re typing along and realize you forgot a word

The brown fox

Rather than backing up and inserting the missing word, you can just type it at the end

The brown fox quick

and then type 【Meta+t Meta+t】 and quick gets inserted in the proper place.

That’s a nice trick that takes advantage of a side effect of transpose-words and you can use it as is. But Abrams shows how to advise transpose-words so that it handles this case with a single invocation. That means you just type 【Meta+t】 as usual and Emacs does the right thing.

The video is only 8 minutes so you can watch it whenever you have a few minutes. Very nice. Even if you don’t add the advise, it’s worth knowing the trick.

Posted in General | Tagged | 1 Comment

SBCL 1.2.3

The latest version of Steel Bank Common Lisp, 1.2.3, is available at the usual place. As always, it built without problem and successfully ran the test suite. ASDF has been upgraded to 3.1.3 and there are a couple more enhancements and some bug fixes as well. Check the NEWS page for details.

As Christophe Rhodes remarked, this month’s release is less exciting (read controversial) than last month’s. Regardless, SBCL is an excellent Lisp environment that gets better and better with each release.

Update: months → month’s

Posted in Programming | Tagged , | Leave a comment

Temporary Syntax Tables

Xah Lee has a nice post on how to temporarily modify a syntax table. It’s not something you want to do everyday, of course, but the example Lee gives is a real-world case.

It turns out that it’s pretty easy. Just use the with-syntax-table macro and you can change the syntax table in a way useful for the rest of the code in the macro’s body. When control exits the macro expansion, the old syntax table is restored. You can also use the macro to temporarily switch to a different syntax table. If the foregoing seems opaque, the example in Lee’s post will clear things up.

I didn’t know about the with-syntax-table macro so this post is my way of writing it down so I won’t forget. I’m sure I won’t need it often but when I do, it’s just the thing.

Posted in General | Tagged | Leave a comment

Another Reason Closed Source is Harmful

Open Source/Free Software includes a number of tribes. There are the FSF true believers, epitomized by RMS, who hold that software should be free on philosophical/ideological grounds. Even if one could adduce facts showing that Free Software was less optimal than closed source, these folks would be unmoved; it’s a matter of morality.

Then there are folks like Cory Doctorow and ESR who still insist on using strictly Open Software but do so on (mostly) pragmatic grounds. Members of this tribe seek to avoid lock-in or loss of data. It’s easy to see why someone like Doctorow would fear having his data locked in some proprietary format that could become unsupported at the whim of some software company.

There’s also the tribe that is willing to use a proprietary program or OS, when necessary, as long as it doesn’t involve their data. As a Mac user, I fall into this group. Even though I prefer open source, I’m perfectly happy to enjoy the benefits that OS X brings me but I would never use Word, or Pages (the Mac answer to Word), or Numbers (the Mac spreadsheet). Instead all my data is held as plain text (even, thanks to Org Mode, spreadsheet type data). If OS X were to die tomorrow, my data would be just as useful and accessible on Linux or even Windows.

The key here is that you must never commit any data you care about to anything but plain text. If someone or something requires, say, word documents, you write and maintain it in Org- or Markdown-mode and export it to word as Mark Szepieniec and Christophe-Marie Duquesne outlined for résumés. Most of us know and practice all this no matter which tribe we adhere to.

It turns out, though, that it’s a bit more complicated. Jonathan Zdziarski has an interesting post on digital forensics and the threat that closed source poses to it. Most law enforcement agencies use closed source programs to extract data from phones or computers and for other forensic tasks. One problem is that the user has no way of knowing what the programs are doing or how they’re massaging the data. This can present legal problems when, for example, the defense insists on auditing the program that produced the data. In the case of breathalyzers, which are also mostly closed source, this has already resulted in DUI dismissals.

It gets worse though. Zdziarski reports that the results these forensic programs produce are often flat out wrong—even made up in some cases. That’s an obvious problem no matter which side of law enforcement you’re on. If you’re the defendant, you might get wrongly convicted. If you’re with law enforcement, guilty parties may be erroneously cleared.

Closed source isn’t the only problem that Zdziarski describes but it is a large one. We will certainly see cases involving forensics depending on these programs challenged and dismissed. In the mean time, those of us who support open source get to feel smug. Just don’t find yourself on the wrong end of a digital forensics investigation.

Posted in General | Tagged | Leave a comment