Some More Emacs Tips

Gurmeet Manku shares some Emacs Tips n Tricks for Everybody on his homepage. A lot of these will be familiar to most Emacs users and some are things I wouldn’t want to do but it’s a nice list of things you can add to your .emacs file to make your development work easier and more enjoyable.

One interesting example is a pair of functions that swap the numeric keys with the shifted numeric keys (1 ↔ !, 2 ↔ @, 3 ↔ #, etc.). The idea is that this makes typing faster when editing C/C++ code. A companion function maps __ into -> and .. into [] for pointer notation and array indexing. I’m not sure I’d like this but if you do a lot of C/C++ and can deal with the schizophrenia it could be a time saver.

Another useful trick is to add

(modify-syntax-entry ?- "w")

to your emacs-lisp-mode-hook so that - is not considered a word delimiter. Similarly,

(modify-syntax-entry ?_ "w")

when added to c-mode-common-hook will prevent _ from being a word delimiter. I don’t know why this isn’t built into Emacs.

There are a lot of other ideas in the article and it’s well worth a look if you’re interested in ways of streamlining your Emacs work flow.

Posted in General | Tagged | Leave a comment

Clozure Lisp 1.7 Is Out

I haven’t been doing very much Common Lisp programming lately but I noticed that Clozure Common Lisp 1.7 has just been released so I upgraded. I always like to do a (rebuild-ccl :full t) when I install a new version but my recent upgrade to Lion meant that I didn’t have up-to-date build tools so I also downloaded the new Xcode.

I have one of the free developer accounts but this time I just used the App store. The download took forever, of course, and then it just left the installer in Launchpad so I didn’t realize it had finished. Once I got the new Xcode installed the rebuild-ccl completed normally.

Now I should try it out with some miniproject. Programming Praxis is always a good source of that type of thing so perhaps I’ll head over there and try one of their recent exercises out.

Posted in Programming | Tagged , | Leave a comment

Putting Shell Output In The Current Buffer

I’ve long used 【Meta+|】 to pipe a region to a shell command. I use it all the time to pipe troff input to groff and display the results with gxditview. Recently I learned that if you specify a prefix arg it will place the results in the current buffer. The easiest way to do that is simply to type 【Meta+1 Meta+|】. The same trick works with 【Meta+!】. For example, if I want to insert a directory listing into the current buffer I could type 【Meta+1 Meta+!】 ls.

Summary of Key Sequences

  • 【Meta+|】
    Pipe the highlighted region to a shell command and place the output in a ∗Shell Command Output∗ buffer.
  • 【Meta+1 Meta+|】
    Pipe the highlight region to a shell command and place the output in the current buffer.
  • 【Meta+!】
    Run a shell command and place the output in a ∗Shell Command Output∗ buffer.
  • 【Meta+1 Meta+!】
    Run a shell command and place the output in the current buffer.

Update: prefix tag → prefix arg

Posted in General | Tagged | 3 Comments

The New Luddites Redux

It’s been a long time since I’ve written about the New Luddites—the last time was early on in my old blog (1, 2, 3)—but sadly their silliness has surfaced again. I follow Shelly Palmer’s Digital Living blog, which is geared towards the non-technical user of consumer electronics but often has interesting posts. Yesterday he reported on a CNN article that discussed smartphone users obsessively checking their phones. That article, in turn, mentioned a study published in the Journal of Personal and Ubiquitous Computing.

Palmer begins by poking fun at the study for belaboring the obvious but then goes on to take its conclusions seriously and offer “solutions” for our addictive behavior. The study itself is not particularly compelling. Its methodology is ad hoc and its sample sizes are ludicrously small (the largest of the 3 studies had 36 participants, another 12, and the last studied 3 groups with single digit participants).

What are the study’s results that have so alarmed Palmer? The participants checked their smartphones an average of 34 times a day. Really? Assuming they were sleeping 8 hours a day that amounts to checking their phones every half hour. That’s addictive behavior?

First of all, it’s silly to use the word addictive with its overtones of alcoholism or hard drug use. Those habits clearly are harmful both physically and emotionally. The most harmful thing pointed to by the CNN article was that one man’s frequent checking of his smart phone annoyed his wife. Many people today choose to live a digital life: they read ebooks, get their bills as emails, keep electronic diaries, maintain friendships through social media, use Google as a sort of associative memory, and most of all stay connected all the time. The device that enables this lifestyle is the smartphone. Of course these people check their phones frequently.

To devise tactics to reduce smartphone use as Palmer suggests makes no sense except as a bizaro world yes-I-can type of experiment. Twenty years ago the equivalent exercise would have been to go without electricity and air conditioning for a week just to prove you could. Just as it was silly to worry about becoming habituated to electricity and A/C, it’s silly to forgo the advantages of smartphones on the grounds that using them twice an hour is addictive behavior. The New Luddites are always with us, of course, but I was surprised and saddened to see the author of Overcoming the Digital Divide: How to use Social Media and Digital Tools to reinvent yourself and your career give them aid and comfort.

Update: bizzaro → bizaro

Posted in General | Leave a comment

Scrolling With Lion

OS X 10.7 (Lion) has been out for about a week and although that’s probably not enough time for any subtle bugs to surface, it is, apparently, enough time for my natural paranoia to be overcome by my desire to give it a spin. So over the last couple of days I’ve updated my machines with Lion—the upgrades went extraordinarily smoothly; thanks for asking.

I’m still getting used to the OS so I don’t have much of substance to say right now but I can comment on one issue that’s getting mentioned a lot in the reviews that I’ve read: scrolling. If you’ve been paying any attention to Lion, you know by now that Apple has “reversed” the scrolling. Swiping down with two fingers used to scroll towards the bottom of the page; now it scrolls towards the top of the page. It’s easy to reinstate the old behavior with the System Preferences pane but that hasn’t stopped the howls of anguish—not to say outrage—from many of the commenters.

The idea is that the new behavior mimics what happens with iOS when you scroll: you’re grabbing the screen and pushing it up or down. Despite having a small problem with muscle memory, I like the change. The old behavior never made sense to me and only muscle memory and the fact that it had always been that way kept me from being totally confused. If I thought too hard about it I would do the “wrong” thing. After only a few hours the new scrolling already feels natural. Sometimes I start to move in the wrong direction but that is already starting to die out. If you’re having trouble with the change, just imagine that you are putting your fingers on the text and pushing it in the direction you want to go.

I’m sure there’ll be lots of kvetching and dark mumbling about Apple oppressing their users and upsetting the natural order of things but, really, all Apple has done is finally have scrolling make sense. And, you know, if you really, really don’t like it, it’s easy to change it back.

Posted in General | Tagged | Leave a comment

Using set-mark-command To Remember Locations

A couple of days ago I wrote a note to myself post to help me remember the align-regexp functionality. Today’s post is another note to myself. A recent post by Xah Lee reminded me of some Emacs functionality that I am always forgetting. Every time I see the set-mark-command function described I think, “Boy, that’s really useful, I need to remember it.” Then I promptly forget about it again.

The set-mark-command (bound to 【Ctrl+Space】) sets the mark at the point’s position. The normal way of invoking it to remember locations is to type 【Ctrl+Space Ctrl+Space】. The first 【Ctrl+Space】 sets the mark and the second deactivates it so that no region is highlighted. Emacs also pushes the mark onto the buffer’s mark ring and onto the global mark ring.

This is useful because we can use the marks recorded on the mark rings to return to a remembered location. If we want to return to a previous mark in the same buffer, we type 【Ctrl+u Ctrl+Space】 We can move back several locations by repeating the 【Ctrl+u Ctrl+Space】.

If we want to return to a mark which may be in another buffer we can type 【Ctrl+x Ctrl+Space】. This can also be repeated to move back several marks.

This may seem confusing but it boils down to just three key sequences

  • 【Ctrl+Space Ctrl+Space】 to push the current position onto the mark rings.
  • 【Ctrl+u Ctrl+Space】 to return to a previous position in the same buffer.
  • 【Ctrl+x Ctrl+Space】 to return to the previous position, which may or may not be in the same buffer.
Posted in General | Tagged | 6 Comments

More On Borders’ Closing

David Hill over at Singularity Hub has an interesting take on the closing of Borders. While agreeing that Borders’ downfall is a shame, Hill argues that it will actually be a huge win for authors and readers. It will benefit authors, he says, by accelerating the changes in the publishing industry that are putting the authors in control. Those same changes will hasten the move to digital books with all the advantages that brings to readers.

Hill’s post has a link to a post by Alan Rinzier, an industry insider, that is also interesting. Rinzier confirms the trend of greater author control and says, basically, that the publishing industry has no idea what it’s doing and is coming to realize that. As evidence for this, he reports that 80–90 percent of all published books lose money.

Rinzier says that while it’s as hard as ever to write a good book, authors who can now have the opportunity to self publish and control their own destiny. As I suggested in my Future of Books post, the publishing industry is undergoing great change but appears to remain in denial. If they wish to avoid Borders’ fate, they had better get a clue. They still provide a valuable service but they are no longer the gatekeepers that they once were. If their tactics to defend their business models become too annoying, authors and readers will simply route around them and they will learn first hand the meaning of disintermediation. As I also said in that post, I hope that doesn’t happen but I see little reason to be sanguine about their prospects.

Posted in General | Leave a comment

Aligning Text With Regular Expressions in Emacs

This is really a note to myself. A while ago I ran across this post over at RAW SYNTAX about the Emacs command align-regexp. I don’t require this functionality very often but when I do, align-regexp is just what I need. Unfortunately, I keep forgetting about it so I’m writing this post in the hopes that it will help me remember (or at least give me a place to find the name of the function)

The simplest, and probably most common, use case is to align on a particular character. The canonical example is a telephone number list. We just mark the region to align, type 【Meta+x】 align-regexp, and then ( when prompted for the regular expression

# Original list

Joe (555) 111-2222
Mary  (555) 222-1212
Alexandra 123-4567
Francis (555) 333-2121

# After align-regexp

Joe     (555) 111-2222
Mary    (555) 222-1212
Alexandra 123-4567
Francis (555) 333-2121

That’s not quite what we want, though, because Alexandra’s number doesn’t have an area code so there is no open parenthesis to align on. To fix this, we have to use the more complicated version of align-regexp by giving the invocation a prefix arg: 【Ctrl+u Meta+x】 align-regexp. This time we specify .+?\( +\).+ as the regular expression, choose 1 as the group to modify, 1 as the amount of spacing, and no as to whether we should repeat the rule throughout the line. The result is:

Joe       (555) 111-2222
Mary      (555) 222-1212
Alexandra 123-456
Francis   (555) 333-2121

That’s better but what we really want is to align on the -. If we do that directly using the simple case, we get

Joe (555) 111     -2222
Mary  (555) 222   -1212
Alexandra 123     -4567
Francis (555) 333 -2121

which is definitely not what we want. Instead we use the regular expression .+? +\(.*\) and specify -1 as the group to modify or justify. That gives us

Joe        (555) 111-2222
Mary       (555) 222-1212
Alexandra        123-4567
Francis    (555) 333-2121

which is just what we want.

Update: lower case the key sequences.

Update 2: prefix tag → prefix arg

Posted in General | Tagged | 2 Comments

Traveling Without A Computer

Heh heh. OK, that was a teaser title. Of course, I would never go anywhere without a computer; my days of being ABEND are long over. My normal routine is to take my MacBook Pro and associated equipment with me in a backpack. The MacBook essentially mirrors my main iMac machine so I can do anything on the road that I can do at home. Still, flying with the laptop is a hassle. I use the excellent SwissGear ScanSmart Backpack so at least I don’t have to take the computer out of the backpack but it’s still one more thing to deal with at the TSA checkpoints.

Last week, I was in New York visiting family. I decided to try leaving the MacBook at home and depending on my iPad instead. Naturally that precluded any development or serious writing but this was a family visit so, realistically, I wouldn’t be doing much of either. I could have tried depending on my iPhone and avoiding large computers altogether but I wanted to keep up with blogs and news and it’s just too painful to read that sort of thing on the iPhone’s small screen.

Traveling with the iPad is definitely easier. I have a small case for it and the only support equipment it requires is the charging cable. I always travel with the wonderful Scottevest Travel Vest which, with its 22 pockets, has plenty of room for the cable, my iPhone, keys, and all the other stuff I would normally throw in the backpack. When I got to the checkpoint, my shoes and iPad went into a bin and I put the travel vest on top. Just one bin and the iPad is effectively hidden from anyone tempted to get a free computer as they come out of the X-ray machine.

As I’ve mentioned before, I keep up with my feeds with Reeder, which has a very nice and easy to use iPad implementation so it was easy staying current with blogs and news. The iPad has a nice email client that I use for my main email inbox. Mail from mailing lists and things like that go to a gmail account and gmail on the iPad is so good that I prefer to read gmail on the iPad even when I’m home.

The only other thing I needed to do was to manage my blog. I queued up a couple of posts so I didn’t have to worry about writing any entries while I was on the road (I do all that in Emacs anyway so the iPad is not a place to write posts). I was able to moderate forum spam and respond to legitimate comments easily.

Touch typing just isn’t going to happen on the iPad, but I can do a bit better than hunt and peck on the virtual keyboard so responding to emails and blog comments was relatively painless. I suppose I could get a portable keyboard to use with the iPad but, honestly, I don’t feel the need. For the little amount of typing I do on it, the virtual keyboard is fine.

All in all the experiment was a success. Traveling with the iPad was easier than bringing along the MacBook. The few chores I needed to perform were easily handled on the iPad so I didn’t miss having the MacBook. And as a bonus, I didn’t have to carry a book to read on the plane—I just fired up iBooks and enjoyed an ebook.

Update: Over at IT World Brian Proffitt has a post on using (only) an iPad for 7 days.

Posted in General | Tagged | Leave a comment

Xah’s Challenge (Part 2)

Yesterday, I wrote about Xah Lee’s programming challenge and gave a solution similar to Lee’s own. Today I will give another, possibly simpler, solution. We start with some constants and a couple of helper functions:

(defconst brackets '((?( . ?))
                     (?{ . ?})
                     (?[ . ?])
                     (?“ . ?”)
                     (?‹ . ?›)
                     (?« . ?»)
                     (?【 . ?】)
                     (?〈 . ?〉)
                     (?《 . ?》)
                     (?「 . ?」)
                     (?『 . ?』)))

(defconst not-brackets "^(){}[]“”‹›«»【】〈〉《》「」『』")

(defvar filen nil)

(defun brkt-closes-p (brkt obrkt)
  "Predicate to test if brkt closes obrkt."
  (let ((bpair (assoc obrkt brackets)))
    (and bpair (char-equal brkt (cdr bpair)))))

(defun brkt-open-p (brkt)
  "Predicate to test if brkt is an opening bracket."
  (assoc brkt brackets))

As before, we have a list of the beginning and ending brackets but this time they are given as characters rather than strings. The not-brackets constant will be used in a call to skip-chars-forward. The ^ at the beginning of the string specifies all characters except the brackets.

The brkt-closes-p function is a predicate that tests whether its first argument is the closing bracket for the second argument. It just uses the brackets constant as an alist.

The brkt-open-p function is even simpler. It’s a predicate that tests whether its first argument is an opening bracket. That will be true if it’s found in a key position of the brackets alist.

As in the first solution, check-file-for-mismatches gets called to check a single file but all the work is done in brkt-match. The check-file-for-mismatches function merely sets up the temporary buffer for the file and calls brkt-match.

(defun check-file-for-mismatches (fpath)
  "Check a file for mismatched brackets."
  (let ((fb (get-buffer-create "*Temp*")))
    (set-buffer fb)
    (insert-file-contents fpath nil nil nil t)
    (goto-char 1)
    (setq filen fpath)
    (brkt-match ?\f)
    (kill-buffer fb)))

The real checking is done in brkt-match:

 1:  (defun brkt-match (obrkt)
 2:    "Match OBRKT with its closing bracket."
 3:    (let ((open-pos (1- (point))))
 4:      (catch 'exit
 5:        (while t
 6:          (skip-chars-forward not-brackets)
 7:          (if (not (eobp))
 8:              (forward-char))
 9:          (cond
10:           ((eobp) (if (not (char-equal ?\f obrkt))
11:                       (print (format "Unclosed %c in %s at %d"
12:                                      obrkt filen open-pos)))
13:            (throw 'exit t))
14:           ((brkt-closes-p (char-before) obrkt) (throw 'exit t))
15:           ((brkt-open-p (char-before)) (brkt-match (char-before)))
16:           (t (print (format "Mismatch in %s at %d" filen (1- (point))))))))))
17:  

The idea is that brkt-match is passed an opening bracket and searches for the closing bracket (the ?\f passed in by check-file-for-mismatches is a special marker indicating the beginning of file—it matches end-of-buffer). Rather than locate the brackets with a regular expression, we find them by skipping over everything else with skip-chars-forward on line 6.

The processing is done in the while loop on lines 5–16. The catch on line 4 is so that we can exit the while if we match the input or reach end-of-buffer. The cond on lines 9–16 makes the checks:

  1. Lines 10–13
    If we are at end-of-buffer and the input was not ?\f then output an error message about an unclosed bracket. In any case, exit the loop and therefore the function.
  2. Line 14
    If the current character closes the input character, exit the loop and function.
  3. Line 15
    If this is an opening bracket, recursively call brkt-match to try to match it.
  4. Line 16
    If the other tests fail then this is a mismatched bracket. Output an error message and keep looking for the closing bracket matching the input.

We can use the exact same do-files from the first solution to drive check-file-for-mismatches so I won’t repeat it here.

This solution appears radically different from the first but it isn’t really; only the details differ. We use characters instead of strings, skip-chars-forward instead of regular expressions, and recursion instead of an explicit stack but the strategy is the same:

  • Search for the match to an opening bracket
  • If we find another open bracket, save what we’ve done so far and start over with the new opening bracket
  • When we match an opening bracket, pick up where we left off.

It’s an interesting question as to which method is faster. I haven’t tested that aspect but I’m pretty sure the second method is faster. It can do simple character compares instead of the more complex string compares and regular expressions have to do at least as much work as skip-chars-forward.

Update: test → tests. Fixed line numbers in text.

Posted in Programming | Tagged | 3 Comments