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

Emacs Poll On Deleting And The Active Region

Virtually all GUI environments will delete a highlighted region when the 【Backspace】/【Delete】 key is pressed or when a character is inserted into the region. Emacs does not currently do that except in a few minor modes such as CUA mode.

RMS has a post on the help-gnu-emacs list asking for reader input on a proposed change to implement this behavior in Emacs 24. Readers are asked to send an email to emacs-delete-poll at gnu.org answering a few questions about the proposal (basically, whether you’re in favor or not and why). The Emacs 24.0.90 pretest build has this change implemented for those who would like to try it out.

My first reaction when reading it was to be opposed on the grounds that it would be too easy to inadvertently delete a region. After a little thought, though, I realized that I never have any problem with this behavior anywhere else and it’s easy to recover from a mistakenly deleted region in Emacs with 【Ctrl+/】or whatever you use for undo.

RMS doesn’t say, but presumably the change could be disabled for those who don’t want it. (I know, I know everything is configurable in Emacs if you know Emacs Lisp—I mean with a single line in init.el.) If that is the case then it’s hard to see how anyone could object. (Actually, some of the commenters do object even in the face of such a flag on the philosophical grounds.)

My conclusions are that I don’t care whether they make this behavior the default as long as it can be disabled1 and that I will probably disable it on the grounds that I’m used to the old behavior. What do you think?

Footnotes:

1 It looks as if setting delete-active-region to nil disables the change.

Posted in General | Tagged | 1 Comment

R7RS WG1 Update

Via Grant Rettke over at Wisdom and Wonder I found these slides from a talk at LispNYC by John Cowan on September 13. As readers from my old blog will recall, R7RS will be split into a “small Scheme” and a “large Scheme” with the small version being a modest expansion of R5RS and the large version being more like R6RS but still downward compatible with the small version.

Working Group 1 (WG1) is tasked with the small Scheme and has almost completed its work. Cowan expects the final committee draft to be out in a few weeks and after formal comments the final version. Working Group 2 is a subset of WG1 and has pretty much suspended their work until WG1 is completed.

Very often it’s hard to make sense of the slides from a presentation without having heard the actual talk. In this case, however, the slides stand alone very well and are quite informative. If you’re interested in Scheme and want to know what’s coming up, take a few minutes to look through them.

Added at the last minute: Video of Cowan’s talk is here. There’s a nice discussion of it on Slashdot.

Posted in Programming | Tagged | Leave a comment

VimGolf In Emacs

I recently saw a reference to Tim Visher’s VimGolf in Emacs videos on one of my feeds. It looked interesting so I hopped over to take a look. VimGolf is a site that poses editing challenges that are supposed to be done in Vim. As with golf, the winners are those with the lowest number of (key)strokes. Visher uses these challenges as a basis for videos that highlight various Emacs techniques. Why should Vim users have all the fun, after all?

I’ve watched a couple of the videos and already learned something very useful. I’m not sure what people who grew up with computers are doing but when I learned to type in high school—on a typewriter—we were taught to put two spaces after the period at the end of a sentence. This made sense with the monospaced typescript that typewriters produced but is incorrect for proportional fonts. After reading this rant on the practice I started using just a single space. Then one day I read that I could move forward and backward by sentences using 【Meta+a】 and 【Meta+e】. The problem was that it seemed to move by paragraphs instead. Moving by sentences isn’t something I do a lot so I just shrugged my shoulders and moved on.

During one of Visher’s videos, I learned that by default Emacs uses a period and two spaces to delimit sentences. That was why moving by sentence wasn’t working for me. The solution to the problem is trivial: simply put

(setq sentence-end-double-space nil) ;period single space ends sentence

in your .emacs or init.el file. After doing that, moving by sentences worked correctly.

I really recommend these videos. They’re entertaining and full of things that you might not have known. The later videos show the keystrokes on the screen but the earlier ones don’t so you have to pay attention to what Visher is saying. Still, it’s worth the effort. I’m sure I’ll be passing on other tidbits as I learn them.

Posted in General | Tagged | 4 Comments

Using Emacs To Convert Notes For Deft

Yesterday I wrote about Vivek Haldar’s Deft patch that allowed him to integrate his previous notes into Deft. Recall that Deft uses the first line of a note as its subject and names the file for a note deft-XXX whereas Haldar used the filename to describe the subject of the note. After I published that post I thought that it might have been easier to simply convert the existing notes to Deft format by making the file name the first line of the note.

It turns out that it is pretty simple to do that. Let’s assume that the file names are something like /Users/jcs/notes/ideas-for-Hercules-project.txt. We want to take the file name and convert it into a subject line by removing the directory information, removing the extension, changing the dashes to spaces and inserting the result as the first line of the file. Here’s a bit of Emacs Lisp that does that for one file:

(defun file-to-subject (filen)
  (let ((subject (replace-regexp-in-string
                  "-" " "
                  (file-name-sans-extension (file-name-nondirectory filen)))))
    (with-temp-buffer
      (insert-file-contents filen)
      (goto-char 1)
      (insert (concat subject "\n"))
      (write-file filen))))

The let at the beginning of the function converts the file name to a subject line. Then the file is read into a temporary buffer and the subject line is inserted at the beginning of the buffer. Finally the new file is written over the old.

To process all the files in, say, the ~/notes directory we could use the line

(mapc 'file-to-subject (directory-file "~/notes" nil "\\.txt$"))

or for more complicated directory structures we could use Dired to mark the files we want to convert and then use 【w】 (bound to dired-copy-filename-as-kill) to put the file names in the kill ring. Then we could just yank them right into the mapc in place of the (directory-file "~/notes" nil "\\.txt$").

That leaves changing ideas-for-Hercules-project.txt to deft-XXX. That could be done in several ways, with or without Emacs, but if you want to do it in Emacs here’s what you need for one file

(defvar deft-num 0)

(defun deft-name (filen)
  (rename-file filen (format "deft-%03d" (incf deft-num))))

The incf function1 increments deft-num and returns the incremented value so that the first time deft-name is called the format will return deft-001 and then deft-002 and so on.

You can do all the files with a mapc similar to what we used above. Alternatively, we could place the call to deft-name after the call to write-file in the file-to-subject function.

Footnotes:

1 incf is part of the Common Lisp package so you’ll need a (require 'cl) if you don’t already have it.

Posted in Programming | Tagged | Leave a comment