1

Here is a scenario I use constantly in Rust development:

  1. Rust LSP is always on.
  2. I call the LSP feature "format current buffer" (or whatever it's called).
  3. The LSP returns the buffer formatted (internally, the LSP uses rustfmt).

Since Emacs Elisp does not have an LSP implementation, how can I achieve something similar? E.g., I wanna press some button and have the code in the buffer be formatted (not just indented, formatted).

P.S. Emacs newbie here, transitioning from Neovim.

Drew
  • 29,895
  • 7
  • 74
  • 104
Refael Sheinker
  • 713
  • 7
  • 20
  • 1
    I believe that recent Emacs versions do have LSP support (but I'm no expert on this). – Drew May 18 '23 at 17:10
  • @Drew Yes, Emacs 29 that will be out soon, has a built-in LSP server based on the Eglot package, but that does not help since there is still no implementation of the LSP protocol for the Elisp language. – Refael Sheinker May 18 '23 at 18:24
  • 1
    @RefaelSheinker, as you've already found, most (all?) of the available tools are listed at [this EmacsSE thread](https://emacs.stackexchange.com/q/283/28451). And I agree Elisp LSP might be not a bad idea. Now it looks like the common current solution is to customize formatting in own config and per project via [.dir-locals.el](https://github.com/emacs-mirror/emacs/blob/master/.dir-locals.el), might be with a tendency to [this style guide suggestions](https://github.com/bbatsov/emacs-lisp-style-guide). – Y. E. May 19 '23 at 12:43
  • @Y. E. please make your comment an answer and I will accept it. – Refael Sheinker May 20 '23 at 19:53

1 Answers1

1

De facto, Elisp formatting rules are defined in users config files, .dir-locals.el per project, and often follow this style guide suggestions.

Since Emacs 29, for which pretest is now available, new function pp-emacs-lisp-code can be used, which "applies formatting rules appropriate for Emacs Lisp code."

The docstring of the function currently states:

Insert SEXP into the current buffer, formatted as Emacs Lisp code. Use the pp-max-width variable to control the desired line length. Note that this could be slow for large SEXPs.

A number of third-party packages for Elisp formatting are listed in Command that formats (prettifies) Elisp code thread.

Y. E.
  • 687
  • 1
  • 10
  • 29