Sunday, March 23, 2008

Arc + Emacs

So Paul Graham releases Arc, a new dialect of Lisp. It's been out for a little while now, so it's not exactly news, but the language itself is pretty cool. It seems to embrace the idea of programs rapidly changing, by making programs smaller.

So, how does one actually get to play around with Arc? The approach here will get you Arc running within Emacs so that you can send source from Emacs buffers into the running Arc.

Here's what I started off with:
  • Ubuntu 7.10
  • Emacs
  • git

1. Get Arc

Follow the instructions here to grab Arc via git: Git and the Anarki Arc repository: a brief guide. The files you pull will come out in a subdirectory named arc-wiki/.

For the interested, Anarki is the name of the community-maintained release of Arc. It has all of PG's work, plus some niceties. One of those niceties are a couple of Emacs elisp files that we'll be using to tie Emacs and Arc together.

2. Put stuff in your .emacs

Add this to your ~/.emacs:

;; Arc support
(add-to-list 'load-path "/path/to/arc-wiki/extras")
(autoload 'run-arc "inferior-arc"
"Run an inferior Arc process, input and output via buffer *arc*.")
(autoload 'arc-mode "arc"
"Major mode for editing Arc." t)
(add-to-list 'auto-mode-alist '("\\.arc$" . arc-mode))
(setq arc-program-name "/path/to/arc-wiki/arc.sh")

Obviously, replace /path/to with the path from step 1.

By default, the Arc REPL prompt isn't read-only, which can be a bit strange. This will make it read-only:

(add-hook 'inferior-arc-mode-hook
(lambda ()
(set (make-local-variable 'comint-use-prompt-regexp) t)
(set (make-local-variable 'comint-prompt-read-only) t)))

If you use parenface for parenthesis dimming like I do, you can enable it for Arc buffers with this:

(add-hook 'arc-mode-hook
(paren-face-add-support arc-font-lock-keywords-2))
(add-hook 'arc-interaction-mode-hook
(paren-face-add-support arc-font-lock-keywords-2))

And if you use paredit, also as I do, then the following will enable that in Arc buffers:

(add-hook 'arc-mode-hook (lambda () (paredit-mode +1)))

3. Trying it out

In Emacs, find (C-x C-f) your way to a file ending with ".arc", then type M-x run-arc (or select Arc -> Run Inferior Arc), and presto! We're in business.

All the keybindings can be read straight out of /path/to/arc-wiki/extras/inferior-arc.el, but here's the main ones I use:
M-C-x
Send top-level form
C-x C-e
Send S-expression before point
C-c C-l
Load current Arc file (NB: can also unjam read-only prompt if that happens, even if the file is bogus)

Enjoy! Remember to git pull every so often to stay on the cutting edge.

No comments: