Chad Perrin: SOB

8 September 2009

Vim + Scheme

Filed under: Geek — apotheon @ 10:18

If you’re like me, your favorite editor is Vim, and Emacs just seems like a very special form of torture rather than the One True Editor.

If you’re even more like me, you’re a Scheme programmer, currently learning Scheme, or at least interested in learning Scheme.

Chances are good you’ve heard that Emacs is the editor (or IDE) for doing Lisp development. While Scheme isn’t what most people mean when they say Lisp — because, assuming they’re programmers, they’re probably talking about Common Lisp — but the general rules of syntax still apply. In fact, they apply so generally between these two branches of the Lisp family of languages that for the most part the same rules of syntax for managing autoindentation and syntax coloring in the editor apply.

Of course, there’s always DrScheme, which is a GUIfied Scheme IDE using the MzScheme implementation of the language. If you’re quite a lot like me, though, you might prefer the Ypsilon implementation, and want to avoid GUIfied IDEs in favor of text-mode Vim.

Of course, until earlier today I was using the following indentation rules in my .vimrc file:

set autoindent
set expandtab

This doesn’t really perfectly lend itself to Scheme-style code formatting. Luckily, I managed to stumble across some stuff about how to make that work, and pieced this together:

if has("autocmd")
  filetype plugin indent on
  augroup vimrcEx
  autocmd FileType text setlocal textwidth=78
  autocmd BufReadPost *
    \ if line("'\"") > 1 && line("'\"") <= line("$") |
    \   exe "normal! g`\"" |
    \ endif
  augroup END
  set autoindent

I just replaced the lone set autoindent with that, and now when I edit a file named something like foo.scm it automatically indents stuff in a reasonably sane way for Scheme source code.

Unfortunately, I don’t really understand what it’s doing here, and ththe if/else bit doesn’t seem to be doing what it’s supposed to do in the case that I’m editing a plain ol’ foo.txt file. I guess I’ll have to sort the rest of it out later. At least I can script a dirty hack to give me the behavior I want until I figure out how to do it “right”. Even if I’m doing it “wrong”, it’s nice to get Scheme-appropriate indentation when working with Scheme without having to spend as much time tapping the space bar.

I may edit the hell out of this SOB entry later if/when I figure out how to handle the Vim - Scheme case as well as the Vim + Scheme case without having to use kludges to basically rewrite or swap out .vimrc files based on shell aliases.

the real way to do it

It’s much easier to get this right than it looks. Ignore all that crap before. Out of all the .vimrc stuff in this whole SOB entry, all you actually need to be able to get decent indentation for both text and Scheme source files is:

filetype plugin indent on
set autoindent
set expandtab

That’s it. Somehow, it seems like the right answer is always easier than all the complex “solutions” I end up finding at first when I start searching the Web. Ultimately, I figured out how to do it for real by eliminating stuff a piece at a time that didn’t seem like it was strictly necessary for what I wanted, until I decided to throw away everything particular to Scheme other than the filetype directive, and just threw in the stuff I use for most other purposes after it.

All original content Copyright Chad Perrin: Distributed under the terms of the Open Works License