A Common Lisp Quick Reference

Bert Burgemeister has a very nice Common Lisp Quick Reference available for download. The idea is that you print it out, fold the pages, and staple them to form a little booklet. He’s got copies for A4 and letter size paper and also files for nesting or stacking the pages. There’s also a version for reading on screen that has the pages in the natural order.

This is an excellent little reference containing short descriptions of the symbols—functions, special operators, macros, variables, and constants—defined in the ANSI Common Lisp standard. If you use CL, you should give it a look. I am going to include at least the on-screen version in my documentation set and, despite my aversion to data on dead trees I might also print a copy.

Posted in Programming | Tagged | 1 Comment

Multiplication Table In Emacs Revisited

Yesterday, I wrote about generating a 10 × 10 multiplication table in Emacs Lisp. The solution I gave involved using the dolist macro with a list of the numbers 1–10 like this

(dolist (r '(1 2 3 4 5 6 7 8 9 10)) ...)

That’s a little contrived and obviously doesn’t scale well but I couldn’t find a better way. Fortunately jpkotta knew just what to do. He suggested using number-sequence so that the solution becomes

(dolist (r (number-sequence 1 10))
  (dolist (c (number-sequence 1 10))
    (insert (format "%2d " (* r c))))
  (insert "\n"))

As I said in the previous post, none of this is very important so it wouldn’t be worth the followup post except that number-sequence is essentially like the Python range function (number-sequence includes its upper bound while range doesn’t) so together dolist and number-sequence provide a nice Elisp idiom for operating on a sequence of numbers. Thus a construct such as

for x in range(i,j):
    do_something(x)

in Python could be rendered as

(dolist (x (number-sequence i j))
  (do-something x))

in Emacs Lisp. That’s a handy thing to know and I thought it was worth a followup so that jpkotta’s wisdom didn’t get lost in the comments.

Posted in Programming | Tagged , | 2 Comments

Multiplication Table In Emacs Lisp

Xah Lee saw my post about Tim Visher’s video on building a multiplication table in Emacs and decided to experiment. Visher’s video was in the context of VimGolf where the goal was to build the table in the minimum number of keystrokes. Lee wasn’t interested in building the table in the minimum number of keystrokes; he wanted to see how you could do it in various programming languages. He gives solutions in Emacs Lisp, Python, and Perl. That was interesting but then he says

Lisp is a bit verbose for this kinda thing due to the nested syntax.
But you can write it in perl, python, ruby, bash, or any dynamic lang
quickly, and press a button to have emacs call it and insert the
result.

To a Lisper like me this is a challenge that can’t go unanswered. Therefore I started to think about how I could code this concisely with Emacs Lisp. My first thought was to use the dotimes macro but that starts with 0, which complicates things. Then I realized that since we only needed to deal with the numbers 1–10, I could just use dolist like this

(dolist (r '(1 2 3 4 5 6 7 8 9 10))
  (dolist (c '(1 2 3 4 5 6 7 8 9 10))
    (insert (format "%2d " (* r c))))
  (insert "\n"))

Now put the point at the beginning of the next line, type 【Ctrl+x Ctrl+e】 and the table is inserted into the buffer.

None of this is of any importance, of course, but it is fun and instructive. My solution can be criticized on the grounds that it’s really a Common Lisp solution but it does work in Emacs due to the cl.el package. It’s also slightly objectionable to have to list the numbers explicitly but I couldn’t think of a better way. Leave a comment if you have a nicer solution.

Update: Added link to Xah Lee’s post.

Posted in Programming | Tagged , | 10 Comments

The Emacs Help Function

Most Emacs users are familiar with the “common” help functions

Key Function
f Describe function
v Describe variable
k Describe key sequence
a Apropos
i Info

and perhaps a few others such as 【Ctrl+a】 to display the Emacs start page.

It turns out, though, that there are 37 help commands. Some of these, like 【Ctrl+a】, you’ll rarely use and others, like 【Ctrl+m】 (How to order printed manuals) and 【Ctrl+c】 (Display GPL), you’ll probably never use. That still leaves many useful help commands that you may not know about.

You can get a list of the all the options with 【Ctrl+h ?】 but here are some of the more useful ones

key Function
b Display all key bindings
c】 SEQ Show the name of the command that SEQ runs
d】 TOPIC Display commands & variables whose documentation matches TOPIC
e Display the *Messages* buffer
l Display your last 300 keystrokes
m Display documentation of current major mode
p Find packages by topic keyword
r Display Emacs manual in Info
s Display current syntax table
w】 CMD Show which keys run command CMD
C Describe current coding system
F】 CMD Goto Info node documenting CMD
I Describe input method
K】 SEQ Goto Info node documenting command run by SEQ
L Describe language environment

As I said, there are others that may be just what you’re looking for so be sure to check the documentation.

Posted in General | Tagged | 2 Comments

Dennis Ritchie

As I said yesterday, I was on the road last week so I’m late with this post about the death of dmr but I can’t let his passing and accomplishments go unremarked. As he was for many, dmr was one of my heroes. Before he retired, I used to check his homepage at the labs every day. I never failed to learn (a lot) from the papers he posted there and, of course, I loved the stories from the old days when Unix was being built.

Many people have pointed to how Steve Jobs’ death was a big story for many days but hardly anyone outside our community even knows who Ritchie was, let alone that he had died. I wrote about Steve Jobs that his work touches me on a daily basis; the same is true of Ritchie, only more so. Ritchie invented C and was the co-inventor of Unix. For years I earned a living writing C code on Unix systems so the impact of his work on me is profound. The thing is, his impact on your Aunt Millie is nearly as large. Practically every technology has some C or Unix components in them; Rob Pike has a nice summary on the reach of dmr’s influence that’s worth reading. The New York Times Technology section has a good retrospective on his life and death.

I’ve seen many variations on this

#include <stdio.h>
#include <stdlib.h>

int main()
{
    printf( "Goodbye, world\n" );
    exit( EXIT_SUCCESS );
}

tribute in the last week and while it might be trite it seems to me a perfect way of saying goodbye to a remarkable man and for thanking him for everything he has given us.

Posted in General | Leave a comment

Replace Digits By Subscripts In Emacs

Xah Lee had an interesting Emacs Lisp problem last week but I was away and unable to join the fun. The problem is to replace any occurrence of a digit in a string by the equivalent subscript. Thus 1, 2 and so on. As soon as I saw the problem the following solution popped into my mind

(defun replace-digits-by-subscript (str)
  (replace-regexp-in-string
   "[0-9]"
   (lambda (v) (format "%c" (+ (string-to-number v) 8320))) str))

This works because the difference between a digit’s codepoint and the corresponding subscript’s codepoint is always 8320. As each digit is found, it is converted to a number, added to 8320, and then output as a string by the format function.

Someone else has a similar, though slightly more complicated, solution but Lee didn’t like it because it depends on the particular coding of the character sets. I disagree with that. Although it’s a good rule in general not to depend on a character’s encoding, with Unicode there is only one, fixed encoding so there is no harm in making use of the structure of that encoding.

If you wanted to do this interactively on some buffer, a similar solution uses query-replace-regexp with a regular expression of \([0-9]\) and the replacement string

\,(format "%c" (+ \#1 8320))

Actually, it’s too bad replace-regexp-in-string doesn’t support the \, construct as that would make the solution to the original problem even easier.

Posted in Programming | Tagged | 2 Comments

A Screencast on Emacs Dired Basics

If you’re new to Dired and like to learn visually (as compared to, say, reading about a subject), Kenny Meyer has an interesting screencast on Dired basics that you may enjoy. He covers

  • Copying files
  • Deleting files
  • Renaming files
  • Creating new directories
  • Compression and decompression of files
  • Changing file modes
  • Changing other file metadata
  • Marking and unmarking files
  • Operations on multiple files

Meyer is promising further screencasts so if you like this one, you may want to subscribe.

Posted in General | Tagged , | Leave a comment

Delete Backwards To BOL in Emacs

This is another short Emacs tip that’s really a note to myself. The other day, I found myself in the middle of a line and needed to delete everything back to the beginning-of-line. I was sure there was some way to do that but I didn’t know what it was so I just sat on the 【Delete】 key until I reached the beginning of the line.

For the record, the correct way to do that is 【Ctrl+0 Ctrl+k】. That has a certain tractive logic since 0 suggests the beginning-of-line (at least to those of us with vi experience) and, of course, 【Ctrl+k】 means kill the rest of the line. It also has the advantage that it’s easy to type.

Posted in General | Tagged | 6 Comments

Eval And Replace In Emacs

I was watching another of Tim Visher’s VimGolf videos in which he builds a multiplication table by the clever use of keyboard macros. What got my attention, however, was his use of the function eval-and-replace that evaluates the sexp preceding the point and replaces it with its value. This seemed like a useful thing to be able to do but as Visher explained, eval-and-replace is part of the Emacs Starter Kit and not part of Emacs itself.

I have a fairly large init.el that I’ve developed over time and therefore never installed the starter kit even though I’ve heard good things about it. Rather than hunt up the starter kit to get the function, I just rolled my own:

(defun eval-and-replace (value)
  "Evaluate the sexp at point and replace it with its value"
  (interactive (list (eval-last-sexp nil)))
  (kill-sexp -1)
  (insert (format "%S" value)))

The eval-last-sexp in the interactive declaration evaluates the sexp and passes its value to eval-and-replace. Then the (kill-sexp -1) deletes the sexp from the buffer. Finally the value is inserted into the buffer with the insert function.

I originally wrote the function using a let to capture the value of the sexp instead of using the interactive declaration but this seemed a bit shorter and I wanted to use an expression in the interactive declaration, as I wrote about here, to fix the idea in my mind.

I can see all sorts of uses for eval-and-replace. As a trivial example, suppose I want to say 223=8388608 but I don’t remember the value of 223. I could switch over to my calculator buffer to make the calculation and then cut and paste it to my text or I can just write

2^23=(expt 2 23)

and call eval-and-replace to put the value in the text for me.

I’ve added eval-and-replace to my init.el so that I’ll have it available the next time I have a need for it.

Posted in Programming | Tagged , | 10 Comments

Transposing In Emacs

Most Emacs users know about using 【Ctrl+t】 to transpose two letters and 【Meta+t】 to transpose two words but there are two other useful transposition commands.

You can use the sequence 【Ctrl+x Ctrl+t】 to transpose two lines. As with the other transposition commands, it drags the line preceding the point forward so that you can move a line down any number of lines. For example, if you have

Line 1
Line 2
Line 3
Line 4

and the point is at the end of Line 1 or anywhere one Line 2 and you type 【Ctrl+x Ctrl+t】 twice (or give a a prefix of 2) you get

Line 2
Line 3
Line 1
Line 4

The other transposition command is for balanced expressions or sexps. This is a little more useful than you might think—especially if you’re not a Lisp programmer—because of the definition of “balanced expression.” See the explanation of balanced expression in the Emacs manual. To transpose balanced expressions, use the sequence 【Ctrl+Meta+t】.

One final neat feature: if any of these commands is given a repeat count of 0—which nominally makes no sense—then the objects ending after the mark and point are exchanged.

Posted in General | Tagged | 2 Comments