logoalt Hacker News

_ph_ • today at 4:24 PM • 1 reply • view on HN

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.


Replies

chriswarbo • today at 5:04 PM

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!