Sunday, April 20, 2008

OS/X for Emacs Users

So I finally took the big plunge and bought a Mac. Not some company-issued laptop, either. I bought a monster G5 dual-proc something-or-other (you can tell I'm not really a Mac guy) with 8 gigs of RAM. The case is like twice as big as the old PC it's replacing. Got an obscene monitor, too: a Dell 30-inch. And it's OS/X Leopard, which is apparently the latest and greatest. It must be good!

Here's the thing: I've had to spend close to two full days getting "simple" configuration settings working. In particular, I wanted a decent Emacs locally, a decent terminal/shell, and the ability to ssh into remote linux machines and run remote Emacsen.

Should be easy, right? Well, it's actually an enormous, ball-busting pain in the ass, because... I dunno. Just because. It's frigging complicated. I figured I'd write down what I can remember about what I've done so far, in case I need to do it again.

First: I got Carbon Emacs 22.2.1. It's a straightforward install. I think the default font is Monaco 12-pt, but if it's not, that's what I set it to.

Next: I had to plop in the second CD that came with the computer and install the XCode developer kit. I installed everything else on it, too - WebObjects and mumble mumble something.

Next: I had to install something called "MacPorts". This is apparently very similar to, or perhaps the same as, "Darwin Ports". It's sort of the Cygwin equivalent for OS X: it provides a framework for porting stuff to OS/X, and has a few thousand packages ported. It's also kinda CPAN-like, in that it tries (and, also like CPAN, often fails) to grab the dependencies for whatever you're installing. Haven't succeeded in getting it to install the Gimp yet, but some smaller, less ambitious stuff installed just fine.

Then I was able to install mrxvt, which is a decent terminal program. It supports 256 colors, although you have to enable it by putting export TERM=xterm-256color into your .bashrc.

Let's see: oh, Leopard finally lets you swap caps lock and control. Go to System Preferences - Keyboard/Mouse - Keyboard, and click the Modifiers button.

Then I had to dork around with stuff to get mrxvt to use anti-aliased Monaco font. At least I think I did. I had to sudo port install fontconfig, and then I think I just say mrxvt -fa Monaco -fs 12. Oh, and I had to add this stuff to my ~/.Xresources file, which I had to create:

mrxvt*scrollBar: false
mrxvt*loginShell: true
mrxvt*syncTabTitle: true
mrxvt*font: Monaco
mrxvt*faceSize: 11
mrxvt*xft: True
mrxvt*xftFont: Monaco
mrxvt*xftSize: 12
mrxvt*xftAntialias: True
mrxvt*termName: xterm-256color


I think I also added it to ~/.Xdefaults, since I can never remember which is which. (Update: I symlinked .Xdefaults to .Xresources, and also .bash_profile to .bashrc, and life is Good.)

Another Update: I had to sudo port install fontconfig for that Monaco font to work.

This results in a font that looks kind of like the one in Carbon Emacs, although not quite as sophisticated. I think the font may be the same but the antialiasing engine is different. In any case, it's decent enough that I'm not going to mess with it any further.

The Damned Alt Key

There are 10 billion websites out there with discussion threads on how to get the Alt (aka Option) key working as the "alt" modifier in X11 under OS X. Most of them were flat-out wrong, at least for my machine. I tried everything and couldn't get any response from emacs in a terminal window (either local or remote over ssh) other than having Alt+char produce an iso-latin-ish accented char.

After about four or five hours of trial and error, here's what did it:

in ~/.Xresources and ~/.Xdefaults:

XTerm*VT100.metaSendsEscape: True


in ~/.Xmodmap (note that the case _is_ significant):

clear mod2
keycode 63 = Meta_L
keycode 66 = Meta_L
add mod2 = Meta_L


This sets both the Option and Command keys to be the alt modifier. Personally I find the Command key to be a more convenient alt-key than the Option key, since I can type it with my left thumb, whereas the Option key isn't easily reachable with any finger.

If you want to "switch" the behavior of Option and Command, so that Option is alt and Command is... whatever Option normally does on the Mac, in X11, well, you can do this in your ~/.Xmodmap instead:

clear mod1
clear mod2
keycode 63 = Alt_L
keycode 66 = Meta_L
add mod1 = Alt_L Alt_R
add mod2 = Meta_L


I don't know exactly what I'm losing by binding the Command key to be the Alt modifier, but whatever it is, I'm sure I'd do it about 1 billionth as often as I press Alt in Emacs, so it's worth it.

The Backspace/Delete Issue

This particular advice might not work for everyone, but if you tried everything else, try this. It's predicated on my having re-bound Help to C-x C-h, something like 20 years ago, for reasons I can't recall anymore. It's just what the default was at Geoworks.

In any case, the C-h part doesn't work on the Mac out of the box.

The basic problem is that the Delete key on the Mac is bound to DEL, which deletes the character before the cursor. As you would expect. However, the sequence C-h apparently generates the same key code, so in Emacs when you try to use Help (or any command containing a C-h, such as mark-defun), it just deletes the previous character.

I don't know if there's a more elegant fix, but this works for for me. First, before you start Emacs, type this into the shell:

stty erase ^\?


Note that the ^\? is actually three separate characters: a caret, a backslash, and a question-mark. This dorks the Delete key by making it send a ^H, but enables the C-h sequence in Emacs. Note that C-Delete does a normal backward-delete-char, so just remember that when you're backspacing in a terminal, hold the control key down.

Then make sure you have this in your .emacs:

(global-set-key "\C-h" 'backward-delete-char)


Then you automate it all by creating an alias that does the stty erase command and then launches Emacs.

Useful stuff

The xev command is a lifesaver - it tells you exactly which keycodes your keyboard keys generates, and what they're currently bound to. It's in /usr/X11/bin (as is xmodmap), so you should add that to your path.

Carbon Emacs has some variables that start with "mac-" that are worth looking at. I set `mac-option-modifier' to t in order to get the Alt key working as alt (in addition to the Command key as alt, which was the default).

Apparently when you bind Command to Meta_L, it dorks pasting in your X11 windows. However, if you check the "Emulate 3-button mouse" option in X11 Preferences, then Option + mouse will paste. You can copy stuff by selecting it and manually opening the Edit menu and selecting Copy, which is fine by me, since copying across the X clipboard to/from X11 to Aqua apps is rare enough that using the mouse is fine. As long as there's some way to do it, I'm happy.

Annoying Stuff

The Last Great Annoyance I need to resolve, before my Mac has officially become an acceptable substitute for Linux (or for Windows + Cygwin + lots of customization), is the focus behavior. There are at least two serious problems:

1) focus-follows-mouse, which is evidently possible via a "private" (?) API used by some commercial package called VirtualDesktop. If they can do it, then I can figure it out eventually. (I'd have paid them for their damn product but their installer crashes. Fuggers.)

2) sending an event to a window when you click to focus it. The default behavior is that clicking to focus (and raise) a window swallows the event, so if you wanted to interact with some widget, you need to click it *again*.

Both of these are complete and utter capital-b Bullshit, and it's pretty astonishing that after so many decades of people screaming about it, Apple hasn't fixed it yet.

But hey, at least they added the option to swap Caps and Control. So they're getting there!

And aside from the near deal-breaking atrocity of not having a focus-follows-mouse option, OS/X is a pretty awesome operating system.

Anyway, now I've written it all down (as best I can remember), so I'll get back to work.

Sunday, January 28, 2007

First Post!

Hi there. Welcome to the very last post in this blog - at least if you're reading the posts in reverse-chronological order the way they're presented by default. Silly, eh?

My other blog has a life of its own, sorta, so I figured I'd create this personal blog for, you know, the stuff people usually create blogs about. Cat pictures and stuff.



At the 2006 Google Kirkland Holiday Party, which was way over the top, they had a little photo booth where you could hop in and get free pictures. So my wife Linh and I hopped in, and the picture on the right came out. Since I can rarely get Linh to look into a camera (she's shy), I figured I'd scan this one.

Unfortunately, by the time I got around to scanning it 6 weeks later, it was kinda dusty from hanging on my magnetic bulletin board, and the dust particles were blown up to the size of large tropical insects by the FedEx/Kinko's scanner. So I guess I'd better go get my own scanner, clean the picture, and try again.

The Holiday party was cool. It was in the ballroom of the Westin Hotel in downtown Seattle, from 8pm to midnight. There was an open bar, gambling, a big buffet, a surprisingly good cover band, foosball tables, video games, and a huge inflatable racetrack thingy. All the ex-Microsoft Googlers said, awestruck, that it was -way- better than the Microsoft holiday party. As for me, I was at Amazon for 7 years, where they have the holiday party in a cold warehouse in February, so we ex-Amazon Googlers just laughed and nodded knowingly at each other.

I actually have a decent camera; I just never bring it anywhere. I think I'll try to remember to bring it to the Google company ski trip, the week after next. It looks like they're going to set up all kinds of fun stuff in the lodge (we're staying overnight at Crystal Mountain and skiing for 2 days, complements of Google), so it's probably picture-worthy.

Stay tuned for the inevitable cat pictures. Well, we have a dog that people regularly mistake for a cat or a rabbit, so close enough.