Best way to customize auto-formatting/auto-indenting in vim for Common Lisp
Asked Answered
P

4

9

I would like to know the best way to customize auto-formatting/auto-indenting in vim for Common Lisp.

Auto-formatting (I usually do this by typing '==' in command mode per line) works very well for the base lisp language in vim (e.g., defmacro, defun, lambda, if, with-output-to-string), but any time that new language constructs are defined (e.g., using macros), I find that the formatting for the new construct is often not what I'd like it to be.

For example, here's how vim formats 'when (standard lisp construct) and 'awhen (commonly-used anaphoric version of 'when; not part of lisp standard)

(when 'this
  (process 'this))

(awhen 'this
       (process it))

I would like 'awhen to auto-format like 'when. Any ideas how I can do this?

Edit: Thanks Gilligan and Tamas for the Slimv recommendation. As a test, I downloaded MacVim (will need this working with terminal vim, but that's a different problem) and slimv, rsynched the slimv download into ~/.vim, launched MacVim, and loaded a .lisp file.

I then started up the lisp server (done through a GUI with MacVim), which loaded up my default lisp executable and core file.

And* since my core file is already loaded with the language extensions that I commonly use (awhen being one of them), awhen formatted correctly right out of the box.

I really like this solution. Instead of [1] having to learn how to tell vim to indent particular functions properly, and [2] writing the code that does this explicitly for each language extension that I define, and [3] updating that code every time I add a new language construct. Instead I leverage slimv to do the formatting for me. And slimv can 'learn' new language extensions, as long as those macros are already loaded into the lisp core that the server session is using. Pretty slick!

I have found that this works well for a particular class of language extensions. Usually ones defined as a macro, using the &body keyword. This seems to 'do the right thing' most of the time, but there are macros I use that still don't properly auto-format. Although I'd say that this is more likely to be an issue with how the macro is written (non-standard language extension) than anything else.

So, this solution works well for me for most cases, and I didn't have to code (and maintain) anything. Great stuff!

Papaya answered 15/12, 2011 at 1:8 Comment(2)
I'm glad you like slimv. If you find any indentation problems, please report it to me (I'm the author of slimv, you can find my email address in the documentation).Brittni
The Slimv answer isn't so useful for people looking to better indent the standard forms in a Lisp dialect that isn't Common Lisp.Waxler
V
7

This might not be a direct answer to your question but I strongly suggest that you install the slimv plugin: http://www.vim.org/scripts/script.php?script_id=2531

Its a great plugin which integrates SLIME functionality into vim and besides many other things it also comes with an improved indentation for clisp&clojure. It won't indent awhen the way you want though.

Vanegas answered 15/12, 2011 at 21:40 Comment(2)
Slimv will indent also awhen correctly if connected to the swank server and awhen is defined in the current REPL. The indentation is based upon the runtime information taken from the swank server, just like in Emacs with Slime.Brittni
Ah, even better - So to make a long story short, @claytonstanely: you should install slimv :)Vanegas
W
5

For those who are looking for this topic and don't want to run Slimv, because they aren't working with Common Lisp or other reasons, here is the scoop.

Vim's Lisp indentation is not like that for other languages; it has a special "Lisp mode". This mode is turned on by

:set lisp

which is done automatically for .lisp files. Lisp mode isn't a Vim invention; classic Vi implementations have a Lisp mode turned on with :set lisp. (It's not described by POSIX, unfortunately).

Vim's Lisp mode has a simple mechanism for recognizing forms that require operator-style indentation: namely, there is a parameter called lispwords which holds a comma-separated list of identifiers.

You can prove to yourself that this is the identifier list which is used, even when you're editing a Common Lisp .lisp file with syntax highlighting and all. Simply do :set listwords[TAB] and edit the list to remove something from it, such as defun. Then try to reindent a defun: you will see the function-style indentation now instead of the operator-style.

The syntax highlighting support for Common Lisp is separate from Lisp mode's lispwords parameter; it has its own list of identifiers. For example, in Vim 7.3 if you enter this:

(symbol-macrolet ((foo bar))
                 you get indented out to here!)

This is in spite of the fact that symbol-macrolet is recognized and colored. Why? It's because symbol-macrolet does not appear in the rather scanty lispwords list, whereas it does appear in the lisp.vim syntax highlighting definition file.

The upshot is that you can cob together some script which scans your directory of .lisp files for macros and generates a set lispwords=... command that is placed into a directory .vimrc.

Or if you are working on a custom Lisp dialect, you can just make its Vim syntax highlighting file customize lispwords when it loads.

Here is an implementation oversight: the lispwords option has no local value; you cannot use setlocal lispwords ... to give it a buffer-specific value. In other words, it appears that (at least in the Vim 7.3 I'm using under Ubuntu) you can't have two or more buffers open holding different dialects of Lisp with different identifiers for indentation. The default contents of lispwords contains a smattering of Lisp and Scheme symbols to try to be a kind of "one size almost fits all" solution.

Waxler answered 2/10, 2015 at 6:7 Comment(0)
G
1

If you filetype is 'lisp' then I think you need to add indenting rules for your special case in the 'lisp.vim' file in the '/vim7x/indent' directory. You can find a bit more info in help at :h indent-expr and :h indentexpr.

Someone may be able to tell you better, but I believe the default lisp.vim indent file basically does nothing because the built-in function lispindent() is used to get indent values. You will want to:

(1) set function used to get indent values (i.e., indentexpr) to a function in your own indent/lisp.vim file, e.g., GetLispIndent().
(2) in your your GetLispIndent() function you will use lispindent() to get indent values to return for all lines except your special case. See other languages' indent files and read the docs to get an idea for how indentexpr works, e.g, java.vim.

Gehring answered 15/12, 2011 at 1:48 Comment(1)
The lisp.vim file in the indent directory contains next to nothing. That's because Lisp indentation is performed by a special "lisp mode", which traces back to a feature of classic Vi.Waxler
T
1

@Kaz's answer is completely correct, but they don't go all the way to answering the original question. Vim's lispwords config string is a comma-delimited list of words; when any of the words in lispwords is found at the beginning of an S-expression, Vim will change the way that S-expression is indented. In other words, it defines the "standard lisp constructs" to which the OP refers. If you view your current lispwords config with :set lispwords, you'll see "when" is included, but "awhen" is not, resulting in the following indentation:

(when 'this
  (process 'this))

(awhen 'this
       (process it))

To fix this, simply add "awhen" to the config string somewhere in your config, like so:

set lispwords+=awhen,

The trailing comma isn't strictly necessary, but the default value includes it, and is probably wise in case you or a plugin modifier elsewhere. That would turn the original formatting into this:

(when 'this
  (process 'this))

(awhen 'this
  (process it))

(Tested on my current installation of Vim 9.0)

Note that, as @Kaz points out, this config string is universal, so if you have different dialects of lisp their indentation will all be controlled by this setting. Vim is pretty good about auto-detecting lisps, but you may need so :set lisp if it doesn't recognize your filetype or dialect.

Tripper answered 2/10, 2022 at 17:21 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.