A tool I've found really valuable when writing Lisp is https://github.com/shcv/parenmedic
It compares the indentation to the parentheses, and complains if they don't agree. It doesn't care about indentation amount (like I-expressions/Wisp/etc.), only that lines following some opened parens get indented more; lines following some closed parens get indented less; and sibling lines are indented the same amount.
I find this much more accurate at spotting the cause of a problem, compared to simply counting whether the openers and closers balance; e.g. the latter will only spot a missing close parenthesis when it reaches the end of the file. This is especially useful for LLMs, which are terrible at counting parentheses.
Unfortunately, there can be some cases where an auto-indenter (e.g. elisp-autofmt) causes parenmedic to complain. One remedy is to alter the auto-indenter's behaviour by inserting a line-comment; but it's not ideal.
I wonder whether Sift would (knowingly or unknowingly) adhere to the rules parenmedic requires?
It's fairly common in agentic work with lisps to use the OG of indentation-implies-parens, parinfer. https://github.com/parinfer
It's a little disappointing to not see a credit for it in parenmedic. The agentic interface looks interesting though.
AFAIK parinfer requires a specific amount of indentation for each line. That's fine for those who don't mind its rules (and vibe-coders who never see the code anyway!), but it's an all-or-nothing approach, at the exclusion of anything else (like, say, this Sift tool).
I like parenmedic because it only checks relative changes compared to some previous line, i.e. one of:
- Indentation should be greater than the previous line (if some number of parens were left open)
- Indentation should be less than the previous line (if some number of parens were closed off)
- Indentation should be equal to that of the previous sibling (if there is one)
That's a lot more forgiving of different styles and tooling, whilst still being more informative than e.g. Emacs's `check-parens`.
EDIT: Based on the sibling comment I think I maybe misunderstood parinfer... https://news.ycombinator.com/item?id=50749574
Am I getting paranoid or does this text read very LLMish?
It's true it's produced by LLM, after my initial sketch/draft and many correction rounds. I just gave it a full revamp to the blog post, with more natural wording and succinter. Apologies for this
You need to stop reading things and get your agent to read them for you.
Inverse compression once again!
It's 100% AI. The formatter is also AI. I wonder if the AI cares about formatting.
It's always interesting to me when the people who have given up on writing code build tools around it. It seems like some form of inertia, performing a pointless ritual because it feels right to them.
I wonder what goes through their minds as they build tools to enable things that they no longer do.
What an interesting take. I hadn't looked at it that way.
I’ve only vibe coded tools I’ve needed, used, or wanted to play with. And the vast majority were “disposable artefacts”.
However, my guess is people always wanted X. They didn't have the skill or time to build it so now they pull it from the aether to finally see it exist. Then, they themselves dont use it (or need it) so it just fades away.
I think you've hit on why the trend of “everyone can build an app, so no one really cares about your app” is happening.
This tool is already helping humans and LLMs alike. It also helps steer towards best practices, consistent style, etc. I understand you might see this as a slop project, I can ensure you after daily driving Sift for a whole month already I can attest to the polish in the project. But curious if you try it, what you think about it.
"Best practices" are usually cargo cult, and I don't have any idea why AI would care about indentation.
I didn't call this a slop project. I don't see any benefit to using it with AI.
But now that you mention it: I also won't voluntarily touch AI without collecting a significant pay check. Enough to move me towards escaping the need to interact with AI ever again. Partly because of the way it makes projects like this pointless.
I wanted to make a Lua like language that was inspired by Rebol and Smalltalk and unified tables with functions. I wanted this for twenty years. Now Claude code writes in my custom language and it makes me smile.
It's also funny because anyone who actually wrote Lisp would find this bizarrely solving no problem.
Emacs already formats its own Lisp, so does all self-respecting Common Lisp editor/plugin. And they use precise information from the Lisp image (such as lambda list of user-defined macro), no need to reinvent a whole parser that only has worse information.
Not to start a flame war here, but this comment is really "us vs them" . also using hyperboles (typical of LLM, are you even human? :D )
I use this tool daily to great satisfaction, I use agents indeed, I also hack manually in Emacs with my REPLs and linters and formatter.
Absolutely the latter, though that doesn’t mean that you aren’t also getting paranoid.
Nowadays I auto-assume LLM slop text.
I tried to nail down ubiiquitous formatting for a typed nl-like sexpr language I have been mostly vibe coding.
It is surprisingly hard. I ended up mostly copying the algorithm that can be used through raco (the racket command line tool). It is not particularly fast, only about 10lines of code per core-second, but it produces a good enough result. I had to leave some things to the user, but only about 5 forms.
I've been tinkering with common lisp again lately (via VSCode) and I've been looking for something like this! Going to play with it this weekend.
OLIVE format using precise information from the CL image, why you need something like this?
Lisp people: Look at the elegance and simplicity of S-expressions! Isn't it a beauty?
Also Lisp people: Here is a thesis-sized article about formatting them properly O:)
From TFA: In the Lisp world the answer is usually “my editor does it”
And that's where the story ends for most of us. Author's set-up seems to have had CI tools fighting the editor. Most of us would have disabled the misguided CI auto-formatter ...
Actually, due to the Lisp syntax, code formatting is usually a non-issue. It is mostly reduced to correct indenting, where a common style had been established very early on in the language history - which means really long ago. All editors, which are Lisp-aware usually do it very well.
The problem remaining is, that there are editors around, which are not Lisp aware and to my continuing confusion, lots of users, which use them. So there is from time to time the requirement to predictably reformat source files. Of course, also helps a lot with automatic generated code. Just feed it to the formatter. I wrote a tool comparable to gofmt some time ago and it is very helpful.
How does your claim apply to this? https://www.gnu.org/software/emacs/manual/html_node/elisp/In...
For example, how should this Emacs Lisp code be formatted?
(foo
bar
baz
quux)
According to Emacs (definitely a "Lisp-aware editor"), it depends on the definition of `foo`! If `foo` is a macro, it can use `(declare (indent ...))` to specify how its use-sites should be indented. If `foo` happens to be a function, or a macro which doesn't use `(declare (indent ...))`, then Emacs will indent it as above. However, if it's defined like this: (defmacro foo (&rest args)
"Does foo to ARGS."
(declare (indent 2))
(blah))
Then Emacs will indent it as: (foo
bar
baz
quux)
So, at the very least, a "Lisp-aware" formatter would need to resolve names, traverse load paths, etc. to find out whether each call is actually a macro, and whether the definition of such a macro declares an indent rule.But it gets worse! Since we can also do:
(defmacro foo (&rest args)
"Does foo to ARGS."
(declare (indent 'uh-oh))
(blah))
With a definition like that, Emacs will choose how much indentation to use by calling the `uh-oh` function! That makes indentation of Emacs Lisp undecidable!That specific example would always be
(foo
bar
baz
quux)
Once we separate even the first argument on its own line, we have made clear that that is where the division is, and given up on more detail: it's some kind of operator with arguments.If the list is quoted then:
'(foo
bar
baz
quux)
The ambiguity is if you have this: (foo bar
... )
If foo is a function (foo bar
baz
quux)
If it is a macro with a trailing body of forms ("implicit progn" in CL parlance) then we don't know, without the macro definition, how many of the initial arguments are not part of those forms: (foo bar
baz
quux)
or (foo bar
baz
quux)
or (foo bar
baz
quux)
A good solution to all this is parinfer: specifically indent -> parens mode. That is to say, manually or semi-automatically control indent, and let parentheses be inferred.The above issues are all symptoms of the assumption that the programmer controls parentheses, leaving the indentation to be figured out.
But parentheses are the aspect of the code that can only be one way, whereas indentation has latitudes that mean nothing to the machine but visually indicate semantics.
So, switch roles: let the machine deal with the parentheses. Then there is no problem to be solved whatsoever that requires declaration info or execution of functions.
The downside is that if we have a correct inference from parentheses to indentation, it can help to spot errors. For instance, if you write:
(pattern-match pattern
body1
body2)
and the indentation jumps to this: (pattern-match pattern
body1
body2)
then you have an instant reminder that you forgot to write the expression to be analyzed by the pattern, causing your first body form to be interpreted as that.To me, that is minor, and I'm willing to throw it under the bus by controlling indentation.
Parens inference from indentation is not perfect, though! This is illustrated best by cond. If you have a cond clause like this:
((condition ...)
form
form)
parinfer wants to do this: ((condition
form
form))
because the forms are indented, they are assumed to be constituents of the condition expression. The fix for that is to just learn to embrace: (cond
((condition)
form
form)
((condition)
form
form))
Only problem with that is working with your legacy code where you have the indentation.From the article:
> Lisp is a tree of nested lists. The language knows it, and most of the tooling around it pretends otherwise (some with nasty regex magic too).
This was my experience with Clojure too. A lot of "tooling is super easy because it's all lists and code are data structures" and in practice the tooling wasn't there. At all.
sad but true
i once have written a lisp parser for some static analysy tool. in the end i read the code backwards, nade things more easy. cant rember why
that's quite an interesting thought
Question for the author: how does Sift decide indentation for user-defined macros? Since the right layout for a form like `with-foo` or `my-let` depends on how it is defined (body-style vs. plain call), I'd be curious whether you ship a fixed table of known forms, read indent hints from the source, or let users configure it per project. For a formatter that runs in CI, I'd guess predictability matters more than being perfectly right for every macro.
It's all three, in a fixed precedence order, plus a name-shape guess as the last resort. The chain is in `list-indent` in `src/sift/format.scm` (~L895).
On your CI point: yes, that's the stated priority. Nothing is inferred from how a form is used, there's no scanning of other files or dependencies, and the result is idempotent.